How to pass only volume as argument to Soundplay? (audio_common)

asked 2020-09-30 13:16:23 -0600

updated 2020-09-30 13:17:03 -0600

I am using the sound play audio common package. The goal is to have an indicator for my sensor while it is collecting data. Below is the callback.

def sound_play(msg):

    if(msg.data >=0 and msg.data <= 2):
        soundhandle.play(1, 0.2)
        rospy.sleep(0.1)
    elif(msg.data > 2 and msg.data <= 4):
        soundhandle.play(1, 0.4)
        rospy.sleep(0.5)
    elif(msg.data > 4 and msg.data <= 6):
        soundhandle.play(1, 0.6)
        rospy.sleep(0.5)
    elif(msg.data > 6.0 and msg.data <= 8.0):
        soundhandle.play(1, 0.8)
        rospy.sleep(0.5)
    elif(msg.data > 8 and msg.data <= 10.0):
        soundhandle.play(1, 1.0)
        rospy.sleep(0.5)

However, every time I perform soundhandle.play it is opening the sound file (currently just using an inbuilt sound), and playing it. Which is itself not very elegant. However I would like to play it faster; given my sensor data is at a frequency of 200Hz, I would like to reduce the sleep time to 0.1 seconds. Anything with a sleeptime lesser than 0.5 seconds leads to crashing the node. As opening the file that many times gives me this error:

(python:11640): GStreamer-CRITICAL **: 17:41:13.207: Could not add a signal watch to bus bus2691

which might be because of file leaks. So is there a way to just open the file once and only pass the volume as an argument for the voltage level I am receiving?

Is there any better way of having a sound indicator/output for my sensor reading?

edit retag flag offensive close merge delete