miniaudio

repository·master·Indexed 27 days ago

https://github.com/mackron/miniaudio

A single-source C library for audio playback and capture. It provides high-level APIs for sound management via ma_engine and low-level APIs for raw audio data access via ma_decoder and ma_device. The library supports multiple platforms and backends (including WASAPI, Core Audio, ALSA, and Web Audio) without external dependencies. It also includes osaudio, a header-only library for simple blocking audio playback.

Tokens
2K
Snippets
3
Records
7
Agent score
93%

What's inside miniaudio

  1. Use osaudio for simple blocking audio playback

    master

    osaudio provides a simple, blocking read/write API for audio, contrasting with the callback-based model used in miniaudio. It is implemented as a header-only library with a reference implementation that uses miniaudio under the hood.

    To use it, initialize an osaudio_config_t object, configure your desired format, channels, and rate, and then call osaudio_open. Data is sent to the device using osaudio_write, which blocks until all data has been sent.

    #include "osaudio.h"
    
    osaudio_t audio;
    osaudio_config_t config;
    
    // Initialize configuration for output
    saudio_config_init(&config, OSAUDIO_OUTPUT);
    config.format   = OSAUDIO_FORMAT_F32;
    config.channels = 2;
    config.rate     = 48000;
    
    // Open the audio device
    osaudio_open(&audio, &config);
    
    // Write data (this call blocks until data is sent to the device)
    osaudio_write(audio, myAudioData, frameCount);
    
    // Close the device
    osaudio_close(audio);
  2. Build miniaudio

    master

    Miniaudio is a single-source library. To use it, compile miniaudio.c and include miniaudio.h in your project.

    Platform-specific linking requirements:

    • Windows & macOS: No additional linking required.
    • Linux & BSD: Link with -lpthread and -lm.
    • iOS: Compile as Objective-C.
    • General Linux/Unix: Link with -ldl if you encounter dlopen() errors.
    • Atomic operations: If you encounter undefined references to __sync_val_compare_and_swap_8, __atomic_load_8, etc., link with -latomic.

    Integration Best Practice

    Because ABI compatibility is not guaranteed between versions, it is highly recommended to add miniaudio directly to your source tree rather than linking against a DLL/SO.

  3. Build the osaudio reference implementation

    master
    The osaudio header itself has no dependencies. However, the reference implementation (osaudio_miniaudio.c) requires miniaudio. To build your project, compile osaudio_miniaudio.c and include osaudio.h. Ensure your include paths are correctly adjusted so that osaudio_miniaudio.c can locate the miniaudio source/headers.
  4. Use the split miniaudio library files

    master

    If your build environment requires separate .h and .c files instead of a single-header library, you can use the files located in the extras/miniaudio_split/ directory.

    Warning: These files are automatically generated from the main miniaudio.h file. Do not edit these files directly. Any changes or contributions should be made to the main miniaudio.h file to ensure they are preserved and can be correctly propagated.

  5. Play a sound using the high-level API

    master

    Use the ma_engine API for simple sound management, mixing, and effects. This high-level approach handles resource management and playback with minimal boilerplate.

    1. Initialize the engine with ma_engine_init.
    2. Play a sound file using ma_engine_play_sound.
    3. Uninitialize the engine with ma_engine_uninit when finished.
    #include "miniaudio/miniaudio.h"
    
    #include <stdio.h>
    
    int main()
    {
        ma_result result;
        ma_engine engine;
    
        result = ma_engine_init(NULL, &engine);
        if (result != MA_SUCCESS) {
            return -1;
        }
    
        ma_engine_play_sound(&engine, "sound.wav", NULL);
    
        printf("Press Enter to quit...");
        getchar();
    
        ma_engine_uninit(&engine);
    
        return 0;
    }
  6. Decode and play a sound using the low-level API

    master

    For direct access to raw audio data, use the ma_decoder and ma_device APIs. This allows you to provide a custom data callback for processing or streaming audio frames.

    1. Initialize a decoder with ma_decoder_init_file.
    2. Configure a playback device using ma_device_config_init(ma_device_type_playback).
    3. Set the device configuration (format, channels, sample rate) based on the decoder's output.
    4. Assign a data_callback to the device configuration.
    5. Initialize and start the device with ma_device_init and ma_device_start.
    6. In the callback, use ma_decoder_read_pcm_frames to fill the output buffer.
    7. Uninitialize the device and decoder when finished.
    #include "miniaudio/miniaudio.h"
    
    #include <stdio.h>
    
    void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
    {
        ma_decoder* pDecoder = (ma_decoder*)pDevice->pUserData;
        if (pDecoder == NULL) {
            return;
        }
    
        ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount, NULL);
    
        (void)pInput;
    }
    
    int main(int argc, char** argv)
    {
        ma_result result;
        ma_decoder decoder;
        ma_device_config deviceConfig;
        ma_device device;
    
        if (argc < 2) {
            printf("No input file.\n");
            return -1;
        }
    
        result = ma_decoder_init_file(argv[1], NULL, &decoder);
        if (result != MA_SUCCESS) {
            return -2;
        }
    
        deviceConfig = ma_device_config_init(ma_device_type_playback);
        deviceConfig.playback.format   = decoder.outputFormat;
        deviceConfig.playback.channels = decoder.outputChannels;
        deviceConfig.sampleRate        = decoder.outputSampleRate;
        deviceConfig.dataCallback      = data_callback;
        deviceConfig.pUserData         = &decoder;
    
        if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
            printf("Failed to open playback device.\n");
            ma_decoder_uninit(&decoder);
            return -3;
        }
    
        if (ma_device_start(&device) != MA_SUCCESS) {
            printf("Failed to start playback device.\n");
            ma_device_uninit(&device);
            ma_decoder_uninit(&decoder);
            return -4;
        }
    
        printf("Press Enter to quit...");
        getchar();
    
        ma_device_uninit(&device);
        ma_decoder_uninit(&decoder);
    
        return 0;
    }
  7. Supported Backends

    master

    Miniaudio supports a wide range of audio backends across different platforms:

    • Windows: WASAPI, DirectSound, WinMM
    • Apple: Core Audio
    • Linux: ALSA, PulseAudio, JACK
    • OpenBSD: sndio, audio(4)
    • NetBSD/OpenBSD: audio(4)
    • FreeBSD: OSS
    • Android: AAudio (8.0+), OpenSL|ES
    • Emscripten/HTML5: Web Audio
    • Other: Null (Silence), Custom