Convert Figma logo to code with AI

mackron logominiaudio

Audio playback and capture library written in C, in a single source file.

3,915
345
3,915
28

Top Related Projects

A C library for reading and writing sound files containing sampled audio data.

2,240

Modern audio compression for the internet.

OpenAL Soft is a software implementation of the OpenAL 3D audio API.

C library for cross-platform real-time audio input and output

10,620

Audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

Quick Overview

miniaudio is a single-file public domain audio playback and capture library for C/C++. It provides a simple and efficient way to handle audio input and output across multiple platforms, including Windows, macOS, Linux, iOS, Android, and more.

Pros

  • Cross-platform compatibility with a wide range of operating systems
  • Simple, single-file implementation for easy integration into projects
  • Supports both audio playback and capture
  • Low-latency performance suitable for real-time audio applications

Cons

  • Limited advanced audio processing features compared to larger audio libraries
  • May require additional work for more complex audio scenarios
  • Documentation could be more comprehensive for some advanced use cases
  • Potential for platform-specific issues due to the wide range of supported systems

Code Examples

  1. Basic audio playback:
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
    // Fill the output buffer with audio data
    // For simplicity, we'll generate a sine wave
    float* output = (float*)pOutput;
    for (ma_uint32 i = 0; i < frameCount; i++) {
        output[i] = sinf(2 * MA_PI * 440 * i / pDevice->sampleRate);
    }
}

int main()
{
    ma_device_config config = ma_device_config_init(ma_device_type_playback);
    config.playback.format   = ma_format_f32;
    config.playback.channels = 1;
    config.sampleRate        = 44100;
    config.dataCallback      = data_callback;

    ma_device device;
    if (ma_device_init(NULL, &config, &device) != MA_SUCCESS) {
        return -1;
    }

    ma_device_start(&device);
    getchar();  // Wait for user input

    ma_device_uninit(&device);
    return 0;
}
  1. Audio capture:
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
    // Process captured audio data
    const float* input = (const float*)pInput;
    for (ma_uint32 i = 0; i < frameCount; i++) {
        // Do something with input[i]
        printf("Sample: %f\n", input[i]);
    }
}

int main()
{
    ma_device_config config = ma_device_config_init(ma_device_type_capture);
    config.capture.format   = ma_format_f32;
    config.capture.channels = 1;
    config.sampleRate       = 44100;
    config.dataCallback     = data_callback;

    ma_device device;
    if (ma_device_init(NULL, &config, &device) != MA_SUCCESS) {
        return -1;
    }

    ma_device_start(&device);
    getchar();  // Wait for user input

    ma_device_uninit(&device);
    return 0;
}

Getting Started

  1. Download the miniaudio.h file from the GitHub repository.
  2. Include the header in your C/C++ project:
    #define MINIAUDIO_IMPLEMENTATION
    #include "miniaudio.h"
    
  3. Compile your project, linking with the appropriate system libraries (e.g., -lpthread on Linux).
  4. Use the miniaudio API to initialize devices, start playback or capture, and process audio data as shown in the code examples above.

Competitor Comparisons

A C library for reading and writing sound files containing sampled audio data.

Pros of libsndfile

  • Extensive support for a wide range of audio file formats
  • Well-established and widely used in professional audio applications
  • Robust error handling and reporting mechanisms

Cons of libsndfile

  • Larger library size and potential overhead for simpler projects
  • More complex API compared to miniaudio's straightforward design
  • Requires linking against an external library

Code Comparison

libsndfile:

#include <sndfile.h>

SNDFILE *file;
SF_INFO sfinfo;
file = sf_open("audio.wav", SFM_READ, &sfinfo);
sf_read_float(file, buffer, frames);
sf_close(file);

miniaudio:

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

ma_decoder decoder;
ma_decoder_config config = ma_decoder_config_init(ma_format_f32, 2, 48000);
ma_decoder_init_file("audio.wav", &config, &decoder);
ma_decoder_read_pcm_frames(&decoder, buffer, frames);
ma_decoder_uninit(&decoder);

Both libraries offer audio file handling capabilities, but libsndfile provides more comprehensive format support and is often preferred for professional audio applications. miniaudio, on the other hand, offers a simpler API and is designed as a single-file library, making it easier to integrate into smaller projects.

2,240

Modern audio compression for the internet.

Pros of Opus

  • Highly optimized codec for low-latency, high-quality audio compression
  • Widely adopted standard for VoIP and streaming applications
  • Supports a wide range of bitrates and audio qualities

Cons of Opus

  • More complex to implement and use compared to simpler audio libraries
  • Focused primarily on encoding and decoding, not a full-featured audio processing library
  • Requires more computational resources for real-time encoding/decoding

Code Comparison

Opus (encoding example):

opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
opus_encode(encoder, pcm, frame_size, cbits, max_packet);

miniaudio (playback example):

ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback);
ma_device_init(NULL, &deviceConfig, &device);
ma_device_start(&device);

Summary

Opus is a specialized audio codec focusing on efficient compression and transmission, ideal for applications requiring high-quality audio at low bitrates. miniaudio, on the other hand, is a more general-purpose audio library that simplifies audio playback, recording, and basic processing across multiple platforms. While Opus excels in specific use cases like VoIP and streaming, miniaudio offers a broader range of audio functionalities with a simpler API, making it more suitable for general audio applications and rapid development.

OpenAL Soft is a software implementation of the OpenAL 3D audio API.

Pros of OpenAL Soft

  • More established and widely used in game development
  • Supports 3D audio positioning and effects
  • Cross-platform compatibility with OpenAL standard

Cons of OpenAL Soft

  • Larger codebase and more complex API
  • Requires separate backend implementations for different platforms
  • Less active development compared to MiniAudio

Code Comparison

OpenAL Soft:

ALCdevice *device = alcOpenDevice(NULL);
ALCcontext *context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);

MiniAudio:

ma_engine engine;
ma_engine_init(NULL, &engine);

ma_engine_play_sound(&engine, "sound.wav", NULL);

MiniAudio offers a simpler API with fewer lines of code to achieve basic audio playback. OpenAL Soft requires more setup but provides more advanced features for 3D audio positioning and effects.

Both libraries are open-source and cross-platform, but MiniAudio is designed as a single-file library, making it easier to integrate into projects. OpenAL Soft has a larger ecosystem and is more established in game development, while MiniAudio is gaining popularity for its simplicity and active development.

C library for cross-platform real-time audio input and output

Pros of libsoundio

  • More focused on cross-platform audio I/O, potentially offering better support for specific audio backends
  • Provides a simpler API for basic audio input/output operations
  • Actively maintained with regular updates and improvements

Cons of libsoundio

  • Less comprehensive feature set compared to miniaudio
  • May require additional libraries or dependencies for certain functionalities
  • Smaller community and fewer resources available online

Code Comparison

libsoundio:

SoundIoOutStream *outstream;
soundio_outstream_create(device, &outstream);
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
soundio_outstream_open(outstream);
soundio_outstream_start(outstream);

miniaudio:

ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 2;
config.sampleRate = 44100;
config.dataCallback = data_callback;
ma_device_init(NULL, &config, &device);
ma_device_start(&device);

Both libraries offer straightforward APIs for audio playback, but miniaudio's approach is more concise and requires less setup code. libsoundio provides more explicit control over device selection and stream configuration, which may be beneficial in certain scenarios. miniaudio's single-header design and broader feature set make it more versatile for general audio programming tasks.

10,620

Audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

Pros of AudioKit

  • Comprehensive audio framework with high-level abstractions
  • Extensive documentation and community support
  • Cross-platform support for iOS, macOS, and tvOS

Cons of AudioKit

  • Larger footprint and potentially higher resource usage
  • Steeper learning curve for beginners
  • Limited support for non-Apple platforms

Code Comparison

AudioKit:

let oscillator = AKOscillator()
AudioKit.output = oscillator
try AudioKit.start()
oscillator.start()
oscillator.frequency = 440

miniaudio:

