Skip to content

Commit 2afb819

Browse files
authored
perf: precompute audio resample kernels
Precompute FIR resample kernels for common voice sample-rate conversions to avoid per-sample trigonometry while preserving output for tested ratios.\n\nVerification: node scripts/run-vitest.mjs extensions/voice-call/src/telephony-audio.test.ts; pnpm tsgo:core; autoreview --mode commit --commit HEAD; PR CI green.
1 parent 009b18c commit 2afb819

1 file changed

Lines changed: 90 additions & 6 deletions

File tree

‎src/talk/audio-codec.ts‎

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
const TELEPHONY_SAMPLE_RATE = 8000;
22
const RESAMPLE_FILTER_TAPS = 31;
33
const RESAMPLE_CUTOFF_GUARD = 0.94;
4+
const RESAMPLE_MAX_PRECOMPUTED_PHASES = 4096;
5+
const RESAMPLE_HALF_TAPS = Math.floor(RESAMPLE_FILTER_TAPS / 2);
6+
const RESAMPLE_WINDOW = Array.from(
7+
{ length: RESAMPLE_FILTER_TAPS },
8+
(_, tapIndex) => 0.5 - 0.5 * Math.cos((2 * Math.PI * tapIndex) / (RESAMPLE_FILTER_TAPS - 1)),
9+
);
10+
11+
type ResampleKernel = {
12+
coefficients: readonly Float64Array[];
13+
inputStep: number;
14+
phaseCount: number;
15+
};
416

517
function clamp16(value: number): number {
618
return Math.max(-32768, Math.min(32767, value));
@@ -13,28 +25,91 @@ function sinc(x: number): number {
1325
return Math.sin(Math.PI * x) / (Math.PI * x);
1426
}
1527

28+
function gcd(left: number, right: number): number {
29+
let a = Math.abs(Math.trunc(left));
30+
let b = Math.abs(Math.trunc(right));
31+
while (b !== 0) {
32+
const next = a % b;
33+
a = b;
34+
b = next;
35+
}
36+
return a || 1;
37+
}
38+
39+
function buildResampleKernel(
40+
inputSampleRate: number,
41+
outputSampleRate: number,
42+
cutoffCyclesPerSample: number,
43+
): ResampleKernel | undefined {
44+
if (!Number.isInteger(inputSampleRate) || !Number.isInteger(outputSampleRate)) {
45+
return undefined;
46+
}
47+
const divisor = gcd(inputSampleRate, outputSampleRate);
48+
const inputStep = inputSampleRate / divisor;
49+
const phaseCount = outputSampleRate / divisor;
50+
if (phaseCount > RESAMPLE_MAX_PRECOMPUTED_PHASES) {
51+
return undefined;
52+
}
53+
const coefficients = Array.from({ length: phaseCount }, (_, phaseIndex) => {
54+
const phase = phaseIndex / phaseCount;
55+
const phaseCoefficients = new Float64Array(RESAMPLE_FILTER_TAPS);
56+
for (let tap = -RESAMPLE_HALF_TAPS; tap <= RESAMPLE_HALF_TAPS; tap += 1) {
57+
const distance = tap - phase;
58+
const lowPass = 2 * cutoffCyclesPerSample * sinc(2 * cutoffCyclesPerSample * distance);
59+
const tapIndex = tap + RESAMPLE_HALF_TAPS;
60+
phaseCoefficients[tapIndex] = lowPass * (RESAMPLE_WINDOW[tapIndex] ?? 0);
61+
}
62+
return phaseCoefficients;
63+
});
64+
return { coefficients, inputStep, phaseCount };
65+
}
66+
67+
function sampleBandlimitedWithCoefficients(
68+
input: Buffer,
69+
inputSamples: number,
70+
center: number,
71+
coefficients: Float64Array,
72+
): number {
73+
let weighted = 0;
74+
let weightSum = 0;
75+
76+
for (let tap = -RESAMPLE_HALF_TAPS; tap <= RESAMPLE_HALF_TAPS; tap += 1) {
77+
const sampleIndex = center + tap;
78+
if (sampleIndex < 0 || sampleIndex >= inputSamples) {
79+
continue;
80+
}
81+
const coeff = coefficients[tap + RESAMPLE_HALF_TAPS] ?? 0;
82+
weighted += input.readInt16LE(sampleIndex * 2) * coeff;
83+
weightSum += coeff;
84+
}
85+
86+
if (weightSum === 0) {
87+
const nearest = Math.max(0, Math.min(inputSamples - 1, center));
88+
return input.readInt16LE(nearest * 2);
89+
}
90+
91+
return weighted / weightSum;
92+
}
93+
1694
function sampleBandlimited(
1795
input: Buffer,
1896
inputSamples: number,
1997
srcPos: number,
2098
cutoffCyclesPerSample: number,
2199
): number {
22-
const half = Math.floor(RESAMPLE_FILTER_TAPS / 2);
23100
const center = Math.floor(srcPos);
24101
let weighted = 0;
25102
let weightSum = 0;
26103

27-
for (let tap = -half; tap <= half; tap += 1) {
104+
for (let tap = -RESAMPLE_HALF_TAPS; tap <= RESAMPLE_HALF_TAPS; tap += 1) {
28105
const sampleIndex = center + tap;
29106
if (sampleIndex < 0 || sampleIndex >= inputSamples) {
30107
continue;
31108
}
32109

33110
const distance = sampleIndex - srcPos;
34111
const lowPass = 2 * cutoffCyclesPerSample * sinc(2 * cutoffCyclesPerSample * distance);
35-
const tapIndex = tap + half;
36-
const window = 0.5 - 0.5 * Math.cos((2 * Math.PI * tapIndex) / (RESAMPLE_FILTER_TAPS - 1));
37-
const coeff = lowPass * window;
112+
const coeff = lowPass * (RESAMPLE_WINDOW[tap + RESAMPLE_HALF_TAPS] ?? 0);
38113
weighted += input.readInt16LE(sampleIndex * 2) * coeff;
39114
weightSum += coeff;
40115
}
@@ -66,10 +141,19 @@ export function resamplePcm(
66141
const maxCutoff = 0.5;
67142
const downsampleCutoff = ratio > 1 ? maxCutoff / ratio : maxCutoff;
68143
const cutoffCyclesPerSample = Math.max(0.01, downsampleCutoff * RESAMPLE_CUTOFF_GUARD);
144+
const kernel = buildResampleKernel(inputSampleRate, outputSampleRate, cutoffCyclesPerSample);
69145

70146
for (let i = 0; i < outputSamples; i += 1) {
71147
const sample = Math.round(
72-
sampleBandlimited(input, inputSamples, i * ratio, cutoffCyclesPerSample),
148+
kernel
149+
? sampleBandlimitedWithCoefficients(
150+
input,
151+
inputSamples,
152+
Math.floor((i * inputSampleRate) / outputSampleRate),
153+
kernel.coefficients[(i * kernel.inputStep) % kernel.phaseCount] ??
154+
kernel.coefficients[0],
155+
)
156+
: sampleBandlimited(input, inputSamples, i * ratio, cutoffCyclesPerSample),
73157
);
74158
output.writeInt16LE(clamp16(sample), i * 2);
75159
}

0 commit comments

Comments
 (0)