Skip to content

Commit 40c82d7

Browse files
committed
modified the aplay and arecord
1 parent a7d4b7b commit 40c82d7

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

examples/raspi_assistant/handler.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ def __init__(self, baidu_token=None):
3939
def __repr__(self):
4040
return '<BaseHandler>'
4141

42-
def receive(self, sec=6):
42+
def receive(self, sec=4):
4343
self.feedback(generate_response())
44-
print 'arecord begin'
4544
self.audio_handler.arecord(sec)
4645
return self.bv.asr('record.wav')
4746

@@ -52,7 +51,7 @@ def process(self, results):
5251

5352
def execute(self, func_name, result):
5453
func = getattr(ActionHandler, func_name)
55-
return func(self.bv, self.audio_handler, result)
54+
return func(self, result)
5655

5756
@cache
5857
def feedback(self, content=None):
@@ -75,7 +74,7 @@ class ActionHandler(object):
7574
"""docstring for ActionHandler"""
7675

7776
@staticmethod
78-
def default(bv, audio_handler, result):
77+
def default(base_handler, result):
7978
print 'turing run'
8079
robot = TuringRobot(BC.TURING_KEY)
8180
try:
@@ -87,54 +86,51 @@ def default(bv, audio_handler, result):
8786
return content
8887

8988
@staticmethod
90-
def _memo(date, bv, audio_handler):
91-
audio_handler.aplay(bv.tts('请说出内容'))
92-
f = BytesIO()
93-
audio_handler.record(6, f)
89+
def _memo(date, base_handler):
90+
base_handler.feedback('请说出记录内容')
91+
base_handler.audio_handler.arecord(6)
9492
cache_handler = CacheHandler()
9593
cache_handler.zset(date, f.read(), timestamp(), 86400*3)
9694
return '完成记录'
9795

9896
@staticmethod
99-
def memo_today(bv, audio_handler, result):
97+
def memo_today(base_handler, result):
10098
return ActionHandler._memo(
10199
date=datetime.date.today().strftime('%Y-%m-%d'),
102-
bv=bv,
103-
audio_handler=audio_handler
100+
base_handler=base_handler
104101
)
105102

106103
@staticmethod
107-
def memo_tomo(bv, audio_handler, result):
104+
def memo_tomo(base_handler, result):
108105
return ActionHandler._memo(
109106
date=(datetime.date.today() + datetime.timedelta(days=1)).strftime('%Y-%m-%d'),
110-
bv=bv,
111-
audio_handler=audio_handler
107+
base_handler=base_handler
112108
)
113109

114110
@staticmethod
115-
def play_memo(date, audio_handler):
111+
def play_memo(date, base_handler):
116112
cache_handler = CacheHandler()
117113
audio = cache_handler.zget(date, 0, -1)
118114
if audio:
119115
for item in audio:
120-
audio_handler.aplay(audio)
116+
base_handler.audio_handler.aplay(audio)
121117
return '播放结束'
122118
else:
123-
audio_handler.aplay('未找到记录')
119+
base_handler.feedback('未找到记录')
124120
return None
125121

126122
@staticmethod
127-
def play_memo_tomo(bv, audio_handler, result):
123+
def play_memo_tomo(base_handler, result):
128124
return ActionHandler.play_memo(
129125
date=(datetime.date.today() + datetime.timedelta(days=1)).strftime('%Y-%m-%d'),
130-
audio_handler=audio_handler
126+
base_handler=base_handler
131127
)
132128

133129
@staticmethod
134-
def play_memo_today(bv, audio_handler, result):
130+
def play_memo_today(base_handler, result):
135131
return ActionHandler.play_memo(
136132
date=datetime.date.today().strftime('%Y-%m-%d'),
137-
audio_handler=audio_handler
133+
base_handler=base_handler
138134
)
139135

140136
@staticmethod
@@ -144,35 +140,35 @@ def del_memo(date, start, end):
144140
return '删除成功'
145141

146142
@staticmethod
147-
def del_last_memo(bv, audio_handler, result):
143+
def del_last_memo(base_handler, result):
148144
return ActionHandler.del_memo(
149145
date=datetime.date.today().strftime('%Y-%m-%d'),
150146
start=-1,
151147
end=-1
152148
)
153149

