// TRAILER HITS: the movie-trailer boom the teaser card plays under each pop // (deck.mjs teaserHits, build-video.mjs teaserAudioGraph -- the ferret-rescue // finale's), factored out so a post item's SLAM (post-slam.mjs) plays the same // generator: one hit per glyph, the title tier, and optionally the tail's swell. // // Synthesised in ffmpeg, no samples: nothing is downloaded or bundled. The // teaser's graph is byte for byte what it was before this module (its tests, // and so the ferret cut's render key, are unchanged); the slam's names its own // labels (`prefix`) so the graph can sit inside a post segment's. // // Plain ESM, no imports: build-video, deck, post-render and the browser (Play's // live sound, trailerHitSamples) all read it. /** * The hits' tiers (gains relative: the main title is 1): a title's hit is the * biggest, an overline's and a kicker's a little smaller, a second tier's * lighter and shorter. `decay` is the boom's time constant, `f0 → f1` its pitch drop. */ export const TRAILER_HIT_TIERS = Object.freeze({ title: Object.freeze({ gain: 1, decay: 0.42, f0: 92, f1: 40 }), overline: Object.freeze({ gain: 0.72, decay: 0.34, f0: 96, f1: 44 }), kicker: Object.freeze({ gain: 0.8, decay: 0.36, f0: 94, f1: 42 }), sub: Object.freeze({ gain: 0.42, decay: 0.16, f0: 120, f1: 64 }), }); /** The tail's swell (a low rise under a slow entrance): its gain, decay and pitch, the duration the caller's. */ export const TRAILER_SWELL = Object.freeze({ gain: 0.34, decay: 0.7, f0: 46, f1: 62 }); /** The sound's level and ceiling: `limit` is −6 dBFS; `level` is set against the ferret cut's loudness (README). */ export const TRAILER_AUDIO = Object.freeze({ level: 1.4, limit: 0.5 }); /** A number for an aevalsrc expression: 6 decimals, no trailing zeros. */ export const ev6 = (v) => { const s = (Math.round(Number(v) * 1e6) / 1e6).toFixed(6).replace(/\.?0+$/, ""); return s === "-0" ? "0" : s; }; /** The hash noise every layer uses (the same whatever else is in the graph), as an expression of the sample `n`. */ const NOISE = "(2*(sin(n*12.9898+78.233)*43758.5453-floor(sin(n*12.9898+78.233)*43758.5453))-1)"; /** * The hits' sound, as one filtergraph fragment from no inputs to `[a]` * (`prefix` "t": the teaser's `[ta]`, and its inner labels `[tb] [tp] [tr] [tw]`). * * A hit is three layers, each summed over every hit in one `aevalsrc` and * gated to its own span, so each starts on the sample its time names: * - the boom: a sine whose pitch drops from f0 to f1 (most of the way in * ~0.4 s), with its octave for body, on an exponential decay (`decay` is * the time constant; it is inaudible by ~7×) after a 3 ms attack; * - the punch: a burst of noise (50 ms time constant), band-passed (180 Hz–3.2 kHz); * - the tail: a low noise decay (low-passed at 260 Hz) under it. * A swell is the boom's sine rising f0 → f1 under an envelope that peaks * three quarters of the way through `dur` and settles, with a breath of the * low noise. A riser (a dip's, under the black) is a sub whose pitch climbs * f0 → f1 over `dur` and a noise swell -- its own layer, band-passed * 400 Hz–6.5 kHz, present only when there is a riser -- both rising (the sub * squared, the noise cubed) to their peak at the first hit and released over * `decay`. The sum takes a short low-passed echo, then `level`, then a limiter * at `limit` (no auto-level, latency compensated) so hits that overlap still * sum cleanly; trimmed and padded to exactly `seconds`. No hits: digital silence. * * @param {Array<{ kind: string, at: number, gain: number, decay: number, f0: number, f1: number, dur?: number }>} hits * @param {{ seconds: number, rate: number, layout?: string, level?: number, limit?: number, prefix?: string }} o */ export function trailerHitsGraph(hits, { seconds, rate, layout = "stereo", level = TRAILER_AUDIO.level, limit = TRAILER_AUDIO.limit, prefix = "t" }) { const p = prefix; const len = ev6(seconds); const tail = `atrim=end=${len},apad=whole_dur=${len},asetpts=PTS-STARTPTS[${p}a]`; if (!hits.length) return `anullsrc=channel_layout=${layout}:sample_rate=${rate},${tail}`; const noise = NOISE; const boom = []; const punch = []; const rumble = []; const whoosh = []; for (const h of hits) { const a = ev6(h.at); const u = `(t-${a})`; const g = ev6(h.gain); if (h.kind === "riser") { const D = ev6(h.dur); const v = `min(${u},${D})`; const phase = `(${ev6(h.f0)}*${v}+${ev6((h.f1 - h.f0) / (2 * h.dur))}*${v}*${v}+${ev6(h.f1)}*(${u}-${v}))`; const rel = `clip((${ev6(h.dur + h.decay)}-${u})/${ev6(h.decay)},0,1)`; const span = ev6(h.dur + h.decay); boom.push(`if(between(t,${a},${a}+${span}),${g}*0.5*pow(${v}/${D},2)*${rel}*sin(2*PI*${phase}),0)`); whoosh.push(`if(between(t,${a},${a}+${span}),${g}*0.6*pow(${v}/${D},3)*${rel}*${noise},0)`); continue; } if (h.kind === "swell") { const D = h.dur; const peak = ev6(D * 0.75); const v = `min(${u},${ev6(D)})`; const phase = `(${ev6(h.f0)}*${v}+${ev6((h.f1 - h.f0) / (2 * D))}*${v}*${v}+${ev6(h.f1)}*(${u}-${v}))`; const env = `pow(sin(PI/2*min(1,${u}/${peak})),2)*exp(-max(0,${u}-${peak})/${ev6(h.decay)})`; const span = ev6(D * 0.75 + h.decay * 7); boom.push(`if(between(t,${a},${a}+${span}),${g}*0.42*${env}*sin(2*PI*${phase}),0)`); rumble.push(`if(between(t,${a},${a}+${span}),${g}*0.5*${env}*${noise},0)`); continue; } const k = HIT_PITCH_TAU; const phase = `(${ev6(h.f1)}*${u}+${ev6((h.f0 - h.f1) * k)}*(1-exp(-${u}/${k})))`; const span = ev6(h.decay * 7); boom.push( `if(between(t,${a},${a}+${span}),${g}*0.3*min(1,${u}/0.003)*exp(-${u}/${ev6(h.decay)})*` + `(sin(2*PI*${phase})+0.6*exp(-${u}/${ev6(h.decay * 0.6)})*sin(4*PI*${phase})),0)`, ); punch.push(`if(between(t,${a},${a}+0.25),${g}*0.5*exp(-${u}/0.05)*${noise},0)`); rumble.push(`if(between(t,${a},${a}+${ev6(Math.min(2.6, h.decay * 6))}),${g}*0.32*min(1,${u}/0.02)*exp(-${u}/${ev6(h.decay * 1.3)})*${noise},0)`); } const src = (terms) => `aevalsrc=exprs='${terms.length ? terms.join("+") : "0"}':s=${rate}:c=${layout}:d=${len}`; // The riser's layer only when there is one: a teaser without a dip writes the graph it always did. const w = whoosh.length ? [`${src(whoosh)},highpass=f=400,lowpass=f=6500[${p}w]`] : []; return [ `${src(boom)}[${p}b]`, `${src(punch)},highpass=f=180,lowpass=f=3200[${p}p]`, `${src(rumble)},lowpass=f=260,lowpass=f=260[${p}r]`, ...w, `[${p}b][${p}p][${p}r]${w.length ? `[${p}w]` : ""}amix=inputs=${3 + w.length}:normalize=0,aecho=1:1:90|190:0.18|0.09,lowpass=f=5000,` + `volume=${ev6(level)},alimiter=limit=${ev6(limit)}:level=0:latency=1:attack=2:release=80,${tail}`, ].join(";"); } /** The hit's pitch drop's time constant, seconds. */ export const HIT_PITCH_TAU = 0.13; /** * ONE HIT's raw layers as samples (Play's live sound: the same formulas as * trailerHitsGraph's `aevalsrc`, which a browser then filters as the graph does * -- the punch band-passed 180 Hz–3.2 kHz, the rumble low-passed at 260 Hz). * `{ boom, punch, rumble }`, Float32Arrays of `seconds` at `rate`, from the hit's * own start (sample 0 is its `at`). * @param {{ gain: number, decay: number, f0: number, f1: number }} h * @param {number} rate */ export function trailerHitSamples(h, rate = 48000) { const seconds = Math.min(3, h.decay * 7); const n = Math.max(1, Math.round(seconds * rate)); const boom = new Float32Array(n); const punch = new Float32Array(n); const rumble = new Float32Array(n); const k = HIT_PITCH_TAU; const fr = (x) => x - Math.floor(x); for (let i = 0; i < n; i += 1) { const u = i / rate; const phase = h.f1 * u + (h.f0 - h.f1) * k * (1 - Math.exp(-u / k)); boom[i] = h.gain * 0.3 * Math.min(1, u / 0.003) * Math.exp(-u / h.decay) * (Math.sin(2 * Math.PI * phase) + 0.6 * Math.exp(-u / (h.decay * 0.6)) * Math.sin(4 * Math.PI * phase)); const nz = 2 * fr(Math.sin(i * 12.9898 + 78.233) * 43758.5453) - 1; punch[i] = u <= 0.25 ? h.gain * 0.5 * Math.exp(-u / 0.05) * nz : 0; rumble[i] = u <= Math.min(2.6, h.decay * 6) ? h.gain * 0.32 * Math.min(1, u / 0.02) * Math.exp(-u / (h.decay * 1.3)) * nz : 0; } return { boom, punch, rumble, seconds }; }