r/linuxaudio • • 3d ago

Current Options for Linux Sound Software development

Hello, I'm new to sound programming in Linux and I'm trying to understand the current low-level options that are available. The four frameworks I'm aware of are ALSA, Pipewire, PulseAudio and JACK. (Also miniAudio but I was curious about being a little closer to the drivers and don't mind being linux-only). So far, it seems to me that:

  1. All of those frameworks (pw, pa, jack) use ALSA on the backend which is built into the linux kernel directly to work with nearly any sound card.
  2. Pipewire is seen as the thing that will eventually replace JACK and PulseAudio, both of which will have legacy support
  3. Many distros are moving to Pipewire but not all quite yet
  4. Writing a program in ALSA guarantees that it the program will work on nearly any distro more so than pipewire/jack since it's only dependent on ALSA which is compiled into any linux kernel that also contains any of the other 3 frameworks

That in mind, it seems best to develop in either pipewire or ALSA. I'd like to make programs that are as minimal as possible and can sort of "run on anything", even if that means that development will take longer and I have to minimize feature set(I want to make a retro music tracker in ncurses which only really does basic sampling and synthesis), so I've been leaning towards ALSA. But I've been trying to get ALSA up and running and its been difficult because the documentation seems scattered and incomplete (or so I've read in articles that are linked on alsa-project.org ). I tried to get the device to just initialize and it didn't work, even though I feel like I read the instructions thoroughly and read most of the articles available on the site in the developer intro page. The error code didn't give any description, all I know is that I called "snd_pcm_open()" exactly as described in the documentation and it returned some sort of error code that I'm not sure how to troubleshoot.

I'd spent a little time on it so I figured I'd make a post about it cause I feel like I'm doing something dumb and somebody's gonna reply "why are you doing it that way why not just do this other thing" which would be great cause then I could just do that instead lol

Are there other alternative frameworks that are preferrable to use? Or is it just a skill issue? Maybe at this point I should just be asking in the irc about stuff and help to actually write some documentation but I don't know if I'm really qualified to do that. Any suggestions/criticism appreciated, I'm new so I figured I'd just get my thoughts down so everyone could correct me lol

Edit: I've found some more links and resources that seem to help with understanding some of the Linux Audio situation?

https://www.oreilly.com/library/view/linux-sound-programming/9781484224960/

https://0pointer.de/blog/projects/guide-to-sound-apis.html

https://equalarea.com/paul/alsa-audio.html - going to try to get this example running next, I used a different howto initially, maybe this will work differently?

6 Upvotes

14 comments sorted by

4

u/beatbox9 3d ago

Which major / mainstream distros do not use pipewire today? And why wouldn't you just continue to use pulse, knowing that anyone with pipewire will be fine?

ALSA and pipewire/pulse/jack/ucm have different purposes.

Similar to how amdgpu/nvidia-open have different purposes from x11/wayland.

1

u/toon_link_776 3d ago

That's what I'd read, I may have read something dated. I guess nowadays pipewire is standard?

2

u/beatbox9 3d ago

Pipewire is standard on most operating systems; but most apps are written for pulseaudio, with a handful of audio-specific apps written for jack.

4

u/Brer1Rabbit 3d ago edited 3d ago

ALSA is a bit of a beast. If you want to use it directly study the example programs: there's a pcm.c file and a few others. While long, aplay.c is really good too.

Most frameworks that build on top of ALSA want to run an event loop for you, and have you register callbacks. I had a use case where I had my own event loop and wanted to read samples one frame at a time. Basically start the stream then call into my own ALSA wrapper to advance a cursor to the next frame, processing those samples. It's just under 500 lines of code, uses memory mapped access, and handles error recovery decently well. I won't call if bullet proof but it could be a good starting point if you're looking to roll your own. https://github.com/brer-rabbit/zoxnoxious/blob/main/raspberry_pi/src/zalsa.c

1

u/toon_link_776 3d ago

