forked from AndyF/dumble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (60 loc) · 1.8 KB
/
app.py
File metadata and controls
78 lines (60 loc) · 1.8 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
import rumps
import subprocess
import pyaudio
import wave
import sys
import time
rumps.debug_mode(True) # turn on command line logging information for development - default is off
@rumps.clicked('About')
def about(sender):
subprocess.call(['say', 'Dumble Abracadabra'])
@rumps.clicked('Listen')
def listen(_):
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "output.wav"
if sys.platform == 'darwin':
CHANNELS = 1
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print "* recording"
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print "* done recording"
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
import speech_recognition as sr
r = sr.Recognizer()
with sr.WavFile("output.wav") as source: # use "test.wav" as the audio source
audio = r.record(source) # extract audio data from the file
cmd = r.recognize(audio)
try:
subprocess.call(['say', cmd])
app.title = cmd
time.sleep(2.5)
app.title = 'Listening...'
except LookupError: # speech is unintelligible
subprocess.call(['say', 'The wifi is really slow. Please try again.'])
if __name__ == "__main__":
app = rumps.App('Dumble', title='Listening...', icon='favicon.png')
app.menu = [
rumps.MenuItem('About', dimensions=(18, 18)),
rumps.MenuItem('Listen', dimensions=(18, 18))
]
app.run()