Skip to content

Instantly share code, notes, and snippets.

@bbence84
Created December 27, 2022 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbence84/04d1935a92a4b4ab3d44bf182ea4bcc1 to your computer and use it in GitHub Desktop.
Save bbence84/04d1935a92a4b4ab3d44bf182ea4bcc1 to your computer and use it in GitHub Desktop.
import speech_recognition as sr
from preferredsoundplayer import playsound
def start_listening():
r = sr.Recognizer()
r.dynamic_energy_threshold = True
r.energy_threshold = 100
r.pause_threshold = 0.7
while(1):
try:
with sr.Microphone(device_index = 0) as source:
r.dynamic_energy_threshold = True
r.energy_threshold = 100
r.pause_threshold = 0.7
r.adjust_for_ambient_noise(source, duration=0.2)
print("Say something!")
audio = r.listen(source)
spokenText = r.recognize_google(audio, show_all=False)
print("Text recognized: ", spokenText)
playsound("CantinaBand3.wav",block=True)
except sr.RequestError as e:
print("Request error; {0}".format(e))
except sr.UnknownValueError as e:
print("Unknown error occurred: {0}".format(e))
playsound("CantinaBand3.wav",block=True)
def main():
start_listening()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment