fix wav encoding
This commit is contained in:
parent
54b0f1661b
commit
60f1975958
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user