Skip to content

Instantly share code, notes, and snippets.

@atucom
Created March 17, 2021 15:13
Show Gist options
  • Save atucom/84f380d2299690c588c8d301e72ad6cf to your computer and use it in GitHub Desktop.
Save atucom/84f380d2299690c588c8d301e72ad6cf to your computer and use it in GitHub Desktop.
Simple functions for python3 text-to-speech and speech-to-text
#stolen examples from public documentation for TTS and STT.
#install everything with:
# brew install portaudio
# pip3 install PyAudio
# pip3 install speechrecognition
# pip3 install pyttsx3
def text_to_speech(text, rate=225):
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate', rate)
engine.say(text)
engine.runAndWait()
def speech_to_text(mic_index=2):
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone(device_index=mic_index) as source:
r.adjust_for_ambient_noise(source)
print("Speak Now.")
audio = r.listen(source)
print('Recognizing...')
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print(r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment