underflow test: add --device and --raw params

This commit is contained in:
Andrew Kelley 2015-10-13 00:20:56 -07:00
parent db0e3d136c
commit b202f6746b
2 changed files with 112 additions and 38 deletions

View File

@ -55,7 +55,7 @@ static const double PI = 3.14159265358979323846264338328;
static double seconds_offset = 0.0;
static void write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int frame_count_max) {
double float_sample_rate = outstream->sample_rate;
double seconds_per_frame = 1.0f / float_sample_rate;
double seconds_per_frame = 1.0 / float_sample_rate;
struct SoundIoChannelArea *areas;
int err;

View File

@ -13,6 +13,7 @@
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
__attribute__ ((cold))
__attribute__ ((noreturn))
@ -27,23 +28,54 @@ static void panic(const char *format, ...) {
}
static int usage(char *exe) {
fprintf(stderr, "Usage: %s [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n", exe);
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);
return 1;
}
static const float PI = 3.1415926535f;
static float seconds_offset = 0.0f;
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;
static bool caused_underflow = false;
static struct SoundIo *soundio = NULL;
static float seconds_end = 9.0f;
static double seconds_end = 9.0f;
static void write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int frame_count_max) {
float float_sample_rate = outstream->sample_rate;
float seconds_per_frame = 1.0f / float_sample_rate;
double float_sample_rate = outstream->sample_rate;
double seconds_per_frame = 1.0 / float_sample_rate;
struct SoundIoChannelArea *areas;
int err;
if (!caused_underflow && seconds_offset >= 3.0f) {
if (!caused_underflow && seconds_offset >= 3.0) {
caused_underflow = true;
sleep(3);
}
@ -65,13 +97,13 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m
const struct SoundIoChannelLayout *layout = &outstream->layout;
float pitch = 440.0f;
float radians_per_second = pitch * 2.0f * PI;
double pitch = 440.0;
double radians_per_second = pitch * 2.0 * PI;
for (int frame = 0; frame < frame_count; frame += 1) {
float sample = sinf((seconds_offset + frame * seconds_per_frame) * radians_per_second);
double 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;
write_sample(areas[channel].ptr, sample);
areas[channel].ptr += areas[channel].step;
}
}
seconds_offset += seconds_per_frame * frame_count;
@ -96,31 +128,42 @@ static void underflow_callback(struct SoundIoOutStream *outstream) {
int main(int argc, char **argv) {
char *exe = argv[0];
enum SoundIoBackend backend = SoundIoBackendNone;
char *device_id = NULL;
bool raw = false;
int sample_rate = 0;
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '-') {
i += 1;
if (i >= argc) {
return usage(exe);
} else if (strcmp(arg, "--backend") == 0) {
if (strcmp("dummy", argv[i]) == 0) {
backend = SoundIoBackendDummy;
} else if (strcmp("alsa", argv[i]) == 0) {
backend = SoundIoBackendAlsa;
} else if (strcmp("pulseaudio", argv[i]) == 0) {
backend = SoundIoBackendPulseAudio;
} else if (strcmp("jack", argv[i]) == 0) {
backend = SoundIoBackendJack;
} else if (strcmp("coreaudio", argv[i]) == 0) {
backend = SoundIoBackendCoreAudio;
} else if (strcmp("wasapi", argv[i]) == 0) {
backend = SoundIoBackendWasapi;
} else {
fprintf(stderr, "Invalid backend: %s\n", argv[i]);
return 1;
}
if (strcmp(arg, "--raw") == 0) {
raw = true;
} else {
return usage(exe);
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]);
} else {
return usage(exe);
}
}
} else {
return usage(exe);
@ -143,11 +186,24 @@ int main(int argc, char **argv) {
soundio_flush_events(soundio);
int default_out_device_index = soundio_default_output_device_index(soundio);
if (default_out_device_index < 0)
panic("no output device found");
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);
}
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
if (selected_device_index < 0)
panic("Output device not found");
struct SoundIoDevice *device = soundio_get_output_device(soundio, selected_device_index);
if (!device)
panic("out of memory");
@ -157,6 +213,24 @@ int main(int argc, char **argv) {
outstream->format = SoundIoFormatFloat32NE;
outstream->write_callback = write_callback;
outstream->underflow_callback = underflow_callback;
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;
}
if ((err = soundio_outstream_open(outstream)))
panic("unable to open device: %s", soundio_strerror(err));