2015-07-30 19:07:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Andrew Kelley
|
|
|
|
*
|
|
|
|
* This file is part of libsoundio, which is MIT licensed.
|
|
|
|
* See http://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2015-07-30 19:18:56 +00:00
|
|
|
#include <soundio/soundio.h>
|
2015-07-30 19:07:58 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2015-08-20 21:48:19 +00:00
|
|
|
#include <unistd.h>
|
2015-10-13 07:20:56 +00:00
|
|
|
#include <stdint.h>
|
2015-07-30 19:07:58 +00:00
|
|
|
|
|
|
|
__attribute__ ((cold))
|
|
|
|
__attribute__ ((noreturn))
|
|
|
|
__attribute__ ((format (printf, 1, 2)))
|
|
|
|
static void panic(const char *format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usage(char *exe) {
|
2015-10-13 07:20:56 +00:00
|
|
|
fprintf(stderr, "Usage: %s [options]\n"
|
|
|
|
"Options:\n"
|
|
|
|
" [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n"
|
|
|
|
" [--device id]\n"
|
|
|
|
" [--raw]\n"
|
|
|
|
" [--sample-rate hz]\n"
|
|
|
|
, exe);
|
2015-07-30 19:07:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-13 07:20:56 +00:00
|
|
|
static void write_sample_s16ne(char *ptr, double sample) {
|
|
|
|
int16_t *buf = (int16_t *)ptr;
|
|
|
|
double range = (double)INT16_MAX - (double)INT16_MIN;
|
|
|
|
double val = sample * range / 2.0;
|
|
|
|
*buf = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_sample_s32ne(char *ptr, double sample) {
|
|
|
|
int32_t *buf = (int32_t *)ptr;
|
|
|
|
double range = (double)INT32_MAX - (double)INT32_MIN;
|
|
|
|
double val = sample * range / 2.0;
|
|
|
|
*buf = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_sample_float32ne(char *ptr, double sample) {
|
|
|
|
float *buf = (float *)ptr;
|
|
|
|
*buf = sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_sample_float64ne(char *ptr, double sample) {
|
|
|
|
double *buf = (double *)ptr;
|
|
|
|
*buf = sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void (*write_sample)(char *ptr, double sample);
|
|
|
|
static const double PI = 3.14159265358979323846264338328;
|
|
|
|
static double seconds_offset = 0.0;
|
2015-07-30 19:07:58 +00:00
|
|
|
static bool caused_underflow = false;
|
|
|
|
static struct SoundIo *soundio = NULL;
|
2015-10-13 07:20:56 +00:00
|
|
|
static double seconds_end = 9.0f;
|
2015-07-30 19:07:58 +00:00
|
|
|
|
2015-08-05 04:57:46 +00:00
|
|
|
static void write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int frame_count_max) {
|
2015-10-13 07:20:56 +00:00
|
|
|
double float_sample_rate = outstream->sample_rate;
|
|
|
|
double seconds_per_frame = 1.0 / float_sample_rate;
|
2015-08-04 07:56:03 +00:00
|
|
|
struct SoundIoChannelArea *areas;
|
2015-07-30 19:07:58 +00:00
|
|
|
int err;
|
|
|
|
|
2015-10-13 07:20:56 +00:00
|
|
|
if (!caused_underflow && seconds_offset >= 3.0) {
|
2015-07-30 19:07:58 +00:00
|
|
|
caused_underflow = true;
|
2015-08-20 21:48:19 +00:00
|
|
|
sleep(3);
|
2015-07-30 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (seconds_offset >= seconds_end) {
|
|
|
|
soundio_wakeup(soundio);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-05 04:57:46 +00:00
|
|
|
int frames_left = frame_count_max;
|
|
|
|
|
2015-07-30 19:07:58 +00:00
|
|
|
for (;;) {
|
2015-08-05 04:57:46 +00:00
|
|
|
int frame_count = frames_left;
|
2015-07-30 19:07:58 +00:00
|
|
|
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count)))
|
|
|
|
panic("%s", soundio_strerror(err));
|
|
|
|
|
|
|
|
if (!frame_count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const struct SoundIoChannelLayout *layout = &outstream->layout;
|
|
|
|
|
2015-10-13 07:20:56 +00:00
|
|
|
double pitch = 440.0;
|
|
|
|
double radians_per_second = pitch * 2.0 * PI;
|
2015-07-30 19:07:58 +00:00
|
|
|
for (int frame = 0; frame < frame_count; frame += 1) {
|
2015-10-13 07:20:56 +00:00
|
|
|
double sample = sinf((seconds_offset + frame * seconds_per_frame) * radians_per_second);
|
2015-07-30 19:07:58 +00:00
|
|
|
for (int channel = 0; channel < layout->channel_count; channel += 1) {
|
2015-10-13 07:20:56 +00:00
|
|
|
write_sample(areas[channel].ptr, sample);
|
|
|
|
areas[channel].ptr += areas[channel].step;
|
2015-07-30 19:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
seconds_offset += seconds_per_frame * frame_count;
|
|
|
|
|
2015-08-04 07:56:03 +00:00
|
|
|
if ((err = soundio_outstream_end_write(outstream))) {
|
2015-07-30 19:07:58 +00:00
|
|
|
if (err == SoundIoErrorUnderflow)
|
|
|
|
return;
|
|
|
|
panic("%s", soundio_strerror(err));
|
|
|
|
}
|
2015-08-05 04:57:46 +00:00
|
|
|
|
|
|
|
frames_left -= frame_count;
|
|
|
|
if (frames_left <= 0)
|
|
|
|
break;
|
2015-07-30 19:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void underflow_callback(struct SoundIoOutStream *outstream) {
|
|
|
|
static int count = 0;
|
|
|
|
fprintf(stderr, "underflow %d\n", count++);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char *exe = argv[0];
|
|
|
|
enum SoundIoBackend backend = SoundIoBackendNone;
|
2015-10-13 07:20:56 +00:00
|
|
|
char *device_id = NULL;
|
|
|
|
bool raw = false;
|
|
|
|
int sample_rate = 0;
|
2015-07-30 19:07:58 +00:00
|
|
|
for (int i = 1; i < argc; i += 1) {
|
|
|
|
char *arg = argv[i];
|
2015-08-28 03:53:28 +00:00
|
|
|
if (arg[0] == '-' && arg[1] == '-') {
|
2015-10-13 07:20:56 +00:00
|
|
|
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 1;
|
|
|
|
}
|
|
|
|
} else if (strcmp(arg, "--device") == 0) {
|
|
|
|
device_id = argv[i];
|
|
|
|
} else if (strcmp(arg, "--sample-rate") == 0) {
|
|
|
|
sample_rate = atoi(argv[i]);
|
2015-08-28 03:53:28 +00:00
|
|
|
} else {
|
2015-10-13 07:20:56 +00:00
|
|
|
return usage(exe);
|
2015-08-28 03:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-30 19:07:58 +00:00
|
|
|
} else {
|
|
|
|
return usage(exe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "You should hear a sine wave for 3 seconds, then some period of silence or glitches,\n"
|
|
|
|
"then you should see at least one buffer underflow message, then hear a sine\n"
|
2015-08-22 00:11:23 +00:00
|
|
|
"wave for 3 seconds, then the program should exit successfully.\n"
|
|
|
|
"WASAPI does not report buffer underflows.\n");
|
2015-07-30 19:07:58 +00:00
|
|
|
|
|
|
|
if (!(soundio = soundio_create()))
|
|
|
|
panic("out of memory");
|
|
|
|
|
|
|
|
int err = (backend == SoundIoBackendNone) ?
|
|
|
|
soundio_connect(soundio) : soundio_connect_backend(soundio, backend);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
panic("error connecting: %s", soundio_strerror(err));
|
|
|
|
|
2015-08-04 07:56:03 +00:00
|
|
|
soundio_flush_events(soundio);
|
|
|
|
|
2015-10-13 07:20:56 +00:00
|
|
|
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)
|
|
|
|
panic("Output device not found");
|
2015-07-30 19:07:58 +00:00
|
|
|
|
2015-10-13 07:20:56 +00:00
|
|
|
struct SoundIoDevice *device = soundio_get_output_device(soundio, selected_device_index);
|
2015-07-30 19:07:58 +00:00
|
|
|
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;
|
|
|
|
outstream->underflow_callback = underflow_callback;
|
2015-10-13 07:20:56 +00:00
|
|
|
outstream->sample_rate = sample_rate;
|
|
|
|
|
|
|
|
if (soundio_device_supports_format(device, SoundIoFormatFloat32NE)) {
|
|
|
|
outstream->format = SoundIoFormatFloat32NE;
|
|
|
|
write_sample = write_sample_float32ne;
|
|
|
|
} else if (soundio_device_supports_format(device, SoundIoFormatFloat64NE)) {
|
|
|
|
outstream->format = SoundIoFormatFloat64NE;
|
|
|
|
write_sample = write_sample_float64ne;
|
|
|
|
} else if (soundio_device_supports_format(device, SoundIoFormatS32NE)) {
|
|
|
|
outstream->format = SoundIoFormatS32NE;
|
|
|
|
write_sample = write_sample_s32ne;
|
|
|
|
} else if (soundio_device_supports_format(device, SoundIoFormatS16NE)) {
|
|
|
|
outstream->format = SoundIoFormatS16NE;
|
|
|
|
write_sample = write_sample_s16ne;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "No suitable device format available.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2015-07-30 19:07:58 +00:00
|
|
|
|
|
|
|
if ((err = soundio_outstream_open(outstream)))
|
|
|
|
panic("unable to open device: %s", soundio_strerror(err));
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
while (seconds_offset < seconds_end)
|
|
|
|
soundio_wait_events(soundio);
|
|
|
|
|
|
|
|
soundio_outstream_destroy(outstream);
|
|
|
|
soundio_device_unref(device);
|
|
|
|
soundio_destroy(soundio);
|
|
|
|
return 0;
|
|
|
|
}
|