fix(voice): handle exact consumed-sample progress and direct native-rate playback
This commit is contained in:
@@ -32,22 +32,22 @@
|
||||
* { type: 'idle' } buffer ran dry after playing
|
||||
* { type: 'overflow' } bounded queue filled — caller fails the session safely
|
||||
*/
|
||||
const BOUND_BYTES = 96000; // contract cap on outstanding SOURCE PCM16
|
||||
const RESAMPLE_MARGIN = 64; // slack for fractional-boundary rounding (never widens the source bound)
|
||||
const BOUND_BYTES = 96000; // contract cap on outstanding SOURCE PCM16
|
||||
const RESAMPLE_MARGIN = 64; // slack for fractional-boundary rounding (never widens the source bound)
|
||||
const REPORT_INTERVAL_MS = 250;
|
||||
|
||||
class PcmPlaybackProcessor extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super();
|
||||
this.sourceCapacity = Math.floor(BOUND_BYTES / 2); // 48,000 SOURCE samples (admission bound)
|
||||
this.ratio = 1; // SOURCE samples per OUTPUT sample (resolved on first push)
|
||||
this.sized = false; // true once the output ring matches the real context rate
|
||||
this.ratio = 1; // SOURCE samples per OUTPUT sample (resolved on first push)
|
||||
this.sized = false; // true once the output ring matches the real context rate
|
||||
this.capacity = this.sourceCapacity; // OUTPUT slots currently allocated (grows for upsampling)
|
||||
this.buf = new Float32Array(this.capacity);
|
||||
this.r = 0;
|
||||
this.w = 0;
|
||||
this.count = 0; // OUTPUT samples currently queued
|
||||
this.queuedSource = 0; // SOURCE samples currently queued (the admission bound unit)
|
||||
this.count = 0; // OUTPUT samples currently queued
|
||||
this.queuedSource = 0; // SOURCE samples currently queued (the admission bound unit)
|
||||
this.prebuffer = Math.floor(sampleRate * 0.08); // jitter buffer before (re)starting
|
||||
this.started = false;
|
||||
this.prev = 0;
|
||||
@@ -56,17 +56,17 @@ class PcmPlaybackProcessor extends AudioWorkletProcessor {
|
||||
// `emittedOut` let flush() emit the exact OUTPUT count for all admitted source; the last
|
||||
// chunk window is retained so the tail can be interpolated correctly.
|
||||
this.admittedSrc = 0; // SOURCE samples admitted this turn
|
||||
this.emittedOut = 0; // OUTPUT samples emitted for them so far
|
||||
this.emittedOut = 0; // OUTPUT samples emitted for them so far
|
||||
this.windowSrc = null; // last chunk's src window (for tail interpolation)
|
||||
this.windowN = 0;
|
||||
// Per-audio_ref attribution. `refQueue` keeps the still-queued OUTPUT span of each ref
|
||||
// (merged per ref so it stays ≤ the number of turns); `refPlayedOut` keeps each ref's
|
||||
// cumulative OUTPUT samples consumed. Together they tie queued audio to its own ref and
|
||||
// keep progress cumulative and correct across ref changes.
|
||||
this.refQueue = []; // [{ ref, out }] out = remaining OUTPUT samples of that ref
|
||||
this.refQueue = []; // [{ ref, out }] out = remaining OUTPUT samples of that ref
|
||||
this.refPlayedOut = new Map();
|
||||
this.playingRef = null; // ref currently being consumed / reported
|
||||
this.pendingRef = null; // audio_ref of the most recent push (the one flush() tail belongs to)
|
||||
this.playingRef = null; // ref currently being consumed / reported
|
||||
this.pendingRef = null; // audio_ref of the most recent push (the one flush() tail belongs to)
|
||||
this.unreportedOut = 0;
|
||||
this.overflowPosted = false;
|
||||
this.port.onmessage = (e) => {
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user