Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-needles-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/agents": patch
---

Ensure resampling is skipped for empty audio frames
5 changes: 5 additions & 0 deletions agents/src/stt/stt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ export abstract class SpeechStream implements AsyncIterableIterator<SpeechEvent>
}
}

if (frame.samplesPerChannel === 0) {
this.input.put(frame);
return;
}

if (this.resampler) {
const frames = this.resampler.push(frame);
for (const frame of frames) {
Expand Down
12 changes: 10 additions & 2 deletions agents/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,22 @@ export function resampleStream({
let resampler: AudioResampler | null = null;
const transformStream = new TransformStream<AudioFrame, AudioFrame>({
transform(chunk: AudioFrame, controller: TransformStreamDefaultController<AudioFrame>) {
if (chunk.samplesPerChannel === 0) {
controller.enqueue(chunk);
return;
}
if (!resampler) {
resampler = new AudioResampler(chunk.sampleRate, outputRate);
}
for (const frame of resampler.push(chunk)) {
controller.enqueue(frame);
}
for (const frame of resampler.flush()) {
controller.enqueue(frame);
},
flush(controller) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This moves the flushing to the flush of the transform stream.

I don't know if the previous implemenation was intended to always flush after every insertion?

@toubatbrian do you happen to remember why this code path is flushing after each chunk?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the original PR: #541

What are the difference between flushing for each frames v.s. flushing at the very end? I guess technically they are the same but maybe flushing on every frame would incur some more performance costs?

if (resampler) {
for (const frame of resampler.flush()) {
controller.enqueue(frame);
}
}
},
});
Expand Down