r/linuxdev • • Sep 27 '13
How can the Linux audio infrastructure mess be fixed?
One complaint that has been leveled against Linux is that the audio infrastructure is messy and thusly has too many failure modes. I'm using Linux and enjoying it, but I think there may be a valid complaint here. I'm currently new to programming, and I still haven't learned digital audio signal processing, but I have plenty of free time. I could learn a great deal in a reasonable amount of time. I'd love to develop something that can replace parts of the infrastructure with a single framework. My question is: what are your thoughts on it? What would be the best route to clean up the infrastructure? Does it need to be cleaned up at all?
View all comments
Show parent comment
121
u/wadcann Sep 27 '13 edited Sep 28 '13
[continued from parent]
Software-based mixing (especially userspace mixing, as the sound servers did, versus ALSA-style kernel-side software mixing) is not as good as hardware-based mixing. Sometimes a program doesn't get to run immediately. In order to not have the audio break up, the absolute worst case, the longest amount of time that it can take before an audio server runs, needs to be computed. Then the audio server has to buffer that much data for mixing, so that the buffer never drains. (It's also nice to have a little bit buffered above-and-beyond that to improve mixing efficiency, if you're using userspace sound-server mixing, since it costs some CPU time to switch between running processes, and that reduces the number of context switches to perform.)
Hardware has to do a tiny bit of buffering too, but because it can be made to be a simple, dedicated system, the "worst case" can be easily made very small, and hence the buffer very small. There also isn't overhead with context-switching.
This buffer meant latency: play a sound, and it would take some time before the sound came out the speakers. That might be 50 milliseconds or even more, which is quite noticeable. This was a major concern of a lot of people in the early 2000s, where distros had been widely settling on sound servers to address the problem.
Mixing often provides resampling, since otherwise, if two different programs are wanting to play back sounds at two different sample rates, they would be unable to play at the same time. Some audio servers (esd being a major culprit; not sure whether ALSA's dmix even provided resampling, but if so, probably it) provided very poor-quality resampling. Playing sound at an off frequency could make it sound staticy or otherwise garbled.
In addition, sound servers often provided their own idea of volume. So now there were two volume controls (ALSA or OSS might provide one, two, three, or more for the card), and another for the sound server. Trying to figure out why sound wasn't coming out could involve looking at several different audio control panels as well as setting in the application.
So, now you've got a rough idea of some of the issues people were running into. Programs had to be written to support four or five sound subsystems, which game and app developers generally were not interested in doing. The user had to configure them to use a sound driver subsystem. If two sound servers were running at the same time, some sound wouldn't play back. There were often three, four, or possibly more volumes associated with a sound card, all of which were stored in different places, and no great tools for diagnosing just why sound wasn't coming out of a card. Distros were happily switching to new systems that often weren't quite done (PulseAudio was probably the most egregious offender here), so knowing how things worked on system X often meant not understanding system Y. Flash, which most people used for things like YouTube, only talked to ALSA and didn't go through a sound server; good for latency, but could mean that it wouldn't work if someone was playing music in the background.
PulseAudio
PulseAudio deserves its entire category of catastrophe. PulseAudio is another audio server. It was more ambitious in some ways than esd and artsd, and intended to replace them; most distros went through a slow, painful shift to PulseAudio.
PulseAudio wanted to provide several features: eliminate some of the latency introduced by earlier sound servers, let different users have their own audio based on what user was logged in on a screen, per-application volumes, etc.
The problem was that at this point, most stuff was kinda-sorta working and people had things kinda-sorta figured out. PulseAudio:
aoss-style LD-PRELOAD hack for OSS compatibility calledpadsp, which had the same issues asaoss.Finally, PulseAudio was really not ready when distros started including it. A lot of people simply had major chunks of their system not working when it went in, across multiple distro releases (like, years). Uninstalling PulseAudio wound up being the normal "sound isn't working" fix for years (and falling back to ALSA's real hardware interface rather than the virtual PulseAudio stuff). These people, of course, had a different configuration when they upgraded, so they hit their own, different problems. Diagnosing a system's sound problems might now involve ALSA, PulseAudio, a lot of older settings and obsolete troubleshooting fixes online for older problems that werre no longer present, multiple control panels, and people talking about the fine details of the Linux library loader in dealing with LD_PRELOAD.
[more to come in child]