-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice.py
More file actions
89 lines (80 loc) · 2.44 KB
/
voice.py
File metadata and controls
89 lines (80 loc) · 2.44 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'''
Nicholas Lin
voice.py
4/2/20
'''
import os
import re
import time
import playsound
import keyboard
import speech_recognition as sr
from gtts import gTTS
from pygame import mixer
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.dynamic_energy_threshold = True
r.adjust_for_ambient_noise(source)
playsound.playsound("start_beep.mp3")
audio = r.listen(source, timeout = None)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print(e)
playsound.playsound("stop_beep.mp3")
return said.lower()
def speak(text):
print(text)
tts = gTTS(text)
filename = "voice.mp3"
tts.save(filename)
mixer.init()
mixer.music.load(filename)
mixer.music.play()
while(mixer.music.get_busy()):
if keyboard.is_pressed('s'):
mixer.music.stop()
break
# Used for reading long input
def read(text):
words = text.split(" ")
read_it = ""
for x in range(len(words)):
read_it += words[x] + " "
if "." in words[x] or x == len(words)-1:
speak(read_it)
read_it = ""
if keyboard.is_pressed('s'):
break
'''
while(play_obj.is_playing()):
if input() != "":
print("keyboard interrupt detected")
play_obj.stop()
voice_input = ""
def init_listen():
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening
# start listening in the background (note that we don't have to do this inside a `with` statement)
stop_listening = r.listen_in_background(m, callback)
# calling this function requests that the background listener stop listening
#stop_listening(wait_for_stop=False)
# this is called from the background thread
def callback(recognizer, audio):
global voice_input
# received audio data, now we'll recognize it using Google Speech Recognition
try:
said = recognizer.recognize_google(audio).lower()
playsound.playsound("activation_beep.mp3")
print(said)
voice_input = said
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))
'''