Sound problem: how to control output of sound outside of Kodi

  • I have a Raspberry Pi running LibreELEC 9.2.

    It can output sound via HDMI, and also from its 3.5mm (Analogue) port.

    I am having some issues getting the Pi to output sound to the ports I want, from applications outside of Kodi. More specifically, I would like to be able to control whether sound will be sent to HDMI, Analogue, or both, from non-Kodi applications, such as mpd, aplay, etc. I have a limited understanding of ALSA and linux sound.

    I have added dtparam=audio=on to the config.txt file to enable ALSA.

    In Kodi, I can set the audio output device. The menu there shows seven options:

    ALSA: Default (bcm2835 ALSA bcm2835 ALSA)

    ALSA: bcm2835 ALSA, bcm2835 ALSA

    ALSA: bcm2835 ALSA, bcm2835 IEC958/HDMI S/PDIF

    PI: HDMI

    PI: Analogue

    PI: HDMI and Analogue

    PULSE: Default, Bluetooth Audio (PULSEAUDIO)

    So far everything is fine. I normally set the audio device to PI: HDMI, and the sound from Kodi goes there as expected. When set to PI: HDMI and Analogue, the sound from Kodi goes to both places as expected.

    For applications outside of Kodi, however, I'm having a hard time understanding how to get sound to work the way I want.

    From an ssh shell command line, to view the ALSA devices, I do:

    and to view a list of ALSA defined PCMS:

    When I test these devices/PCMs by playing a small WAV file with aplay, it seems that default, sysdefault, and device hw:0,0 all output sound to the HDMI device; ie958, hw:0,1 and hw:0,2 all play no sound and return an error message :

    aplay: set_params:1345: Channels count non available

    So, all these devices either output to HDMI, or nothing. None of them output to the Analogue port (logically, one would think there would be a device for that).

    It is possible, however, to output to the Analogue port via ALSA by using amixer to change the content num=3 to 1. So if one does

    # amixer cset numid=3 1

    ...then all the devices/PCMs that were outputting to HDMI will now send to Analogue. You can then reset num=3 to 2 for HDMI output, or 0 for automatic. But this switches ALSA globally, rather than allow each application to use the sound output device it needs.

    So, I am left with the following questions.

    1. Kodi is able to output to both HDMI and Analogue simultaneously. Shouldn't ALSA have the same capability?

    2. Do the Kodi audio devices PI:* use ALSA? If not, what do they use? Could this system be used for applications outside of Kodi as well?

    3. If the Raspberry Pi has two ports to output sound, should there not be two ALSA devices that can access each of them?

    and the bottom line:

    4. How can I control which port sound gets output to for non-Kodi applications?

    Thank you for any help you can provide.