154150
@staticmethod
155-
def del_first_memo(bv, audio_handler, result):
151+
def del_first_memo(base_handler, result):
156152
return ActionHandler.del_memo(
157153
date=datetime.date.today().strftime('%Y-%m-%d'),
158154
start=0,
159155
end=0
160156
)
161157

162158
@staticmethod
163-
def del_all_memo(bv, audio_handler, result):
159+
def del_all_memo(base_handler, result):
164160
return ActionHandler.del_memo(
165161
date=datetime.date.today().strftime('%Y-%m-%d'),
166162
start=0,
167163
end=-1
168164
)
169165

170166
@staticmethod
171-
def weather_tomo(bv, audio_handler, result):
167+
def weather_tomo(base_handler, result):
172168
return ActionHandler.query_weather('tomo')
173169

174170
@staticmethod
175-
def weather_today(bv, audio_handler, result):
171+
def weather_today(base_handler, result):
176172
return ActionHandler.query_weather('today')
177173

178174
@staticmethod

examples/raspi_assistant/utils.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import base64
77
from functools import wraps
88
from hashlib import md5
9-
from subprocess import Popen, STDOUT
9+
from subprocess import Popen, PIPE
1010
from io import BytesIO
1111
from tempfile import NamedTemporaryFile
1212

@@ -148,18 +148,29 @@ def record(self, record_seconds, file_):
148148
wf.writeframes(''.join(frames))
149149
wf.close()
150150

151-
def arecord(self, record_seconds):
152-
p = Popen(['arecord', '-r', '16000', '-D', 'plughw:1,0', '-f', 'S16_LE', '-d', str(record_seconds), 'record.wav'])
153-
p.wait()
151+
def arecord(self, record_seconds, is_temp=False, file_=BC.INPUT_NAME):
152+
if is_temp:
153+
p = Popen(
154+
['arecord', '-r', '16000', '-D', 'plughw:1,0', '-f', 'S16_LE', '-d', str(record_seconds), '-'],
155+
stdout=PIPE,
156+
stderr=PIPE)
157+
stdin, _ = p.communicate()
158+
return stdin
159+
else:
160+
p = Popen(
161+
['arecord', '-r', '16000', '-D', 'plughw:1,0', '-f', 'S16_LE', '-d', str(record_seconds), file_])
162+
p.wait()
154163

155-
def aplay(self, file_):
156-
p = Popen(['aplay', file_])
164+
def aplay(self, is_temp=False, file_=BC.OUTPUT_NAME):
165+
if is_temp:
166+
p = Popen(['aplay', '-'], stdin=file_)
167+
else:
168+
p = Popen(['aplay', file_])
157169
p.wait()
158170

159171
def play(self, file_):
160172
wf = wave.open(file_, 'rb')
161173
p = pyaudio.PyAudio()
162-
print wf.getparams()
163174
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
164175
channels=wf.getnchannels(),
165176
rate=wf.getframerate(),
@@ -206,7 +217,9 @@ def delete(self, name):
206217
def expire(self, name, ttl):
207218
return self.client.expire(name, ttl)
208219

209-
def zset(self, name, key, score, ttl=None):
220+
def zset(self, name, key, score, ttl=None, is_audio=True):
221+
if is_audio:
222+
key = base64.b64encode(key)
210223
if ttl:
211224
pipeline = self.client.pipeline()
212225
pipeline.zadd(name, score, key)
@@ -215,8 +228,12 @@ def zset(self, name, key, score, ttl=None):
215228
else:
216229
self.client.zadd(name, score, key)
217230

218-
def zget(self, name, start, end):
219-
return self.client.zrange(name, start, end)
231+
def zget(self, name, start, end, is_audio=True):
232+
ret = self.client.zrange(name, start, end)
233+
if is_audio:
234+
return base64.b64decode(ret)
235+
else:
236+
return ret
220237

221238
def zdel(self, name, start, end):
222239
# zremrangebyrank

0 commit comments

Comments
 (0)