🔉️ Simultaneous HDMI+analog audio output on Raspberry Pi with ALSA
By OctoSpacc
Caution
The content of this page has been entirely machine-translated into English, from Italiano. Therefore, it might contain any kind of errors.
In my new mission to integrate, in the form of a home retrogaming console, any computer that works with a well-established configuration with RetroArch, and that allows me to play by passing ROMs and save files between different devices… the Raspberry Pi, with installed the Linux distro RetroPie, popped out as the ideal piece of the puzzle.
Without digressing on the matter itself, for which I instead have more information on my MicroBlog ([https://octospacc.altervista.org/series/gaming-1/](https://octospacc.altervista.org/series/gaming- 1/)), for each of the problems alleviated by this configuration, just as many uncomfortable ones emerged, in theory simple to solve but in practice not at all trivial… and one of these concerns audio.
The output problem 🚰️
Wanting to make the most of this Rasperino so set up, to be able to play by adapting to different situations at home, it is essential to be able to choose moment by moment which audio output to listen to. Basically, there is the audio transmitted via HDMI, the logical and often only choice for using the TV speakers… and then there is the one emitted on the 3.5 mm analog jack port, integrated into the single-board computer, very useful instead to play with headphones or earphones (because the port is closer, and because the volume is controllable without being at the mercy of the terrifying TV firmware).
All seemingly trivial, until I realized that, out-of-the-box, the system offers nothing either to change output on the fly (for example, with a key combination, or a quick setting in the main menu), nor the possibility of firing the audio on both outputs regardless.
In reality, it seems that on some systems you can set the audio output to automatic mode1, that is (as on any device today) goes to or from the external output when the connector is plugged in or out, but… aside from the fact that it is said to be unreliable, this feature is missing on my Raspberry Pi 3, for some reason.
The solution: everything 💡️
In such a disgusting situation, where the audio output would have to be changed manually every time by returning to the system menu, and then opening the very ugly (and from gamepads, very uncomfortable; the keys are all worn out) ncurses
configuration menu, and then going back - subtracting precious minutes of gaming - the least bad option was to, in some way, force this output simultaneously.
It couldn’t be impossible, given that, according to what I read during the research, Kodi on Raspino succeeds…
ALSA configuration 🔩️
The main problem was to configure ALSA, the basic Linux sound system, (and generally the only one pre-installed on non-desktop distros like RetroPie), to work like this here. Well, it’s a shame that ALSA is notoriously difficult to manage, to the point that there are tons of dead threads on the web about exactly how to solve this problem here…
After having searched everywhere in vain, finding long or short configurations but all equally broken, I feel an art attack and decide to… ask ChatGPT2 . Here, beyond a few stupid oversights, which resulted in me wasting even more time, the artificial intelligence gave me a minimal, understandable configuration that works as necessary.
The final configuration is therefore the following, it assumes purely stereo audio and can be used directly on Raspberry Pi 3 without modifications… or it can be a starting point for other uses and other devices (where the parameters obviously need to be changed). The optional part at the end makes the analog card the default for software volume control (for programs like alsamixer
), instead of the digital default, which is useful for use with headphones.
The contents must be copied into a new file /etc/asound.conf
:
# Output audio su entrambe le schede
pcm.!default {
type plug
slave.pcm "both"
}
pcm.both {
type route
slave.pcm {
type multi
slaves {
a { # HDMI
pcm "hw:0,0"
channels 2
}
b { # Analogico
pcm "hw:1,0"
channels 2
}
}
bindings {
0 { slave a; channel 0 }
1 { slave a; channel 1 }
2 { slave b; channel 0 }
3 { slave b; channel 1 }
}
}
ttable {
0.0 = 1 # HDMI Sx
1.1 = 1 # HDMI Dx
0.2 = 1 # Analogico Sx
1.3 = 1 # Analogico Dx
}
}
# Controllo volume default sull'analogico (opzionale)
ctl.!default {
type hw
card 1
}
Trolling setup 👿️
You should pay attention to other files that could take precedence over the one inserted (as system) in /etc/asound.conf
, for example ~/.asoundrc
(which is the user configuration, precisely in the user’s home folder , in this case /home/pi/.asoundrc
).
The latter is created automatically by RetroPie when you use its menu to choose the audio output… which is not a problem, because with the new configuration we can also throw away that menu, but you have to know that that file is now , and in the future it will be (if recreated by accidentally opening that evil menu), deleted.
Configuration testing 🔉️
This step would be optional, and you can go straight to trying games, especially if the configuration has been copied precisely and for the same hardware, but… if you are already in the terminal, or don’t take long to use it, a testing costs nothing.
You connect the Rasperino to an HDMI device with speakers, then connect other speakers, or headphones, whatever, to the analog output, simply run the speaker-test
program, and listen to see if the noise comes out.
If your ears say everything is OK, then everything is fine here, and you don’t even have to worry about any of the horrible writing that comes out on the screen. You can stop the program with CTRL+C.
If you don’t hear anything, it’s time to start alsamixer
, check the levels, and switch the outputs if they are… While, if the program closes immediately with errors, I don’t know what to say: on my machine works.
If you perceive that the two audio sources are not in perfect synchrony… I think it is completely normal, and that there is no way to fix it (also because a configuration with various ALSA plugins did not work for me), but in this case case doesn’t matter anyway.
RetroArch setup 👾️
Rightly trying to start a game at this point, beware: disappointment is around the corner. In fact, here’s the even simpler thing for which I wasted even more time: making sure that the audio works well in RetroArch, without crackling, jerking, skipping pieces, or generally distorting.
In my case, the default audio driver used by RetroArch is alsathread
… which for some reason gives these problems, while the normal alsa
driver does not.
Then just launch the main RetroArch screen (from the EmulationStation menu: RetroPie > RetroArch), from which go to Settings > Drivers, and set Audio to alsa
.
Then, before exiting, to save the changes: Main Menu > Configuration File > Save Current Configuration.
At this point, returning to the system menu, it’s time to try a game to see if everything is ok… and in my case it was. In theory, using the non-threaded ALSA driver can have a negative impact on overall system performance3, and therefore of the emulation, but in my tests on the fly I didn’t notice any difference… even if I didn’t try anything heavy. Therefore I didn’t even have the need to change other audio options, such as the resampler or synchronization, but in case of problems it’s a good idea to tinker there; there aren’t many other ways.
Bluetooth audio? (No.) 💙️
I was almost about to continue down the path of self-inflicted torture, to somehow manage to fit Bluetooth headphones into this whole setup, but… as useful as they would be for playing remotely from a TV, even if I wanted to waste even more time set them up, it’s probably not worth it. Better to buy an audio jack to Bluetooth adapter, or directly use something wired with an extension cord!
Considering how much my old smartphone started to stutter simply because I played videos with Bluetooth earphones instead of wired ones, I don’t think that the same setup on a Raspino would benefit the performance. And anyway, I couldn’t even connect a pair of Bluetooth headphones; during pairing, an error traceback is displayed on the screen, but afterwards the device is paired… but cannot connect. Maybe the Bluetooth audio drivers are also missing, or something.
Furthermore, I have a feeling that it is in any case impossible to make Bluetooth audio work (well) with ALSA, because, even in this case, the web doesn’t offer much advice. However, PulseAudio is an additional pain to set up, notoriously adds extra CPU load, and forces you to choose between having delays or glitches in the audio. Its successor, PipeWire, has fewer problems but is even more cumbersome.
Conclusion 📢️
In hindsight, it was really bullshit; it didn’t even deserve such a long note. The important thing is that everything works now, and I can finally do my gaming!
Unfortunately, for now, once again I have not found any way to change the volume emitted by the Raspberry while you are in a game, and to do so you will have to open alsamixer
(oops) on a TTY (also reachable from the system menu by going to RetroPie > Audio > Mixer).
If I really need it, I’ll invent at worst a program similar to AlsaMixer, just to change the volumes, but which can actually be used without a keyboard… or, ideally, something that allows you to do the same at any time, without changing the active app , with a key combination; nothing for now!
P.S: This was going to be a rant — or, as a dictionary would say, an invective — within a post on the MicroBlog, but after writing 2 paragraphs I had a hunch that I would solve the problem quickly enough so as not to justify talking about it right away… and, this time too, I’m happy I listened to the occult spirits! A note was then issued, because the vitriolic text had lost its meaning, and it was worth adding more precise details.
I’ll repeat it until I die, it’s shocking: on Linux you can do anything if you want, and in theory it’s all simple (I say this genuinely without irony), but in practice nothing ever works by default, and in fixing things in practice there is always some snag. In this case, RetroPie uses ALSA […], which is the worst thing mankind has ever invented to configure… I have about ten web pages open about it, and so far the only results I’ve had [ …] there were errors on the TTY and silence on the speakers, rather than simultaneous output. It’s The End. 🗡️
~ Literally me at my lowest point.
🏷️ Notes and References_
-
“Switching audio output between jack and HDMI on RetroPie”: [https://lofi-gaming.org.uk/blog/2014/04/switching-audio-output-between-jack-and-hdmi-on-retropie/](https://lofi-gaming.org.uk /blog/2014/04/switching-audio-output-between-jack-and-hdmi-on-retropie/); the photos of the screens show that on certain systems there is the elusive automatic output… ↩
-
Here for reference is the conversation we had with ChatGPT, even if it isn’t very useful in itself: https://chatgpt.com/share/66ebfd94-b7e8-8001-89c4-e7424dd128a9. ↩
-
From “Use “alsathread” instead of “alsa” in RetroArch on RPI3+”: [https://forum.batocera.org/d/5509-use-alsathread-instead-of-alsa-in-retroarch-on-rpi3](https://forum.batocera.org/d/5509-use- alsathread-instead-of-alsa-in-retroarch-on-rpi3): “I’ve noticed that using “alsathread” instead of “alsa” driver in RetroArch leads better audio sync and frame smoothing on non-native screen framerates […] less sound cracks and lags on low-performance games/cores.” ↩