forked from CoderBotOrg/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.py
More file actions
219 lines (176 loc) · 6.06 KB
/
audio.py
File metadata and controls
219 lines (176 loc) · 6.06 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
############################################################################
# CoderBot, a didactical programmable robot.
# Copyright (C) 2014, 2015 Roberto Previtera <info@coderbot.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
############################################################################
import os
import time
from sys import byteorder
from array import array
from struct import pack
import logging
import pyaudio
import wave
import audioop
import logging
try:
from pocketsphinx.pocketsphinx import Decoder
from sphinxbase.sphinxbase import *
except:
logging.info("pocketsphinx not available")
CHUNK_SIZE = 4096
FORMAT = pyaudio.paInt16
RATE = 44100
MODELDIR = "/home/pi/coderbot/psmodels/"
SOUNDDIR = "./sounds/"
class Audio:
_instance = None
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = Audio()
return cls._instance
def __init__(self):
self.pyaudio = pyaudio.PyAudio()
try:
self.stream_in = self.pyaudio.open(format=FORMAT, channels=1, input_device_index=0, rate=RATE,
input=True,
frames_per_buffer=CHUNK_SIZE)
self.stream_in.start_stream()
except:
logging.info("Audio: input stream not available")
def exit(self):
# cleanup stuff.
self.stream_in.close()
self.pyaudio.terminate()
def say(self, what, locale='en'):
if what and "$" in what:
os.system ('omxplayer sounds/' + what[1:])
elif what and len(what):
os.system ('espeak -v' + locale + ' -p 90 -a 200 -s 150 -g 10 "' + what + '" 2>>/dev/null')
def normalize(self, snd_data):
"Average the volume out"
MAXIMUM = 16384
#times = float(MAXIMUM) / audioop.rms(snd_data, 2)
times = float(MAXIMUM)/max(abs(i) for i in snd_data)
logging.info("times: " + str(times))
r = array('h')
for i in snd_data:
r.append(int(i*times))
return r
def record(self, elapse):
num_silent = 0
snd_started = False
c = 0
r = array('h')
t = time.time()
while time.time() - t < elapse:
try:
snd_data = array('h', self.stream_in.read(CHUNK_SIZE))
r.extend(snd_data)
except IOError as ex:
if ex[1] != pyaudio.paInputOverflowed:
raise
#buf = '\x00' * CHUNK_SIZE #white noise
logging.info("white noise")
logging.info("read: " + str(len(r)) + " elapse: " + str(time.time() - t))
sample_width = self.pyaudio.get_sample_size(FORMAT)
r = self.normalize(r)
return sample_width, r
def record_to_file(self, filename, elapse):
sample_width, data = self.record(elapse)
data = pack('<' + ('h'*len(data)), *data)
wf = wave.open(SOUNDDIR + filename, 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
def play(self, filename):
os.system ('omxplayer sounds/' + filename)
"""
# open the file for reading.
wf = wave.open(SOUNDDIR + filename, 'rb')
# open stream based on the wave object which has been input.
stream = self.pyaudio.open(format =
self.pyaudio.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
# read data (based on the chunk size)
data = wf.readframes(CHUNK_SIZE)
# play stream (looping from beginning of file to the end)
while data != '':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(CHUNK_SIZE)
logging.info("play")
# cleanup stuff.
stream.close()
"""
def hear(self, level, elapse=1.0):
sig_hear = False
ts_total = time.time()
ts_signal = None
while time.time() - ts_total < elapse:
try:
snd_data = self.stream_in.read(CHUNK_SIZE)
snd_rms = audioop.rms(snd_data, 2)
logging.info("snd.rms: " + str(snd_rms))
if snd_rms > level:
sig_hear = True
break
except IOError as ex:
if ex[1] != pyaudio.paInputOverflowed:
raise
buf = '\x00' * CHUNK_SIZE #white noise
logging.info("white noise")
except AttributeError:
pass
return sig_hear
def speech_recog(self, model):
# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', '/usr/local/share/pocketsphinx/model/en-us/en-us')
config.set_int('-ds', 2)
config.set_int('-topn', 3)
config.set_int('-maxwpf', 5)
#config.set_string('-kws', MODELDIR + model + '.txt')
config.set_string('-lm', MODELDIR + model + '.lm')
config.set_string('-dict', MODELDIR + model + '.dict')
decoder = Decoder(config)
decoder.start_utt()
tstamp = time.time()
recog_text = ''
while len(recog_text) < 1:
try:
buf = self.stream_in.read(CHUNK_SIZE)
logging.info("actual voice")
decoder.process_raw(buf, False, False)
if decoder.hyp().hypstr != '':
recog_text += decoder.hyp().hypstr
print "text: " + decoder.hyp().hypstr
tstamp = time.time()
except IOError as ex:
if ex[1] != pyaudio.paInputOverflowed:
raise
buf = '\x00' * CHUNK_SIZE #white noise
logging.info("white noise")
except AttributeError:
pass
decoder.end_utt()
logging.info("recog text: " + recog_text)
return recog_text