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
|
|
|
|
*/
|
|
|
|
|
2015-07-30 19:18:56 +00:00
|
|
|
#include <soundio/soundio.h>
|
2015-07-04 09:57:06 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2015-07-04 10:55:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2015-07-04 09:57:06 +00:00
|
|
|
|
|
|
|
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) {
|
2015-07-29 08:13:57 +00:00
|
|
|
fprintf(stderr, "Usage: %s [--dummy] [--alsa] [--pulseaudio] [--jack]\n", exe);
|
2015-07-20 07:35:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-04 10:55:00 +00:00
|
|
|
static const float PI = 3.1415926535f;
|
|
|
|
static float seconds_offset = 0.0f;
|
2015-08-05 04:57:46 +00:00
|
|
|
static void write_callback(struct SoundIoOutStream *outstream, int frame_count_min, int frame_count_max) {
|
2015-07-13 16:17:20 +00:00
|
|
|
float float_sample_rate = outstream->sample_rate;
|
2015-07-04 10:55:00 +00:00
|
|
|
float seconds_per_frame = 1.0f / float_sample_rate;
|
2015-08-05 04:57:46 +00:00
|
|
|
struct SoundIoChannelArea *areas;
|
2015-07-16 03:57:00 +00:00
|
|
|
int err;
|
2015-07-04 10:55:00 +00:00
|
|
|
|
2015-08-05 04:57:46 +00:00
|
|
|
int frames_left = frame_count_max;
|
2015-07-21 05:55:30 +00:00
|
|
|
|
2015-08-04 07:56:03 +00:00
|
|
|
for (;;) {
|
2015-08-05 04:57:46 +00:00
|
|
|
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
|
|
|
|
2015-07-13 16:17:20 +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);
|
2015-07-13 16:17:20 +00:00
|
|
|
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;
|
|
|
|
|
2015-08-04 07:56:03 +00:00
|
|
|
if ((err = soundio_outstream_end_write(outstream))) {
|
2015-07-30 05:39:27 +00:00
|
|
|
if (err == SoundIoErrorUnderflow)
|
|
|
|
return;
|
2015-07-16 03:57:00 +00:00
|
|
|
panic("%s", soundio_strerror(err));
|
2015-07-30 05:39:27 +00:00
|
|
|
}
|
2015-08-05 04:57:46 +00:00
|
|
|
|
|
|
|
frames_left -= frame_count;
|
|
|
|
if (frames_left <= 0)
|
|
|
|
break;
|
2015-07-04 10:55:00 +00:00
|
|
|
}
|
2015-07-04 09:57:06 +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;
|
|
|
|
for (int i = 1; i < argc; i += 1) {
|
|
|
|
char *arg = argv[i];
|
|
|
|
if (strcmp("--dummy", arg) == 0) {
|
|
|
|
backend = SoundIoBackendDummy;
|
|
|
|
} else if (strcmp("--alsa", arg) == 0) {
|
|
|
|
backend = SoundIoBackendAlsa;
|
|
|
|
} else if (strcmp("--pulseaudio", arg) == 0) {
|
|
|
|
backend = SoundIoBackendPulseAudio;
|
2015-07-29 08:13:57 +00:00
|
|
|
} else if (strcmp("--jack", arg) == 0) {
|
|
|
|
backend = SoundIoBackendJack;
|
2015-07-20 07:35:46 +00:00
|
|
|
} else {
|
|
|
|
return usage(exe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 09:57:06 +00:00
|
|
|
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)
|
2015-07-10 09:21:47 +00:00
|
|
|
panic("error connecting: %s", soundio_strerror(err));
|
2015-07-04 09:57:06 +00:00
|
|
|
|
2015-08-02 05:10:43 +00:00
|
|
|
soundio_flush_events(soundio);
|
|
|
|
|
2015-07-24 03:55:36 +00:00
|
|
|
int default_out_device_index = soundio_default_output_device_index(soundio);
|
2015-07-04 09:57:06 +00:00
|
|
|
if (default_out_device_index < 0)
|
|
|
|
panic("no output device found");
|
|
|
|
|
|
|
|
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
|
|
|
|
if (!device)
|
2015-07-14 04:30:37 +00:00
|
|
|
panic("out of memory");
|
2015-07-04 09:57:06 +00:00
|
|
|
|
2015-07-30 17:26:36 +00:00
|
|
|
fprintf(stderr, "Output device: %s\n", device->name);
|
2015-07-04 09:57:06 +00:00
|
|
|
|
2015-07-13 16:17:20 +00:00
|
|
|
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;
|
2015-07-04 09:57:06 +00:00
|
|
|
|
2015-07-13 16:17:20 +00:00
|
|
|
if ((err = soundio_outstream_open(outstream)))
|
|
|
|
panic("unable to open device: %s", soundio_strerror(err));
|
|
|
|
|
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));
|
|
|
|
|
2015-07-13 16:17:20 +00:00
|
|
|
if ((err = soundio_outstream_start(outstream)))
|
2015-07-10 09:21:47 +00:00
|
|
|
panic("unable to start device: %s", soundio_strerror(err));
|
2015-07-04 10:37:50 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
soundio_wait_events(soundio);
|
|
|
|
|
2015-07-13 16:17:20 +00:00
|
|
|
soundio_outstream_destroy(outstream);
|
2015-07-04 09:57:06 +00:00
|
|
|
soundio_device_unref(device);
|
|
|
|
soundio_destroy(soundio);
|
2015-07-01 08:02:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|