perf(voice): reduce transmit latency
- FFmpeg: frame_duration 10ms, compression_level 0, nobuffer/low_delay flags, minimal probe - Frontend: buffer 4096→1024 samples (~170ms→~42ms), faster base64 with chunking - Remove processor.connect(destination) to avoid audio feedback loop Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
a4c2b93314
commit
7b74061788
@@ -50,8 +50,16 @@ export class VoiceTransmitter {
|
|||||||
"-ar", "48000", // Output sample rate: 48kHz
|
"-ar", "48000", // Output sample rate: 48kHz
|
||||||
"-ac", "2", // Output channels: stereo
|
"-ac", "2", // Output channels: stereo
|
||||||
"-application", "lowdelay", // Low delay mode for real-time
|
"-application", "lowdelay", // Low delay mode for real-time
|
||||||
"-frame_duration", "20", // 20ms frames
|
"-frame_duration", "10", // 10ms frames (smaller = less latency)
|
||||||
"-packet_loss", "0", // No packet loss expected
|
"-packet_loss", "0", // No packet loss expected
|
||||||
|
"-compression_level", "0", // Fastest compression (lowest CPU latency)
|
||||||
|
"-cutoff", "20000", // Audio cutoff frequency
|
||||||
|
"-vsync", "0", // No video sync
|
||||||
|
"-fflags", "nobuffer", // No buffering
|
||||||
|
"-flags", "low_delay", // Low delay processing
|
||||||
|
"-strict", "experimental",
|
||||||
|
"-analyzeduration", "0", // No analysis duration
|
||||||
|
"-probesize", "32", // Minimal probe size
|
||||||
"pipe:1", // Write to stdout
|
"pipe:1", // Write to stdout
|
||||||
], {
|
], {
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
|||||||
@@ -58,10 +58,10 @@ export function useAudioTransmit(socketRef: {
|
|||||||
const audioContext = new AudioContextCtor({ sampleRate: SAMPLE_RATE });
|
const audioContext = new AudioContextCtor({ sampleRate: SAMPLE_RATE });
|
||||||
audioContextRef.current = audioContext;
|
audioContextRef.current = audioContext;
|
||||||
const source = audioContext.createMediaStreamSource(stream);
|
const source = audioContext.createMediaStreamSource(stream);
|
||||||
const processor = audioContext.createScriptProcessor(4096, 1, 1);
|
const processor = audioContext.createScriptProcessor(1024, 1, 1);
|
||||||
processorRef.current = processor;
|
processorRef.current = processor;
|
||||||
source.connect(processor);
|
source.connect(processor);
|
||||||
processor.connect(audioContext.destination);
|
// Don't connect processor to destination (no monitoring feedback)
|
||||||
processor.onaudioprocess = (event) => {
|
processor.onaudioprocess = (event) => {
|
||||||
if (!socketRef.current || socketRef.current.readyState !== WebSocket.OPEN)
|
if (!socketRef.current || socketRef.current.readyState !== WebSocket.OPEN)
|
||||||
return;
|
return;
|
||||||
@@ -70,15 +70,15 @@ export function useAudioTransmit(socketRef: {
|
|||||||
for (let i = 0; i < inputData.length; i++)
|
for (let i = 0; i < inputData.length; i++)
|
||||||
pcmData[i] = Math.max(-1, Math.min(1, inputData[i])) * 32767;
|
pcmData[i] = Math.max(-1, Math.min(1, inputData[i])) * 32767;
|
||||||
|
|
||||||
// Convert to base64
|
// Fast base64 using Uint8Array + btoa
|
||||||
const bytes = new Uint8Array(pcmData.buffer);
|
const uint8 = new Uint8Array(pcmData.buffer);
|
||||||
let binary = '';
|
let base64 = '';
|
||||||
for (let i = 0; i < bytes.length; i++) {
|
const chunkSize = 8192;
|
||||||
binary += String.fromCharCode(bytes[i]);
|
for (let i = 0; i < uint8.length; i += chunkSize) {
|
||||||
|
const chunk = uint8.subarray(i, i + chunkSize);
|
||||||
|
base64 += btoa(String.fromCharCode(...chunk));
|
||||||
}
|
}
|
||||||
const base64 = btoa(binary);
|
|
||||||
|
|
||||||
// Send as JSON for backend to forward to Redis
|
|
||||||
socketRef.current.send(JSON.stringify({
|
socketRef.current.send(JSON.stringify({
|
||||||
type: 'voice_transmit',
|
type: 'voice_transmit',
|
||||||
buffer: base64
|
buffer: base64
|
||||||
|
|||||||
Reference in New Issue
Block a user