libsoundio  1.1.0
sio_record.c

Record audio to an output file.Supports specifying device and backend to use.

/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of libsoundio, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <unistd.h>
struct RecordContext {
struct SoundIoRingBuffer *ring_buffer;
};
static enum SoundIoFormat prioritized_formats[] = {
SoundIoFormatFloat32NE,
SoundIoFormatFloat32FE,
SoundIoFormatS32NE,
SoundIoFormatS32FE,
SoundIoFormatS24NE,
SoundIoFormatS24FE,
SoundIoFormatS16NE,
SoundIoFormatS16FE,
SoundIoFormatFloat64NE,
SoundIoFormatFloat64FE,
SoundIoFormatU32NE,
SoundIoFormatU32FE,
SoundIoFormatU24NE,
SoundIoFormatU24FE,
SoundIoFormatU16NE,
SoundIoFormatU16FE,
};
static int prioritized_sample_rates[] = {
48000,
44100,
96000,
24000,
0,
};
static int min_int(int a, int b) {
return (a < b) ? a : b;
}
static void read_callback(struct SoundIoInStream *instream, int frame_count_min, int frame_count_max) {
struct RecordContext *rc = instream->userdata;
struct SoundIoChannelArea *areas;
int err;
char *write_ptr = soundio_ring_buffer_write_ptr(rc->ring_buffer);
int free_bytes = soundio_ring_buffer_free_count(rc->ring_buffer);
int free_count = free_bytes / instream->bytes_per_frame;
if (free_count < frame_count_min) {
fprintf(stderr, "ring buffer overflow\n");
exit(1);
}
int write_frames = min_int(free_count, frame_count_max);
int frames_left = write_frames;
for (;;) {
int frame_count = frames_left;
if ((err = soundio_instream_begin_read(instream, &areas, &frame_count))) {
fprintf(stderr, "begin read error: %s", soundio_strerror(err));
exit(1);
}
if (!frame_count)
break;
if (!areas) {
// Due to an overflow there is a hole. Fill the ring buffer with
// silence for the size of the hole.
memset(write_ptr, 0, frame_count * instream->bytes_per_frame);
} else {
for (int frame = 0; frame < frame_count; frame += 1) {
for (int ch = 0; ch < instream->layout.channel_count; ch += 1) {
memcpy(write_ptr, areas[ch].ptr, instream->bytes_per_sample);
areas[ch].ptr += areas[ch].step;
write_ptr += instream->bytes_per_sample;
}
}
}
if ((err = soundio_instream_end_read(instream))) {
fprintf(stderr, "end read error: %s", soundio_strerror(err));
exit(1);
}
frames_left -= frame_count;
if (frames_left <= 0)
break;
}
int advance_bytes = write_frames * instream->bytes_per_frame;
soundio_ring_buffer_advance_write_ptr(rc->ring_buffer, advance_bytes);
}
static void overflow_callback(struct SoundIoInStream *instream) {
static int count = 0;
fprintf(stderr, "overflow %d\n", ++count);
}
static int usage(char *exe) {
fprintf(stderr, "Usage: %s [options] outfile.wav\n"
"Options:\n"
" [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n"
" [--device id]\n"
" [--raw]\n"
, exe);
return 1;
}
int main(int argc, char **argv) {
char *exe = argv[0];
char *device_id = NULL;
bool is_raw = false;
char *outfile = NULL;
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '-') {
if (strcmp(arg, "--raw") == 0) {
is_raw = true;
} else if (++i >= argc) {
return usage(exe);
} else if (strcmp(arg, "--backend") == 0) {
if (strcmp("dummy", argv[i]) == 0) {
} else if (strcmp("alsa", argv[i]) == 0) {
backend = SoundIoBackendAlsa;
} else if (strcmp("pulseaudio", argv[i]) == 0) {
} else if (strcmp("jack", argv[i]) == 0) {
backend = SoundIoBackendJack;
} else if (strcmp("coreaudio", argv[i]) == 0) {
} else if (strcmp("wasapi", argv[i]) == 0) {
} else {
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
return 1;
}
} else if (strcmp(arg, "--device") == 0) {
device_id = argv[i];
} else {
return usage(exe);
}
} else if (!outfile) {
outfile = argv[i];
} else {
return usage(exe);
}
}
if (!outfile)
return usage(exe);
struct RecordContext rc;
struct SoundIo *soundio = soundio_create();
if (!soundio) {
fprintf(stderr, "out of memory\n");
return 1;
}
int err = (backend == SoundIoBackendNone) ?
soundio_connect(soundio) : soundio_connect_backend(soundio, backend);
if (err) {
fprintf(stderr, "error connecting: %s", soundio_strerror(err));
return 1;
}
struct SoundIoDevice *selected_device = NULL;
if (device_id) {
for (int i = 0; i < soundio_input_device_count(soundio); i += 1) {
if (device->is_raw == is_raw && strcmp(device->id, device_id) == 0) {
selected_device = device;
break;
}
}
if (!selected_device) {
fprintf(stderr, "Invalid device id: %s\n", device_id);
return 1;
}
} else {
selected_device = soundio_get_input_device(soundio, device_index);
if (!selected_device) {
fprintf(stderr, "No input devices available.\n");
return 1;
}
}
fprintf(stderr, "Device: %s\n", selected_device->name);
if (selected_device->probe_error) {
fprintf(stderr, "Unable to probe device: %s\n", soundio_strerror(selected_device->probe_error));
return 1;
}
int sample_rate = 0;
int *sample_rate_ptr;
for (sample_rate_ptr = prioritized_sample_rates; *sample_rate_ptr; sample_rate_ptr += 1) {
if (soundio_device_supports_sample_rate(selected_device, *sample_rate_ptr)) {
sample_rate = *sample_rate_ptr;
break;
}
}
if (!sample_rate)
sample_rate = selected_device->sample_rates[0].max;
enum SoundIoFormat *fmt_ptr;
for (fmt_ptr = prioritized_formats; *fmt_ptr != SoundIoFormatInvalid; fmt_ptr += 1) {
if (soundio_device_supports_format(selected_device, *fmt_ptr)) {
fmt = *fmt_ptr;
break;
}
}
fmt = selected_device->formats[0];
FILE *out_f = fopen(outfile, "wb");
if (!out_f) {
fprintf(stderr, "unable to open %s: %s\n", outfile, strerror(errno));
return 1;
}
struct SoundIoInStream *instream = soundio_instream_create(selected_device);
if (!instream) {
fprintf(stderr, "out of memory\n");
return 1;
}
instream->format = fmt;
instream->sample_rate = sample_rate;
instream->userdata = &rc;
if ((err = soundio_instream_open(instream))) {
fprintf(stderr, "unable to open input stream: %s", soundio_strerror(err));
return 1;
}
fprintf(stderr, "%s %dHz %s interleaved\n",
const int ring_buffer_duration_seconds = 30;
int capacity = ring_buffer_duration_seconds * instream->sample_rate * instream->bytes_per_frame;
rc.ring_buffer = soundio_ring_buffer_create(soundio, capacity);
if (!rc.ring_buffer) {
fprintf(stderr, "out of memory\n");
return 1;
}
if ((err = soundio_instream_start(instream))) {
fprintf(stderr, "unable to start input device: %s", soundio_strerror(err));
return 1;
}
// Note: in this example, if you send SIGINT (by pressing Ctrl+C for example)
// you will lose up to 1 second of recorded audio data. In non-example code,
// consider a better shutdown strategy.
for (;;) {
sleep(1);
int fill_bytes = soundio_ring_buffer_fill_count(rc.ring_buffer);
char *read_buf = soundio_ring_buffer_read_ptr(rc.ring_buffer);
size_t amt = fwrite(read_buf, 1, fill_bytes, out_f);
if ((int)amt != fill_bytes) {
fprintf(stderr, "write error: %s\n", strerror(errno));
return 1;
}
soundio_ring_buffer_advance_read_ptr(rc.ring_buffer, fill_bytes);
}
soundio_device_unref(selected_device);
soundio_destroy(soundio);
return 0;
}
soundio_flush_events
void soundio_flush_events(struct SoundIo *soundio)
Atomically update information for all connected devices.
soundio_ring_buffer_read_ptr
char * soundio_ring_buffer_read_ptr(struct SoundIoRingBuffer *ring_buffer)
Do not read more than capacity.
SoundIoDevice::soundio
struct SoundIo * soundio
Read-only. Set automatically.
Definition: soundio.h:385
SoundIoInStream::read_callback
void(* read_callback)(struct SoundIoInStream *, int frame_count_min, int frame_count_max)
In this function call soundio_instream_begin_read and soundio_instream_end_read as many times as nece...
Definition: soundio.h:639
soundio_ring_buffer_free_count
int soundio_ring_buffer_free_count(struct SoundIoRingBuffer *ring_buffer)
Returns how many bytes of the buffer is free, ready for writing.
soundio_get_input_device
struct SoundIoDevice * soundio_get_input_device(struct SoundIo *soundio, int index)
Always returns a device.
soundio_format_string
const char * soundio_format_string(enum SoundIoFormat format)
Returns string representation of format.
soundio_device_supports_format
bool soundio_device_supports_format(struct SoundIoDevice *device, enum SoundIoFormat format)
Convenience function.
SoundIoBackendJack
@ SoundIoBackendJack
Definition: soundio.h:220
soundio_ring_buffer_advance_read_ptr
void soundio_ring_buffer_advance_read_ptr(struct SoundIoRingBuffer *ring_buffer, int count)
count in bytes.
soundio_instream_begin_read
int soundio_instream_begin_read(struct SoundIoInStream *instream, struct SoundIoChannelArea **areas, int *frame_count)
Call this function when you are ready to begin reading from the device buffer.
SoundIoDevice::name
char * name
User-friendly UTF-8 encoded text to describe the device.
Definition: soundio.h:397
soundio_connect_backend
int soundio_connect_backend(struct SoundIo *soundio, enum SoundIoBackend backend)
Instead of calling soundio_connect you may call this function to try a specific backend.
SoundIoFormatInvalid
@ SoundIoFormatInvalid
Definition: soundio.h:236
SoundIoInStream::sample_rate
int sample_rate
Sample rate is the number of frames per second.
Definition: soundio.h:605
SoundIoInStream::layout
struct SoundIoChannelLayout layout
Defaults to Stereo, if available, followed by the first layout supported.
Definition: soundio.h:609
soundio_default_input_device_index
int soundio_default_input_device_index(struct SoundIo *soundio)
returns the index of the default input device returns -1 if there are no devices or if you never call...
soundio_device_unref
void soundio_device_unref(struct SoundIoDevice *device)
Remove 1 to the reference count of device.
SoundIoFormat
SoundIoFormat
For your convenience, Native Endian and Foreign Endian constants are defined which point to the respe...
Definition: soundio.h:235
soundio_connect
int soundio_connect(struct SoundIo *soundio)
Tries soundio_connect_backend on all available backends in order.
SoundIoDevice::probe_error
int probe_error
This is set to a SoundIoError representing the result of the device probe.
Definition: soundio.h:489
SoundIoDevice::formats
enum SoundIoFormat * formats
List of formats this device supports.
Definition: soundio.h:414
SoundIoDevice::id
char * id
A string of bytes that uniquely identifies this device.
Definition: soundio.h:395
SoundIoSampleRateRange::max
int max
Definition: soundio.h:311
SoundIo
The size of this struct is not part of the API or ABI.
Definition: soundio.h:324
SoundIoInStream
The size of this struct is not part of the API or ABI.
Definition: soundio.h:595
SoundIoChannelArea::ptr
char * ptr
Base address of buffer.
Definition: soundio.h:317
SoundIoBackendNone
@ SoundIoBackendNone
Definition: soundio.h:219
SoundIoDevice::sample_rates
struct SoundIoSampleRateRange * sample_rates
Sample rate is the number of frames per second.
Definition: soundio.h:443
soundio_ring_buffer_write_ptr
char * soundio_ring_buffer_write_ptr(struct SoundIoRingBuffer *ring_buffer)
Do not write more than capacity.
SoundIoFormatS8
@ SoundIoFormatS8
Signed 8 bit.
Definition: soundio.h:237
SoundIoChannelLayout::name
const char * name
Definition: soundio.h:303
SoundIoInStream::bytes_per_frame
int bytes_per_frame
computed automatically when you call soundio_instream_open
Definition: soundio.h:667
SoundIoChannelLayout::channel_count
int channel_count
Definition: soundio.h:304
soundio_instream_create
struct SoundIoInStream * soundio_instream_create(struct SoundIoDevice *device)
Allocates memory and sets defaults.
soundio_instream_destroy
void soundio_instream_destroy(struct SoundIoInStream *instream)
You may not call this function from SoundIoInStream::read_callback.
SoundIoBackendAlsa
@ SoundIoBackendAlsa
Definition: soundio.h:222
SoundIoDevice
The size of this struct is not part of the API or ABI.
Definition: soundio.h:383
soundio_instream_end_read
int soundio_instream_end_read(struct SoundIoInStream *instream)
This will drop all of the frames from when you called soundio_instream_begin_read.
soundio_ring_buffer_advance_write_ptr
void soundio_ring_buffer_advance_write_ptr(struct SoundIoRingBuffer *ring_buffer, int count)
count in bytes.
soundio_strerror
const char * soundio_strerror(int error)
Get a string representation of a SoundIoError.
soundio_destroy
void soundio_destroy(struct SoundIo *soundio)
soundio_ring_buffer_create
struct SoundIoRingBuffer * soundio_ring_buffer_create(struct SoundIo *soundio, int requested_capacity)
requested_capacity in bytes.
SoundIoDevice::is_raw
bool is_raw
Raw means that you are directly opening the hardware device and not going through a proxy such as dmi...
Definition: soundio.h:474
soundio_device_sort_channel_layouts
void soundio_device_sort_channel_layouts(struct SoundIoDevice *device)
Sorts channel layouts by channel count, descending.
SoundIoInStream::userdata
void * userdata
Defaults to NULL. Put whatever you want here.
Definition: soundio.h:626
SoundIoBackendPulseAudio
@ SoundIoBackendPulseAudio
Definition: soundio.h:221
SoundIoBackendDummy
@ SoundIoBackendDummy
Definition: soundio.h:225
soundio_input_device_count
int soundio_input_device_count(struct SoundIo *soundio)
When you call soundio_flush_events, a snapshot of all device state is saved and these functions merel...
SoundIoChannelArea::step
int step
How many bytes it takes to get from the beginning of one sample to the beginning of the next sample.
Definition: soundio.h:320
SoundIoChannelArea
The size of this struct is OK to use.
Definition: soundio.h:315
SoundIoBackend
SoundIoBackend
Definition: soundio.h:218
SoundIoInStream::bytes_per_sample
int bytes_per_sample
computed automatically when you call soundio_instream_open
Definition: soundio.h:669
SoundIoBackendCoreAudio
@ SoundIoBackendCoreAudio
Definition: soundio.h:223
soundio_device_supports_sample_rate
bool soundio_device_supports_sample_rate(struct SoundIoDevice *device, int sample_rate)
Convenience function.
soundio_ring_buffer_fill_count
int soundio_ring_buffer_fill_count(struct SoundIoRingBuffer *ring_buffer)
Returns how many bytes of the buffer is used, ready for reading.
SoundIoBackendWasapi
@ SoundIoBackendWasapi
Definition: soundio.h:224
SoundIoInStream::format
enum SoundIoFormat format
Defaults to #SoundIoFormatFloat32NE, followed by the first one supported.
Definition: soundio.h:601
soundio_instream_start
int soundio_instream_start(struct SoundIoInStream *instream)
After you call this function, SoundIoInStream::read_callback will be called.
soundio.h
soundio_instream_open
int soundio_instream_open(struct SoundIoInStream *instream)
After you call this function, SoundIoInStream::software_latency is set to the correct value.
SoundIoFormatU8
@ SoundIoFormatU8
Unsigned 8 bit.
Definition: soundio.h:238
SoundIoInStream::overflow_callback
void(* overflow_callback)(struct SoundIoInStream *)
This optional callback happens when the sound device buffer is full, yet there is more captured audio...
Definition: soundio.h:644
soundio_create
struct SoundIo * soundio_create(void)
Create a SoundIo context.