-
Notifications
You must be signed in to change notification settings - Fork 154
/
soundplay_node.py
executable file
·462 lines (426 loc) · 18.4 KB
/
soundplay_node.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python
# ***********************************************************
# * Software License Agreement (BSD License)
# *
# * Copyright (c) 2009, Willow Garage, Inc.
# * All rights reserved.
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions
# * are met:
# *
# * * Redistributions of source code must retain the above copyright
# * notice, this list of conditions and the following disclaimer.
# * * Redistributions in binary form must reproduce the above
# * copyright notice, this list of conditions and the following
# * disclaimer in the documentation and/or other materials provided
# * with the distribution.
# * * Neither the name of the Willow Garage nor the names of its
# * contributors may be used to endorse or promote products derived
# * from this software without specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# * POSSIBILITY OF SUCH DAMAGE.
# ***********************************************************
import os
import sys
import threading
import traceback
import yaml
import actionlib
import roslib
import rospkg
import rospy
from diagnostic_msgs.msg import DiagnosticArray
from diagnostic_msgs.msg import DiagnosticStatus
from diagnostic_msgs.msg import KeyValue
from sound_play.msg import SoundRequest
from sound_play.msg import SoundRequestAction
from sound_play.msg import SoundRequestFeedback
from sound_play.msg import SoundRequestResult
from sound_play.sound_type import SoundType
try:
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject as GObject
from gi.repository import Gst as Gst
except Exception:
str = """
**************************************************************
Error opening pygst. Is gstreamer installed?
**************************************************************
"""
rospy.logfatal(str)
# print str
exit(1)
class SoundPlayNode(object):
_feedback = SoundRequestFeedback()
_result = SoundRequestResult()
def stopdict(self, dict):
for sound in dict.values():
sound.stop()
def stopall(self):
self.stopdict(self.builtinsounds)
self.stopdict(self.filesounds)
self.stopdict(self.voicesounds)
def select_sound(self, data):
if data.sound == SoundRequest.PLAY_FILE:
if not data.arg2:
if data.arg not in self.filesounds.keys():
rospy.logdebug(
'command for uncached wave: "%s"' % data.arg)
try:
self.filesounds[data.arg] = SoundType(
data.arg, self.device, data.volume)
except Exception:
rospy.logerr(
'Error setting up to play "%s".'
'Does this file exist on the machine'
'on which sound_play is running?' % data.arg)
return
else:
rospy.logdebug('command for cached wave: "%s"' % data.arg)
filesound = self.filesounds[data.arg]
if filesound.sound.get_property('volume') != data.volume:
rospy.logdebug(
'volume for cached wave has changed,'
'resetting volume')
filesound.sound.set_property('volume', data.volume)
sound = self.filesounds[data.arg]
else:
absfilename = os.path.join(
roslib.packages.get_pkg_dir(data.arg2), data.arg)
if absfilename not in self.filesounds.keys():
rospy.logdebug(
'command for uncached wave: "%s"' % absfilename)
try:
self.filesounds[absfilename] = SoundType(
absfilename, self.device, data.volume)
except Exception:
rospy.logerr(
'Error setting up to play "%s" from package "%s".'
'Does this file exist on the machine '
'on which sound_play is running?'
% (data.arg, data.arg2))
return
else:
rospy.logdebug(
'command for cached wave: "%s"' % absfilename)
filesound = self.filesounds[absfilename]
if filesound.sound.get_property('volume') != data.volume:
rospy.logdebug(
'volume for cached wave has changed,'
'resetting volume')
filesound.sound.set_property('volume', data.volume)
sound = self.filesounds[absfilename]
elif data.sound == SoundRequest.SAY:
voice_key = data.arg + '---' + data.arg2
if voice_key not in self.voicesounds.keys():
rospy.logdebug('command for uncached text: "%s"' % voice_key)
if self.plugin is None:
rospy.logerr(
'Plugin is not found {}.'.format(self.plugin_name))
else:
if data.arg2 == '':
voice = self.default_voice
else:
voice = data.arg2
wavfilename = self.plugin.sound_play_say_plugin(
data.arg, voice)
if wavfilename is None:
rospy.logerr('Failed to generate wavfile.')
else:
self.voicesounds[voice_key] = SoundType(
wavfilename, self.device, data.volume)
else:
rospy.logdebug('command for cached text: "%s"' % voice_key)
voicesound = self.voicesounds[voice_key]
if voicesound.sound.get_property('volume') != data.volume:
rospy.logdebug(
'volume for cached text has changed, resetting volume')
voicesound.sound.set_property('volume', data.volume)
sound = self.voicesounds[voice_key]
else:
rospy.logdebug('command for builtin wave: %i' % data.sound)
if ((data.sound in self.builtinsounds and
data.volume != self.builtinsounds[data.sound].volume)
or data.sound not in self.builtinsounds):
params = self.builtinsoundparams[data.sound]
volume = data.volume
# use the second param as a scaling for the input volume
if params[1] != 1:
volume = (volume + params[1])/2
self.builtinsounds[data.sound] = SoundType(
params[0], self.device, volume)
sound = self.builtinsounds[data.sound]
if sound.staleness != 0 and data.command != SoundRequest.PLAY_STOP:
# This sound isn't counted in active_sounds
rospy.logdebug("activating %i %s" % (data.sound, data.arg))
self.active_sounds = self.active_sounds + 1
sound.staleness = 0
return sound
def callback(self, data):
if not self.initialized:
return
self.mutex.acquire()
try:
if (data.sound == SoundRequest.ALL
and data.command == SoundRequest.PLAY_STOP):
self.stopall()
else:
sound = self.select_sound(data)
sound.command(data.command)
except Exception as e:
rospy.logerr('Exception in callback: %s' % str(e))
rospy.loginfo(traceback.format_exc())
finally:
self.mutex.release()
rospy.logdebug("done callback")
# Purge sounds that haven't been played in a while.
def cleanupdict(self, dict):
purgelist = []
for key, sound in iter(dict.items()):
try:
staleness = sound.get_staleness()
except Exception as e:
rospy.logerr(
'Exception in cleanupdict for sound (%s): %s'
% (str(key), str(e)))
# Something is wrong. Let's purge and try again.
staleness = 100
# print "%s %i"%(key, staleness)
if staleness >= 10:
purgelist.append(key)
# Sound is playing
if staleness == 0:
self.active_sounds = self.active_sounds + 1
for key in purgelist:
rospy.logdebug('Purging %s from cache' % key)
# clean up resources
dict[key].dispose()
del dict[key]
def cleanup(self):
self.mutex.acquire()
try:
self.active_sounds = 0
self.cleanupdict(self.filesounds)
self.cleanupdict(self.voicesounds)
self.cleanupdict(self.builtinsounds)
except Exception:
rospy.loginfo(
'Exception in cleanup: %s' % sys.exc_info()[0])
finally:
self.mutex.release()
def diagnostics(self, state):
try:
da = DiagnosticArray()
ds = DiagnosticStatus()
ds.name = rospy.get_caller_id().lstrip('/') + ": Node State"
if state == 0:
ds.level = DiagnosticStatus.OK
ds.message = "%i sounds playing" % self.active_sounds
ds.values.append(
KeyValue("Active sounds", str(self.active_sounds)))
ds.values.append(
KeyValue(
"Allocated sound channels",
str(self.num_channels)))
ds.values.append(
KeyValue(
"Buffered builtin sounds",
str(len(self.builtinsounds))))
ds.values.append(
KeyValue(
"Buffered wave sounds",
str(len(self.filesounds))))
ds.values.append(
KeyValue(
"Buffered voice sounds",
str(len(self.voicesounds))))
elif state == 1:
ds.level = DiagnosticStatus.WARN
ds.message = "Sound device not open yet."
else:
ds.level = DiagnosticStatus.ERROR
ds.message = "Can't open sound device." +\
"See http://wiki.ros.org/sound_play/Troubleshooting"
da.status.append(ds)
da.header.stamp = rospy.get_rostime()
self.diagnostic_pub.publish(da)
except Exception as e:
rospy.loginfo('Exception in diagnostics: %s' % str(e))
def execute_cb(self, data):
data = data.sound_request
if not self.initialized:
rospy.logerr('soundplay_node is not initialized yet.')
self._as.set_aborted()
return
self.mutex.acquire()
# Force only one sound at a time
self.stopall()
try:
if (data.sound == SoundRequest.ALL
and data.command == SoundRequest.PLAY_STOP):
self.stopall()
else:
sound = self.select_sound(data)
sound.command(data.command)
r = rospy.Rate(self.loop_rate)
start_time = rospy.get_rostime()
success = True
while sound.get_playing():
sound.update()
if self._as.is_preempt_requested():
rospy.loginfo('sound_play action: Preempted')
sound.stop()
self._as.set_preempted()
success = False
break
self._feedback.playing = sound.get_playing()
self._feedback.stamp = rospy.get_rostime() - start_time
self._as.publish_feedback(self._feedback)
r.sleep()
if success:
self._result.playing = self._feedback.playing
self._result.stamp = self._feedback.stamp
rospy.loginfo('sound_play action: Succeeded')
self._as.set_succeeded(self._result)
except Exception as e:
self._as.set_aborted()
rospy.logerr(
'Exception in actionlib callback: %s' % str(e))
rospy.loginfo(traceback.format_exc())
finally:
self.mutex.release()
rospy.logdebug("done actionlib callback")
def __init__(self):
Gst.init(None)
# Start gobject thread to receive gstreamer messages
GObject.threads_init()
self.g_loop = threading.Thread(target=GObject.MainLoop().run)
self.g_loop.daemon = True
self.g_loop.start()
rospy.init_node('sound_play')
self.loop_rate = rospy.get_param('~loop_rate', 100)
self.device = rospy.get_param("~device", "default")
self.default_voice = rospy.get_param('~default_voice', None)
self.plugin_name = rospy.get_param(
'~plugin', 'sound_play/festival_plugin')
self.diagnostic_pub = rospy.Publisher(
"/diagnostics", DiagnosticArray, queue_size=1)
rootdir = os.path.join(
roslib.packages.get_pkg_dir('sound_play'), 'sounds')
# load plugin
rospack = rospkg.RosPack()
depend_pkgs = rospack.get_depends_on('sound_play', implicit=False)
depend_pkgs = ['sound_play'] + depend_pkgs
rospy.loginfo("Loading from plugin definitions")
plugin_yamls = []
for depend_pkg in depend_pkgs:
manifest = rospack.get_manifest(depend_pkg)
plugin_yaml = manifest.get_export('sound_play', 'plugin')
if len(plugin_yaml) != 0:
plugin_yamls += plugin_yaml
for plugin_y in plugin_yaml:
rospy.logdebug('Loading plugin in {}'.format(plugin_y))
plugin_dict = {}
for plugin_yaml in plugin_yamls:
if not os.path.exists(plugin_yaml):
rospy.logerr(
'Failed to load plugin yaml: {}'.format(plugin_yaml))
rospy.logerr(
'Missing plugin yaml: {}'.format(plugin_yaml))
continue
with open(plugin_yaml) as f:
plugin_descs = yaml.safe_load(f)
for plugin_desc in plugin_descs:
plugin_dict[plugin_desc['name']] = plugin_desc['module']
self.plugin = None
if self.plugin_name in plugin_dict.keys():
plugin_module = plugin_dict[self.plugin_name]
mod = __import__(plugin_module.split('.')[0])
for sub_mod in plugin_module.split('.')[1:]:
mod = getattr(mod, sub_mod)
self.plugin = mod()
self.builtinsoundparams = {
SoundRequest.BACKINGUP: (
os.path.join(rootdir, 'BACKINGUP.ogg'), 0.1),
SoundRequest.NEEDS_UNPLUGGING: (
os.path.join(rootdir, 'NEEDS_UNPLUGGING.ogg'), 1),
SoundRequest.NEEDS_PLUGGING: (
os.path.join(rootdir, 'NEEDS_PLUGGING.ogg'), 1),
SoundRequest.NEEDS_UNPLUGGING_BADLY: (
os.path.join(rootdir, 'NEEDS_UNPLUGGING_BADLY.ogg'), 1),
SoundRequest.NEEDS_PLUGGING_BADLY: (
os.path.join(rootdir, 'NEEDS_PLUGGING_BADLY.ogg'), 1),
}
self.no_error = True
self.initialized = False
self.active_sounds = 0
self.mutex = threading.Lock()
self.sub = rospy.Subscriber("robotsound", SoundRequest, self.callback)
self._as = actionlib.SimpleActionServer(
'sound_play', SoundRequestAction,
execute_cb=self.execute_cb, auto_start=False)
self.mutex.acquire()
# For ros startup race condition
self.sleep(0.5)
self.diagnostics(1)
while not rospy.is_shutdown():
while not rospy.is_shutdown():
self.init_vars()
self.no_error = True
self.initialized = True
self.mutex.release()
if not self._as.action_server.started:
self._as.start()
try:
self.idle_loop()
# Returns after inactive period to test device availability
# print "Exiting idle"
except Exception:
rospy.loginfo(
'Exception in idle_loop: %s' % sys.exc_info()[0])
finally:
self.mutex.acquire()
self.diagnostics(2)
self.mutex.release()
def init_vars(self):
self.num_channels = 10
self.builtinsounds = {}
self.filesounds = {}
self.voicesounds = {}
self.hotlist = []
if not self.initialized:
rospy.loginfo('sound_play node is ready to play sound')
def sleep(self, duration):
try:
rospy.sleep(duration)
except rospy.exceptions.ROSInterruptException:
pass
def get_sound_length(self):
sound_length = len(self.builtinsounds) +\
len(self.voicesounds) + len(self.filesounds)
return sound_length
def idle_loop(self):
self.last_activity_time = rospy.get_time()
while (not rospy.is_shutdown()
and (rospy.get_time() - self.last_activity_time < 10
or self.get_sound_length() > 0)):
# print("idle_loop")
self.diagnostics(0)
self.sleep(1)
self.cleanup()
# print("idle_exiting")
if __name__ == '__main__':
SoundPlayNode()