Fix playback stopping when fallback audio ends

This commit is contained in:
Olive Vaughn 2026-10-04 00:58:51 -04:00
parent ee603a351b
commit e5b61bfcdd
2 changed files with 50 additions and 10 deletions

View file

@ -192,6 +192,23 @@
(.arrayBuffer response)))
(.then decode-bytes!)))
(defn fit-buffer
"Fit fallback audio to the open timeline, padding with silence or trimming.
The buffer and clock must share a duration so looping wraps at the timeline's
end rather than repeating a short soundtrack underneath a longer animation."
[^js buffer seconds]
(let [rate (.-sampleRate buffer)
frames (max 1 (js/Math.ceil (* seconds rate)))]
(if (= frames (.-length buffer))
buffer
(let [channels (.-numberOfChannels buffer)
fitted (.createBuffer (js/OfflineAudioContext. channels 1 rate)
channels frames rate)]
(dotimes [c channels]
(.set (.getChannelData fitted c)
(.subarray (.getChannelData buffer c) 0 (min frames (.-length buffer)))))
fitted))))
(defn clock-source!
"Promise of `{:buffer :seconds}` — the audio the transport runs its clock on
while symbol `sid` is open, and how long that clock is.
@ -216,7 +233,9 @@
(and fallback-url (= sid (clip/opens-on document)))
(-> (decode! fallback-url)
(.then (fn [^js b] {:buffer b :seconds (.-duration b)})))
(.then (fn [b]
(let [seconds (/ (clip/output-frames document sid) (:fps document))]
{:buffer (fit-buffer b seconds) :seconds seconds}))))
:else
{:buffer nil
@ -236,12 +255,8 @@
symbol played against the whole take's soundtrack would run ten frames and then
keep the clock going for minutes."
[document sid fallback-url store]
(-> (buffer! document sid store)
(.then (fn [buffer]
(cond
buffer (wav-url buffer)
(and fallback-url (= sid (clip/opens-on document))) fallback-url
:else (let [rate 44100
n (max 1 (js/Math.ceil (* rate (/ (clip/output-frames document sid)
(:fps document)))))]
(wav-url (.createBuffer (js/OfflineAudioContext. 1 1 rate) 1 n rate))))))))
(-> (clock-source! document sid fallback-url store)
(.then (fn [{:keys [buffer seconds]}]
(wav-url (or buffer
(.createBuffer (js/OfflineAudioContext. 1 1 44100)
1 (max 1 (js/Math.ceil (* 44100 seconds))) 44100)))))))