From 92ebb478492e28cf925c407e85591da84c3b3a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=A4usler?= Date: Mon, 30 Jun 2025 04:02:25 +0200 Subject: [PATCH] fix: try smoothing playback --- speech_to_text.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/speech_to_text.py b/speech_to_text.py index 1b08158..5c6cb68 100644 --- a/speech_to_text.py +++ b/speech_to_text.py @@ -240,13 +240,31 @@ class SpeechToText: ) print(f"Using device default sample rate: {device_info['defaultSampleRate']}") - # Play the audio - chunk_size = 1024 - data = wf.readframes(chunk_size) + # Play the audio with larger buffer for smoother playback + # Using larger chunk size and adding a small delay to allow the buffer to fill + chunk_size = 8192 # Increased from 1024 to reduce stuttering - while len(data) > 0: - output_stream.write(data) + # Pre-buffer data for smoother playback + audio_data = [] + while True: data = wf.readframes(chunk_size) + if len(data) == 0: + break + audio_data.append(data) + + # Reset file position for playback + wf.rewind() + + # Set a larger buffer size for output + buffer_size = chunk_size * 4 + + print(f"Playing audio with buffer size {buffer_size} bytes") + + # Play the buffered data with lower CPU load + for data in audio_data: + output_stream.write(data) + # Small sleep to reduce CPU load and allow buffer to process + time.sleep(0.005) # Clean up resources output_stream.stop_stream()