Skip to content

Commit

Permalink
works with non-default device (mic)
Browse files Browse the repository at this point in the history
  • Loading branch information
helboukkouri committed Apr 18, 2024
1 parent 83f98a4 commit 0727112
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 12 additions & 4 deletions speech_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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():
Expand Down

0 comments on commit 0727112

Please sign in to comment.