libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.97k stars 1.84k forks source link

Possible to use ALSA software devices (i.e. plughw, dmix, dsnoop, etc?) #8717

Open W3AXL opened 11 months ago

W3AXL commented 11 months ago

Hello all!

Trying to use SDL2 for my application (well, technically SDL2 via the SipsorceryMedia.SDL2 C# library) and I'm hitting an issue on my linux test system.

Using the device names from aplay -l/arecord -l, everything works fine. However, I use ALSA dmix & dsnoop devices to split/combine multiple PCM audio channels to a single 16-in/8-out audio interface. These "software devices" are defined in my .asoundrc file like so:

...
pcm.line_in_11 {
    type plug
    slave.pcm {
        type softvol
        slave.pcm {
            type dsnoop
            ipc_key 12342
            slave ins
            bindings.0 10
        }
    }
}
...
pcm.line_out_3 {
    type plug
    slave.pcm {
        type softvol
        slave.pcm {
            type dmix
            ipc_key 4242
            slave outs
            bindings.0 2
        }
    }
}
...

And show up with the capital "L" aplay/arecord switches as follows:

$ aplay -L
...
line_in_11
line_in_12
line_in_13
line_in_14
line_in_15
line_out_3
line_out_4
line_out_5
line_out_6
...

However, using these device names in OpenAudioDevice() returns 0. Is it possible to use these "software" ALSA audio devices with SDL2, or are we restricted to hardware devices only?

W3AXL commented 10 months ago

I am able to test this as soon as changes are ready - unfortunately my C isn't terribly good otherwise I'd take a crack at writing the PR myself.

The most basic setup to test this would be to create (or modify) your ~/.asoundrc file to include a simple dsnoop PCM definition (as explained here):

pcm.dsnooptest {
    type dsnoop
    slave {
        pcm "hw:0,0" 
        channels 2 
    }
}

of course using the correct device definition for your system in place of hw:0,0

Then you can run the testaudiocapture example program. Currently I get the following error when it attempts to open the PCM dsnoop device:

$ ./testaudiocapture line_in_11
INFO: Using audio driver: alsa
INFO:  Capture device #0: 'USB Audio Device, USB Audio'
INFO:  Capture device #1: 'US-16x08, USB Audio'
INFO: Opening default playback device...
INFO: Opening capture device 'line_in_11'...
ERROR: Couldn't open an audio device for capture: No such device.!
slouken commented 5 months ago

Can you check https://github.com/libsdl-org/SDL/pull/9753 and see if it works for you?