diff --git a/example/sine.c b/example/sine.c index d8cccfc..be9a9e7 100644 --- a/example/sine.c +++ b/example/sine.c @@ -25,6 +25,12 @@ static void panic(const char *format, ...) { abort(); } +static int usage(char *exe) { + fprintf(stderr, "Usage: %s [--dummy] [--alsa] [--pulseaudio]\n", exe); + return 1; +} + + static const float PI = 3.1415926535f; static float seconds_offset = 0.0f; static void write_callback(struct SoundIoOutStream *outstream, int requested_frame_count) { @@ -69,12 +75,29 @@ static void error_callback(struct SoundIoOutStream *device, int err) { } int main(int argc, char **argv) { + 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; + } else { + return usage(exe); + } + } + struct SoundIo *soundio = soundio_create(); if (!soundio) panic("out of memory"); - int err; - if ((err = soundio_connect(soundio))) + int err = (backend == SoundIoBackendNone) ? + soundio_connect(soundio) : soundio_connect_backend(soundio, backend); + + if (err) panic("error connecting: %s", soundio_strerror(err)); int default_out_device_index = soundio_get_default_output_device_index(soundio); diff --git a/src/dummy.cpp b/src/dummy.cpp index fa3f7fa..f9854ab 100644 --- a/src/dummy.cpp +++ b/src/dummy.cpp @@ -138,7 +138,7 @@ static int outstream_open_dummy(SoundIoPrivate *si, SoundIoOutStreamPrivate *os) } os->backend_data = osd; - osd->buffer_size = outstream->bytes_per_frame * outstream->buffer_duration; + osd->buffer_size = outstream->bytes_per_frame * outstream->sample_rate * outstream->buffer_duration; soundio_ring_buffer_init(&osd->ring_buffer, osd->buffer_size); @@ -185,13 +185,19 @@ static int outstream_begin_write_dummy(SoundIoPrivate *si, int byte_count = *frame_count * outstream->bytes_per_frame; assert(byte_count <= osd->buffer_size); - char *write_ptr = soundio_ring_buffer_write_ptr(&osd->ring_buffer); - for (int ch = 0; ch < outstream->layout.channel_count; ch += 1) { - osd->areas[ch].ptr = write_ptr + outstream->bytes_per_sample * ch; - osd->areas[ch].step = outstream->bytes_per_frame; - } + int free_byte_count = soundio_ring_buffer_free_count(&osd->ring_buffer); + int free_frame_count = free_byte_count / outstream->bytes_per_frame; + *frame_count = min(*frame_count, free_frame_count); - *out_areas = osd->areas; + if (free_frame_count) { + char *write_ptr = soundio_ring_buffer_write_ptr(&osd->ring_buffer); + for (int ch = 0; ch < outstream->layout.channel_count; ch += 1) { + osd->areas[ch].ptr = write_ptr + outstream->bytes_per_sample * ch; + osd->areas[ch].step = outstream->bytes_per_frame; + } + + *out_areas = osd->areas; + } return 0; } @@ -319,14 +325,15 @@ int soundio_dummy_init(SoundIoPrivate *si) { destroy_dummy(si); return SoundIoErrorNoMem; } - device->layout_count = 1; - device->layouts = allocate(1); + device->layout_count = soundio_channel_layout_builtin_count(); + device->layouts = allocate(device->layout_count); if (!device->layouts) { soundio_device_unref(device); destroy_dummy(si); return SoundIoErrorNoMem; } - device->layouts[0] = *soundio_channel_layout_get_builtin(SoundIoChannelLayoutIdMono); + for (int i = 0; i < device->layout_count; i += 1) + device->layouts[i] = *soundio_channel_layout_get_builtin(i); int err; if ((err = set_all_device_formats(device))) { @@ -371,14 +378,15 @@ int soundio_dummy_init(SoundIoPrivate *si) { return SoundIoErrorNoMem; } - device->layout_count = 1; - device->layouts = allocate(1); + device->layout_count = soundio_channel_layout_builtin_count(); + device->layouts = allocate(device->layout_count); if (!device->layouts) { soundio_device_unref(device); destroy_dummy(si); return SoundIoErrorNoMem; } - device->layouts[0] = *soundio_channel_layout_get_builtin(SoundIoChannelLayoutIdMono); + for (int i = 0; i < device->layout_count; i += 1) + device->layouts[i] = *soundio_channel_layout_get_builtin(i); int err; if ((err = set_all_device_formats(device))) {