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?

34 Upvotes

36 comments sorted by

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:

  • Introduced another incompatible API that broke existing systems.
  • I'm not familiar with the details, but apparently tended to choose over-aggressively small buffering, causing sound to break up.
  • To try to keep existing ALSA-compatible applications working, had a plugin for ALSA that would create a virtual sound card that would intercept sound supposedly going to ALSA, bounce it all the way back out to userspace in PulseAudio, then send it back down to the real sound card (probably in ALSA). This not only made for more volume controls, but it meant that trying to diagnose the situation was confusing for even technically-ept users, who mostly had to go discover what was going on one-by-one.
  • To try to let two different users work on two different consoles and have their own sounds playing, would cut off the audio, check the user on the new console, then put audio associated with the user back up. I guess it's a cute idea, but the practical effect was that switching consoles -- something that Linux console users do all the time -- resulted in audio cutting out for half a second every time, very disruptive when listening to music.
  • Provided a few different modes of operation; per-user and systemwide. Each had its own permission issues and things to deal with.
  • Had its own aoss-style LD-PRELOAD hack for OSS compatibility called padsp, which had the same issues as aoss.

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]

103

u/wadcann Sep 27 '13 edited Sep 28 '13

[continued from parent]

Other sound systems

I'm not familiar with Bluetooth audio or Firewire, but apparently both have their own audio subsystems. I think that ALSA and Pulseaudio can both talk directly to at least Bluetooth.

Compatibility libraries

Obviously, application developers don't care about most of this. 95% of video game vendors want to play back a simple stereo audio stream and have it show up on the user's headphones.

The result was the development of compatibility libraries, so that instead of having to support all of these interfaces, applications could write to one compatibility library and have things work everywhere.

Major libraries to do this included SDL (which also handled other things, but did a good job of having multiple audio backends) and libao. OpenAL was also used by a number of ported games, though it has different goals (mostly providing 3d audio with Doppler effects). Applications would be written to these libraries. These all could route audio to different backends. They often had configuration (OpenAL had a text config file. SDL used environment variables, though out-of-box configuration was mostly sensible on most systems.)

This meant that switching applications to use a different audio system had another layer of indirection/complexity/place where things could go wrong. You've got some script that set SDL_AUDIODRIVER=alsa to stop using OSS back in the day? Great...until you're trying to get your program to use PulseAudio (via the native interface) and can't figure out why it's going through ALSA.

Audio patch panel, JACK

Often, people have different sound applications that they want to use, and want to hook up to each other. Maybe they've got a synthesizer and a visual effects program for DJing. Maybe they've got some program to pull in MIDI. Back in the day, this would have been done via physical boxes, with audio cables connecting one box to another called a patch panel.

In the computer world, it makes no sense to run audio out of a sound card just to let things be hooked up across programs. People developed systems for letting one program on a computer talk to another on a computer.

Windows has some expensive, proprietary program whose name I don't recall to be a virtual patch panel; as long as two audio programs support it, the user can use it to "connect" a virtual output in one program to a virtual input in another.

Linux has an open-source system called JACK. This does a good job of eliminating latency that prior audio servers had introduced, and pretty much serves the same role.