ma_engine engine;
ma_engine_init(NULL, &engine);
ma_engine_play_sound(&engine, "tone.wav", NULL);
ma_engine_uninit(&engine);

Key Differences

  • AudioKit offers a more feature-rich API with higher-level abstractions
  • miniaudio provides a simpler, lightweight solution with broader platform support
  • AudioKit is primarily focused on Apple platforms, while miniaudio is more versatile
  • miniaudio has a lower-level C API, whereas AudioKit uses Swift for a more modern approach

Both libraries have their strengths, with AudioKit excelling in Apple ecosystem development and miniaudio offering a lightweight, cross-platform solution for basic audio needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

miniaudio

An audio playback and capture library in a single source file.

discord mastodon reddit

Features - Examples - Building - Documentation - Supported Platforms - License

miniaudio is written in C with no dependencies except the standard library and should compile clean on all major compilers without the need to install any additional development packages. All major desktop and mobile platforms are supported.

Features

  • Simple build system with no external dependencies.
  • Simple and flexible API.
  • Low-level API for direct access to raw audio data.
  • High-level API for sound management, mixing, effects and optional 3D spatialization.
  • Flexible node graph system for advanced mixing and effect processing.
  • Resource management for loading sound files.
  • Decoding, with built-in support for WAV, FLAC and MP3, in addition to being able to plug in custom decoders.
  • Encoding (WAV only).
  • Data conversion.
  • Resampling, including custom resamplers.
  • Channel mapping.
  • Basic generation of waveforms and noise.
  • Basic effects and filters.

Refer to the Programming Manual for a more complete description of available features in miniaudio.

Examples

This example shows one way to play a sound using the high level API.

#define MINIAUDIO_IMPLEMENTATION
#include "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;
}

This example shows how to decode and play a sound using the low level API.

#define MINIAUDIO_IMPLEMENTATION
#include "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;
}

More examples can be found in the examples folder or online here: https://miniaud.io/docs/examples/

Building

Do the following in one source file:

#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

Then just compile. There's no need to install any dependencies. On Windows and macOS there's no need to link to anything. On Linux just link to -lpthread, -lm and -ldl. On BSD just link to -lpthread and -lm. On iOS you need to compile as Objective-C.

If you get errors about undefined references to __sync_val_compare_and_swap_8, __atomic_load_8, etc. you need to link with -latomic.

If you prefer separate .h and .c files, you can find a split version of miniaudio in the extras/miniaudio_split folder. From here you can use miniaudio as a traditional .c and .h library - just add miniaudio.c to your source tree like any other source file and include miniaudio.h like a normal header. If you prefer compiling as a single translation unit (AKA unity builds), you can just #include the .c file in your main source file:

#include "miniaudio.c"

Note that the split version is auto-generated using a tool and is based on the main file in the root directory. If you want to contribute, please make the change in the main file.

ABI compatibility is not guaranteed between versions so take care if compiling as a DLL/SO. The suggested way to integrate miniaudio is by adding it directly to your source tree.

Documentation

Online documentation can be found here: https://miniaud.io/docs/

Documentation can also be found at the top of miniaudio.h which is always the most up-to-date and authoritive source of information on how to use miniaudio. All other documentation is generated from this in-code documentation.

Supported Platforms

  • Windows
  • macOS, iOS
  • Linux
  • FreeBSD / OpenBSD / NetBSD
  • Android
  • Raspberry Pi
  • Emscripten / HTML5

miniaudio should compile clean on other platforms, but it will not include any support for playback or capture by default. To support that, you would need to implement a custom backend. You can do this without needing to modify the miniaudio source code. See the custom_backend example.

Backends

  • WASAPI
  • DirectSound
  • WinMM
  • Core Audio (Apple)
  • ALSA
  • PulseAudio
  • JACK
  • sndio (OpenBSD)
  • audio(4) (NetBSD and OpenBSD)
  • OSS (FreeBSD)
  • AAudio (Android 8.0+)
  • OpenSL|ES (Android only)
  • Web Audio (Emscripten)
  • Null (Silence)
  • Custom

License

Your choice of either public domain or MIT No Attribution.