Monday 31 August 2020

Detecting when input is not being used for Python SoundDevice/PortAudio on Linux

I am using Python's sounddevice library (which runs PortAudio underneath) to record audio on Linux (Raspbian Buster desktop), but it appears that more than one instance of my script will cause an error PaErrorcode -9985 which I believe is due to PortAudio (or the OS?) not allowing multiple programs to access an audio input at the same time.

I am a noob but read about Try blocks and encapsulated the part of my code that uses sounddevice in a Try block where it keeps retrying until the error code is no longer given. However, it seems that sounddevice will not recognize the input is free even after it becomes free. For example, running sounddevice.default.device on the 2nd instance will produce [-1,3] instead of the first instance's [0,0] even after the first instance closes. Weirdly there are usually only two devices:

1st instance sounddevice.query_devices() result:

* 0 sysdefault, ALSA (128 in, 128 out)
  1 dmix, ALSA (0 in, 2 out)

2nd instance's result:

  0 snd_rpi_hifiberry_dacplusadcpro: HiFiBerry DAC+ADC Pro HiFi multicodec-0 (hw:0,0), ALSA (0 in, 2 out)
  1 sysdefault, ALSA (0 in, 128 out)
  2 dmix, ALSA (0 in, 2 out)
< 3 default, ALSA (0 in, 2 out)

In addition to the Try block like this, I also tried reimporting sounddevice and rechecking but that also did not work. I haven't been able to find any solution from searching the error code, reading the documentation for sounddevice or portaudio. I also looked into a pidfile but that won't help if I can't get sounddevice to re-recognize the proper inputs.

Overall, I am trying to figure out how to get sound device to recognize that the input is free and to use it.



from Detecting when input is not being used for Python SoundDevice/PortAudio on Linux

No comments:

Post a Comment