Skip to content

Commit ce3909f

Browse files
committed
Explicitly close AudioResampler instances too free up resources
1 parent bf6811f commit ce3909f

12 files changed

Lines changed: 77 additions & 55 deletions

File tree

agents/src/inference/interruption/interruption_stream.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ export class InterruptionStreamBase {
414414

415415
async close(): Promise<void> {
416416
if (!this.inputStream.closed) await this.inputStream.close();
417+
this.resampler?.close();
417418
this.model.removeStream(this);
418419
}
419420
}

agents/src/stt/stt.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ export abstract class SpeechStream implements AsyncIterableIterator<SpeechEvent>
445445
if (!this.queue.closed) this.queue.close();
446446
if (!this.output.closed) this.output.close();
447447
if (!this.abortController.signal.aborted) this.abortController.abort();
448+
this.resampler?.close();
448449
this.closed = true;
449450
}
450451

agents/src/tts/fallback_adapter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ class FallbackChunkedStream extends ChunkedStream {
366366
});
367367
audioReceived = true;
368368
}
369+
resampler.close();
369370
}
370371

371372
// Verify audio was actually received - silent failures should trigger fallback
@@ -519,6 +520,7 @@ class FallbackSynthesizeStream extends SynthesizeStream {
519520
});
520521
this.audioPushed = true;
521522
}
523+
resampler.close();
522524
}
523525
} finally {
524526
// processOutput and forwardBufferToTTS run in parallel.

agents/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ export function resampleStream({
723723
for (const frame of resampler.flush()) {
724724
controller.enqueue(frame);
725725
}
726+
resampler.close();
726727
}
727728
},
728729
});

agents/src/voice/generation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ async function forwardAudio(
839839
for (const f of resampler.flush()) {
840840
await audioOutput.captureFrame(f);
841841
}
842+
resampler.close();
842843
}
843844
} catch (e) {
844845
if (e instanceof IdleTimeoutError) {

agents/src/voice/recorder_io/recorder_io.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ export class RecorderIO {
352352
} finally {
353353
inReader.releaseLock();
354354
outReader.releaseLock();
355+
this.inResampler?.close();
356+
this.outResampler?.close();
355357

356358
if (!this.closeFuture.done) {
357359
this.closeFuture.resolve();

plugins/google/src/beta/realtime/realtime_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ export class RealtimeSession extends llm.RealtimeSession {
810810
this.#closed = true;
811811

812812
this.sessionShouldClose.set();
813+
this.inputResampler?.close();
813814

814815
await this.closeActiveSession();
815816

@@ -1660,6 +1661,7 @@ export class RealtimeSession extends llm.RealtimeSession {
16601661
if (this.inputResampler) {
16611662
if (frame.sampleRate !== this.inputResamplerInputRate) {
16621663
// input audio changed to a different sample rate
1664+
this.inputResampler.close();
16631665
this.inputResampler = undefined;
16641666
this.inputResamplerInputRate = undefined;
16651667
}

plugins/phonic/src/realtime/realtime_model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ export class RealtimeSession extends llm.RealtimeSession {
440440
this.instructionsReady.resolve();
441441
this.toolsReady.resolve();
442442
this.closeCurrentGeneration({ interrupted: false });
443+
this.inputResampler?.close();
443444
this.socket?.close();
444445
await this.connectTask;
445446
await super.close();
@@ -748,6 +749,7 @@ export class RealtimeSession extends llm.RealtimeSession {
748749
private *resampleAudio(frame: AudioFrame): Generator<AudioFrame> {
749750
if (this.inputResampler) {
750751
if (frame.sampleRate !== this.inputResamplerInputRate) {
752+
this.inputResampler.close();
751753
this.inputResampler = undefined;
752754
this.inputResamplerInputRate = undefined;
753755
}

plugins/silero/src/vad.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ export class VADStream extends baseStream {
388388
}
389389
}
390390
}
391+
resampler?.close();
391392
});
392393
}
393394

plugins/test/src/stt.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ const makeTestSpeech = (targetSampleRate: number, chunkDuration?: number): Audio
130130
output.push(...resampler.push(frame));
131131
}
132132
output.push(...resampler.flush());
133+
resampler.close();
133134
frames = output;
134135
}
135136

0 commit comments

Comments
 (0)