• Stars
    star
    1,933
  • Rank 23,975 (Top 0.5 %)
  • Language
    C
  • License
    MIT License
  • Created over 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

libsoundio

C library providing cross-platform audio input and output. The API is suitable for real-time software such as digital audio workstations as well as consumer software such as music players.

This library is an abstraction; however in the delicate balance between performance and power, and API convenience, the scale is tipped closer to the former. Features that only exist in some sound backends are exposed.

Features and Limitations

  • Supported operating systems:
    • Windows 7+
    • MacOS 10.10+
    • Linux 3.7+
  • Supported backends:
  • Exposes both raw devices and shared devices. Raw devices give you the best performance but prevent other applications from using them. Shared devices are default and usually provide sample rate conversion and format conversion.
  • Exposes both device id and friendly name. id you could save in a config file because it persists between devices becoming plugged and unplugged, while friendly name is suitable for exposing to users.
  • Supports optimal usage of each supported backend. The same API does the right thing whether the backend has a fixed buffer size, such as on JACK and CoreAudio, or whether it allows directly managing the buffer, such as on ALSA, PulseAudio, and WASAPI.
  • C library. Depends only on the respective backend API libraries and libc. Does not depend on libstdc++, and does not have exceptions, run-time type information, or setjmp.
  • Errors are communicated via return codes, not logging to stdio.
  • Supports channel layouts (also known as channel maps), important for surround sound applications.
  • Ability to monitor devices and get an event when available devices change.
  • Ability to get an event when the backend is disconnected, for example when the JACK server or PulseAudio server shuts down.
  • Detects which input device is default and which output device is default.
  • Ability to connect to multiple backends at once. For example you could have an ALSA device open and a JACK device open at the same time.
  • Meticulously checks all return codes and memory allocations and uses meaningful error codes.
  • Exposes extra API that is only available on some backends. For example you can provide application name and stream names which is used by JACK and PulseAudio.

Synopsis

Complete program to emit a sine wave over the default device using the best backend:

#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)
{
    const struct SoundIoChannelLayout *layout = &outstream->layout;
    float float_sample_rate = outstream->sample_rate;
    float seconds_per_frame = 1.0f / float_sample_rate;
    struct SoundIoChannelArea *areas;
    int frames_left = frame_count_max;
    int err;

    while (frames_left > 0) {
        int frame_count = frames_left;

        if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
            fprintf(stderr, "%s\n", soundio_strerror(err));
            exit(1);
        }

        if (!frame_count)
            break;

        float pitch = 440.0f;
        float radians_per_second = pitch * 2.0f * PI;
        for (int frame = 0; frame < frame_count; frame += 1) {
            float sample = sinf((seconds_offset + frame * seconds_per_frame) * radians_per_second);
            for (int channel = 0; channel < layout->channel_count; channel += 1) {
                float *ptr = (float*)(areas[channel].ptr + areas[channel].step * frame);
                *ptr = sample;
            }
        }
        seconds_offset = fmodf(seconds_offset +
            seconds_per_frame * frame_count, 1.0f);

        if ((err = soundio_outstream_end_write(outstream))) {
            fprintf(stderr, "%s\n", soundio_strerror(err));
            exit(1);
        }

        frames_left -= frame_count;
    }
}

int main(int argc, char **argv) {
    int err;
    struct SoundIo *soundio = soundio_create();
    if (!soundio) {
        fprintf(stderr, "out of memory\n");
        return 1;
    }

    if ((err = soundio_connect(soundio))) {
        fprintf(stderr, "error connecting: %s", soundio_strerror(err));
        return 1;
    }

    soundio_flush_events(soundio);

    int default_out_device_index = soundio_default_output_device_index(soundio);
    if (default_out_device_index < 0) {
        fprintf(stderr, "no output device found");
        return 1;
    }

    struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
    if (!device) {
        fprintf(stderr, "out of memory");
        return 1;
    }

    fprintf(stderr, "Output device: %s\n", device->name);

    struct SoundIoOutStream *outstream = soundio_outstream_create(device);
    outstream->format = SoundIoFormatFloat32NE;
    outstream->write_callback = write_callback;

    if ((err = soundio_outstream_open(outstream))) {
        fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
        return 1;
    }

    if (outstream->layout_error)
        fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));

    if ((err = soundio_outstream_start(outstream))) {
        fprintf(stderr, "unable to start device: %s", soundio_strerror(err));
        return 1;
    }

    for (;;)
        soundio_wait_events(soundio);

    soundio_outstream_destroy(outstream);
    soundio_device_unref(device);
    soundio_destroy(soundio);
    return 0;
}

Backend Priority

When you use soundio_connect, libsoundio tries these backends in order. If unable to connect to that backend, due to the backend not being installed, or the server not running, or the platform is wrong, the next backend is tried.

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

If you don't like this order, you can use soundio_connect_backend to explicitly choose a backend to connect to. You can use soundio_backend_count and soundio_get_backend to get the list of available backends.

API Documentation

Building

Install the dependencies:

  • cmake
  • ALSA library (optional)
  • libjack2 (optional)
  • libpulseaudio (optional)
mkdir build
cd build
cmake ..
make
sudo make install

Building for Windows

You can build libsoundio with mxe. Follow the requirements section to install the packages necessary on your system. Then somewhere on your file system:

git clone https://github.com/mxe/mxe
cd mxe
make MXE_TARGETS='x86_64-w64-mingw32.static i686-w64-mingw32.static' gcc

Then in the libsoundio source directory (replace "/path/to/mxe" with the appropriate path):

mkdir build-win32
cd build-win32
cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/mxe/usr/i686-w64-mingw32.static/share/cmake/mxe-conf.cmake
make
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

Testing

For each backend, do the following:

  1. Run the unit tests: ./unit_tests. To see test coverage, install lcov, run make coverage, and then view coverage/index.html in a browser.
  2. Run the example ./sio_list_devices and make sure it does not crash, and the output looks good. If valgrind is available, use it.
  3. Run ./sio_list_devices --watch and make sure it detects when you plug and unplug a USB microphone.
  4. Run ./sio_sine and make sure you hear a sine wave. For backends with raw devices, run ./sio_sine --device id --raw (where 'id' is a device id you got from sio_list_devices and make sure you hear a sine wave.
    • Use 'p' to test pausing, 'u' to test unpausing, 'q' to test cleanup.
    • 'c' for clear buffer. Clear buffer should not pause the stream and it should also not cause an underflow.
    • Use 'P' to test pausing from the callback, and then 'u' to unpause.
  5. Run ./underflow and read the testing instructions that it prints.
  6. Run ./sio_microphone and ensure that it is both recording and playing back correctly. If possible use the --in-device and --out-device parameters to test a USB microphone in raw mode.
  7. Run ./backend_disconnect_recover and read the testing instructions that it prints.
  8. Run ./latency and make sure the printed beeps line up with the beeps that you hear.

Building the Documentation

Ensure that doxygen is installed, then:

make doc

Then look at html/index.html in a browser.

More Repositories

1

groovebasin

Music player server with a web-based user interface.
JavaScript
1,851
star
2

node-s3-client

high level amazon s3 client for node.js
JavaScript
1,003
star
3

poop

Performance Optimizer Observation Platform
Zig
882
star
4

naught

Zero downtime deployment for your Node.js server using builtin cluster API
JavaScript
793
star
5

jamulator

(unmaintained) recompiling NES roms into native executables
Go
388
star
6

tetris

A simple tetris clone written in zig programming language.
Zig
381
star
7

libgroove

streaming audio processing library
C
290
star
8

HellOS

"hello world" x86 kernel example
Zig
262
star
9

node-diacritics

remove diacritics from strings ("ascii folding") - Node.js module
JavaScript
259
star
10

waveform

simultaneously transcode and generate visuals for an audio file
C
251
star
11

clashos

multiplayer arcade game for bare metal Raspberry Pi 3 B+
Zig
216
star
12

genesis

Genesis Digital Audio Workstation
C++
176
star
13

chem

2d canvas-based rapid prototyping game engine
JavaScript
176
star
14

swig-email-templates

Node.js module for rendering emails with swig templates and email-friendly inline CSS using boost.
JavaScript
162
star
15

node-groove

bindings to libgroove - music player backend library
C++
157
star
16

node-mv

Like `fs.rename`, but works across devices, and works with directories. Think of the unix utility `mv`.
JavaScript
155
star
17

ffmpeg

FFmpeg Zig package
C
137
star
18

zig-window

window client library
C++
116
star
19

sdl-zig-demo

SDL2 hello world in zig
Zig
115
star
20

node-waveform

simultaneously transcode audio and generate visuals - Node.js module
C
101
star
21

zig-vulkan-triangle

simple triangle displayed using vulkan, xcb, and zig
Zig
98
star
22

node-s3-cli

command line utility to go along with node s3 module
JavaScript
97
star
23

malcheck

Test your code with malcheck to make sure it handles out of memory conditions correctly.
C
96
star
24

zig-wasi

Minimal WASI interpreter
C
91
star
25

zasm

multi-target assembler and disassembler
Zig
90
star
26

mpd.js

Connect to a music player daemon server, send commands, emit events.
JavaScript
89
star
27

PyDaw

python library to mess with Digital Audio Workstations. FL Studio project files (.flp) supported.
C++
87
star
28

node-flp

FL Studio project file parser for node.js
JavaScript
77
star
29

node-tmx-parser

node.js module to parse and load tiled map editor maps (see mapeditor.org)
JavaScript
71
star
30

node-astar

Generic A* algorithm for node.js
JavaScript
67
star
31

node-sox

(unmaintained) node.js interface to the sox audio utility
JavaScript
61
star
32

zig-async-demo

Comparing concurrent code example programs between other languages and Zig
Zig
55
star
33

mcserve

wraps minecraft server and gives you a web interface
JavaScript
54
star
34

StaticHttpFileServer

Zig module for serving a directory of files from memory via HTTP
Zig
49
star
35

zig-general-purpose-allocator

work-in-progress general purpose allocator intended to be eventually merged into Zig standard library. live streamed development
Zig
46
star
36

autodoc

Zig Documentation Generator
Zig
45
star
37

liblaxjson

C library for parsing JSON config files
C
33
star
38

node-perlin-noise

perlin noise generator for node.js
JavaScript
33
star
39

lua-in-the-browser

using zig to build lua for webassembly
C
32
star
40

node-fd-slicer

safely create multiple ReadStream or WriteStream objects from the same file descriptor
JavaScript
30
star
41

flag2struct

simple CLI tool for converting zig source code using flags-style declarations to packed structs
Zig
29
star
42

rucksack

texture packer and resource bundler
C
28
star
43

mime

zig package for mapping extensions to mime types
Zig
28
star
44

connect-sse

connect middleware for server sent events (EventSource)
JavaScript
27
star
45

PyWaveform

Python library to create an image of a song's waveform
C
26
star
46

zig-stage1

Exploring replacing Zig's stage1 compiler with pure C code that outputs pure C code
C
25
star
47

xml

Tokenize XML
Zig
24
star
48

libavfilter-example

small example of using libavfilter to filter audio
C
23
star
49

node-plan

(unmaintained, deprecated, abandoned) Execute a complicated dependency graph of tasks with smooth progress events.
JavaScript
20
star
50

connect-static

static file server middleware for connect. loads files once at startup and saves gzipped versions in memory
JavaScript
19
star
51

dotfiles

linux yo
Nix
18
star
52

pyedsdk

Python library to control cameras via EDSDK
C
18
star
53

groove-rs

rust bindings to libgroove - streaming audio processing library
Rust
16
star
54

node-pend

dead-simple optimistic async helper in javascript
JavaScript
16
star
55

evo

specification, reference implementation, and examples of Evo, the programming language made for being the DNA of genetic algorithms
Zig
15
star
56

mediablast

(unmaintained, deprecated, abandoned) open source media processing server
JavaScript
14
star
57

purgatory

escape from the circles of hell - 7 hour game jam
JavaScript
14
star
58

browserify-lite

browserify, minus some of the advanced features and heavy dependencies
JavaScript
14
star
59

pillagers

Real time strategy game with space physics
JavaScript
12
star
60

hackerrank

my solutions to hackerrank puzzles
Go
11
star
61

connect-nocache

connect middleware to insert no cache headers
JavaScript
11
star
62

andrewkelley.me

my personal site
HTML
11
star
63

pulseaudio

pulseaudio with the build system replaced by zig
C
11
star
64

SIMD-test

exploring SIMD optimization
C
10
star
65

truthfinder

TruthFinder.org website
Python
10
star
66

mc-bot-server

(unmaintained) server that spins up minecraft bots
JavaScript
9
star
67

zig-mandelbrot-gl

mandelbrot set in zig
Zig
9
star
68

node-yawl

yet another websockets library for Node.js
JavaScript
8
star
69

clashproto

prototyping the game for andrewrk/clashos
Zig
8
star
70

planet-evo

evolution simulation software
C++
8
star
71

advent-of-code

https://adventofcode.com
Zig
8
star
72

node-music-library-index

node module to build a searchable javascript object model given track metadata objects
JavaScript
7
star
73

github-popularity-contest

see who has the most collective stars
JavaScript
7
star
74

node-human-size

tiny node.js module to get human readable file size from byte count
JavaScript
7
star
75

libmp3lame

libmp3lame with the build system replaced by zig
C
7
star
76

node-stream-counter

node.js module to keep track of how many bytes have been written to a stream
JavaScript
7
star
77

mpd

a fork of mpd to add library management, better search, and a sophisticated dynamic playlist
C
7
star
78

boost

Inline CSS into your HTML source
JavaScript
6
star
79

Secure-WordVault

(unmaintained, deprecated, abandoned) Enables you to store sensitive information in a portable manner
C++
6
star
80

dominion

Node.js module and command line program to play Dominion, the card game by Donald X. Vaccarino.
JavaScript
6
star
81

TrenchBowl

simple music player UI to demonstrate libgroove
C++
6
star
82

node-spritesheet

node.js module: given a list of image files, create a spritesheet using cairo
JavaScript
6
star
83

chem-cli

html5 canvas game engine optimized for rapid development - command line interface
JavaScript
5
star
84

face-the-music

indie speed run game jam
JavaScript
5
star
85

ruff

little tool to help my dad quickly find info in a .csv file
C++
5
star
86

gbremote

Groove Basin remote control command line app and Node.js module
JavaScript
5
star
87

planetarius

Ludum Dare 30 Entry. networked multiplayer arcade shooter
JavaScript
5
star
88

archerbot

mineflayer bot that engages you in a gentlemanly duel
JavaScript
5
star
89

holocaust

html5 video game - rebuild society after a nuclear holocaust ravages the world
JavaScript
4
star
90

Camlift-Controller

Controls a Canon camera and operates a motorized lift
Visual Basic
4
star
91

swig-dummy-context

given a swig template, create a dummy context which is useful for template composing tools
JavaScript
4
star
92

node-passthrough-truncate

truncate the last N bytes of a stream - Node.js module
JavaScript
3
star
93

scrabble

Scrabble solving AI
3
star
94

spacefight

vaporware 3D space-dogfighting simulator game
C++
3
star
95

math3d-rs

computer-graphics matrix calculations for dummies like me
Rust
3
star
96

lemming-js

PyWeek #12 entry ported to JavaScript with chem
JavaScript
3
star
97

pypowerusb

Python library to control a PowerUSB
C
3
star
98

opengl-multi-window-test

see if multiple windows in opengl causes a framerate issue
C
3
star
99

disinfecticide

A game about controlling a disease outbreak.
JavaScript
2
star
100

socketio-ssl-test

test whether we can use socket.io with xhr requests securely on an insecure page
JavaScript
2
star