| 208 | } |
| 209 | |
| 210 | export function togglePlayPause() { |
| 211 | const eng = audioEngine; |
| 212 | const tx = eng ?? multitrack; |
| 213 | if (!tx) return; |
| 214 | if (tx.isPlaying()) { |
| 215 | tx.pause(); |
| 216 | // The engine emits no play/pause events (the multitrack stays silent), so |
| 217 | // the play-button visual that the ws "pause" handler normally toggles must |
| 218 | // be driven here directly. |
| 219 | if (eng) playBtn.classList.remove("playing"); |
| 220 | return; |
| 221 | } |
| 222 | const ctx = tx.audioContext; |
| 223 | // Safari requires play() to be called synchronously within the user-gesture |
| 224 | // handler. Resume the AudioContext fire-and-forget so the context becomes |
| 225 | // live, then call play() immediately on the same tick. |
| 226 | if (ctx && ctx.state === "suspended") { |
| 227 | ctx.resume().catch(() => {}); |
| 228 | } |
| 229 | // Snap playhead to loopStart on play (DAW convention). |
| 230 | if (loopEnabled && totalDuration > 0) { |
| 231 | tx.setTime(loopStart); |
| 232 | } |
| 233 | if (eng) { |
| 234 | eng.play(); |
| 235 | playBtn.classList.add("playing"); |
| 236 | stopBtn.classList.remove("stopped"); |
| 237 | } else { |
| 238 | _playWhenReady(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | export function stopTransport() { |
| 243 | const eng = audioEngine; |