libsoundio Documentation

repository·master·Indexed 24 days ago

https://github.com/andrewrk/libsoundio

A high-performance C library for cross-platform audio input and output. Designed for real-time applications like DAWs and consumer software, libsoundio supports multiple backends including JACK, PulseAudio, ALSA, CoreAudio, and WASAPI. It provides features for raw and shared device access, channel layout support, and event monitoring with low overhead.

Tokens
1.4K
Snippets
3
Records
5
Agent score
35%

What's inside libsoundio

  1. Core features and capabilities of libsoundio

    master

    libsoundio is a C library designed for high-performance, cross-platform audio I/O.

    Key Capabilities:

    • Device Access: Supports both raw devices (best performance, exclusive access) and shared devices (default, provides sample rate/format conversion).
    • Identification: Provides both id (persistent across plug/unplug events, good for config files) and friendly name (for user display).
    • Backend Optimization: Automatically handles backend-specific requirements like fixed buffer sizes (JACK/CoreAudio) vs. direct buffer management (ALSA/PulseAudio/WASAPI).
    • Event Monitoring: Can monitor device changes and backend disconnections (e.g., JACK/PulseAudio server shutdown).
    • Channel Layouts: Full support for channel maps/layouts for surround sound.
    • Multi-Backend: Ability to connect to multiple backends simultaneously (e.g., ALSA and JACK at once).
    • Low Overhead: No dependency on libstdc++, no exceptions, no RTTI, and no setjmp.
  2. Understand libsoundio backend priority and selection

    master

    When calling soundio_connect, the library attempts to connect to backends in a specific default order. If a backend is unavailable (not installed, server not running, or incorrect platform), it moves to the next one.

    Default Priority Order:

    1. JACK
    2. PulseAudio
    3. ALSA (Linux)
    4. CoreAudio (OSX)
    5. WASAPI (Windows)
    6. Dummy

    To bypass this order and select a specific backend, use soundio_connect_backend. You can discover available backends using soundio_backend_count and soundio_get_backend.

  3. Build libsoundio for Windows using MXE

    master

    To cross-compile for Windows, use the mxe toolchain. First, build the MXE targets, then run cmake in the libsoundio directory pointing to the MXE toolchain file.

    # Build MXE targets
    git clone https://github.com/mxe/mxe
    cd mxe
    make MXE_TARGETS='x86_64-w64-mingw32.static i686-w64-mingw32.static' gcc
    
    # Build libsoundio for 64-bit Windows
    mkdir build-win64
    cd build-win64
    cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/mxe/usr/x86_64-w64-mingw32.static/share/cmake/mxe-conf.cmake
    make
  4. Install and build libsoundio

    master

    To build libsoundio from source, ensure you have cmake and the optional backend libraries (ALSA, libjack2, or libpulseaudio) installed. Use the following commands to build and install:

    mkdir build
    cd build
    cmake ..
    make
    sudo make install
  5. Emit a sine wave using libsoundio

    master

    This example demonstrates how to initialize libsoundio, connect to the default output device, and use a write callback to generate a sine wave. Key steps include:

    1. soundio_create() to initialize the library.
    2. soundio_connect() to connect to the audio backend.
    3. soundio_default_output_device_index() to find the default device.
    4. soundio_outstream_create() and soundio_outstream_open() to prepare the stream.
    5. Setting a write_callback to provide audio data.
    6. soundio_outstream_start() to begin playback.
    #include <soundio/soundio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    static const float PI = 3.1415926535f;
    static float seconds_offset = 0.0f;
    static void write_callback(struct SoundIoOutStream *outstream, 
            int frame_count_min, int frame_count_max)
    {
        // ... implementation of sine wave generation ...
    }
    
    int main(int argc, char **argv) {
        struct SoundIo *soundio = soundio_create();
        soundio_connect(soundio);
        soundio_flush_events(soundio);
    
        int default_out_device_index = soundio_default_output_device_index(soundio);
        struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
        struct SoundIoOutStream *outstream = soundio_outstream_create(device);
        outstream->format = SoundIoFormatFloat32NE;
        outstream->write_callback = write_callback;
    
        soundio_outstream_open(outstream);
        soundio_outstream_start(outstream);
    
        for (;;) soundio_wait_events(soundio);
    
        soundio_outstream_destroy(outstream);
        soundio_device_unref(device);
        soundio_destroy(soundio);
        return 0;
    }