Skip to content

Instantly share code, notes, and snippets.

@BenHamm
Created April 3, 2023 17:30
Show Gist options
  • Save BenHamm/7c3ebb6bbca8b60efcff5023cbd41695 to your computer and use it in GitHub Desktop.
Save BenHamm/7c3ebb6bbca8b60efcff5023cbd41695 to your computer and use it in GitHub Desktop.
import speech_recognition as sr
from text_to_speech import text_to_speech #a different module, but you can guess what it does
import pygame
pygame.mixer.init()
def capture_speech():
r = sr.Recognizer()
with sr.Microphone() as source: # use the default microphone as the audio source
pygame.mixer.Sound("open_mic.wav").play() #play a prompt sound so that the user knows that they can speak
audio = r.listen(source) # listen for the first phrase and extract it into audio data
try:
text = r.recognize_google(audio) #the speech recongition library in python lets you swap out for google or whisper
except: # if speech is unintelligible or absent
text_to_speech('I did not hear that. Please try again.')
text = capture_speech()
return text
if __name__ == "__main__":
text = capture_speech()
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment