-
Notifications
You must be signed in to change notification settings - Fork 0
/
bag_listen.py
85 lines (63 loc) · 2.44 KB
/
bag_listen.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
import rosbag
from python_qt_binding.QtGui import QGraphicsScene, QGraphicsView, QIcon
from python_qt_binding.QtCore import QSize, qWarning, qErrnoWarning
from naoqi_bridge_msgs.msg import AudioBuffer
class AudioPlayer(object):
def __init__(self):
self.player = None
try:
import ossaudiodev
self.device = ossaudiodev.open('w')
self.device.setfmt(ossaudiodev.AFMT_S16_LE)
self.player = 'oss'
except Exception, e:
qWarning('Could not open dsp device :%s'%e)
try:
import alsaaudio
self.device = alsaaudio.PCM()
self.device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
self.player = 'alsa'
qWarning('Using alsaaudio. If sound quality is not good enough, consider installing osspd')
except Exception, e:
raise e
self.channels = None
self.speed = None
def setChannels(self, channels):
if not self.channels or self.channels != channels:
self.channels = channels
if self.player == 'oss':
self.device.channels(channels)
elif self.player == 'alsa':
self.device.setchannels(channels)
def setRate(self, rate):
if not self.speed or self.speed != rate:
self.speed = rate
if self.player == 'oss':
self.device.speed(rate)
elif self.player == 'alsa':
self.device.setrate(rate)
def play(self, data):
if self.player == 'oss' or self.player == 'alsa':
self.device.write(data)
bag = rosbag.Bag('/home/mmoreaux/rosbag/2016-02-04-14-37-41.bag')
msg = None
for topic, msg, t in bag.read_messages(topics=['/pepper_robot/audio']):
print topic
break
player = AudioPlayer()
player.setChannels(len(msg.channelMap))
player.setRate(msg.frequency)
for topic, msg, t in bag.read_messages(topics=['/pepper_robot/audio']):
msgToPlay = list(msg.data)
# play a chunk of the recorded data (very small chunk !!! :p )
for idx in xrange(0, len(msgToPlay), 4):
msgChunk = msgToPlay[idx : idx+4]
dataBuff = ""
# Turn the list of int16 into a buffer of raw data
for i in range (0,len(msgChunk)) :
if msgChunk[i]<0 :
msgChunk[i]=msgChunk[i]+65536
dataBuff = dataBuff + chr(msgChunk[i]%256)
dataBuff = dataBuff + chr( (msgChunk[i] - (msgChunk[i]%256)) /256)
player.play(dataBuff)
bag.close()