Live data from Hacker News

Show HN: Nanoalsa, asoundlib replacement for embedded systems

gitlab.com

1–2 of 2 posts

Show HN: Nanoalsa, asoundlib replacement for embedded systems

#1
Hi,

I wanted to use ALSA in an embedded system, and that's when I realized how bad the official asoundlib library actually is. It is huge, bloated, requires tons of configuration files, tries to load shared libraries in run-time, not thread-safe (due to the use of signals), often segfaults, and leaks memory badly (its developers arrogantly deny that, but valgrind confirms that it really does).

Unfortunately the Linux ALSA interface isn't documented at all (there's a doc for sound card driver writers, called "kernel API", and the asoundlib high-level interface for applications, called "library API", but nothing about the ioctl interface, https://www.alsa-project.org/alsa-doc/alsa-lib/). So I was curious how much of the asoundlib's bloat we can get rid off. I've spent days figuring out what asoundlib actually does, and strace was my best friend during this experiment.

https://gitlab.com/bztsrc/nanoalsa

I've concluded my findings in Nano ALSA, an MIT licensed, stb-style single header library (12k, ca. 300 SLoC). It's based on the UNIX philosophy, "do one thing only, but do that right". So it can do one thing, and one thing only. It can be used to easily play PCM data, which is what most applications want. It does not load shared libraries in run-time, it does not use signals, it does not have a mixer nor a sequencer, just good ol' PCM playback all there is. K.I.S.S.

Example usage:

alsa_t ctx; /* internal state (few bytes only) */

alsa_open(&ctx, 0, 0, SNDRV_PCM_FORMAT_S16_LE, 44100, 2); /* specify requested characteristics */

alsa_write(&ctx, buf, numframes); /* play the audio */

alsa_close(&ctx); /* free resources */

That's all to it. (Also has an async interface too, which is unlike asoundlib's, signal-free and thread-safe.)

Show HN: Nanoalsa, asoundlib replacement for embedded systems
gitlab.com

Re: Show HN: Nanoalsa, asoundlib replacement for embedded systems

#2
Two things:

- I've added transparent PCM resampling, so it's ca. 400 SLoC now. The API hasn't changed, and the feature can be turned off with a define.

- Bad project name, I've just found out that there's already another project by this name: https://github.com/psqli/nanoalsa

The two are not related in any way: that's GPL3 licensed, mine is MIT. That's a .c source, mine is a single header library. And the API is totally different too (pcm_open, pcm_set, pcm_hw_params_setup, pcm_write, versus alsa_open and alsa_write). That supports recording, mine does not. That does not support async playback, mine does.