libsoundio/README.md

308 lines
11 KiB
Markdown
Raw Normal View History

2015-06-30 20:49:36 +00:00
# libsoundio
2015-07-29 01:47:28 +00:00
C99 library providing cross-platform audio input and output. The API is
2015-06-30 20:49:36 +00:00
suitable for real-time software such as digital audio workstations as well
as consumer software such as music players.
2015-07-29 01:47:28 +00:00
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.
2015-07-01 08:02:44 +00:00
2015-07-29 01:47:28 +00:00
**This project is a work-in-progress.**
2015-07-06 06:42:56 +00:00
## Features and Limitations
2015-07-20 05:36:50 +00:00
* Supported backends:
2015-07-30 03:55:28 +00:00
- [JACK](http://jackaudio.org/)
2015-07-20 05:36:50 +00:00
- [PulseAudio](http://www.freedesktop.org/wiki/Software/PulseAudio/)
- [ALSA](http://www.alsa-project.org/)
- [CoreAudio](https://developer.apple.com/library/mac/documentation/MusicAudio/Conceptual/CoreAudioOverview/Introduction/Introduction.html)
- [WASAPI](https://msdn.microsoft.com/en-us/library/windows/desktop/dd371455%28v=vs.85%29.aspx)
2015-08-01 01:52:51 +00:00
- Dummy (silence)
2015-08-13 19:23:22 +00:00
* 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.
2015-07-20 05:36:50 +00:00
* 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](http://latentcontent.net/2007/12/05/libpng-worst-api-ever/).
* Errors are communicated via return codes, not logging to stdio.
2015-07-20 05:36:50 +00:00
* 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.
2015-07-29 01:47:28 +00:00
* Ability to get an event when the backend is disconnected, for example when
the JACK server or PulseAudio server shuts down.
2015-07-24 03:55:36 +00:00
* Detects which input device is default and which output device is default.
2015-07-20 05:36:50 +00:00
* 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.
2015-08-12 18:01:06 +00:00
* 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.
2015-07-20 05:36:50 +00:00
## Synopsis
Complete program to emit a sine wave over the default device using the best
backend:
```c
#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;
2015-08-05 05:04:40 +00:00
static void write_callback(struct SoundIoOutStream *outstream,
int frame_count_min, int frame_count_max)
{
const struct SoundIoChannelLayout *layout = &outstream->layout;
2015-07-20 05:36:50 +00:00
float float_sample_rate = outstream->sample_rate;
float seconds_per_frame = 1.0f / float_sample_rate;
2015-08-05 05:04:40 +00:00
struct SoundIoChannelArea *areas;
int frames_left = frame_count_max;
2015-07-20 05:36:50 +00:00
int err;
2015-08-05 05:04:40 +00:00
while (frames_left > 0) {
int frame_count = frames_left;
2015-07-21 05:55:30 +00:00
2015-08-25 07:04:51 +00:00
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
fprintf(stderr, "%s\n", soundio_strerror(err));
exit(1);
}
2015-07-20 05:36:50 +00:00
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 += seconds_per_frame * frame_count;
2015-08-25 07:04:51 +00:00
if ((err = soundio_outstream_end_write(outstream))) {
fprintf(stderr, "%s\n", soundio_strerror(err));
exit(1);
}
2015-07-21 05:55:30 +00:00
2015-08-05 05:04:40 +00:00
frames_left -= frame_count;
2015-07-20 05:36:50 +00:00
}
}
int main(int argc, char **argv) {
2015-08-05 05:04:40 +00:00
int err;
2015-07-20 05:36:50 +00:00
struct SoundIo *soundio = soundio_create();
2015-08-25 07:04:51 +00:00
if (!soundio) {
fprintf(stderr, "out of memory\n");
return 1;
}
2015-07-20 05:36:50 +00:00
2015-08-25 07:04:51 +00:00
if ((err = soundio_connect(soundio))) {
fprintf(stderr, "error connecting: %s", soundio_strerror(err));
return 1;
}
2015-07-20 05:36:50 +00:00
2015-08-05 05:04:40 +00:00
soundio_flush_events(soundio);
int default_out_device_index = soundio_default_output_device_index(soundio);
2015-08-25 07:04:51 +00:00
if (default_out_device_index < 0) {
fprintf(stderr, "no output device found");
return 1;
}
2015-07-20 05:36:50 +00:00
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
2015-08-25 07:04:51 +00:00
if (!device) {
fprintf(stderr, "out of memory");
return 1;
}
2015-07-20 05:36:50 +00:00
2015-08-05 05:04:40 +00:00
fprintf(stderr, "Output device: %s\n", device->name);
2015-07-20 05:36:50 +00:00
struct SoundIoOutStream *outstream = soundio_outstream_create(device);
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
2015-08-25 07:04:51 +00:00
if ((err = soundio_outstream_open(outstream))) {
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
return 1;
}
2015-07-20 05:36:50 +00:00
if (outstream->layout_error)
fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));
2015-08-25 07:04:51 +00:00
if ((err = soundio_outstream_start(outstream))) {
fprintf(stderr, "unable to start device: %s", soundio_strerror(err));
return 1;
}
2015-07-20 05:36:50 +00:00
for (;;)
soundio_wait_events(soundio);
soundio_outstream_destroy(outstream);
soundio_device_unref(device);
soundio_destroy(soundio);
return 0;
}
```
### Backend Priority
2015-06-30 20:49:36 +00:00
2015-07-20 06:06:17 +00:00
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.
2015-07-01 08:02:44 +00:00
0. JACK
0. PulseAudio
0. ALSA (Linux)
0. CoreAudio (OSX)
2015-07-20 05:36:50 +00:00
0. WASAPI (Windows)
2015-07-01 08:02:44 +00:00
0. Dummy
2015-07-20 05:36:50 +00:00
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.
2015-08-26 09:40:04 +00:00
[API Documentation](http://libsound.io/doc)
2015-07-20 05:36:50 +00:00
2015-07-01 08:02:44 +00:00
## Contributing
2015-07-02 22:54:36 +00:00
libsoundio is programmed in a tiny subset of C++11:
2015-07-01 08:02:44 +00:00
* No STL.
* No `new` or `delete`.
* No `class`. All fields in structs are `public`.
2015-08-04 18:03:19 +00:00
* No constructors or destructors.
2015-07-01 08:02:44 +00:00
* No exceptions or run-time type information.
* No references.
* No linking against libstdc++.
2015-06-30 20:49:36 +00:00
2015-07-20 05:36:50 +00:00
Do not be fooled - this is a *C library*, not a C++ library. We just take
2015-07-02 22:54:36 +00:00
advantage of a select few C++11 compiler features such as templates, and then
link against libc.
2015-07-02 10:48:27 +00:00
### Building
2015-07-02 22:54:36 +00:00
Install the dependencies:
* cmake
* ALSA library (optional)
* libjack2 (optional)
* libpulseaudio (optional)
2015-07-02 10:48:27 +00:00
```
2015-07-02 22:54:36 +00:00
mkdir build
cd build
cmake ..
make
sudo make install
2015-07-02 10:48:27 +00:00
```
### Building for Windows
2015-07-02 10:48:27 +00:00
You can build libsoundio with [mxe](http://mxe.cc/). Follow the
[requirements](http://mxe.cc/#requirements) section to install the
packages necessary on your system. Then somewhere on your file system:
```
2015-07-02 22:54:36 +00:00
git clone https://github.com/mxe/mxe
cd mxe
make MXE_TARGETS='x86_64-w64-mingw32.static i686-w64-mingw32.static' gcc
2015-07-02 10:48:27 +00:00
```
Then in the libsoundio source directory (replace "/path/to/mxe" with the
appropriate path):
```
mkdir build-win32
cd build-win32
2015-07-02 22:54:36 +00:00
cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/mxe/usr/i686-w64-mingw32.static/share/cmake/mxe-conf.cmake
make
2015-07-02 10:48:27 +00:00
```
```
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:
0. Run the unit tests: `./unit_tests`. To see test coverage, install lcov, run
`make coverage`, and then view `coverage/index.html` in a browser.
0. Run the example `./sio_list_devices` and make sure it does not crash, and
the output looks good. If valgrind is available, use it.
0. Run `./sio_list_devices --watch` and make sure it detects when you plug and
unplug a USB microphone.
0. Run `./sio_sine` and make sure you hear a sine wave. For backends with raw
devices, run `./sio_sine --device id` (where 'id' is a device id you got
from `sio_list_devices` and make sure you hear a sine wave.
0. Run `./underflow` and read the testing instructions that it prints.
0. 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.
2015-08-26 09:48:46 +00:00
### Building the Documentation
2015-08-26 09:40:04 +00:00
Ensure that [doxygen](http://www.stack.nl/~dimitri/doxygen/) is installed,
then:
```
2015-08-26 09:48:46 +00:00
make doc
2015-08-26 09:40:04 +00:00
```
Then look at `html/index.html` in a browser.
2015-06-30 20:49:36 +00:00
## Roadmap
0. Make sure PulseAudio can handle refresh devices crashing before
block_until_have_devices
0. Integrate into libgroove and test with Groove Basin
2015-07-30 03:55:28 +00:00
0. clear buffer maybe could take an argument to say how many frames to not clear
2015-07-28 20:36:31 +00:00
0. Verify that JACK xrun callback context is the same as process callback.
If not, might need to hav xrun callback set a flag and have process callback
call the underflow callback.
2015-07-24 21:03:49 +00:00
0. Create a test for pausing and resuming input and output streams.
2015-08-25 00:42:57 +00:00
- Should pause/resume be callable from outside the callbacks?
- Ensure double pausing / double resuming works fine.
- test clearing the buffer
2015-07-25 01:43:14 +00:00
0. Create a test for the latency / synchronization API.
- Input is an audio file and some events indexed at particular frame - when
listening the events should line up exactly with a beat or visual
indicator, even when the latency is large.
- Play the audio file, have the user press an input right at the beat. Find
out what the frame index it thinks the user pressed it at and make sure
that is correct.
2015-07-29 01:47:28 +00:00
0. Create a test for input stream overflow handling.
2015-07-25 01:43:14 +00:00
0. Allow calling functions from outside the callbacks as long as they first
call lock and then unlock when done.
0. write detailed docs on buffer underflows explaining when they occur, what state
changes are related to them, and how to recover from them.
0. Detect PulseAudio server going offline and emit `on_backend_disconnect`.
0. Support for stream icon.
- PulseAudio: XDG icon name
- WASAPI: path to .exe, .dll, or .ico
- CoreAudio: CFURLRef image file
2015-07-01 08:02:44 +00:00
## Planned Uses for libsoundio
* [Genesis](https://github.com/andrewrk/genesis)
* [libgroove](https://github.com/andrewrk/libgroove) ([Groove Basin](https://github.com/andrewrk/groovebasin))