Skip to content

Commit adf7560

Browse files
committed
fix: Use audio duration instead of chunk count for playback buffer threshold
- Changed hasSufficientBuffer() to check bytes instead of chunk count - Fixes issue where x.ai (and potentially other providers) send larger audio chunks - Previously waited for 6 chunks which could mean 3+ seconds with large chunks - Now waits for 60ms of audio data (2880 bytes at 24kHz) regardless of chunk size - This ensures consistent playback start timing across all providers
1 parent 0a7e105 commit adf7560

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

app/src/main/java/ch/fhnw/pepper_realtime/manager/AudioPlayer.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,14 @@ class AudioPlayer {
376376
}
377377

378378
private fun hasSufficientBuffer(): Boolean {
379-
// Start only when we have a small headroom of chunks to avoid initial underflow
380-
val minChunks = 6 // ~60ms at 10ms frames
381-
return audioBuffer.size >= minChunks
379+
// Start only when we have enough audio to avoid initial underflow.
380+
// Using bytes instead of chunk count because chunk sizes vary by provider:
381+
// - OpenAI: ~10ms chunks (240 bytes)
382+
// - x.ai: larger chunks (potentially 200-500ms)
383+
// - Google: variable chunk sizes
384+
// Target: ~60ms of audio = 60 * 24 * 2 = 2880 bytes at 24kHz mono PCM16
385+
val minBufferBytes = 60 * sampleRateHz * bytesPerSample * channels / 1000
386+
return queuedBytes.get() >= minBufferBytes
382387
}
383388

384389
/**

0 commit comments

Comments
 (0)