handmade.network » Forums » Work-in-Progress » mini_al: A C/C++ cross-platform, single file, public domain audio library
mackron
David Reid
3 posts
#13417 mini_al: A C/C++ cross-platform, single file, public domain audio library
2 weeks, 1 day 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
25 posts
1 project
#13418 mini_al: A C/C++ cross-platform, single file, public domain audio library
2 weeks, 1 day 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
2 weeks, 1 day 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
3 posts
#13479 mini_al: A C/C++ cross-platform, single file, public domain audio library
1 week, 2 days 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 :)