-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeechIO.py
61 lines (46 loc) · 1.45 KB
/
SpeechIO.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
49
50
51
52
53
54
55
56
57
58
59
60
61
import speech_recognition as sr
from gtts import gTTS
from playsound import playsound
import os
def stt(prompt):
r = sr.Recognizer()
with sr.Microphone() as source:
while True:
print('adjusting for ambient noise')
r.adjust_for_ambient_noise(source)
try:
print("Please say something...")
tts(prompt)
audio = r.listen(source,timeout=3,phrase_time_limit=3)
break
except sr.WaitTimeoutError:
print('timeout error, say it again')
continue
try:
said=r.recognize_google(audio)
print(f"You have said: {said}")
# if r.recognize_google(audio) == "stop":
# quit()
except Exception as e:
print(f"STT Error: {str(e)}")
return stt(prompt)
except :
return stt(prompt)
finally:
del r
del source
return said
def tts(input_text):
text = input_text
language = 'en'
try:
os.remove('sound.mp3')
except:
pass
obj = gTTS(text=text, lang=language, slow=False)
obj.save('sound.mp3')
playsound('sound.mp3')
if __name__ == '__main__':
# stt('bruh')
tts('bruh momentos unos')
pass