Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portaudio won't play sound if another program is currently playing

Tags:

c

portaudio

I have a program using PortAudio. Currently I'm starting it up with Pa_OpenDefaultStream(&stream, 0, 2, paFloat32, 44100, 256, audioCB, udata), but when I compile and run my program it won't play sound if another program is currently running (such as a music player). The error string is PortAudio error: Device unavailable

I think I need to be running Pa_OpenStream instead, but I'm not sure what I should be passing it to make it play nice with other programs.

for reference, I tried printing out the information on the available devices, but I'm not sure what to do with this information.

Device 0
name /dev/dsp
hostAPI 0
maxInputChannels 16
maxOutPutChannels 0
defaultLowInputLatency 0.011610
defaultLowOutputLatency -0.117208
defaultHighInputLatency 0.046440
defaultHighOutputLatency 0.000000
defaultSampleRate 44100.000000
Device 1
name /dev/dsp1
hostAPI 0
maxInputChannels 16
maxOutPutChannels 0
defaultLowInputLatency 0.011610
defaultLowOutputLatency -0.117208
defaultHighInputLatency 0.046440
defaultHighOutputLatency 0.000000
defaultSampleRate 44100.000000
Default Device -1

While writing this question, I also just noticed that the maxOutputchannels changes depending on whether or not there's another program trying to play sound. At least I think that's what made the difference. I had a paused youtube video in anther browser tab that I closed, and when I reran my program now it prints this:

Device 0
name /dev/dsp
hostAPI 0
maxInputChannels 16
maxOutPutChannels 16
defaultLowInputLatency 0.011610
defaultLowOutputLatency 0.011610
defaultHighInputLatency 0.046440
defaultHighOutputLatency 0.046440
defaultSampleRate 44100.000000
Device 1
name /dev/dsp1
hostAPI 0
maxInputChannels 16
maxOutPutChannels 0
defaultLowInputLatency 0.011610
defaultLowOutputLatency 0.011610
defaultHighInputLatency 0.046440
defaultHighOutputLatency 0.046440
defaultSampleRate 44100.000000
Default Device 0
like image 819
Alex Avatar asked May 15 '11 05:05

Alex


1 Answers

This .asoundrc file should allow you to use software mixing using your ALSA sound. You may need to change the pcm "hw:1,0" line to match your hardware. This example is from the ALSA Wiki

pcm.!default {
    type plug
    slave.pcm "dmixer"
}

pcm.dmixer  {
    type dmix
    ipc_key 1024
    slave {
        pcm "hw:1,0"
        period_time 0
        period_size 1024
        buffer_size 4096
        rate 44100
    }
    bindings {
        0 0
        1 1
    }
}

ctl.dmixer {
    type hw
    card 0
}
like image 160
Seth Avatar answered Sep 20 '22 00:09

Seth