fix wav encoding

This commit is contained in:
Dobromir Popov 2024-09-10 12:18:06 +03:00
parent 54b0f1661b
commit 60f1975958

View File

@ -14,6 +14,7 @@ import ctypes
import io import io
import time import time
import json5 import json5
import wave
# Load configuration from config.json # Load configuration from config.json
def load_config(): def load_config():
@ -51,7 +52,6 @@ def record_audio():
frames = [] frames = []
print("Recording...") print("Recording...")
start_time = time.time()
# Record while both keyboard and mouse button are pressed # Record while both keyboard and mouse button are pressed
while keyboard.is_pressed(KB_KEY) and mouse.is_pressed(button=MOUSE_BTN): while keyboard.is_pressed(KB_KEY) and mouse.is_pressed(button=MOUSE_BTN):
@ -65,12 +65,19 @@ def record_audio():
stream.close() stream.close()
audio.terminate() audio.terminate()
# Store recorded audio in an in-memory stream # Store the recorded audio in an in-memory stream as a valid WAV file
audio_data = b''.join(frames) memory_stream = io.BytesIO()
memory_stream = io.BytesIO(audio_data)
# Save audio to disk asynchronously as a side task with wave.open(memory_stream, 'wb') as wave_file:
threading.Thread(target=save_audio_to_disk, args=("output.wav", audio_data, audio.get_sample_size(pyaudio.paInt16), 1, 16000)).start() 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 return memory_stream