Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | /// <reference path="./signalsmith-stretch.d.ts" /> /** * Offline time stretching with Signalsmith Stretch (MIT; `signalsmith-stretch`, WASM in an AudioWorklet): the signal is * rendered through a stretch node in an `OfflineAudioContext`. The library is loaded only when this is used. */ /** Room after the stretched signal for the stretcher's tail. */ const TAIL_SECONDS = 0.25; interface StretchNode extends AudioNode { addBuffers(buffers: Float32Array[]): Promise<number>; schedule(change: { active?: boolean; input?: number; output?: number; rate?: number; semitones?: number; }): Promise<unknown> | void; latency(): Promise<number> | number; } /** * `channels` played `rate` times as fast at their own pitch (the result lasts `length / rate` samples), stretched by * Signalsmith Stretch. Rejects where AudioWorklet or WASM is unavailable; the caller falls back to WSOLA. */ export async function signalsmithStretchChannels( channels: readonly Float32Array[], sampleRate: number, rate: number, onProgress?: (fraction: number) => void, ): Promise<Float32Array<ArrayBuffer>[]> { const inputLength = channels[0]?.length ?? 0; const outputLength = Math.max(1, Math.round(inputLength / rate)); const { default: SignalsmithStretch } = (await import('signalsmith-stretch')) as unknown as { default: (context: BaseAudioContext, options?: AudioWorkletNodeOptions) => Promise<StretchNode>; }; const quantum = 128 / sampleRate; const tail = Math.round(TAIL_SECONDS * sampleRate); // Sized for up to a second of node latency before the signal. The worklet handles its messages between render // quanta, so the buffers and the start are set while the render is suspended after its first quantum. const context = new OfflineAudioContext(channels.length, outputLength + tail + Math.round(sampleRate), sampleRate); // One (unconnected) input: the processor renders nothing without it. const node = await SignalsmithStretch(context, { numberOfInputs: 1, numberOfOutputs: 1, outputChannelCount: [channels.length], }); node.connect(context.destination); let startSeconds = 0; void context.suspend(quantum).then(async () => { await node.addBuffers(channels.map((channel) => new Float32Array(channel))); const latency = await node.latency(); // The node compensates for its own latency when a change is scheduled at least that far ahead. startSeconds = Math.ceil((quantum + latency) * sampleRate + 1) / sampleRate; await node.schedule({ active: true, input: 0, output: startSeconds, rate, semitones: 0 }); await context.resume(); }); const poll = onProgress && setInterval(() => { if (startSeconds > 0) { onProgress(Math.max(0, Math.min(1, ((context.currentTime - startSeconds) * sampleRate) / outputLength))); } }, 100); const rendered = await context.startRendering().finally(() => clearInterval(poll)); onProgress?.(1); const start = Math.round(startSeconds * sampleRate); return Array.from({ length: channels.length }, (_, channel) => { const out = new Float32Array(outputLength); out.set(rendered.getChannelData(channel).subarray(start, start + outputLength)); return out; }); } |