/ proof-of-concept / instrawave-compose
Sign in
proof-of-concept / instrawave-compose
instrawave-compose / src / render / mix.ts
33 lines · 1.2 KBBlameHistoryRaw
1/**
2 * Small pure helpers for combining traces from different renderers at
3 * different rates. Resampling is linear interpolation, which is adequate
4 * here because the instrawave traces arrive oversampled (the solver rate,
5 * around 114 kHz) and the content sits far below either rate.
6 */
7export function resampleLinear(x: Float32Array, srFrom: number, srTo: number): Float32Array {
8 if (x.length === 0) return new Float32Array(0);
9 const n = Math.max(1, Math.round((x.length * srTo) / srFrom));
10 const out = new Float32Array(n);
11 for (let i = 0; i < n; i++) {
12 const s = (i * srFrom) / srTo;
13 const k = Math.min(x.length - 2, Math.floor(s));
14 const f = Math.min(1, Math.max(0, s - k));
15 out[i] = (1 - f) * x[k] + f * x[Math.min(x.length - 1, k + 1)];
16 }
17 return out;
20export function peak(x: Float32Array): number {
21 let p = 0;
22 for (let i = 0; i < x.length; i++) {
23 const a = Math.abs(x[i]);
24 if (a > p) p = a;
25 }
26 return p;
29/** dst += src * gain, clipped to dst's length. */
30export function mixInto(dst: Float32Array, src: Float32Array, gain = 1): void {
31 const n = Math.min(dst.length, src.length);
32 for (let i = 0; i < n; i++) dst[i] += src[i] * gain;
↑↓ move↵openescclose