|
| 1 | +/** |
| 2 | + * Shared volume-automation utilities used by both the renderer (offline PCM |
| 3 | + * baking in audioVolumeEnvelope.ts) and the preview runtime (per-tick gain |
| 4 | + * applied in syncRuntimeMedia). |
| 5 | + * |
| 6 | + * Keeping the two concerns in one place ensures preview and render derive the |
| 7 | + * envelope from the same logic and the same probe samples. |
| 8 | + */ |
| 9 | + |
| 10 | +export interface VolumeKeyframe { |
| 11 | + time: number; |
| 12 | + volume: number; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Normalise raw keyframes to track-relative seconds: subtract `trackStart`, |
| 17 | + * clamp to [0,1], sort, de-duplicate, and prepend a `baseVolume` anchor at |
| 18 | + * t=0 when the first keyframe starts after the clip's begin. |
| 19 | + * |
| 20 | + * Returns an empty array when all keyframes are invalid — the caller should |
| 21 | + * treat an empty envelope as "no automation, use static volume." |
| 22 | + */ |
| 23 | +export function normaliseEnvelope( |
| 24 | + keyframes: VolumeKeyframe[], |
| 25 | + trackStart: number, |
| 26 | + baseVolume: number, |
| 27 | +): VolumeKeyframe[] { |
| 28 | + const points = keyframes |
| 29 | + .filter((k) => Number.isFinite(k.time) && Number.isFinite(k.volume)) |
| 30 | + .map((k) => ({ |
| 31 | + time: Math.max(0, k.time - trackStart), |
| 32 | + volume: Math.max(0, Math.min(1, k.volume)), |
| 33 | + })) |
| 34 | + .sort((a, b) => a.time - b.time); |
| 35 | + |
| 36 | + const deduped: VolumeKeyframe[] = []; |
| 37 | + for (const point of points) { |
| 38 | + const previous = deduped.at(-1); |
| 39 | + if (previous && Math.abs(previous.time - point.time) < 1e-9) { |
| 40 | + previous.volume = point.volume; |
| 41 | + } else { |
| 42 | + deduped.push(point); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (deduped.length === 0) return deduped; |
| 47 | + if (deduped[0]!.time > 0) { |
| 48 | + deduped.unshift({ time: 0, volume: Math.max(0, Math.min(1, baseVolume)) }); |
| 49 | + } |
| 50 | + return deduped; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Linearly interpolate the gain at time `t` (track-relative seconds) from a |
| 55 | + * normalised envelope produced by `normaliseEnvelope`. Returns 1 when the |
| 56 | + * envelope is empty. |
| 57 | + */ |
| 58 | +export function interpolateVolumeGain(envelope: VolumeKeyframe[], t: number): number { |
| 59 | + if (envelope.length === 0) return 1; |
| 60 | + |
| 61 | + let segment = 0; |
| 62 | + while (segment < envelope.length - 2 && t >= envelope[segment + 1]!.time) { |
| 63 | + segment += 1; |
| 64 | + } |
| 65 | + |
| 66 | + const a = envelope[segment]!; |
| 67 | + const b = envelope[segment + 1] ?? a; |
| 68 | + const span = b.time - a.time; |
| 69 | + const progress = span <= 0 ? 0 : Math.min(1, Math.max(0, (t - a.time) / span)); |
| 70 | + return a.volume + (b.volume - a.volume) * progress; |
| 71 | +} |
| 72 | + |
| 73 | +// fallow-ignore-next-line complexity |
| 74 | +/** |
| 75 | + * Probe a single media element's volume automation by seeking a GSAP timeline |
| 76 | + * through the element's active window. |
| 77 | + * |
| 78 | + * Runs synchronously in the browser. The timeline is left at its current |
| 79 | + * position after the probe (the next transport tick re-seeks it to `t`). |
| 80 | + * |
| 81 | + * Returns null when the element has no detectable automation (volume never |
| 82 | + * changes from its initial `data-volume` value). |
| 83 | + */ |
| 84 | +export function probeElementVolumeKeyframes( |
| 85 | + el: HTMLAudioElement | HTMLVideoElement, |
| 86 | + seekTimeline: (t: number) => void, |
| 87 | + compositionDuration: number, |
| 88 | + sampleFps: number, |
| 89 | +): VolumeKeyframe[] | null { |
| 90 | + const start = Number.parseFloat(el.dataset.start ?? "0") || 0; |
| 91 | + const endAttr = Number.parseFloat(el.dataset.end ?? ""); |
| 92 | + const durAttr = Number.parseFloat(el.dataset.duration ?? ""); |
| 93 | + const end = |
| 94 | + Number.isFinite(endAttr) && endAttr > start |
| 95 | + ? endAttr |
| 96 | + : Number.isFinite(durAttr) && durAttr > 0 |
| 97 | + ? start + durAttr |
| 98 | + : compositionDuration; |
| 99 | + |
| 100 | + const staticAttr = Number.parseFloat(el.dataset.volume ?? ""); |
| 101 | + const staticVolume = Number.isFinite(staticAttr) ? Math.max(0, Math.min(1, staticAttr)) : 1; |
| 102 | + |
| 103 | + // Reset to data-volume so GSAP captures the correct FROM value. |
| 104 | + el.volume = staticVolume; |
| 105 | + |
| 106 | + const step = 1 / Math.min(60, Math.max(1, sampleFps)); |
| 107 | + const sampleStart = Math.max(0, start); |
| 108 | + const sampleEnd = Math.min(compositionDuration, end); |
| 109 | + |
| 110 | + const keyframes: VolumeKeyframe[] = []; |
| 111 | + for (let t = sampleStart; t <= sampleEnd + 1e-6; t += step) { |
| 112 | + const bounded = Math.min(sampleEnd, t); |
| 113 | + seekTimeline(bounded); |
| 114 | + const raw = Number(el.volume); |
| 115 | + if (!Number.isFinite(raw)) continue; |
| 116 | + const volume = Math.max(0, Math.min(1, raw)); |
| 117 | + const last = keyframes.at(-1); |
| 118 | + if (!last || Math.abs(last.volume - volume) > 0.0001 || bounded === sampleEnd) { |
| 119 | + keyframes.push({ time: Number(bounded.toFixed(6)), volume: Number(volume.toFixed(6)) }); |
| 120 | + } |
| 121 | + if (bounded === sampleEnd) break; |
| 122 | + } |
| 123 | + |
| 124 | + const hasAutomation = keyframes.some((kf) => Math.abs(kf.volume - staticVolume) > 0.0001); |
| 125 | + return hasAutomation ? keyframes : null; |
| 126 | +} |
| 127 | + |
| 128 | +export interface RuntimeTimelineRef { |
| 129 | + totalTime?: ((t: number, suppressEvents?: boolean) => unknown) | undefined; |
| 130 | + seek?: ((t: number, suppressEvents?: boolean) => unknown) | undefined; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Probe a media element and, if volume automation is detected, store the |
| 135 | + * keyframes in `cache`. Safe to call with a null timeline — returns early. |
| 136 | + */ |
| 137 | +export function probeAndCacheElementVolume( |
| 138 | + mediaEl: HTMLMediaElement, |
| 139 | + timeline: RuntimeTimelineRef | null | undefined, |
| 140 | + compositionDuration: number, |
| 141 | + cache: WeakMap<HTMLMediaElement, VolumeKeyframe[]>, |
| 142 | +): void { |
| 143 | + if (!timeline) return; |
| 144 | + if (!(mediaEl instanceof HTMLAudioElement) && !(mediaEl instanceof HTMLVideoElement)) return; |
| 145 | + if (compositionDuration <= 0) return; |
| 146 | + |
| 147 | + const seekFn = (t: number) => { |
| 148 | + try { |
| 149 | + if (typeof timeline.totalTime === "function") { |
| 150 | + timeline.totalTime(t, true); |
| 151 | + } else if (typeof timeline.seek === "function") { |
| 152 | + timeline.seek(t, true); |
| 153 | + } |
| 154 | + } catch { |
| 155 | + // ignore seek failures during probe |
| 156 | + } |
| 157 | + }; |
| 158 | + |
| 159 | + const keyframes = probeElementVolumeKeyframes(mediaEl, seekFn, compositionDuration, 60); |
| 160 | + if (keyframes) { |
| 161 | + cache.set(mediaEl, keyframes); |
| 162 | + } |
| 163 | +} |
0 commit comments