jarvis/scratch/voice.py

39 lines
1.4 KiB
Python
Raw Normal View History

2023-06-12 16:06:24 +00:00
#!/usr/bin/env python3
import time
import speech_recognition as sr
# this is called from the background thread
def callback(recognizer, audio):
# received audio data, now we'll recognize it using Google Speech Recognition
try:
print("Processing...")
text = r.recognize_whisper(audio, language="english")
print(f"Whisper thinks you said {text}")
except sr.UnknownValueError:
print("Whisper could not understand audio")
except sr.RequestError as e:
print("Could not request results from Whisper")
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening
# start listening in the background (note that we don't have to do this inside a `with` statement)
stop_listening = r.listen_in_background(m, callback)
# `stop_listening` is now a function that, when called, stops background listening
# do some unrelated computations for 5 seconds
# for _ in range(50):
# time.sleep(0.1) # we're still listening even though the main thread is doing other things
# print('0')
# calling this function requests that the background listener stop listening
# stop_listening(wait_for_stop=False)
# do some more unrelated things
print("Listening...")
while True:
time.sleep(0.1) # we're not listening anymore,
# print('1')