MonEspacePro/docs/video/music.js
2026-10-08 15:30:12 +02:00

145 lines
9.4 KiB
JavaScript

// Musique originale 120 BPM, 30 s, calée sur les temps forts de video-pub.html
process.chdir(__dirname);
const fs = require('fs');
const SR = 48000, DUR = 30, N = SR * DUR;
const L = new Float32Array(N), R = new Float32Array(N);
const BEAT = 0.5;
let seed = 12345; const rnd = () => (seed = (seed * 1664525 + 1013904223) >>> 0) / 4294967296;
const mtof = m => 440 * Math.pow(2, (m - 69) / 12);
function add(t, dur, fn, pan = 0, buf = null) {
const s0 = Math.floor(t * SR), n = Math.floor(dur * SR);
const gl = Math.cos((pan + 1) * Math.PI / 4), gr = Math.sin((pan + 1) * Math.PI / 4);
for (let i = 0; i < n; i++) {
const k = s0 + i; if (k < 0 || k >= N) continue;
const v = fn(i / SR, i);
if (buf) { buf[k] += v; continue; }
L[k] += v * gl * 1.41; R[k] += v * gr * 1.41;
}
}
const inRange = (t, rs) => rs.some(([a, b]) => t >= a - 1e-6 && t < b - 1e-6);
/* ---------- Sections ---------- */
const DRUMS = [[0, 6], [8, 29.5]];
const DROP = [[8, 12], [15, 18], [22, 29.5]]; // arpège lead
const PADS = [[0, 30]];
const CHORDS = [[57, 60, 64], [53, 57, 60], [52, 55, 60], [55, 59, 62]]; // Am F C G
const ROOTS = [45, 41, 48, 43];
const chordAt = t => Math.floor(t / 2) % 4;
/* ---------- Sidechain ---------- */
const kicks = [];
for (let t = 0; t < DUR; t += BEAT) if (inRange(t, DRUMS)) kicks.push(t);
const duck = new Float32Array(N).fill(1);
for (const tk of kicks) for (let i = 0; i < SR * .4; i++) { const k = Math.floor(tk * SR) + i; if (k < N) duck[k] = Math.min(duck[k], 1 - .65 * Math.exp(-i / SR / .11)); }
/* ---------- Batterie ---------- */
function kick(t, g = 1) {
let ph = 0;
add(t, .45, (x) => { const f = 45 + 110 * Math.exp(-x / .04); ph += 2 * Math.PI * f / SR; return g * .95 * Math.sin(ph) * Math.exp(-x / .16) + g * .3 * (rnd() * 2 - 1) * Math.exp(-x / .004); });
}
function clap(t, g = 1) {
let lp = 0, prev = 0;
add(t, .3, (x) => { const n = rnd() * 2 - 1; const hp = n - prev; prev = n; lp += (hp - lp) * .5; const env = (x < .03 ? (Math.exp(-(x % .01) / .003)) : 1) * Math.exp(-x / .09); return g * .5 * lp * env + g * .18 * Math.sin(2 * Math.PI * 190 * x) * Math.exp(-x / .05); }, (rnd() - .5) * .3);
}
function hat(t, g = 1, open = false) {
let prev = 0;
add(t, open ? .25 : .06, (x) => { const n = rnd() * 2 - 1; const hp = n - prev; prev = n; return g * .22 * hp * Math.exp(-x / (open ? .08 : .018)); }, .35);
}
for (let t = 0; t < DUR; t += .125) {
if (!inRange(t, DRUMS)) continue;
const b = Math.round(t / .125) % 8;
if (b % 4 === 0) kick(t);
if (b === 4) clap(t);
if (b === 2 || b === 6) hat(t, 1, inRange(t, DROP));
else hat(t, .45);
}
// roulement de caisse claire avant les ruptures (5,0→6,0 et 7,0→8,0, 14,0→15,0)
for (const [a, b] of [[5, 6], [7, 8], [14, 15], [25, 26]]) {
for (let t = a; t < b - 1e-6;) { const p = (t - a) / (b - a); clap(t, .35 + .6 * p); t += p < .5 ? .125 : .0625; }
}
/* ---------- Basse ---------- */
for (let t = 0; t < 29.5; t += .25) {
if (!inRange(t, DRUMS)) continue;
const b = Math.round(t / .25) % 2; if (b === 0 && !inRange(t, DROP)) continue;
const f = mtof(ROOTS[chordAt(t)] - 12 + (b ? 12 : 0) * 0), d = .22;
let ph = 0, lp = 0;
add(t, d, (x) => { ph = (ph + f / SR) % 1; const saw = 2 * ph - 1; const cut = .02 + .25 * Math.exp(-x / .05); lp += (saw - lp) * cut; return .55 * lp * Math.min(1, x / .004) * Math.exp(-x / .25) * (x > d - .01 ? (d - x) / .01 : 1); });
}
/* ---------- Pad supersaw (pompé par le kick) ---------- */
const pad = new Float32Array(N), padR = new Float32Array(N);
for (let bar = 0; bar < 15; bar++) {
const t = bar * 2; if (!inRange(t, PADS)) continue;
const notes = CHORDS[bar % 4];
for (const m of notes) for (const det of [-.12, 0, .11]) {
const f = mtof(m + det); let ph = rnd(), lp = 0;
const brk = inRange(t, [[6, 8]]);
add(t, 2.05, (x) => { ph = (ph + f / SR) % 1; lp += ((2 * ph - 1) - lp) * (brk ? .03 + .05 * (x / 2) : .09); return .055 * lp * Math.min(1, x / .02) * (x > 1.98 ? Math.max(0, (2.05 - x) / .07) : 1); }, 0, det < 0 ? pad : padR);
}
}
for (let k = 0; k < N; k++) { const g = (inRange(k / SR, [[6, 8]]) ? 1.6 : 1) * duck[k]; L[k] += (pad[k] * .8 + padR[k] * .2) * g; R[k] += (padR[k] * .8 + pad[k] * .2) * g; }
/* ---------- Arpège lead (sections drop) + délai ---------- */
const lead = new Float32Array(N);
for (let t = 0; t < DUR; t += .125) {
if (!inRange(t, DROP)) continue;
const c = CHORDS[chordAt(t)], i = Math.round(t / .125) % 8;
const m = c[[0, 1, 2, 1, 0, 2, 1, 2][i]] + 12 + (i === 7 ? 12 : 0), f = mtof(m);
let ph = 0, lp = 0;
add(t, .2, (x) => { ph = (ph + f / SR) % 1; const sq = ph < .5 ? 1 : -1; lp += (sq - lp) * (.05 + .3 * Math.exp(-x / .03)); return .08 * lp * Math.exp(-x / .07); }, 0, lead);
}
{ const d = Math.floor(.375 * SR); for (let k = 0; k < N; k++) { const dl = k >= d ? lead[k - d] : 0; const dr = k >= 2 * d ? lead[k - 2 * d] : 0; L[k] += (lead[k] + .45 * dl) * duck[k]; R[k] += (lead[k] + .35 * dr) * duck[k]; } }
/* ---------- Effets synchronisés à l'image ---------- */
function boom(t, g = 1) { let ph = 0; add(t, 1.6, (x) => { const f = 38 + 80 * Math.exp(-x / .06); ph += 2 * Math.PI * f / SR; return g * (.8 * Math.sin(ph) * Math.exp(-x / .5) + .25 * (rnd() * 2 - 1) * Math.exp(-x / .08)); }); }
function whoosh(t, d = .35, g = 1) { let lp = 0; add(t - d * .7, d, (x) => { const p = x / d; lp += ((rnd() * 2 - 1) - lp) * (.02 + .5 * p * p); return g * .5 * lp * Math.sin(Math.PI * p); }, 0); }
function riser(a, b, g = 1) { let lp = 0, ph = 0; add(a, b - a, (x) => { const p = x / (b - a); lp += ((rnd() * 2 - 1) - lp) * (.01 + .4 * p * p); ph = (ph + (200 + 1400 * p * p) / SR) % 1; return g * (.35 * lp + .05 * (2 * ph - 1)) * p * p; }); }
function bell(t, m, g = 1, d = .6) { const f = mtof(m); add(t, d, (x) => g * .22 * (Math.sin(2 * Math.PI * f * x) + .35 * Math.sin(2 * Math.PI * f * 2.01 * x) * Math.exp(-x / .08)) * Math.exp(-x / (d / 4)), (rnd() - .5) * .4); }
function beep(t, f, d = .07) { add(t, d, (x) => .16 * Math.sin(2 * Math.PI * f * x) * (x < .005 ? x / .005 : 1) * (x > d - .01 ? (d - x) / .01 : 1)); }
function boing(t) { let ph = 0; add(t, .35, (x) => { const f = 300 + 500 * Math.exp(-x / .06) * (1 + .2 * Math.sin(2 * Math.PI * 18 * x)); ph += 2 * Math.PI * f / SR; return .2 * Math.sin(ph) * Math.exp(-x / .12); }); }
boom(0, .9); // 0,0 accroche
kick(.5, 1.2); boom(.5, .6); // 0,5 impact du tampon
whoosh(1.9, .25); // 1,9 whip pan
boom(2.5, .6); // 2,5 CARTON
boom(3, .4); whoosh(4, .15, .6); whoosh(5.07, .25); // slam / coupe / whip vertical
for (const t of [3.5, 4.5, 5.5]) clap(t, 1.1); // traits rouges
riser(4.5, 6, .9); boom(6, 1.1); // rupture verte
bell(6.25, 81, .7, 1.2); bell(7.5, 88, .7, 1.2); // « Et si… » / « téléphone ? »
riser(7, 8, .7); boom(8, .9); // drop comptoir
boing(8.5); kick(9, .8); // téléphone qui arrive / se pose
beep(9.5, 1975); beep(9.6, 2637, .1); // bip du scan
whoosh(10.42, .2, .6);
riser(11, 12, .6); whoosh(12.1, .35, 1); // zoom à travers l'écran
bell(13, 76, .8); bell(13.5, 81, .6); bell(14, 84, .6); // carte dans le Wallet + pastilles
whoosh(15.05, .3, .8); // volet diagonal
[15.5, 16.25, 16.75, 17.25, 17.5, 17.75].forEach((t, i) => { bell(t, [69, 72, 74, 76, 79, 81][i], .9, .5); kick(t, .5); });
boom(18, 1.2); bell(18, 84, 1, 1.4); bell(18, 88, .8, 1.4); bell(18.06, 93, .6, 1.4); // 10/10 confettis
for (let i = 0; i < 26; i++) hat(18 + i * .03 + rnd() * .02, .7, true);
bell(18.5, 88, .6);
whoosh(20.07, .25); // whip pan
bell(20.75, 88, .8, .5); bell(20.87, 93, .8, .7); // notification
boom(22, .8); // flash
bell(22.35, 81, .5); bell(23.3, 84, .5); bell(24.55, 88, .5); // coches
for (let i = 0; i < 12; i++) beep(24.65 + i * .05, 1200 + i * 90, .03); // compteur 0 → 19 €
bell(25.25, 93, .9, .9); bell(25.25, 88, .6, .9);
boom(26, .7); whoosh(26.1, .3, .6); // signature
boing(26.5); boom(26.5, .5); // point jaune
bell(27.25, 81, .5); bell(27.75, 88, .5);
riser(28.5, 29.5, .7);
boom(29.5, .5); kick(29.75, 1); boom(29.75, .7); // la carte retombe (raccord boucle)
/* ---------- Master ---------- */
let peak = 0;
for (let k = 0; k < N; k++) { L[k] = Math.tanh(L[k] * 1.3); R[k] = Math.tanh(R[k] * 1.3); peak = Math.max(peak, Math.abs(L[k]), Math.abs(R[k])); }
const norm = .89 / peak;
// micro fondu de 5 ms aux extrémités
for (let k = 0; k < 240; k++) { const g = k / 240; L[k] *= g; R[k] *= g; L[N - 1 - k] *= g; R[N - 1 - k] *= g; }
const out = Buffer.alloc(44 + N * 4);
out.write('RIFF', 0); out.writeUInt32LE(36 + N * 4, 4); out.write('WAVE', 8); out.write('fmt ', 12);
out.writeUInt32LE(16, 16); out.writeUInt16LE(1, 20); out.writeUInt16LE(2, 22); out.writeUInt32LE(SR, 24);
out.writeUInt32LE(SR * 4, 28); out.writeUInt16LE(4, 32); out.writeUInt16LE(16, 34); out.write('data', 36); out.writeUInt32LE(N * 4, 40);
for (let k = 0; k < N; k++) { out.writeInt16LE(Math.round(Math.max(-1, Math.min(1, L[k] * norm)) * 32767), 44 + k * 4); out.writeInt16LE(Math.round(Math.max(-1, Math.min(1, R[k] * norm)) * 32767), 46 + k * 4); }
fs.writeFileSync('music.wav', out);
console.log('music.wav ok, peak avant normalisation', peak.toFixed(2));