handmade.network » Forums » Work-in-Progress » mini_al: A C/C++ cross-platform, single file, public domain audio library
mackron
David Reid
7 posts
#13417 mini_al: A C/C++ cross-platform, single file, public domain audio library
1 year ago

For a while now I've been working on a little audio library that I thought some of your guys might be curious about. It's a public domain, single-file C/C++ library with a focus on simplicity. It's primary purpose is to simply connect to a device and handle the data transmission with appropriate data conversions. It is not a 3D audio API.

I've called it mini_al, and you can find it on GitHub: https://github.com/dr-soft/mini_al

There's still a bit a want to do to improve it, notably adding support for macOS and iOS, but it's at a state now where I'm happy to start making people aware of this thing. Let me know what you think!

Features
  • Compilable as both C and C++.
  • Easy to build.
    • Does not require linking to anything on the Windows build and only -ldl on Linux.
    • The Windows build compiles clean with the default installations of modern versions of MSVC, GCC and Clang. There is no need to download any dependencies, configure include paths nor link to any libraries. It should Just Work.
    • The header section does not include any platform specific headers.
  • A very simple API.
  • Transparent data structures with direct access to internal data.
  • Supports both playback and capture on all backends.
  • Automatic format conversion.
    • Data format conversion.
    • Sample rate conversion.
    • Channel mapping/layout.
    • Channel mixing (converting mono to 5.1, etc.)

Supported Platforms
  • Windows (XP+)
  • Linux
  • BSD (via OSS)
  • Android
macOS and iOS support is coming soon(ish) via Core Audio.


Backends
  • WASAPI
  • DirectSound
  • WinMM
  • ALSA
  • OSS
  • OpenSL|ES (Android only)
  • OpenAL
  • Null (Silence)

Simple Playback Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define MAL_IMPLEMENTATION
#include "../mini_al.h"

#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"

#include <stdio.h>

// This is the function that's used for sending more data to the device for playback.
mal_uint32 on_send_frames_to_device(mal_device* pDevice, mal_uint32 frameCount, void* pSamples)
{
    drwav* pWav = (drwav*)pDevice->pUserData;
    if (pWav == NULL) {
        return 0;
    }
    
    return (mal_uint32)drwav_read_s16(pWav, frameCount * pDevice->channels, (mal_int16*)pSamples) / pDevice->channels;
}

int main(int argc, char** argv)
{
    if (argc < 2) {
        printf("No input file.");
        return -1;
    }

    drwav wav;
    if (!drwav_init_file(&wav, argv[1])) {
        printf("Not a valid WAV file.");
        return -2;
    }

    mal_context context;
    if (mal_context_init(NULL, 0, NULL, &context) != MAL_SUCCESS) {
        printf("Failed to initialize context.");
        drwav_uninit(&wav);
        return -3;
    }
    
    mal_device_config config = mal_device_config_init_playback(mal_format_s16, wav.channels, wav.sampleRate, on_send_frames_to_device);
    
    mal_device device;
    if (mal_device_init(&context, mal_device_type_playback, NULL, &config, &wav, &device) != MAL_SUCCESS) {
        printf("Failed to open playback device.");
        mal_context_uninit(&context);
        drwav_uninit(&wav);
        return -4;
    }
    
    mal_device_start(&device);
    
    printf("Press Enter to quit...");
    getchar();
    
    mal_device_uninit(&device);
    mal_context_uninit(&context);
    drwav_uninit(&wav);
    
    return 0;
}
insofaras
Alex Baines
40 posts / 1 project
#13418 mini_al: A C/C++ cross-platform, single file, public domain audio library
1 year ago

This looks great!

I'm now using it in one of my projects instead of SDL (which was only being used for audio).
It was very simple to set up, and I haven't found any issues.

Thanks for putting it together.
vassvik
7 posts

Physics, math and graphics.

#13421 mini_al: A C/C++ cross-platform, single file, public domain audio library
1 year ago

Looks great, indeed.

I managed to create a working port of your "Simple Playback Example" in Odin, by binding to a compiled version of the library. I'm looking forward to see how far I can go with this!
mackron
David Reid
7 posts
#13479 mini_al: A C/C++ cross-platform, single file, public domain audio library
1 year ago

Thanks!

vassvik
I managed to create a working port of your "Simple Playback Example" in Odin, by binding to a compiled version of the library. I'm looking forward to see how far I can go with this!


I just pushed a new version with API changes, just so you're aware :)
mackron
David Reid
7 posts
#15716 mini_al: A C/C++ cross-platform, single file, public domain audio library
4 months, 2 weeks ago

In case anybody was interested, I've just done an update to mini_al. Some additions since my original post:

  • Added support for macOS and iOS via Core Audio.
  • Added support for Emscripten via SDL.
  • Added PulseAudio and JACK backends. PulseAudio is now the default on Linux.
  • Added a decoder abstraction with support for WAV, FLAC, MP3 and Vorbis. This depends on other single file public domain libraries, copies of which are in the mini_al repository.
  • Further simplified the build system by removing the dependency on ALSA development packages for Linux, and WASAPI and DirectSound headers for Windows. mini_al should compile clean out of the box on Windows, macOS and Linux without the need to install any external dependencies.
  • Lots of improvements to format, channel and sample rate conversion.
  • Added support for dithering to format conversion.
  • Lots of bug fixes and optimizations.
  • Lots of breaking API changes. Sorry!
  • Probably a lot of other things I've forgotten about.

Enjoy!
OnQuestionForYou
2 posts
#15779 mini_al: A C/C++ cross-platform, single file, public domain audio library
4 months, 1 week ago

This is great. As far as I'm concerned there are barely (if any) public domain audio libraries such as yours. Congratulations! Two questions:

- Are you thinking in provinding an abstraction layer? I see you did some work for raylib's audio lib, but it would be nice to avoid external dependencies.
- Where does one learn to program audio software? It looks more confusing than 3D stuff.
mackron
David Reid
7 posts
#15831 mini_al: A C/C++ cross-platform, single file, public domain audio library
3 months, 4 weeks ago

What do you mean exactly by an abstraction layer? If you are talking about high level APIs like mixing, 3D, effects, etc. then they are planned, but a fair way away.

Regarding how to learn audio programming, I taught myself everything I know about audio programming by just browsing the internet (and I still have a lot to learn). It's not as difficult as 3D, but the platform-specific stuff is a nightmare! :)
OnQuestionForYou
2 posts
#15888 mini_al: A C/C++ cross-platform, single file, public domain audio library
3 months, 3 weeks ago

Glad to know you are working on it. These headers lib are a godsend. Cheers.