0

For a project including a moving robot arm, i need a "Geiger-Müller counter"-like distance-based alarm. For that i wrote a python module and tried to add the posibility, if the robot arm is on the left side of an object, the sound is only on the left speaker, right side analogue. For that i looked into the sounddevice library, where an easy channel mapping is possible. As can be seen in the code-snippet 1 below, and like in the docu, i am calling sd.play() and waiting with sd.wait() till the sound finishes.

When i am using a sample-sound with a length of 6 secs, everything works fine. But if i am using the desired sound, a short beep-sound (under 1 sec) the code is not working. The script ends after ~1 sec without any sound. I can fix this via adding a sleep-statement (commented out in the code).

But for the context i need a smaller playing-window than 0.5 sec. Does anyone know how i can fix this or work around?

I tried to use pyaudio instead, but wasnt able to get the mono .wav file into a stereo byte array, for switching the audio dynamically on left/right speaker. (code-snippet2) But there i am always encountering an error ("asscii codec cant decode byte on pos 2: ordinal not in range(128)")

Snippet1:

    import sounddevice as sd
    import soundfile as sf

    data, fs = sf.read(args.filename, dtype='float32')

    byte = np.array(data)

    sd.play(byte, fs)
    #time.sleep(0.5)
    status = sd.wait()

Snippet2:

data = wave.readframes(chunkSize)

stereo_signal = np.zeros([len(data), 2])   
stereo_signal[:,0] = data[:]
stereo_signal[:,1] = 0
Rackor
  • 61
  • 2
  • 7
  • If it doesn't work for short sounds, please file an issue at https://github.com/spatialaudio/python-sounddevice/issues. Please provide a link to the actual sound file and fully working (even though minimal) example script. – Matthias Jul 17 '19 at 19:40
  • BTW, you don't need the line `byte = np.array(data)`. The `soundfile` module already gives you a NumPy array. – Matthias Jul 17 '19 at 19:40

0 Answers0