10

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
Alex
  • 14,973
  • 13
  • 59
  • 94
  • Are you using ALSA, OSS or a different backend for your sound? – Seth May 18 '11 at 12:42
  • I think ALSA, but I'm not totally sure. I'm running Ubuntu 10.04LTS, so most likely whatever the default for that is. How do I check to be sure? – Alex May 18 '11 at 17:02
  • OK. Unless you've re-compiled your kernel, you are using ALSA. Check out `http://www.alsa-project.org/main/index.php/Asoundrc` I'm posting an example .asoundrc below – Seth May 21 '11 at 09:36

1 Answers1

3

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
}
Seth
  • 2,683
  • 18
  • 18
  • Other programs, like Audacity, which use Portaudio don't have the same problem that my program is having. Why would I need to change my configuration if other programs can get it to work? – Alex May 21 '11 at 16:46
  • /dev/dsp is the OSS emulation for ALSA, so you'll need the asound software mixing. But alternatively, if you want to use pulse audio as your interface to the backend, you can use a different interface. Check out http://alsa.opensrc.org/OSS_emulation and http://stackoverflow.com/questions/2351884/why-does-portaudio-not-play-nicely-with-other-audio-programs-or-how-can-i-get-it – Seth May 22 '11 at 15:23
  • I'm still not clear why I have to go editing configuration files to get my audio working with my program. Why is it that other programs run just fine but mine can't? What am I doing differently? – Alex May 24 '11 at 05:59
  • They are likely opening the ALSA device directly. Going through OSS emulation (`/dev/dsp`) requires software mixing if you want multiple sources. If you would rather open the ALSA device, you will need at least PortAudio v19. – Seth May 24 '11 at 13:33