Skip to content
Open
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/steady-stereo-resampling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/agents': patch
---

Preserve audio channels during stream resampling and reconfigure the resampler when the input channel count changes.
36 changes: 36 additions & 0 deletions agents/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,42 @@ world
}
});

it('preserves stereo channels and duration when resampling', async () => {
const inputFrame = createAudioFrame(48000, 960, 2);
const inputStream = new ReadableStream<AudioFrame>({
start(controller) {
controller.enqueue(inputFrame);
controller.close();
},
});
const frames = await streamToArray(
resampleStream({ stream: inputStream, outputRate: 16000 }),
);
expect(frames.length).toBeGreaterThan(0);
expect(frames.every((frame) => frame.channels === 2)).toBe(true);
expect(frames.reduce((total, frame) => total + frame.samplesPerChannel, 0)).toBe(320);
});

it('reconfigures the resampler when the channel count changes', async () => {
const inputStream = new ReadableStream<AudioFrame>({
start(controller) {
controller.enqueue(createAudioFrame(48000, 960, 1));
controller.enqueue(createAudioFrame(48000, 960, 2));
controller.close();
},
});
const frames = await streamToArray(
resampleStream({ stream: inputStream, outputRate: 16000 }),
);
for (const channels of [1, 2]) {
expect(
frames
.filter((frame) => frame.channels === channels)
.reduce((total, frame) => total + frame.samplesPerChannel, 0),
).toBe(320);
}
});

it('should handle same input and output rate', async () => {
const sampleRate = 44100;
const inputFrame = createAudioFrame(sampleRate, 1024);
Expand Down
10 changes: 8 additions & 2 deletions agents/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ export function resampleStream({
}): ReadableStream<AudioFrame> {
let resampler: AudioResampler | null = null;
let currentInputRate = 0;
let currentChannels = 0;
const transformStream = new TransformStream<AudioFrame, AudioFrame>({
transform(chunk: AudioFrame, controller: TransformStreamDefaultController<AudioFrame>) {
if (chunk.samplesPerChannel === 0) {
Expand All @@ -783,15 +784,20 @@ export function resampleStream({
return;
}

if (!resampler || currentInputRate !== chunk.sampleRate) {
if (
!resampler ||
currentInputRate !== chunk.sampleRate ||
currentChannels !== chunk.channels
) {
if (resampler) {
for (const frame of resampler.flush()) {
controller.enqueue(frame);
}
resampler.close();
}
resampler = new AudioResampler(chunk.sampleRate, outputRate);
resampler = new AudioResampler(chunk.sampleRate, outputRate, chunk.channels);
currentInputRate = chunk.sampleRate;
currentChannels = chunk.channels;
}

for (const frame of resampler.push(chunk)) {
Expand Down