-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (46 loc) · 1.78 KB
/
main.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
62
63
64
# Python program to translate
# speech to text and text to speech
# https://vb-audio.com/Voicemeeter/index.htm
# https://rogueamoeba.com/loopback/
# https://www.planetradiocity.com/how-to-play-desktop-audio-through-mic
import speech_recognition as sr
import pyttsx3
# Initialize the recognizer
r = sr.Recognizer()
# Function to convert text to
# speech
def SpeakText(command):
# Initialize the engine
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
# Loop infinitely for user to
# speak
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
while(1):
# Exception handling to handle
# exceptions at the runtime
print("======Start======")
try:
# use the microphone as source for input.
# with sr.Microphone(device_index=15) as source2:
with sr.Microphone(device_index=1) as source2:
# wait for a second to let the recognizer
# adjust the energy threshold based on
# the surrounding noise level
r.adjust_for_ambient_noise(source2, duration=0.2)
#listens for the user's input
audio2 = r.listen(source2)
print("======Stop======")
# Using ggogle to recognize audio
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
print(MyText)
SpeakText(MyText)
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occured")
except Exception as e:
print(e)