// Speed without the chipmunk: timeStretch keeps the pitch and the edges. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { stretchedLength, timeStretch, wantsStretch } from "./timeStretch.mjs"; const SR = 48000; const tone = (hz, seconds, sr = SR, amp = 0.5) => { const x = new Float32Array(Math.round(seconds * sr)); for (let i = 0; i < x.length; i++) x[i] = amp * Math.sin((2 * Math.PI * hz * i) / sr); return x; }; // The frequency, from upward zero crossings over the middle of the signal // (away from either edge). const frequency = (x, sr = SR) => { const a = Math.floor(x.length * 0.1); const b = Math.floor(x.length * 0.9); let n = 0; let first = -1; let last = -1; for (let i = a + 1; i < b; i++) { if (x[i - 1] < 0 && x[i] >= 0) { if (first < 0) first = i; last = i; n++; } } return ((n - 1) * sr) / (last - first); }; test("1x (and anything within a hair of it) is played as it is", () => { assert.equal(wantsStretch(1), false); assert.equal(wantsStretch(1.004), false); assert.equal(wantsStretch(1.25), true); assert.equal(wantsStretch(0.75), true); assert.equal(wantsStretch(0), false); assert.equal(wantsStretch(Number.NaN), false); const x = tone(220, 0.5); const [y] = timeStretch([x], SR, 1); assert.deepEqual(y, x); assert.notEqual(y, x, "a copy, not the decoded buffer's own array"); }); test("the length is the input's divided by the rate, on every channel", () => { for (const rate of [1.25, 1.5, 2, 3, 4, 0.5]) { const out = timeStretch([tone(220, 2), tone(330, 2)], SR, rate); assert.equal(out.length, 2); for (const ch of out) assert.equal(ch.length, stretchedLength(2 * SR, rate), `at ${rate}x`); } }); test("the pitch stays where it was: a 220 Hz tone at 2x is still 220 Hz", () => { for (const rate of [1.25, 1.5, 2, 2.5, 3, 3.5, 4]) { const [y] = timeStretch([tone(220, 3)], SR, rate); const f = frequency(y); assert.ok(Math.abs(f - 220) < 3, `${f.toFixed(1)} Hz at ${rate}x`); // A tape-speed playback would have been 220 * rate. } // And a lower voice, a higher one, a slower speed. for (const hz of [110, 440]) { const f = frequency(timeStretch([tone(hz, 3)], SR, 1.75)[0]); assert.ok(Math.abs(f - hz) < hz * 0.015, `${f.toFixed(1)} Hz for ${hz} Hz at 1.75x`); } const f = frequency(timeStretch([tone(220, 2)], SR, 0.5)[0]); assert.ok(Math.abs(f - 220) < 3, `${f.toFixed(1)} Hz at 0.5x`); }); test("a steady tone comes out steady: no dips where the frames overlap", () => { const [y] = timeStretch([tone(220, 3)], SR, 2); // RMS over 10 ms windows, away from the edges, stays near the tone's own // (0.5 / sqrt 2): an out-of-phase overlap would cancel and dip. const want = 0.5 / Math.SQRT2; const w = SR / 100; for (let s = w * 5; s + w < y.length - w * 5; s += w) { let e = 0; for (let i = s; i < s + w; i++) e += y[i] * y[i]; const rms = Math.sqrt(e / w); assert.ok(Math.abs(rms - want) < want * 0.08, `rms ${rms.toFixed(3)} at ${(s / SR).toFixed(2)} s`); } }); test("the range begins with its own first samples, unfaded", () => { const x = tone(180, 1); const [y] = timeStretch([x], SR, 2); for (let i = 0; i < 600; i++) assert.ok(Math.abs(y[i] - x[i]) < 1e-6, `sample ${i}`); }); test("silence stays silent and a short range is still the right length", () => { const [z] = timeStretch([new Float32Array(SR)], SR, 1.5); assert.equal(z.length, stretchedLength(SR, 1.5)); assert.ok(z.every((v) => v === 0)); // Under one frame: resampled, the length still right. const [s] = timeStretch([tone(220, 0.02)], SR, 2); assert.equal(s.length, stretchedLength(Math.round(0.02 * SR), 2)); assert.deepEqual(timeStretch([], SR, 2), []); }); test("the decode cap (120 s of stereo) stretches in well under a click's patience", () => { const a = tone(140, 120); const b = tone(150, 120); const t = performance.now(); const out = timeStretch([a, b], SR, 1.5); const ms = performance.now() - t; assert.equal(out[0].length, stretchedLength(a.length, 1.5)); // Generous for a loaded CI box; on this machine it is a few hundred ms. assert.ok(ms < 4000, `${Math.round(ms)} ms`); }); test("the range ends with its own last samples, unfaded", () => { const x = tone(180, 1); for (const rate of [1.5, 2]) { const [y] = timeStretch([x], SR, rate); for (let i = 1; i <= 600; i++) { assert.ok(Math.abs(y[y.length - i] - x[x.length - i]) < 1e-6, `sample -${i} at ${rate}x`); } } });