-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio.py
71 lines (49 loc) · 1.4 KB
/
audio.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
65
66
67
68
69
70
71
import pyaudio
import wave
import speech_recognition as sr
import subprocess
from commands import Commander
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
#def echo(text):
# subprocess.call('echo ' + text, shell=True)
running= True
def play_audio(filename):
chunk = 1024
wf = wave.open(filename, 'rb')
pa = pyaudio.PyAudio()
stream = pa.open(
format=pa.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True
)
data_stream = wf.readframes(chunk)
while data_stream:
stream.write(data_stream)
data_stream = wf.readframes(chunk)
stream.close()
pa.terminate()
r = sr.Recognizer()
cmd = Commander()
def initSpeech():
print("Listening...")
play_audio("audio_initiate.wav")
with sr.Microphone() as source:
print("Say Something")
audio = r.listen(source)
play_audio("audio_end.wav")
command = ""
try:
command = r.recognize_google(audio)
except:
print("Couldn't understand you, bro.")
print("Your command:")
print(command)
if command in ["quit", "exit", "bye", "goodbye"]:
global running
running = False
cmd.discover(command)
#speak.Speak(command)
while running == True:
initSpeech()