-
Notifications
You must be signed in to change notification settings - Fork 0
/
voicecommand.py
executable file
·100 lines (88 loc) · 2.91 KB
/
voicecommand.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import speech_recognition as sr
import pyaudio
import os
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 32000
CHUNK = 2048
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "sound_file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK, )
print("recording command...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("finished recording")
import wave
import os
import time
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
r = sr.Recognizer()
with sr.AudioFile('sound_file.wav') as source:
audio = r.record(source)
r.adjust_for_ambient_noise(source)
with open(r"../google_cred.json", "r") as f:
credentials_json = f.read()
try:
answer = r.recognize_google_cloud(audio, credentials_json=credentials_json, language='en-US')
except sr.UnknownValueError:
print("Google Cloud Speech could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Cloud Speech service; {0}".format(e))
except:
print('Some other error occured.')
import subprocess
from fft_notes import extract
from birdwrite import tweety
try:
print(answer)
except NameError:
print('try again')
if answer == 'tequila ': # change to tequila for demo
os.system('aplay Tequila.wav')
exec(open('client.py').read())
time.sleep(8)
S_RECORD_SECONDS = 5
S_WAVE_OUTPUT_FILENAME = "input_song.wav"
s_audio = pyaudio.PyAudio()
print('\nPrepare to sing your melody!\n')
time.sleep(5)
# start Recording
s_stream = s_audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK, )
print ("recording song...")
s_frames = []
for i in range(0, int(RATE / CHUNK * S_RECORD_SECONDS)):
s_data = s_stream.read(CHUNK)
s_frames.append(s_data)
print ("finished recording")
# stop Recording
s_stream.stop_stream()
s_stream.close()
s_audio.terminate()
s_waveFile = wave.open(S_WAVE_OUTPUT_FILENAME, 'wb')
s_waveFile.setnchannels(CHANNELS)
s_waveFile.setsampwidth(s_audio.get_sample_size(FORMAT))
s_waveFile.setframerate(RATE)
s_waveFile.writeframes(b''.join(s_frames))
s_waveFile.close()
notes = extract(S_WAVE_OUTPUT_FILENAME)
tweety(notes)
os.system('aplay birdsong.wav')
else:
print('Sorry, who are you?')