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
1 change: 1 addition & 0 deletions plugins/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"zod": "^3.25.76"
},
"dependencies": {
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"fastest-levenshtein": "^1.0.16",
"vitest": "^4.0.17"
},
Expand Down
50 changes: 49 additions & 1 deletion plugins/test/src/tts.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,63 @@
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
import type { stt } from '@livekit/agents';
import { type AudioBuffer, initializeLogger, tokenize, tts as ttslib } from '@livekit/agents';
import type { AudioFrame } from '@livekit/rtc-node';
import { type AudioFrame, combineAudioFrames } from '@livekit/rtc-node';
import { distance } from 'fastest-levenshtein';
import { spawn } from 'node:child_process';
import { ReadableStream } from 'stream/web';
import { describe, expect, it } from 'vitest';

const TEXT =
'The people who are crazy enough to think they can change the world are the ones who do.';

const compressedFormats = new Set([
'mp3',
'aac',
'ogg',
'flac',
'wav',
'mov,mp4,m4a,3gp,3g2,mj2',
'matroska,webm',
'mpeg',
]);

const assertPCM = async (frames: AudioFrame[]) => {
const frame = combineAudioFrames(frames);
const data = new Uint8Array(frame.data.buffer, frame.data.byteOffset, frame.data.byteLength);

const container = await new Promise<string | undefined>((resolve, reject) => {
const ffmpeg = spawn(ffmpegInstaller.path, [
'-hide_banner',
'-probesize',
'32',
'-analyzeduration',
'0',
'-i',
'pipe:0',
]);
let stderr = '';

ffmpeg.stderr.setEncoding('utf8');
ffmpeg.stderr.on('data', (chunk: string) => {
stderr += chunk;
});
ffmpeg.stdin.on('error', () => {});
ffmpeg.on('error', reject);
ffmpeg.on('close', () => {
resolve(stderr.match(/Input #0, (.*), from 'pipe:0':/)?.[1]);
});

ffmpeg.stdin.end(data);
});

if (container && compressedFormats.has(container)) {
throw new Error(`Audio data isn't PCM (detected ${container})`);
}
};

const validate = async (frames: AudioBuffer, stt: stt.STT, text: string, threshold: number) => {
const event = await stt.recognize(frames);
const eventText = event.alternatives![0].text.toLowerCase().replace(/\s/g, ' ').trim();
Expand Down Expand Up @@ -64,6 +111,7 @@ export const tts = async (
frames.push(event.frame);
}

await assertPCM(frames);
await validate(frames, stt, TEXT, 0.2);
stream.close();
});
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading