fix(voice): handle exact consumed-sample progress and direct native-rate playback

This commit is contained in:
amirhosein mazaheri
2026-10-12 17:51:52 +03:30
parent b3eb4a6de5
commit 97427a1581
2 changed files with 63 additions and 12 deletions
@@ -150,6 +150,45 @@ describe('pcm-playback.worklet', () => {
expect(peakSource).toBeLessThan(proc.sourceCapacity);
});
describe('exact consumed-sample progress', () => {
it('plays the 364800-byte incident fully before generation ends at 24 kHz', async () => {
const proc = await loadPlaybackProcessor(24000);
for (let chunk = 0; chunk < 38; chunk++) {
push(proc, frame(chunk * 4800, 4800), 'aud-incident');
drain(proc, 128);
}
// No audio_ended/flush: every native-rate sample can already be rendered.
expect(proc.refPlayedOut.get('aud-incident')).toBe(182400);
expect(playedFor(proc, 'aud-incident')).toBe(7600);
expect(proc.count).toBe(0);
expect(overflowCount(proc)).toBe(0);
});
it('renders native-rate PCM directly without an inserted leading sample', async () => {
const proc = await loadPlaybackProcessor(24000);
const pcm = frame(200, 4800);
push(proc, pcm, 'aud-1');
const output = new Float32Array(128);
proc.process([[]], [[output]]);
expect(Array.from(output)).toEqual(Array.from(pcm.slice(0, 128), (v) => v / 32768));
expect(proc.refPlayedOut.get('aud-1')).toBe(128);
});
for (const rate of [24000, 48000]) {
it(`reports 1001 ms after rendering every sample at ${rate} Hz`, async () => {
const proc = await loadPlaybackProcessor(rate);
push(proc, frame(0, 24024), 'aud-1');
flush(proc);
drain(proc);
expect(proc.refPlayedOut.get('aud-1')).toBe((rate * 1001) / 1000);
expect(playedFor(proc, 'aud-1')).toBe(1001);
const values = proc.out.filter((m) => m.audioRef === 'aud-1').map((m) => m.playedMs ?? 0);
expect(values).toEqual([...values].sort((a, b) => a - b));
expect(values.every((ms) => ms <= 1001)).toBe(true);
});
}
});
describe('fractional resampling across chunk boundaries', () => {
// 6,000 source samples = 250 ms at 24 kHz. A linear resampler carries ~1 sample of latency
// across a boundary, so reported progress is within 1 ms of 250 — but it must never