libsoundio/example/sio_sine.c

184 lines
5.9 KiB
C
Raw Normal View History

2015-07-01 08:02:44 +00:00
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of libsoundio, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include <soundio/soundio.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
2015-07-04 10:55:00 +00:00
#include <string.h>
#include <math.h>
static void panic(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
abort();
}
2015-07-20 07:35:46 +00:00
static int usage(char *exe) {
fprintf(stderr, "Usage: %s [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi] [--device id] [--raw]\n",
exe);
return EXIT_FAILURE;
2015-07-20 07:35:46 +00:00
}
2015-07-04 10:55:00 +00:00
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) {
float float_sample_rate = outstream->sample_rate;
2015-07-04 10:55:00 +00:00
float seconds_per_frame = 1.0f / float_sample_rate;
struct SoundIoChannelArea *areas;
2015-07-16 03:57:00 +00:00
int err;
2015-07-04 10:55:00 +00:00
int frames_left = frame_count_max;
2015-07-21 05:55:30 +00:00
for (;;) {
int frame_count = frames_left;
2015-07-16 03:57:00 +00:00
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count)))
panic("%s", soundio_strerror(err));
2015-07-04 10:55:00 +00:00
2015-07-16 03:57:00 +00:00
if (!frame_count)
break;
2015-07-04 10:55:00 +00:00
const struct SoundIoChannelLayout *layout = &outstream->layout;
2015-07-04 10:55:00 +00:00
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) {
2015-07-16 03:57:00 +00:00
float *ptr = (float*)(areas[channel].ptr + areas[channel].step * frame);
*ptr = sample;
2015-07-04 10:55:00 +00:00
}
}
seconds_offset += seconds_per_frame * frame_count;
if ((err = soundio_outstream_end_write(outstream))) {
if (err == SoundIoErrorUnderflow)
return;
2015-07-16 03:57:00 +00:00
panic("%s", soundio_strerror(err));
}
frames_left -= frame_count;
if (frames_left <= 0)
break;
2015-07-04 10:55:00 +00:00
}
}
2015-07-23 23:49:44 +00:00
static void underflow_callback(struct SoundIoOutStream *outstream) {
static int count = 0;
fprintf(stderr, "underflow %d\n", count++);
}
2015-07-01 08:02:44 +00:00
int main(int argc, char **argv) {
2015-07-20 07:35:46 +00:00
char *exe = argv[0];
enum SoundIoBackend backend = SoundIoBackendNone;
char *device_id = NULL;
bool raw = false;
2015-07-20 07:35:46 +00:00
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '-') {
if (strcmp(arg, "--raw") == 0) {
raw = true;
} else {
i += 1;
if (i >= argc) {
return usage(exe);
} else if (strcmp(arg, "--backend") == 0) {
if (strcmp(argv[i], "dummy") == 0) {
backend = SoundIoBackendDummy;
} else if (strcmp(argv[i], "alsa") == 0) {
backend = SoundIoBackendAlsa;
} else if (strcmp(argv[i], "pulseaudio") == 0) {
backend = SoundIoBackendPulseAudio;
} else if (strcmp(argv[i], "jack") == 0) {
backend = SoundIoBackendJack;
} else if (strcmp(argv[i], "coreaudio") == 0) {
backend = SoundIoBackendCoreAudio;
} else if (strcmp(argv[i], "wasapi") == 0) {
backend = SoundIoBackendWasapi;
} else {
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
return EXIT_FAILURE;
}
} else if (strcmp(arg, "--device") == 0) {
device_id = argv[i];
} else {
return usage(exe);
}
}
2015-07-20 07:35:46 +00:00
} else {
return usage(exe);
}
}
struct SoundIo *soundio = soundio_create();
if (!soundio)
panic("out of memory");
2015-07-20 07:35:46 +00:00
int err = (backend == SoundIoBackendNone) ?
soundio_connect(soundio) : soundio_connect_backend(soundio, backend);
if (err) {
fprintf(stderr, "Unable to connect to backend: %s\n", soundio_strerror(err));
return EXIT_FAILURE;
}
soundio_flush_events(soundio);
int selected_device_index = -1;
if (device_id) {
int device_count = soundio_output_device_count(soundio);
for (int i = 0; i < device_count; i += 1) {
struct SoundIoDevice *device = soundio_get_output_device(soundio, i);
if (strcmp(device->id, device_id) == 0 && device->is_raw == raw) {
selected_device_index = i;
break;
}
}
} else {
selected_device_index = soundio_default_output_device_index(soundio);
}
if (selected_device_index < 0) {
fprintf(stderr, "Output device not found\n");
return EXIT_SUCCESS;
}
struct SoundIoDevice *device = soundio_get_output_device(soundio, selected_device_index);
if (!device)
panic("out of memory");
fprintf(stderr, "Output device: %s\n", device->name);
struct SoundIoOutStream *outstream = soundio_outstream_create(device);
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
2015-07-23 23:49:44 +00:00
outstream->underflow_callback = underflow_callback;
if ((err = soundio_outstream_open(outstream))) {
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
return EXIT_FAILURE;
}
2015-07-22 07:04:17 +00:00
if (outstream->layout_error)
fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));
if ((err = soundio_outstream_start(outstream)))
panic("unable to start device: %s", soundio_strerror(err));
for (;;)
soundio_wait_events(soundio);
soundio_outstream_destroy(outstream);
soundio_device_unref(device);
soundio_destroy(soundio);
return EXIT_SUCCESS;
2015-07-01 08:02:44 +00:00
}