fix(voice): handle exact consumed-sample progress and direct native-rate playback
This commit is contained in:
@@ -162,7 +162,8 @@ class PcmPlaybackProcessor extends AudioWorkletProcessor {
|
||||
this.configure(ratio);
|
||||
|
||||
// OUTPUT samples this chunk's main loop will add — an upper bound on the exact count.
|
||||
const produced = this.pos < n ? Math.ceil((n - this.pos) / this.ratio) : 0;
|
||||
const produced =
|
||||
this.ratio === 1 ? n : this.pos < n ? Math.ceil((n - this.pos) / this.ratio) : 0;
|
||||
|
||||
// Bounded queue — two consistent guards, each in its own unit:
|
||||
// 1) SOURCE bound: the contract 96,000-byte cap, invariant across context rates.
|
||||
@@ -173,6 +174,16 @@ class PcmPlaybackProcessor extends AudioWorkletProcessor {
|
||||
this.queuedSource += n;
|
||||
this.admittedSrc += n; // every admitted source sample must eventually become output
|
||||
|
||||
// Matching rates need no interpolation or deferred tail. Render every admitted
|
||||
// sample even while generation is still open; progress reflects only consumed PCM.
|
||||
if (this.ratio === 1) {
|
||||
this.pendingRef = audioRef;
|
||||
for (let i = 0; i < n; i++) this.write(int16[i] / 32768);
|
||||
this.creditQueue(n);
|
||||
this.emittedOut += n;
|
||||
return;
|
||||
}
|
||||
|
||||
const src = new Float32Array(n + 1);
|
||||
src[0] = this.prev;
|
||||
for (let i = 0; i < n; i++) src[i + 1] = int16[i] / 32768;
|
||||
@@ -296,7 +307,8 @@ class PcmPlaybackProcessor extends AudioWorkletProcessor {
|
||||
// OUTPUT samples at the context rate → milliseconds; floor never claims an unheard ms and
|
||||
// stays monotonic because it is cumulative. Output-time equals source-time (SRC preserves
|
||||
// duration), so this equals the contract's played source / srcRate.
|
||||
const playedMs = Math.floor((playedOut / sampleRate) * 1000);
|
||||
// Multiply integer counts first to avoid rounding an exact whole millisecond down.
|
||||
const playedMs = Math.floor((playedOut * 1000) / sampleRate);
|
||||
if (playedMs <= 0) return;
|
||||
this.port.postMessage({ type: 'progress', audioRef: ref, playedMs });
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user