However, it means yet another audio server and incompatible audio sound server API for applications to support. Generally-speaking, pro audio will use this (i.e. if you want to use serious Linux pro audio stuff like ardour, you're going to be hooking up to JACK. Then "consumer" audio today, simple playback and recording from a mic, mostly goes through PulseAudio.

Summary of issues

  • Often, a lot of complexity was exposed without providing a simple interface for users who didn't want all of the extra controls (e.g. ALSA's settings).
  • Sometimes a lack of Linux support from vendors meant that a piece of hardware wasn't supported (though this has been solved for a long time, and in fact, Linux's support for older audio cards tends to mean that it significantly beats Windows on hardware audio support).
  • People developing new systems often placed very little weight on providing hard backwards compatibility, or saw backwards compatibility as a short-term bridge to get people onto their new system. They were willing to break apps from people after a couple of years; there's no commitment to keep software working for ten years, say. This is a major concern I have with Wayland/Mir, which are demonstrating exactly the same sorts of positions ("oh, once you switch to my Wayland system, you can kinda/sorta use XWayland to kinda-sorta make X apps work...mostly...well, window managers won't work...and some utility programs might not work quite right...but my system is new and clever") that caused years of Linux audio compatibility breakages and frustrated users and looks to me to be heading straight down the same path.
  • Often, it was hard for users to diagnose problems, because the different audio systems never had a single UI. As a user, I want to deal with "sound", and so I go look at my sound control panel or want to run a "mixer" control panel or something. To this day, I cannot name a single mixer (volume control/sound control panel/etc) program that worked with all the various Linux audio systems or would let someone get an idea of how the audio on their system was being routed or play test audio out each. All it took was one output being muted or one program using a different backend to cause trouble. A new user would need to use "aumixer" (OSS), "alsamixer" (ALSA), "pavucontrol" (PulseAudio), and so forth, with nothing even telling them that these different systems existed.
  • A number of systems were included before they were really ready, probably because distros wanted to encourage their use. This is a very expensive practice in terms of user time sunk on fixing things.
  • There is some split between the traditional command-line Unix guys (people like me who want config files) and the I-came-from-Windows crowd (people who do not want config files and do want interactive sliders and don't care about command-line tools or mixers). PulseAudio tended strongly towards the Windows crowd, and ALSA the Unix crowd, and both tended to annoy people in the other crowd.
  • Many of the attempts to mitigate situations with incompatible interfaces involved introducing more incompatible interfaces.

Today, I'm not sure how much there is to fix. Most people are using PulseAudio for consumer stuff and JACK for pro stuff and simply time and maturity has meant that a lot of the wrinkles have been smoothed out, handled by the distro maintainers. I think that a single app that shows a diagram of where sound is "going" would be nice, and understands the various sound systems, but I don't think that re-engineering chunks of the system would solve much (and would make things worse). What Linux really needs is (a) more weight given to backwards-compatibility and using mature systems and making sure that things are done before they go in and (b) giving app vendors time to adapt to a given system.

7

u/[deleted] Sep 28 '13

Whoa, thanks. Audio was one of the reasons I gave up on Linux back in 2009 and haven't revisited it since (outside of using it for dev work and hosting). I'm happy to give Ubuntu 13.10 a try, though.

+/u/altcointip $3

5

u/ALTcointip Sep 28 '13

[Verified]: /u/im14 -> /u/wadcann, 0.0225782 ^Bitcoin(s) __($3)__ [help] **[tipping_stats]**

-4

u/[deleted] Sep 28 '13

[removed] — view removed comment

1

u/[deleted] Sep 28 '13 edited Sep 28 '13

[removed] — view removed comment

-2

u/[deleted] Sep 28 '13

[removed] — view removed comment

1

u/[deleted] Sep 28 '13

[removed] — view removed comment

1

u/[deleted] Sep 28 '13

I will not tolerate behavior like this from either of you. Be constructive and civil or don't come here.

2

u/[deleted] Sep 28 '13

Sorry, it was just such an opinionated hateful generalizing post, without any facts or reasoning whatsoever. Which I pointed out by writing the exact opposite opinion.

(He) never did make any point, but was just spurting opinions mixed with condescending language.

I'm perfectly fine with moderators not wanting this, it's just noise of zero value, and I'll delete these 3 posts if you want.

1

u/[deleted] Sep 28 '13

Just make sure not to let it happen on this subreddit again. I want this to be a good place for people who devleop on Linux. Flamewars over operating systems are not good. Thank you for promising to follow that guideline. And I believe I've removed the posts you made, unless they're still showing up.

→ More replies

4

u/kzr_pzr Sep 27 '13

Great explanation and a window to the history. I have to work with Linux audio at work (building HA clusters with "uninterruptible" audio output). It just causes me huge headache. Now I know a bit why it is so. Thank you.

BTW, we do stuff in old Java 1.5 which has its own oddities. Actually, you mentioned the worst one from my POV: while all 'standard' desktop apps use PulseAudio to interface the sound card, Java uses ALSA's device directly. There is no way to play sound from our Java app while a stream is playing in background. At one point, we had to use additional USB sound card with additional speakers to meet the project's specification.

2

u/wadcann Sep 28 '13

Actually, you mentioned the worst one from my POV: while all 'standard' desktop apps use PulseAudio to interface the sound card, Java uses ALSA's device directly.

In a distro with the virtual ALSA sound card set as default, the thing that routes back up to PulseAudio, you may be okay.

1

u/[deleted] Sep 28 '13

Today, I'm not sure how much there is to fix.

According to some game developers a lot. For instance the World of Goo Linux port, it was mentioned that audio timing was by far their biggest issue. That is some time ago now, but just a couple of days ago Valve also named audio as the biggest problem for developing Linux games.

Audio works fine for me, but I have an extremely simple audio setup, exactly because I can't configure it properly anyway, so I don't even use my M-Audio anymore, although it is superior to the Realtek HD I use now.

1

u/uep Nov 14 '13 edited Nov 14 '13

Honestly, from the perspective inside the kernel, I think ALSA is amazing. Its modular framework is impressive. It's crazy complicated, but it manages a ton of possible different combinations of hardware. If you hook things up in the right way, it handles a lot of redundant work for a driver automatically.

edit: grammatical sanity.

-3

u/caspy7 Sep 28 '13

If I'd gotten through all of this I'm confident I'd be telling you how good of an explanation this was.