-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
48 lines (40 loc) · 1.09 KB
/
speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import speech_recognition as sr
import pyaudio
from os import path
import pyttsx
import sounddevice as sd
import soundfile as sf
def tts(data):
engine = pyttsx.init()
engine.setProperty('rate', 60)
engine.say(data)
engine.runAndWait()
def play():
(data,fs)=sf.read('hii.wav')
sd.play(data,fs)
sd.wait()
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
print("done\n")
# recognize speech using Google Speech Recognition
try:
data = r.recognize_google(audio)
strdata= data.encode('utf-8')
print("Google Speech Recognition thinks you said \n " + strdata)
return strdata
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
return 'Error'
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return 'Error'
print("\ndone\n")
if __name__=='__main__':
#tts()
tts('Good Morning')
#play()
x=listen()