diff --git a/.gitignore b/.gitignore index 0723b44..51440fd 100644 --- a/.gitignore +++ b/.gitignore @@ -162,3 +162,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +user_query.wav diff --git a/speech_recognition.py b/speech_recognition.py index 386f65d..89008e6 100644 --- a/speech_recognition.py +++ b/speech_recognition.py @@ -7,12 +7,14 @@ from faster_whisper import WhisperModel USER_QUERY_AUDIO_FILE = "user_query.wav" +retries = 0 def record_audio(): + global retries + channels = 1 - device = None - device_info = sd.query_devices(device, 'input') + device_info = sd.query_devices(0) samplerate = int(device_info['default_samplerate']) q = queue.Queue() @@ -26,13 +28,19 @@ def callback(indata, frames, time, status): input('Press a key to start recording..') # Make sure the file is opened before recording anything: with sf.SoundFile(USER_QUERY_AUDIO_FILE, mode='w', samplerate=samplerate, channels=channels) as file: - with sd.InputStream(samplerate=samplerate, device=device, channels=channels, callback=callback): + with sd.InputStream(samplerate=samplerate, device=device_info["name"], channels=channels, callback=callback): print("[[ Recording started ]]") while True: file.write(q.get()) + except sd.PortAudioError: + # Allow for a couple retries + if retries < 2: + retries += 1 + record_audio() + except KeyboardInterrupt: - pass + retries = 0 def initialize_whisper_speech_recognition():