Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the abusive usage of GST_STATE_NULL to pause all cached sounds … #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions sound_play/scripts/soundplay_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class soundtype:
STOPPED = 0
LOOPING = 1
COUNTING = 2
PAUSED = 3

def __init__(self, file, volume = 1.0):
self.lock = threading.RLock()
Expand Down Expand Up @@ -134,23 +135,31 @@ def stop(self):
finally:
self.lock.release()

def pause(self):
if self.state != self.PAUSED:
self.lock.acquire()
try:
self.sound.set_state(Gst.State.PAUSED)
self.state = self.PAUSED
finally:
self.lock.release()

def single(self):
self.lock.acquire()
try:
rospy.logdebug("Playing %s"%self.uri)
self.staleness = 0
if self.state == self.LOOPING:
self.stop()

self.pause()
self.sound.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0)
self.sound.set_state(Gst.State.PLAYING)
self.state = self.COUNTING
finally:
self.lock.release()

def command(self, cmd):
if cmd == SoundRequest.PLAY_STOP:
self.stop()
if cmd == SoundRequest.PLAY_STOP: #misunderstood here should be paused in my opinion
self.stop() #paused
elif cmd == SoundRequest.PLAY_ONCE:
self.single()
elif cmd == SoundRequest.PLAY_START:
Expand Down Expand Up @@ -182,19 +191,19 @@ class soundplay:
_feedback = SoundRequestFeedback()
_result = SoundRequestResult()

def stopdict(self,dict):
def pausedict(self,dict):
for sound in dict.values():
sound.stop()
sound.pause()

def stopall(self):
self.stopdict(self.builtinsounds)
self.stopdict(self.filesounds)
self.stopdict(self.voicesounds)
def pauseall(self):
self.pausedict(self.builtinsounds)
self.pausedict(self.filesounds)
self.pausedict(self.voicesounds)

def select_sound(self, data):
if data.sound == SoundRequest.PLAY_FILE:
if not data.arg2:
if not data.arg in self.filesounds.keys() or self.filesounds[data.arg].volume != data.volume:
if not data.arg in self.filesounds.keys():
rospy.logdebug('command for uncached wave: "%s"'%data.arg)
try:
self.filesounds[data.arg] = soundtype(data.arg, data.volume)
Expand All @@ -203,10 +212,14 @@ def select_sound(self, data):
return
else:
rospy.logdebug('command for cached wave: "%s"'%data.arg)
if self.filesounds[data.arg].sound.get_property('volume') != data.volume:
rospy.logdebug('volume for cached wave has changed, resetting volume')
self.filesounds[data.arg].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 not absfilename in self.filesounds.keys() or self.filesounds[absfilename].volume != data.volume:
if not absfilename in self.filesounds.keys():
rospy.logdebug('command for uncached wave: "%s"'%absfilename)
try:
self.filesounds[absfilename] = soundtype(absfilename, data.volume)
Expand All @@ -215,10 +228,12 @@ def select_sound(self, data):
return
else:
rospy.logdebug('command for cached wave: "%s"'%absfilename)
if self.filesounds[absfilename].sound.get_property('volume') != data.volume:
rospy.logdebug('volume for cached wave has changed, resetting volume')
self.filesounds[absfilename].sound.set_property('volume', data.volume)
sound = self.filesounds[absfilename]
elif data.sound == SoundRequest.SAY:
print data
if not data.arg in self.voicesounds.keys() or self.voicesounds[data.arg].volume != data.volume:
if not data.arg in self.voicesounds.keys():
rospy.logdebug('command for uncached text: "%s"' % data.arg)
txtfile = tempfile.NamedTemporaryFile(prefix='sound_play', suffix='.txt')
(wavfile,wavfilename) = tempfile.mkstemp(prefix='sound_play', suffix='.wav')
Expand All @@ -240,6 +255,9 @@ def select_sound(self, data):
txtfile.close()
else:
rospy.logdebug('command for cached text: "%s"'%data.arg)
if self.voicesounds[data.arg].sound.get_property('volume') != data.volume:
rospy.logdebug('volume for cached text has changed, resetting volume')
self.voicesounds[data.arg].sound.set_property('volume', data.volume)
sound = self.voicesounds[data.arg]
else:
rospy.logdebug('command for builtin wave: %i'%data.sound)
Expand All @@ -265,10 +283,10 @@ def callback(self,data):
return
self.mutex.acquire()
# Force only one sound at a time
self.stopall()
self.pauseall()
try:
if data.sound == SoundRequest.ALL and data.command == SoundRequest.PLAY_STOP:
self.stopall()
if data.sound == SoundRequest.ALL and data.command == SoundRequest.PLAY_STOP:#should be paused
self.stopall()# should be paused
else:
sound = self.select_sound(data)
sound.command(data.command)
Expand Down