Problems with ofSoundStream and ofSoundPlayer

hi,
in my application I need to play audio files (using ofSoundPlayer) and some generated audio coming from an NDI network stream (with ofSoundStream). The two don’t have to be played simultaneously.

Using ofSoundStream and audioIn is straightforward enough, but unfortunately when ofSoundPlayer is used it somehow fails to release the audio device. Trying to subsequently open ofSoundStream leads to the following error:

RtApiAlsa::getDeviceInfo: snd_pcm_open error for device (hw:0,0), Device or resource busy.
[ error ] RtApiAlsa::probeDeviceOpen: pcm device (hw:0,0) won't open for output.

The error is not fixed by calling ofSoundStreamClose() before opening it again.

Interestingly enough playing a video (with gstreamer based ofVideoPlayer) does not lead to any error, so it must be something related to the sound player. I tried deleting the player object after it stops but this does not fix the issue.

So is this a bug or I am missing something, may be obvious?

thanks for helping
Davide

Hi,
You can’t use ofSoundPlayer and ofSoundStream together. It’s either one or the other. There’s code for an ofSoundStream based sample player somewhere, I certainly got one working at one stage.

When you mention gstreamer, do you mean ofVideoPlayer + ofSoundplayer work together? That’s the normal behaviour.

Yes I understand they cannot operate at the same time, but it is not possible to use ofSoundStream after an ofSoundPlayer has been used, then closed and deleted.

It looks like ofSoundPlayer fails to release some audio related resource. I’ll try to write a MWE and post it here

Ah ok. That’s not good, and that’s probably a bug!
Can you give some specs? What distro and what version of OF?

The minimum example to reproduce the bug is this, in ofApp:setup():

ofSoundPlayer *soundPlayer = new ofSoundPlayer();
soundPlayer->load("test.mp3");
soundPlayer->play();
delete soundPlayer;

ofSoundStreamSetup(2, 0, 44100, 256, 4);

ofSoundStreamSetup fails with an error. Remove the first four lines and it runs fine.

My setup is Ubuntu Mate 19.04, of0.10.1 nightly (I can’t remember exactly which one, but I’m confident the soundPlayer code has not been changed much in latest releases). Code built with make on command line.

That does sound like a bug!
Could you open an issue on Github?

I wonder, though if you do soundPlayer->stop(); before you delete it does that help?

Ok done. Issue is #6476.

I also tried adding a stop() but unfortunately it doesn’t help.

you might be able to use a sound stream and a sound player by using the pulse audio api for the stream instead of the default alsa. the sound player might be opening a global resource that uses alsa itself and blocks anything else from using it but pulseaudio should be running in the background already so it might work

1 Like

Yes it works! Thank you :slight_smile:

Just in case someone else needs a hint this is what I did:

ofSoundStreamSettings settings;

settings.bufferSize = 256;
settings.numBuffers = 4;
settings.numInputChannels = 0;
settings.numOutputChannels = 2;
settings.sampleRate = 48000;
settings.setOutListener(this);
settings.setApi(ofSoundDevice::PULSE);

ofSoundStreamSetup(settings);

Hi, I did what were originaly trying to achieve with my addon ofxSoundObjects and an additional one ofxSoundObjects_NDI