playback_microphone_audio.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import array
3 import collections
4 import pyaudio
5 import time
6 
7 import rospy
8 import gcloud_speech_msgs.msg as gcloud_speech_msgs
9 
10 LinearPcm16Le16000Audio = gcloud_speech_msgs.LinearPcm16Le16000Audio
11 
12 MSG_FRAME_PER_SLICE = 1600
13 
14 _audio_queue = collections.deque()
15 
16 
17 def RosCallback(msg):
18  _audio_queue.append(msg.data)
19  rospy.loginfo("Received {} bytes.".format(len(msg.data)))
20 
21 
22 def PaCallback(in_data, frame_count, time_info, status):
23  assert frame_count == MSG_FRAME_PER_SLICE * 2
24 
25  while True:
26  try:
27  # For some reason putting in 10 chunks per second to PyAudio introduces a
28  # lot of clicking sound. This hack only combines two chunks so that only
29  # PyAudio only needs to handle 5 chunks per seconds, and the clicking
30  # sound went alway.
31  data_1 = _audio_queue.popleft()
32  data_2 = _audio_queue.popleft()
33  data = data_1 + data_2
34  break
35  except:
36  time.sleep(1)
37 
38  return (data, pyaudio.paContinue)
39 
40 
42  rospy.init_node('playback_microphone_audio', anonymous=True)
43  rospy.Subscriber(
44  "/cogrob/microphone_audio", LinearPcm16Le16000Audio, RosCallback,
45  queue_size=10)
46  pa = pyaudio.PyAudio()
47  stream = pa.open(
48  format=pyaudio.paInt16, channels=1, rate=16000, output=True,
49  stream_callback=PaCallback, frames_per_buffer=MSG_FRAME_PER_SLICE * 2)
50 
51  stream.start_stream()
52  rospy.spin()
53  pa.terminate()
54 
55 
56 if __name__ == '__main__':
57  try:
59  except rospy.ROSInterruptException:
60  pass
def PaCallback(in_data, frame_count, time_info, status)


gcloud_speech_utils
Author(s):
autogenerated on Wed Jun 5 2019 21:24:09