Yeah I got the miniaudio callback running, and saw that most of them use some variation of that. thanks for the reference

3

u/jason_gates 3d ago edited 3d ago

Hi,

Linux is open source. Thus, I would ( as I have ) take advantage of existing mature ( I.E. proven track record ),open source audio projects. If you are not familiar with use of a "step" debugger, I would invest time in learning their use. There are many software development vendors who donate free gui style debuggers ( either standalone or integrated in an IDE ). A step debugger allows you to run an application program ( like a DAW ) and inspect the instructions and data structures as the application runs. The trick is, you need to have an audio program who's source code is available and a project which can re-built with debugger support. Hence, open source projects, virtually will already have project build instructions to add debugger support. In many areas this is remedial programming processing. My experience is, that is still the very best way to study programming ( including specific areas like audio).

I would take a look at the Arch Linux website https://archlinux.org/ . Look at the details they provide for every application they distribute. Each dependency is listed, how that dependency is used is also listed ( I.E. for make/build only ). It is one of the reasons why distributions like Arch are popular for developers.

The Arch Linux WIKI https://wiki.archlinux.org/ is also a great resource for finding open source audio applications.

Hopefully that makes sense. I am writing this really quickly.

2

u/toon_link_776 3d ago

No problem, I've been using the GNU debugger, I'll see how that works, maybe I'll look at the fasttracker II clone? thanks for the advice

2

u/63626978 3d ago

All of those frameworks (pw, pa, jack) use ALSA on the backend

This is btw not true for some cases, PW + BlueTooth or JACK + FireWire, for example, or even PipeWire on JACK etc.

As for which API to use it kinda depends on what you're building. Strictly consumer audio should just use PA, strictly pro audio target JACK. I've seen either categories use PortAudio as well, which is a portable abstraction and works with Windows and mac os as well.

Games are kind of a different thing as they are considered consumer audio but may need lower latency, just use a framework (engine) in that case.

1

u/toon_link_776 3d ago

Yeah I saw the diagram in "Linux Sound Programming" that shows all of the dependencies in Chapter 4, seems like its more complex than a linear "this depends on that and this goes to the driver", more like a spaghetti lol. It would probably be technically easier to just use pulse, but I'd like the program to be usable on any distro, even the extremely minimal ones, and it seems like alsa will work on even relatively bare distributions, which is what I was mostly trying for.

1

u/63626978 3d ago

For broad compatibility really just use PortAudio or a similar abstraction with weak linking to libjack etc. so at runtime you determine what's available. VCV Rack for example does this and VLC player as well

The problem with only using ALSA is that many older systems with PulseAudio will not allow your program to use the same ALSA device, you'll have to request it from PA first and then PA stops using it.

4

u/rileyrgham 3d ago

Pipewire is the way. It can harness the others. It's a fools errand to go any other way. Alsa is a mess btw.

2

u/mandale321 3d ago

Just a correction about the "four frameworks": as you noted, pipewire does use ALSA as a backend. But also, pipwire implements pulseaudio and jack. So now in modern distros, pulseaudio will actually be piwewire-pulseaudio and jack will actually be pipewire-jack.

Note that, in addition, pipewire also implements libasound, which is the ALSA high level library ! So a device can also "talk" to ALSA via libasound like if it was directly calling low level, owning the whole device, but actually be also abstracted by pipewire.

So you choices, if you want realtime (i think your use case requires that) is actually either libasound or jack. jack is very well documented, worth a try.

miniAudio is something else altogether: it's cross platform and cross api (has pulse, alsa and jack backends) and offerts also various audio abstractions like those needed for game dev, for instance.

1

u/Specialist-Dog-501 2d ago

Not an expert here, just my 5cents. Jack, alsa have exclusive access to audio out, so no other applications can stream through that output. Pipewire "resolves" that and manages multiple audio outs from different sources. The price for this-in my case: latency. If your apps can work exclusively or cannot tolerate latency you can use alsa, otherwise pipewire is better to support.