From c760bc4df7459050cc7a0174f88f0bf6ecaea909 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 8 Jul 2015 00:42:17 -0700 Subject: [PATCH] default sample rate is not as important as I thought --- README.md | 2 -- example/microphone.c | 4 ++-- example/sine.c | 6 +++--- src/dummy.cpp | 6 ++---- src/pulseaudio.cpp | 8 ++++---- src/soundio.cpp | 7 +++++-- src/soundio.h | 15 ++++++++++++--- test/unit_tests.cpp | 2 +- 8 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index bcd7c7d..4476b09 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,6 @@ view `coverage/index.html` in a browser. ## Roadmap - 0. ALSA `list_devices` should list default, dmix, etc. - 0. ALSA default devices are "default" and "default" respectively 0. implement ALSA (Linux) backend, get examples working 0. pipe record to playback example working with dummy linux, osx, windows 0. pipe record to playback example working with pulseaudio linux diff --git a/example/microphone.c b/example/microphone.c index 5a4214c..48e4e6c 100644 --- a/example/microphone.c +++ b/example/microphone.c @@ -79,11 +79,11 @@ int main(int argc, char **argv) { double latency = 0.1; struct SoundIoInputDevice *input_device; - soundio_input_device_create(in_device, SoundIoSampleFormatFloat, latency, NULL, + soundio_input_device_create(in_device, SoundIoSampleFormatFloat, 48000, latency, NULL, read_callback, &input_device); struct SoundIoOutputDevice *output_device; - soundio_output_device_create(out_device, SoundIoSampleFormatFloat, latency, NULL, + soundio_output_device_create(out_device, SoundIoSampleFormatFloat, 48000, latency, NULL, write_callback, underrun_callback, &output_device); if ((err = soundio_input_device_start(input_device))) diff --git a/example/sine.c b/example/sine.c index ffd5213..8e2c0f8 100644 --- a/example/sine.c +++ b/example/sine.c @@ -30,7 +30,7 @@ static float seconds_offset = 0.0f; static void write_callback(struct SoundIoOutputDevice *output_device, int requested_frame_count) { //device->bytes_per_frame; - float float_sample_rate = output_device->device->default_sample_rate; + float float_sample_rate = output_device->sample_rate; float seconds_per_frame = 1.0f / float_sample_rate; while (requested_frame_count > 0) { @@ -90,8 +90,8 @@ int main(int argc, char **argv) { soundio_device_description(device)); struct SoundIoOutputDevice *output_device; - soundio_output_device_create(device, SoundIoSampleFormatFloat, 0.1, NULL, - write_callback, underrun_callback, &output_device); + soundio_output_device_create(device, SoundIoSampleFormatFloat, 48000, + 0.1, NULL, write_callback, underrun_callback, &output_device); if ((err = soundio_output_device_start(output_device))) panic("unable to start device: %s", soundio_error_string(err)); diff --git a/src/dummy.cpp b/src/dummy.cpp index 94804e0..9c7d05a 100644 --- a/src/dummy.cpp +++ b/src/dummy.cpp @@ -35,13 +35,12 @@ struct SoundIoDummy { static void playback_thread_run(void *arg) { SoundIoOutputDevice *output_device = (SoundIoOutputDevice *)arg; - SoundIoDevice *device = output_device->device; SoundIoOutputDeviceDummy *opd = (SoundIoOutputDeviceDummy *)output_device->backend_data; double start_time = soundio_os_get_time(); long frames_consumed = 0; - double time_per_frame = 1.0 / (double)device->default_sample_rate; + double time_per_frame = 1.0 / (double)output_device->sample_rate; while (opd->abort_flag.test_and_set()) { soundio_os_cond_timed_wait(opd->cond, nullptr, opd->period); @@ -143,8 +142,7 @@ static int output_device_init_dummy(SoundIo *soundio, } output_device->backend_data = opd; - SoundIoDevice *device = output_device->device; - int buffer_frame_count = output_device->latency * device->default_sample_rate; + int buffer_frame_count = output_device->latency * output_device->sample_rate; opd->buffer_size = output_device->bytes_per_frame * buffer_frame_count; opd->period = output_device->latency * 0.5; diff --git a/src/pulseaudio.cpp b/src/pulseaudio.cpp index 67e865a..438856f 100644 --- a/src/pulseaudio.cpp +++ b/src/pulseaudio.cpp @@ -581,7 +581,7 @@ static int output_device_init_pa(SoundIo *soundio, pa_sample_spec sample_spec; sample_spec.format = to_pulseaudio_sample_format(output_device->sample_format); - sample_spec.rate = device->default_sample_rate; + sample_spec.rate = output_device->sample_rate; sample_spec.channels = device->channel_layout.channel_count; pa_channel_map channel_map = to_pulseaudio_channel_map(&device->channel_layout); @@ -595,7 +595,7 @@ static int output_device_init_pa(SoundIo *soundio, pa_stream_set_write_callback(opd->stream, playback_stream_write_callback, output_device); pa_stream_set_underflow_callback(opd->stream, playback_stream_underflow_callback, output_device); - int bytes_per_second = output_device->bytes_per_frame * device->default_sample_rate; + int bytes_per_second = output_device->bytes_per_frame * output_device->sample_rate; int buffer_length = output_device->bytes_per_frame * ceil(output_device->latency * bytes_per_second / (double)output_device->bytes_per_frame); @@ -747,7 +747,7 @@ static int input_device_init_pa(SoundIo *soundio, pa_sample_spec sample_spec; sample_spec.format = to_pulseaudio_sample_format(input_device->sample_format); - sample_spec.rate = device->default_sample_rate; + sample_spec.rate = input_device->sample_rate; sample_spec.channels = device->channel_layout.channel_count; pa_channel_map channel_map = to_pulseaudio_channel_map(&device->channel_layout); @@ -764,7 +764,7 @@ static int input_device_init_pa(SoundIo *soundio, pa_stream_set_state_callback(stream, recording_stream_state_callback, input_device); pa_stream_set_read_callback(stream, recording_stream_read_callback, input_device); - int bytes_per_second = input_device->bytes_per_frame * device->default_sample_rate; + int bytes_per_second = input_device->bytes_per_frame * input_device->sample_rate; int buffer_length = input_device->bytes_per_frame * ceil(input_device->latency * bytes_per_second / (double)input_device->bytes_per_frame); diff --git a/src/soundio.cpp b/src/soundio.cpp index e2cbdbf..5c6c201 100644 --- a/src/soundio.cpp +++ b/src/soundio.cpp @@ -290,7 +290,7 @@ void soundio_output_device_write(struct SoundIoOutputDevice *output_device, int soundio_output_device_create(struct SoundIoDevice *device, - enum SoundIoSampleFormat sample_format, + enum SoundIoSampleFormat sample_format, int sample_rate, double latency, void *userdata, void (*write_callback)(struct SoundIoOutputDevice *, int frame_count), void (*underrun_callback)(struct SoundIoOutputDevice *), @@ -310,6 +310,7 @@ int soundio_output_device_create(struct SoundIoDevice *device, output_device->write_callback = write_callback; output_device->underrun_callback = underrun_callback; output_device->sample_format = sample_format; + output_device->sample_rate = sample_rate; output_device->latency = latency; output_device->bytes_per_frame = soundio_get_bytes_per_frame(sample_format, device->channel_layout.channel_count); @@ -344,7 +345,8 @@ int soundio_output_device_start(struct SoundIoOutputDevice *output_device) { } int soundio_input_device_create(struct SoundIoDevice *device, - enum SoundIoSampleFormat sample_format, double latency, void *userdata, + enum SoundIoSampleFormat sample_format, int sample_rate, + double latency, void *userdata, void (*read_callback)(struct SoundIoInputDevice *), struct SoundIoInputDevice **out_input_device) { @@ -362,6 +364,7 @@ int soundio_input_device_create(struct SoundIoDevice *device, sid->read_callback = read_callback; sid->sample_format = sample_format; sid->latency = latency; + sid->sample_rate = sample_rate; sid->bytes_per_frame = soundio_get_bytes_per_frame(sample_format, device->channel_layout.channel_count); diff --git a/src/soundio.h b/src/soundio.h index 87a2ae4..cfcc51d 100644 --- a/src/soundio.h +++ b/src/soundio.h @@ -115,9 +115,15 @@ struct SoundIoDevice { char *name; char *description; struct SoundIoChannelLayout channel_layout; + + // these values might not actually matter. audio hardware has a set of + // {sample format, sample rate} that they support. you can't know + // whether something worked until you try it. + // these values can be unknown enum SoundIoSampleFormat default_sample_format; - double default_latency; int default_sample_rate; + + double default_latency; enum SoundIoDevicePurpose purpose; int ref_count; bool is_raw; @@ -127,6 +133,7 @@ struct SoundIoOutputDevice { void *backend_data; struct SoundIoDevice *device; enum SoundIoSampleFormat sample_format; + int sample_rate; double latency; int bytes_per_frame; @@ -139,6 +146,7 @@ struct SoundIoInputDevice { void *backend_data; struct SoundIoDevice *device; enum SoundIoSampleFormat sample_format; + int sample_rate; double latency; int bytes_per_frame; @@ -283,7 +291,7 @@ enum SoundIoDevicePurpose soundio_device_purpose(const struct SoundIoDevice *dev // Output Devices int soundio_output_device_create(struct SoundIoDevice *device, - enum SoundIoSampleFormat sample_format, + enum SoundIoSampleFormat sample_format, int sample_rate, double latency, void *userdata, void (*write_callback)(struct SoundIoOutputDevice *, int frame_count), void (*underrun_callback)(struct SoundIoOutputDevice *), @@ -309,7 +317,8 @@ void soundio_output_device_clear_buffer(struct SoundIoOutputDevice *output_devic // Input Devices int soundio_input_device_create(struct SoundIoDevice *device, - enum SoundIoSampleFormat sample_format, double latency, void *userdata, + enum SoundIoSampleFormat sample_format, int sample_rate, + double latency, void *userdata, void (*read_callback)(struct SoundIoInputDevice *), struct SoundIoInputDevice **out_input_device); void soundio_input_device_destroy(struct SoundIoInputDevice *input_device); diff --git a/test/unit_tests.cpp b/test/unit_tests.cpp index 3d2b951..ec58133 100644 --- a/test/unit_tests.cpp +++ b/test/unit_tests.cpp @@ -37,7 +37,7 @@ static void test_create_output_device(void) { soundio_device_name(device); soundio_device_description(device); struct SoundIoOutputDevice *output_device; - soundio_output_device_create(device, SoundIoSampleFormatFloat, 0.1, NULL, + soundio_output_device_create(device, SoundIoSampleFormatFloat, 48000, 0.1, NULL, write_callback, underrun_callback, &output_device); soundio_output_device_destroy(output_device); soundio_device_unref(device);