framepress 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -18
- package/assets/backdrops/river-dusk.jpg +0 -0
- package/bin/camera.mjs +190 -0
- package/bin/ffmpeg.mjs +23 -0
- package/bin/mcp.mjs +233 -0
- package/bin/record.mjs +58 -13
- package/bin/sound.mjs +372 -0
- package/bin/studio-video.mjs +282 -0
- package/bin/studio.mjs +494 -0
- package/bin/take.mjs +251 -0
- package/examples/synthetic-desktop/check-alignment.mjs +28 -0
- package/examples/synthetic-desktop/check-sound.mjs +104 -0
- package/examples/synthetic-desktop/desktop.html +343 -0
- package/examples/synthetic-desktop/make.mjs +102 -0
- package/native/Package.swift +9 -0
- package/native/Sources/FramepressCapture/Capture.swift +325 -0
- package/native/Sources/FramepressCapture/Command.swift +219 -0
- package/native/Sources/FramepressCapture/Input.swift +202 -0
- package/native/bin/framepress-capture +0 -0
- package/package.json +17 -6
package/bin/sound.mjs
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
// Sound for recordings: every sound is synthesized here from the event log and the camera
|
|
2
|
+
// track, sample by sample, so its onset sits exactly on the event time. No samples are
|
|
3
|
+
// bundled. 48 kHz stereo float, normalized to an integrated loudness (ITU-R BS.1770 gating)
|
|
4
|
+
// with a lookahead limiter under a sample peak ceiling.
|
|
5
|
+
//
|
|
6
|
+
// clicks: a press is a short bright transient (a few damped modes of a plastic shell plus a
|
|
7
|
+
// band of noise) on a small low thump; a release is the same, quieter, higher and shorter.
|
|
8
|
+
// Drag start and drop are a press and a release, so they get the same.
|
|
9
|
+
// keys: a softer, lower tap (body mode plus a duller noise band) with a quiet key return
|
|
10
|
+
// 60 to 90 ms later. Each key gets its own level, pitch and decay.
|
|
11
|
+
// scroll: a tiny detent tick every SCROLL.step points of scroll travel, very quiet.
|
|
12
|
+
// whoosh: band-passed noise whose level and centre follow camera speed (zoom rate and pan
|
|
13
|
+
// speed), low enough to feel rather than hear.
|
|
14
|
+
// music: optional. 'soft-keys' synthesizes a warm electric piano bed (76 BPM, Fmaj9 Dm9
|
|
15
|
+
// Bbmaj9 Cadd9, soft bass, slow pad, light reverb); 'pad' a plain chord bed; any other value
|
|
16
|
+
// is a file decoded with ffmpeg and looped to length. The bed is faded in and out and set
|
|
17
|
+
// LEVELS.musicUnder dB under the effects' integrated loudness (times --music-level).
|
|
18
|
+
import { ffmpegPath } from './ffmpeg.mjs';
|
|
19
|
+
import { spawn } from 'node:child_process';
|
|
20
|
+
import { writeFile } from 'node:fs/promises';
|
|
21
|
+
|
|
22
|
+
export const RATE = 48000;
|
|
23
|
+
// Levels in dB before normalization, relative to each other. Clicks lead; keys sit under
|
|
24
|
+
// them; scroll and whoosh are texture. The mix then goes to LOUDNESS.target.
|
|
25
|
+
export const LEVELS = { click: 0, release: -7, key: -7, keyReturn: -19, scroll: -27, whoosh: -24, music: -8, musicUnder: -14 };
|
|
26
|
+
export const LOUDNESS = { target: -18, ceiling: -1.5, maxReduction: 6, lookahead: 0.002, release: 0.02 };
|
|
27
|
+
export const SCROLL = { step: 28 };
|
|
28
|
+
|
|
29
|
+
// Seeded random numbers so a render is repeatable.
|
|
30
|
+
function rng(seed) {
|
|
31
|
+
let s = seed >>> 0 || 1;
|
|
32
|
+
return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; };
|
|
33
|
+
}
|
|
34
|
+
const db = x => 10 ** (x / 20);
|
|
35
|
+
|
|
36
|
+
// One RBJ biquad, run in place over a mono buffer.
|
|
37
|
+
function biquad(buf, type, f, q) {
|
|
38
|
+
const w = 2 * Math.PI * f / RATE, c = Math.cos(w), al = Math.sin(w) / (2 * q);
|
|
39
|
+
let b0, b1, b2;
|
|
40
|
+
if (type === 'bp') { b0 = al; b1 = 0; b2 = -al; }
|
|
41
|
+
else if (type === 'hp') { b0 = (1 + c) / 2; b1 = -(1 + c); b2 = (1 + c) / 2; }
|
|
42
|
+
else { b0 = (1 - c) / 2; b1 = 1 - c; b2 = (1 - c) / 2; }
|
|
43
|
+
const a0 = 1 + al, a1 = -2 * c, a2 = 1 - al;
|
|
44
|
+
let x1 = 0, x2 = 0, y1 = 0, y2 = 0;
|
|
45
|
+
for (let i = 0; i < buf.length; i++) {
|
|
46
|
+
const x = buf[i], y = (b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2) / a0;
|
|
47
|
+
x2 = x1; x1 = x; y2 = y1; y1 = y; buf[i] = y;
|
|
48
|
+
}
|
|
49
|
+
return buf;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// A short percussive voice: damped sine modes plus an enveloped noise band, then a gentle
|
|
53
|
+
// high-pass. Starts at full level within 0.25 ms, so the onset is the event time.
|
|
54
|
+
function voice(r, { modes, noise, thump, len }) {
|
|
55
|
+
const n = Math.round(len * RATE), out = new Float32Array(n);
|
|
56
|
+
for (const [f, decay, amp] of modes) {
|
|
57
|
+
const ph = r() * 2 * Math.PI;
|
|
58
|
+
for (let i = 0; i < n; i++) out[i] += amp * Math.sin(2 * Math.PI * f * i / RATE + ph) * Math.exp(-i / RATE / decay);
|
|
59
|
+
}
|
|
60
|
+
if (noise) {
|
|
61
|
+
const nb = new Float32Array(n);
|
|
62
|
+
for (let i = 0; i < n; i++) nb[i] = (r() * 2 - 1) * Math.exp(-i / RATE / noise.decay);
|
|
63
|
+
biquad(nb, 'bp', noise.f, noise.q);
|
|
64
|
+
for (let i = 0; i < n; i++) out[i] += noise.amp * nb[i];
|
|
65
|
+
}
|
|
66
|
+
if (thump) for (let i = 0; i < n; i++) out[i] += thump.amp * Math.sin(2 * Math.PI * thump.f * i / RATE) * Math.exp(-i / RATE / thump.decay);
|
|
67
|
+
const att = Math.round(0.00025 * RATE);
|
|
68
|
+
for (let i = 0; i < att; i++) out[i] *= i / att;
|
|
69
|
+
// Fade the last third to zero so the voice never ends on a step (an audible tick).
|
|
70
|
+
const fade = Math.round(n / 3);
|
|
71
|
+
for (let i = 0; i < fade; i++) out[n - 1 - i] *= 0.5 - 0.5 * Math.cos(Math.PI * i / fade);
|
|
72
|
+
biquad(out, 'hp', 90, 0.7);
|
|
73
|
+
let peak = 0; for (const x of out) peak = Math.max(peak, Math.abs(x));
|
|
74
|
+
for (let i = 0; i < n; i++) out[i] /= peak || 1;
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const vary = (r, x, spread) => x * (1 + (r() * 2 - 1) * spread);
|
|
79
|
+
|
|
80
|
+
export function clickVoice(r, release = false) {
|
|
81
|
+
const p = vary(r, release ? 1.12 : 1, 0.05), d = vary(r, release ? 0.7 : 1, 0.15);
|
|
82
|
+
return voice(r, {
|
|
83
|
+
len: 0.1,
|
|
84
|
+
modes: [[1180 * p, 0.016 * d, 0.32], [2150 * p, 0.009 * d, 0.6], [3420 * p, 0.0055 * d, 0.45], [5600 * p, 0.0028 * d, 0.3], [7900 * p, 0.0012 * d, 0.15]],
|
|
85
|
+
noise: { f: 4200 * p, q: 1.2, decay: 0.0022 * d, amp: 2.2 },
|
|
86
|
+
thump: { f: vary(r, release ? 260 : 190, 0.08), decay: 0.02 * d, amp: release ? 0.25 : 0.5 },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function keyVoice(r, ret = false) {
|
|
91
|
+
const p = vary(r, ret ? 1.3 : 1, 0.09), d = vary(r, 1, 0.2);
|
|
92
|
+
return voice(r, {
|
|
93
|
+
len: 0.1,
|
|
94
|
+
modes: [[620 * p, 0.012 * d, 0.5], [1450 * p, 0.007 * d, 0.45], [2700 * p, 0.0035 * d, 0.25]],
|
|
95
|
+
noise: { f: 1900 * p, q: 0.9, decay: 0.004 * d, amp: 1.6 },
|
|
96
|
+
thump: { f: vary(r, 140, 0.1), decay: 0.014 * d, amp: 0.25 },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function tickVoice(r) {
|
|
101
|
+
return voice(r, { len: 0.02, modes: [[vary(r, 4800, 0.06), 0.0009, 0.6]], noise: { f: 6500, q: 1.5, decay: 0.0006, amp: 1.2 } });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Mix a mono voice into stereo at sample s with gain g and pan -1..1 (equal power).
|
|
105
|
+
function place(L, R, v, s, g, pan) {
|
|
106
|
+
const a = (pan + 1) * Math.PI / 4, gl = g * Math.cos(a) * Math.SQRT2, gr = g * Math.sin(a) * Math.SQRT2;
|
|
107
|
+
for (let i = 0; i < v.length && s + i < L.length; i++) if (s + i >= 0) { L[s + i] += v[i] * gl; R[s + i] += v[i] * gr; }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// The sound events a render plays, from loaded (trim-shifted) events. Times in seconds.
|
|
111
|
+
export function soundCues(events, duration, width = null) {
|
|
112
|
+
const cues = [], pan = x => width ? Math.max(-1, Math.min(1, (x / width) * 2 - 1)) * 0.25 : 0;
|
|
113
|
+
let travel = 0;
|
|
114
|
+
for (const e of events) {
|
|
115
|
+
if (!(e.t >= 0 && e.t < duration)) continue;
|
|
116
|
+
if (e.type === 'down') cues.push({ t: e.t, kind: 'click', pan: pan(e.x) });
|
|
117
|
+
else if (e.type === 'up') cues.push({ t: e.t, kind: 'release', pan: pan(e.x) });
|
|
118
|
+
else if (e.type === 'key') cues.push({ t: e.t, kind: 'key', pan: pan(e.x) * 0.5 });
|
|
119
|
+
else if (e.type === 'scroll') {
|
|
120
|
+
travel += Math.hypot(e.dx ?? 0, e.dy ?? 0);
|
|
121
|
+
if (travel >= SCROLL.step) { travel %= SCROLL.step; cues.push({ t: e.t, kind: 'scroll', pan: pan(e.x) }); }
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return cues;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Camera speed per frame from the rig track: zoom rate in octaves per second plus pan speed
|
|
128
|
+
// in output widths per second, smoothed. Returns a per-sample envelope 0..1.
|
|
129
|
+
export function cameraSpeed(track, fps, W, n) {
|
|
130
|
+
const sp = new Float32Array(track.length);
|
|
131
|
+
for (let i = 1; i < track.length; i++) {
|
|
132
|
+
const a = track[i - 1], b = track[i];
|
|
133
|
+
const zr = Math.abs(Math.log2(b.cz / a.cz)) * fps;
|
|
134
|
+
const pr = Math.hypot(b.cx - a.cx, b.cy - a.cy) * b.cz / W * fps;
|
|
135
|
+
sp[i] = zr + pr;
|
|
136
|
+
}
|
|
137
|
+
const env = new Float32Array(n);
|
|
138
|
+
for (let s = 0; s < n; s++) {
|
|
139
|
+
const f = s / RATE * fps, i = Math.min(sp.length - 1, Math.floor(f)), w = f - i;
|
|
140
|
+
env[s] = sp[i] + ((sp[Math.min(sp.length - 1, i + 1)] ?? 0) - sp[i]) * w;
|
|
141
|
+
}
|
|
142
|
+
// One-pole smoothing (about 40 ms) so the noise swells rather than steps.
|
|
143
|
+
const k = 1 - Math.exp(-1 / (0.04 * RATE));
|
|
144
|
+
for (let s = 1; s < n; s++) env[s] = env[s - 1] + (env[s] - env[s - 1]) * k;
|
|
145
|
+
// A full zoom in moves about 1.5 octaves/s at peak; saturate softly.
|
|
146
|
+
for (let s = 0; s < n; s++) env[s] = Math.tanh(env[s] / 1.6);
|
|
147
|
+
return env;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function whoosh(L, R, env, r, g) {
|
|
151
|
+
const n = env.length, nb = new Float32Array(n);
|
|
152
|
+
// Brown-ish noise (leaky integral of white) is softer than white.
|
|
153
|
+
let b = 0; for (let i = 0; i < n; i++) { b = 0.985 * b + (r() * 2 - 1) * 0.15; nb[i] = b; }
|
|
154
|
+
// Band-pass whose centre follows speed: 260 Hz when slow up to about 1.1 kHz at full speed.
|
|
155
|
+
const out = new Float32Array(n);
|
|
156
|
+
let x1 = 0, x2 = 0, y1 = 0, y2 = 0;
|
|
157
|
+
for (let i = 0; i < n; i++) {
|
|
158
|
+
const f = 260 + 850 * env[i], w = 2 * Math.PI * f / RATE, c = Math.cos(w), al = Math.sin(w) / (2 * 0.8), a0 = 1 + al;
|
|
159
|
+
const y = (al * nb[i] - al * x2 + 2 * c * y1 - (1 - al) * y2) / a0;
|
|
160
|
+
x2 = x1; x1 = nb[i]; y2 = y1; y1 = y; out[i] = y;
|
|
161
|
+
}
|
|
162
|
+
let rms = 0; for (let i = 0; i < n; i++) rms += out[i] * out[i]; rms = Math.sqrt(rms / n) || 1;
|
|
163
|
+
for (let i = 0; i < n; i++) {
|
|
164
|
+
const e = env[i] ** 1.4, v = out[i] / rms * 0.25 * e * g;
|
|
165
|
+
L[i] += v; R[i] += v * 0.92;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// A soft procedural bed: slow chord changes of detuned sines through a gentle low-pass.
|
|
170
|
+
function pad(n, r) {
|
|
171
|
+
const L = new Float32Array(n), R = new Float32Array(n);
|
|
172
|
+
const chords = [[220, 277.18, 329.63, 415.3], [196, 246.94, 293.66, 369.99], [174.61, 220, 261.63, 329.63], [196, 246.94, 293.66, 392]];
|
|
173
|
+
const bar = 4 * RATE;
|
|
174
|
+
for (let c = 0; c * bar < n; c++) {
|
|
175
|
+
const notes = chords[c % chords.length];
|
|
176
|
+
for (const f0 of notes) for (const det of [-0.0025, 0.0025]) {
|
|
177
|
+
const f = f0 * (1 + det), ph = r() * 2 * Math.PI, start = c * bar - RATE, end = Math.min(n, (c + 1) * bar + RATE);
|
|
178
|
+
for (let s = Math.max(0, start); s < end; s++) {
|
|
179
|
+
const u = (s - start) / (end - start), env = Math.sin(Math.PI * u) ** 2;
|
|
180
|
+
const v = Math.sin(2 * Math.PI * f * s / RATE + ph) * env * 0.08;
|
|
181
|
+
if (det < 0) L[s] += v; else R[s] += v;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return [biquad(L, 'lp', 1800, 0.7), biquad(R, 'lp', 1800, 0.7)];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
const midi = m => 440 * 2 ** ((m - 69) / 12);
|
|
190
|
+
|
|
191
|
+
// One electric piano note: a sine carrier phase-modulated by itself (the bark fades in
|
|
192
|
+
// a quarter second), a quiet high tine on the attack, a soft 6 ms attack and a pitch
|
|
193
|
+
// dependent decay. Mono, at most `len` seconds.
|
|
194
|
+
function epNote(buf, s0, f, vel, r, len = 4.2) {
|
|
195
|
+
const n = Math.min(buf.length - s0, Math.round(len * RATE)), decay = 1.9 * (220 / f) ** 0.35, ph = r() * 6.283;
|
|
196
|
+
for (let i = 0; i < n; i++) {
|
|
197
|
+
const t = i / RATE, w = 2 * Math.PI * f * t + ph;
|
|
198
|
+
const idx = 0.15 + 0.85 * vel * Math.exp(-t / 0.22);
|
|
199
|
+
const env = Math.min(1, t / 0.006) * Math.exp(-t / decay) * (i > n - 2400 ? (n - i) / 2400 : 1);
|
|
200
|
+
buf[s0 + i] += vel * (Math.sin(w + idx * Math.sin(w)) * env + 0.07 * vel * Math.sin(7.02 * w) * Math.exp(-t / 0.04) * Math.min(1, t / 0.003));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Freeverb-style room: eight damped combs into four allpasses, offset per channel.
|
|
205
|
+
function reverb(x, spread) {
|
|
206
|
+
const sc = RATE / 44100, out = new Float32Array(x.length);
|
|
207
|
+
for (const d0 of [1557, 1617, 1491, 1422, 1277, 1356, 1188, 1116]) {
|
|
208
|
+
const d = Math.round((d0 + spread) * sc), line = new Float32Array(d); let k = 0, lp = 0;
|
|
209
|
+
for (let i = 0; i < x.length; i++) { const y = line[k]; lp = y * 0.6 + lp * 0.4; line[k] = x[i] + lp * 0.83; out[i] += y; k = (k + 1) % d; }
|
|
210
|
+
}
|
|
211
|
+
for (const d0 of [556, 441, 341, 225]) {
|
|
212
|
+
const d = Math.round((d0 + spread) * sc), line = new Float32Array(d); let k = 0;
|
|
213
|
+
for (let i = 0; i < out.length; i++) { const b = line[k], v = out[i]; line[k] = v + b * 0.5; out[i] = b - v; k = (k + 1) % d; }
|
|
214
|
+
}
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// A soft electric piano bed: one chord per bar at 76 BPM, gently strummed, upper notes
|
|
219
|
+
// struck again on beat 3, a sparse top-line note, a round sine bass and a slow filtered pad
|
|
220
|
+
// under it, all through a light room. Sits low and warm: low-passed at 2.8 kHz.
|
|
221
|
+
function softKeys(n, r) {
|
|
222
|
+
const beat = 60 / 76, bar = 4 * beat;
|
|
223
|
+
const prog = [
|
|
224
|
+
{ bass: 41, keys: [57, 60, 64, 67], top: 72 }, // Fmaj9
|
|
225
|
+
{ bass: 38, keys: [53, 57, 60, 64], top: 69 }, // Dm9
|
|
226
|
+
{ bass: 46, keys: [57, 60, 62, 65], top: 74 }, // Bbmaj9 (no root in the hands)
|
|
227
|
+
{ bass: 36, keys: [55, 60, 62, 64], top: 67 }, // Cadd9
|
|
228
|
+
];
|
|
229
|
+
const ep = new Float32Array(n), epR = new Float32Array(n), padL = new Float32Array(n), padR = new Float32Array(n), bass = new Float32Array(n);
|
|
230
|
+
const at = t => Math.round(t * RATE);
|
|
231
|
+
for (let b = 0; b * bar * RATE < n; b++) {
|
|
232
|
+
const c = prog[b % prog.length], t0 = b * bar;
|
|
233
|
+
// Strum low to high, 28 to 40 ms apart, with a little velocity shape.
|
|
234
|
+
c.keys.forEach((m, i) => { const s = at(t0 + 0.012 + i * (0.028 + r() * 0.012)); if (s < n) epNote(i % 2 ? epR : ep, s, midi(m), 0.34 + 0.05 * i + r() * 0.05, r); });
|
|
235
|
+
c.keys.slice(2).forEach((m, i) => { const s = at(t0 + 2 * beat + i * 0.035); if (s < n) epNote(i % 2 ? ep : epR, s, midi(m), 0.2 + r() * 0.04, r, 3); });
|
|
236
|
+
const s = at(t0 + (b % 2 ? 3.5 : 1.5) * beat); if (s < n) epNote(b % 2 ? ep : epR, s, midi(c.top), 0.17, r, 3);
|
|
237
|
+
// Bass: a round sine with a little second harmonic, 30 ms attack.
|
|
238
|
+
const sb = at(t0), fb = midi(c.bass);
|
|
239
|
+
for (let i = 0; i < at(bar + 0.4) && sb + i < n; i++) { const t = i / RATE; bass[sb + i] += 0.38 * Math.min(1, t / 0.03) * Math.exp(-t / 1.6) * (Math.sin(2 * Math.PI * fb * t) + 0.18 * Math.sin(4 * Math.PI * fb * t)); }
|
|
240
|
+
// Pad: four voices of soft harmonics, slow swell over the bar.
|
|
241
|
+
const p0 = at(t0 - 0.6), p1 = Math.min(n, at(t0 + bar + 0.9));
|
|
242
|
+
for (const m of [c.bass + 12, ...c.keys.slice(0, 3)]) for (const [det, buf] of [[-0.003, padL], [0.003, padR]]) {
|
|
243
|
+
const f = midi(m) * (1 + det), ph = r() * 6.283;
|
|
244
|
+
for (let s2 = Math.max(0, p0); s2 < p1; s2++) {
|
|
245
|
+
const u = (s2 - p0) / (p1 - p0), env = Math.sin(Math.PI * u) ** 1.5, w = 2 * Math.PI * f * s2 / RATE + ph;
|
|
246
|
+
buf[s2] += 0.05 * env * (Math.sin(w) + 0.3 * Math.sin(2 * w) + 0.1 * Math.sin(3 * w));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
biquad(padL, 'lp', 900, 0.6); biquad(padR, 'lp', 900, 0.6); biquad(bass, 'lp', 300, 0.7);
|
|
251
|
+
const L = new Float32Array(n), R = new Float32Array(n);
|
|
252
|
+
for (let i = 0; i < n; i++) {
|
|
253
|
+
// Slow autopan on the piano (0.23 Hz), as a stage tremolo would, but gentle.
|
|
254
|
+
const a = 0.12 * Math.sin(2 * Math.PI * 0.23 * i / RATE);
|
|
255
|
+
L[i] = (ep[i] * 0.8 + epR[i] * 0.45) * (1 + a) + padL[i] + bass[i];
|
|
256
|
+
R[i] = (ep[i] * 0.45 + epR[i] * 0.8) * (1 - a) + padR[i] + bass[i];
|
|
257
|
+
}
|
|
258
|
+
for (const ch of [L, R]) { biquad(ch, 'lp', 2800, 0.6); biquad(ch, 'hp', 45, 0.7); }
|
|
259
|
+
// Reverb send: mids only, so the room never muddies the bass.
|
|
260
|
+
const send = new Float32Array(n); for (let i = 0; i < n; i++) send[i] = (L[i] + R[i]) * 0.5;
|
|
261
|
+
biquad(send, 'hp', 250, 0.7); biquad(send, 'lp', 4000, 0.7);
|
|
262
|
+
const wL = reverb(send, 0), wR = reverb(send, 23);
|
|
263
|
+
for (let i = 0; i < n; i++) { L[i] += wL[i] * 0.016; R[i] += wR[i] * 0.016; }
|
|
264
|
+
// Long fades: 1.5 s in, 2 s out, raised cosine.
|
|
265
|
+
const fin = 1.5 * RATE, fout = 2 * RATE;
|
|
266
|
+
for (let i = 0; i < n; i++) { const u = Math.min(1, i / fin, (n - 1 - i) / fout), g = 0.5 - 0.5 * Math.cos(Math.PI * Math.max(0, u)); L[i] *= g; R[i] *= g; }
|
|
267
|
+
return [L, R];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function decodeMusic(file, n, ffmpeg) {
|
|
271
|
+
const p = spawn(ffmpeg, ['-hide_banner', '-loglevel', 'error', '-i', file, '-vn', '-ac', '2', '-ar', String(RATE), '-f', 'f32le', 'pipe:1'], { stdio: ['ignore', 'pipe', 'inherit'] });
|
|
272
|
+
const chunks = []; for await (const c of p.stdout) chunks.push(c);
|
|
273
|
+
const code = await new Promise(res => p.once('close', res));
|
|
274
|
+
if (code !== 0) throw Error(`Could not decode music file ${file}`);
|
|
275
|
+
const buf = Buffer.concat(chunks), f = new Float32Array(buf.buffer, buf.byteOffset, buf.length >> 2), m = f.length >> 1;
|
|
276
|
+
if (!m) throw Error(`Music file ${file} has no audio`);
|
|
277
|
+
const L = new Float32Array(n), R = new Float32Array(n);
|
|
278
|
+
for (let s = 0; s < n; s++) { L[s] = f[2 * (s % m)]; R[s] = f[2 * (s % m) + 1]; }
|
|
279
|
+
return [L, R];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Integrated loudness, ITU-R BS.1770-4: K-weighting, 400 ms blocks with 75% overlap,
|
|
283
|
+
// absolute gate -70 LUFS and relative gate -10 LU. 48 kHz coefficients.
|
|
284
|
+
export function loudness(L, R) {
|
|
285
|
+
const k = ch => {
|
|
286
|
+
const y = new Float32Array(ch.length);
|
|
287
|
+
const run = (src, dst, b, a) => { let x1 = 0, x2 = 0, y1 = 0, y2 = 0; for (let i = 0; i < src.length; i++) { const x = src[i], o = b[0] * x + b[1] * x1 + b[2] * x2 - a[1] * y1 - a[2] * y2; x2 = x1; x1 = x; y2 = y1; y1 = o; dst[i] = o; } };
|
|
288
|
+
run(ch, y, [1.53512485958697, -2.69169618940638, 1.19839281085285], [1, -1.69065929318241, 0.73248077421585]);
|
|
289
|
+
run(y, y, [1, -2, 1], [1, -1.99004745483398, 0.99007225036621]);
|
|
290
|
+
return y;
|
|
291
|
+
};
|
|
292
|
+
const a = k(L), b = k(R), blk = 0.4 * RATE, hop = 0.1 * RATE, ms = [];
|
|
293
|
+
for (let s = 0; s + blk <= a.length; s += hop) { let e = 0; for (let i = s; i < s + blk; i++) e += a[i] * a[i] + b[i] * b[i]; ms.push(e / blk); }
|
|
294
|
+
const lufs = e => -0.691 + 10 * Math.log10(e);
|
|
295
|
+
const abs = ms.filter(e => lufs(e) > -70); if (!abs.length) return -Infinity;
|
|
296
|
+
const rel = lufs(abs.reduce((x, y) => x + y, 0) / abs.length) - 10;
|
|
297
|
+
const g = abs.filter(e => lufs(e) > rel);
|
|
298
|
+
return lufs(g.reduce((x, y) => x + y, 0) / g.length);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Lookahead peak limiter: gain follows the minimum needed over the lookahead window and
|
|
302
|
+
// releases exponentially. Output never exceeds the ceiling.
|
|
303
|
+
function limit(L, R, ceiling) {
|
|
304
|
+
const n = L.length, la = Math.round(LOUDNESS.lookahead * RATE), rel = Math.exp(-1 / (LOUDNESS.release * RATE)), c = db(ceiling);
|
|
305
|
+
const need = new Float32Array(n);
|
|
306
|
+
for (let i = 0; i < n; i++) { const p = Math.max(Math.abs(L[i]), Math.abs(R[i])); need[i] = p > c ? c / p : 1; }
|
|
307
|
+
// Running minimum over the next `la` samples (simple, la is small).
|
|
308
|
+
const gmin = new Float32Array(n);
|
|
309
|
+
for (let i = 0; i < n; i++) { let m = 1; for (let j = i; j < Math.min(n, i + la + 1); j++) m = Math.min(m, need[j]); gmin[i] = m; }
|
|
310
|
+
let g = 1, most = 1;
|
|
311
|
+
for (let i = 0; i < n; i++) {
|
|
312
|
+
g = gmin[i] < g ? gmin[i] : Math.min(gmin[i], 1 - (1 - g) * rel);
|
|
313
|
+
most = Math.min(most, g); L[i] *= g; R[i] *= g;
|
|
314
|
+
}
|
|
315
|
+
return -20 * Math.log10(most);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Build the soundtrack. events: loaded (trim-shifted) events. track: rig.track (per frame
|
|
319
|
+
// camera). opts: { clicks, keys, scroll, whoosh } gains (1 default, 0 mutes) and music.
|
|
320
|
+
export async function soundtrack({ events, duration, frames, fps, track, W, width, opts, ffmpeg, seed = 1 }) {
|
|
321
|
+
const n = Math.round(frames / fps * RATE), L = new Float32Array(n), R = new Float32Array(n), r = rng(seed);
|
|
322
|
+
const cues = soundCues(events, duration, width), returns = [];
|
|
323
|
+
for (const c of cues) {
|
|
324
|
+
const s = Math.round(c.t * RATE), hum = db((r() * 2 - 1) * (c.kind === 'key' ? 2.5 : 1.2));
|
|
325
|
+
if (c.kind === 'click' && opts.clicks) place(L, R, clickVoice(r), s, db(LEVELS.click) * opts.clicks * hum, c.pan);
|
|
326
|
+
if (c.kind === 'release' && opts.clicks) place(L, R, clickVoice(r, true), s, db(LEVELS.release) * opts.clicks * hum, c.pan);
|
|
327
|
+
if (c.kind === 'key' && opts.keys) {
|
|
328
|
+
place(L, R, keyVoice(r), s, db(LEVELS.key) * opts.keys * hum, c.pan);
|
|
329
|
+
const back = s + Math.round((0.06 + r() * 0.03) * RATE);
|
|
330
|
+
place(L, R, keyVoice(r, true), back, db(LEVELS.keyReturn) * opts.keys * hum, c.pan);
|
|
331
|
+
returns.push({ t: back / RATE, kind: 'key-return', pan: c.pan });
|
|
332
|
+
}
|
|
333
|
+
if (c.kind === 'scroll' && opts.scroll) place(L, R, tickVoice(r), s, db(LEVELS.scroll) * opts.scroll * hum, c.pan);
|
|
334
|
+
}
|
|
335
|
+
let musicUnder = null;
|
|
336
|
+
if (opts.whoosh && track?.length) whoosh(L, R, cameraSpeed(track, fps, W, n), r, db(LEVELS.whoosh) * opts.whoosh);
|
|
337
|
+
if (opts.music) {
|
|
338
|
+
const [mL, mR] = opts.music === 'soft-keys' ? softKeys(n, rng(seed + 101)) : opts.music === 'pad' ? pad(n, r) : await decodeMusic(opts.music, n, ffmpeg ?? ffmpegPath());
|
|
339
|
+
// Level: LEVELS.musicUnder dB under the effects' integrated loudness; with no effects,
|
|
340
|
+
// the old RMS rule. --music-level scales either.
|
|
341
|
+
const fx = loudness(L, R), ml = loudness(mL, mR);
|
|
342
|
+
let rms = 0; for (let i = 0; i < n; i++) rms += mL[i] * mL[i] + mR[i] * mR[i]; rms = Math.sqrt(rms / (2 * n)) || 1;
|
|
343
|
+
const g = (Number.isFinite(fx) && Number.isFinite(ml) ? db(fx + LEVELS.musicUnder - ml) : db(LEVELS.music) / rms * 0.25) * (opts.musicLevel ?? 1);
|
|
344
|
+
const fin = 0.5 * RATE, fout = 1.0 * RATE;
|
|
345
|
+
for (let i = 0; i < n; i++) { const f = Math.min(1, i / fin, (n - i) / fout); L[i] += mL[i] * g * f; R[i] += mR[i] * g * f; }
|
|
346
|
+
if (Number.isFinite(fx) && Number.isFinite(ml)) musicUnder = +(20 * Math.log10(g) + ml - fx).toFixed(2);
|
|
347
|
+
}
|
|
348
|
+
// Normalize: gain to the target, but never ask the limiter for more than maxReduction.
|
|
349
|
+
const before = loudness(L, R);
|
|
350
|
+
let peak = 0; for (let i = 0; i < n; i++) peak = Math.max(peak, Math.abs(L[i]), Math.abs(R[i]));
|
|
351
|
+
let gain = 0, reduction = 0, measured = -Infinity;
|
|
352
|
+
if (Number.isFinite(before)) {
|
|
353
|
+
gain = LOUDNESS.target - before;
|
|
354
|
+
const headroom = LOUDNESS.ceiling + LOUDNESS.maxReduction - 20 * Math.log10(peak);
|
|
355
|
+
gain = Math.min(gain, headroom);
|
|
356
|
+
const g = db(gain); for (let i = 0; i < n; i++) { L[i] *= g; R[i] *= g; }
|
|
357
|
+
reduction = limit(L, R, LOUDNESS.ceiling);
|
|
358
|
+
measured = loudness(L, R);
|
|
359
|
+
}
|
|
360
|
+
let out = 0; for (let i = 0; i < n; i++) out = Math.max(out, Math.abs(L[i]), Math.abs(R[i]));
|
|
361
|
+
return { L, R, cues: [...cues, ...returns].sort((a, b) => a.t - b.t), stats: { lufs: +measured.toFixed(2), peak_dbfs: +(20 * Math.log10(out || 1e-9)).toFixed(2), gain_db: +gain.toFixed(2), limiter_db: +reduction.toFixed(2), music_vs_fx_lu: musicUnder } };
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// 32-bit float stereo WAV.
|
|
365
|
+
export async function writeWav(path, L, R) {
|
|
366
|
+
const n = L.length, data = Buffer.alloc(n * 8), h = Buffer.alloc(44);
|
|
367
|
+
for (let i = 0; i < n; i++) { data.writeFloatLE(L[i], i * 8); data.writeFloatLE(R[i], i * 8 + 4); }
|
|
368
|
+
h.write('RIFF', 0); h.writeUInt32LE(36 + data.length, 4); h.write('WAVE', 8); h.write('fmt ', 12);
|
|
369
|
+
h.writeUInt32LE(16, 16); h.writeUInt16LE(3, 20); h.writeUInt16LE(2, 22); h.writeUInt32LE(RATE, 24);
|
|
370
|
+
h.writeUInt32LE(RATE * 8, 28); h.writeUInt16LE(8, 32); h.writeUInt16LE(32, 34); h.write('data', 36); h.writeUInt32LE(data.length, 40);
|
|
371
|
+
await writeFile(path, Buffer.concat([h, data]));
|
|
372
|
+
}
|