i-rinat / apulse

PulseAudio emulation for ALSA
MIT License
610 stars 35 forks source link

Apulse does not work with dmix #85

Closed susobaco closed 6 years ago

susobaco commented 6 years ago

Hi, I noticed that when using abrowser (firefox) it doesn't allow you to use another audio playback program. The console output is as follows:

APULSE_PLAYBACK_DEVICE=dmix apulse abrowser

_[apulse] [error] do_connect_pcm: can't set sample format 14 ("FLOAT_LE") for playback device "dmix". Error code -22 (Argumento inválido) [apulse] [error] do_connectpcm: failed to open ALSA device. Apulse does no resampling or format conversion, leaving that task to ALSA plugins. Ensure that selected device is capable of playing a particular sample format at a particular rate. They have to be supported by either hardware directly, or by "plug" and "dmix" ALSA plugins which will perform required conversions on CPU.

More information: dpkg -s alsa-base | grep Version Version: 1.0.25+dfsg-0ubuntu5 dpkg -s abrowser | grep Version Version: 59.0.2+build1-0ubuntu0.16.04.3+8.0trisquel56

Distribution, Trisquel Flidas based in ubuntu xenial

latest version of apulse. need more information? Thank you

i-rinat commented 6 years ago

Hi.

dmix mixes multiple sound sources, but they have to have the same format and frequency. I believe, dmix dynamically chooses sample format based on hardware capabilities and what the first audio application requested. Then, all other applications must use the same format and sampling frequency. That's what I believe is happening on your machine. apulse were asked to playback 32-bit float samples (FLOAT_LE) and relayed that to the dmix device you've chosen. But dmix device said it cannot play that.

Try to use plug:dmix instead of dmix. (APULSE_PLAYBACK_DEVICE=plug:dmix apulse abrowser). That puts plug before dmix. Module plug is a universal sample format and frequency converter. It adapts anything to anything. In other words, while dmix allows multiple audio sources at once, they have to be the same exact format, which also should be supported by hardware. A combination of plug and dmix allows multiple audio sources of various formats simultaneously. Also, they need not to be supported by hardware natively. Conversion will be done automatically.

By the way, do you really need to use APULSE_PLAYBACK_DEVICE? If you are not doing any fancy audio configuration, defaults should work fine.

susobaco commented 6 years ago

Thanks for the answer. The problem is that when I open abrowser with apulse, it does not work with dmix, as I try to open audacious or vlc and it gives me a device opening error message, as it is busy. If I open audacious and vlc first, no problem, but apulse abrowser, it doesn't sound. I've tried what you said, and it's the same thing.

i-rinat commented 6 years ago

What about device called default? Did you try it?

Do you have any custom ALSA configuration (.asoundrc)?

susobaco commented 6 years ago

I have already done all the tests, with an asoundrc with dmix, without asoundrc, and the same problem continues

i-rinat commented 6 years ago

Sorry, I don't know what to try next.

There is nothing special in apulse regarding how it uses ALSA, it's just like any other application. Maybe with the only difference is that apulse doesn't try to resample on its own, as audio players like MPlayer do.

If I were to check various ALSA devices, I'd use mpv, as it starts faster that a browser. Say, you may try mpv --audio-device=alsa/dmix test.ogg to check dmix device. Or to try mpv --audio-device=alsa/plug:dmix test.ogg to test plug:dmix. If you find configuration which allows multiple instances of mpv with audio files with different sample rates play audio simultaneously, that device string should also work for apulse. As far as I know, audio resampling is disabled in mpv by default, so it asks ALSA that exact sample format what it got from a media file.

hishamhm commented 6 years ago

@i-rinat thanks once again for the pointers! the game of catch-up with Firefox continues: my setup stopped working this week again. Following your recommendation and changing dmix (which worked until recently) to plug:dmix. I guess Firefox changed the sound format they're feeding to PulseAudio?

i-rinat commented 6 years ago

I guess Firefox changed the sound format they're feeding to PulseAudio?

Don't think so. As long as I remember, Firefox used float32 samples. Maybe something have changed in ALSA libraries?

my setup stopped working this week again

Haven't checked yet, but could it be more strict sandbox that doesn't allow reading files from /usr/share/alsa anymore? That could explain why you are trying to specify ALSA device directly rather that relying on implicit default.

susobaco commented 6 years ago

These are the results: mpv --audio-device=plug/dmix Alien\ Nation\ El\ legado\ Uldara.avi Playing: Alien Nation El legado Uldara.avi (+) Video --vid=1 (mpeg4) (+) Audio --aid=1 (mp3) Audio --aid=2 (mp3) [ao] Audio output plug not found! [ao] Failed to initialize audio driver 'plug' [ao] This audio driver/device was forced with the --audio-device option. [ao] Try unsetting it. Could not open/initialize audio device -> no sound. Audio: no audio

mpv --audio-device=alsa/dmix Alien\ Nation\ El\ legado\ Uldara.avi Playing: Alien Nation El legado Uldara.avi (+) Video --vid=1 (mpeg4) (+) Audio --aid=1 (mp3) Audio --aid=2 (mp3) AO: [alsa] 48000Hz stereo 2ch s32 VO: [opengl] 560x416 yuv420p AV: 00:00:23 / 01:26:07 (0%) A-V: 0.000

I don't know what's wrong with me, my plug device doesn't work?

i-rinat commented 6 years ago

I don't know what's wrong with me, my plug device doesn't work?

That's OK, since you were just asking mpv to output audio through driver plug. It has no such driver, but has alsa for ALSA and pulse for PulseAudio.

If you want to play sound through plug device in ALSA, you need to use

mpv --audio-device=alsa/plug audio.ogg

But rather using plug, you'd probably want to use plug:dmix:

mpv --audio-device=alsa/plug:dmix audio.ogg

Here is why. dmix is a mixing device. It attaches to hw, which only supports what your hardware directly supports. dmix itself doesn't convert sample formats, so if you want to play float32 samples, but your hardware supports only signed 16-bit integers, it won't work. That's where plug comes to the rescue. It converts whatever you give it to the format of whatever you connected it to.

Looks like browser you are trying to use tries to output float32 samples through PulseAudio. When you are using apulse, it sees the format (FLOAT_LE) and asks ALSA for the same format. Then, if you are using plug:dmix, plug converts float32 to signed 16-bit integers, passes it to dmix. dmix mixes multiple streams (if there are multiple streams), and passes 16-bit integers to the hardware. Hardware generates actual voltage that drives actual speakers.

susobaco commented 6 years ago

Perfect, with this command, it works. Thank you very much. APULSE_PLAYBACK_DEVICE=plug:dmix apulse abrowser