From 60f197595856e9db98dba506c244432616bd9334 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 10 Sep 2024 12:18:06 +0300 Subject: [PATCH] fix wav encoding --- agent-mAId/main.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/agent-mAId/main.py b/agent-mAId/main.py index e9ae8d4..d6264cd 100644 --- a/agent-mAId/main.py +++ b/agent-mAId/main.py @@ -14,6 +14,7 @@ import ctypes import io import time import json5 +import wave # Load configuration from config.json def load_config(): @@ -51,7 +52,6 @@ def record_audio(): frames = [] print("Recording...") - start_time = time.time() # Record while both keyboard and mouse button are pressed while keyboard.is_pressed(KB_KEY) and mouse.is_pressed(button=MOUSE_BTN): @@ -65,12 +65,19 @@ def record_audio(): stream.close() audio.terminate() - # Store recorded audio in an in-memory stream - audio_data = b''.join(frames) - memory_stream = io.BytesIO(audio_data) + # Store the recorded audio in an in-memory stream as a valid WAV file + memory_stream = io.BytesIO() - # Save audio to disk asynchronously as a side task - threading.Thread(target=save_audio_to_disk, args=("output.wav", audio_data, audio.get_sample_size(pyaudio.paInt16), 1, 16000)).start() + with wave.open(memory_stream, 'wb') as wave_file: + wave_file.setnchannels(1) + wave_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16)) + wave_file.setframerate(16000) + wave_file.writeframes(b''.join(frames)) + + memory_stream.seek(0) # Reset the stream position to the beginning for reading + + # Save audio to disk asynchronously as a side task (optional) + threading.Thread(target=save_audio_to_disk, args=("output.wav", b''.join(frames), audio.get_sample_size(pyaudio.paInt16), 1, 16000)).start() return memory_stream