From 18a37da84817d1c9e00017d6b711e8a5f0260f11 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 1 Jul 2015 01:29:35 -0700 Subject: [PATCH] implement getting devices --- example/list_devices.c | 12 +++---- src/dummy.cpp | 8 ++--- src/pulseaudio.cpp | 82 +++++++++++++++++++++--------------------- src/pulseaudio.hpp | 4 +-- src/soundio.cpp | 18 ++++++++++ src/soundio.h | 22 ++++++------ 6 files changed, 82 insertions(+), 64 deletions(-) diff --git a/example/list_devices.c b/example/list_devices.c index 045c8b0..4d190a6 100644 --- a/example/list_devices.c +++ b/example/list_devices.c @@ -29,20 +29,20 @@ static int list_devices(struct SoundIo *soundio) { struct SoundIoDevice *device = soundio_get_input_device(soundio, i); const char *purpose_str = "input"; const char *default_str = (i == default_input) ? " (default)" : ""; - const char *description = soundio_audio_device_description(device); - int sample_rate = soundio_audio_device_sample_rate(device); + const char *description = soundio_device_description(device); + int sample_rate = soundio_device_sample_rate(device); fprintf(stderr, "%s device: %d Hz %s%s\n", purpose_str, sample_rate, description, default_str); - soundio_audio_device_unref(device); + soundio_device_unref(device); } for (int i = 0; i < output_count; i += 1) { struct SoundIoDevice *device = soundio_get_output_device(soundio, i); const char *purpose_str = "output"; const char *default_str = (i == default_output) ? " (default)" : ""; - const char *description = soundio_audio_device_description(device); - int sample_rate = soundio_audio_device_sample_rate(device); + const char *description = soundio_device_description(device); + int sample_rate = soundio_device_sample_rate(device); fprintf(stderr, "%s device: %d Hz %s%s\n", purpose_str, sample_rate, description, default_str); - soundio_audio_device_unref(device); + soundio_device_unref(device); } fprintf(stderr, "%d devices found\n", input_count + output_count); diff --git a/src/dummy.cpp b/src/dummy.cpp index 02ae8aa..fa53e53 100644 --- a/src/dummy.cpp +++ b/src/dummy.cpp @@ -61,7 +61,7 @@ static void destroy_dummy(SoundIo *soundio) { static void flush_events(SoundIo *soundio) { } -static void refresh_audio_devices(SoundIo *soundio) { } +static void refresh_devices(SoundIo *soundio) { } static void output_device_destroy_dummy(SoundIo *soundio, SoundIoOutputDevice *output_device) @@ -233,7 +233,7 @@ int soundio_dummy_init(SoundIo *soundio) { device->purpose = SoundIoDevicePurposeOutput; if (soundio->safe_devices_info->output_devices.append(device)) { - soundio_audio_device_unref(device); + soundio_device_unref(device); return SoundIoErrorNoMem; } } @@ -260,7 +260,7 @@ int soundio_dummy_init(SoundIo *soundio) { device->purpose = SoundIoDevicePurposeInput; if (soundio->safe_devices_info->input_devices.append(device)) { - soundio_audio_device_unref(device); + soundio_device_unref(device); return SoundIoErrorNoMem; } } @@ -268,7 +268,7 @@ int soundio_dummy_init(SoundIo *soundio) { soundio->destroy = destroy_dummy; soundio->flush_events = flush_events; - soundio->refresh_audio_devices = refresh_audio_devices; + soundio->refresh_devices = refresh_devices; soundio->output_device_init = output_device_init_dummy; soundio->output_device_destroy = output_device_destroy_dummy; diff --git a/src/pulseaudio.cpp b/src/pulseaudio.cpp index 4768189..e6d060d 100644 --- a/src/pulseaudio.cpp +++ b/src/pulseaudio.cpp @@ -59,28 +59,28 @@ static void context_state_callback(pa_context *context, void *userdata) { } } -static void destroy_current_audio_devices_info(SoundIo *soundio) { +static void destroy_current_devices_info(SoundIo *soundio) { SoundIoPulseAudio *ah = (SoundIoPulseAudio *)soundio->backend_data; - if (ah->current_audio_devices_info) { - for (int i = 0; i < ah->current_audio_devices_info->input_devices.length; i += 1) - soundio_audio_device_unref(ah->current_audio_devices_info->input_devices.at(i)); - for (int i = 0; i < ah->current_audio_devices_info->output_devices.length; i += 1) - soundio_audio_device_unref(ah->current_audio_devices_info->output_devices.at(i)); + if (ah->current_devices_info) { + for (int i = 0; i < ah->current_devices_info->input_devices.length; i += 1) + soundio_device_unref(ah->current_devices_info->input_devices.at(i)); + for (int i = 0; i < ah->current_devices_info->output_devices.length; i += 1) + soundio_device_unref(ah->current_devices_info->output_devices.at(i)); - destroy(ah->current_audio_devices_info); - ah->current_audio_devices_info = nullptr; + destroy(ah->current_devices_info); + ah->current_devices_info = nullptr; } } -static void destroy_ready_audio_devices_info(SoundIo *soundio) { +static void destroy_ready_devices_info(SoundIo *soundio) { SoundIoPulseAudio *ah = (SoundIoPulseAudio *)soundio->backend_data; - if (ah->ready_audio_devices_info) { - for (int i = 0; i < ah->ready_audio_devices_info->input_devices.length; i += 1) - soundio_audio_device_unref(ah->ready_audio_devices_info->input_devices.at(i)); - for (int i = 0; i < ah->ready_audio_devices_info->output_devices.length; i += 1) - soundio_audio_device_unref(ah->ready_audio_devices_info->output_devices.at(i)); - destroy(ah->ready_audio_devices_info); - ah->ready_audio_devices_info = nullptr; + if (ah->ready_devices_info) { + for (int i = 0; i < ah->ready_devices_info->input_devices.length; i += 1) + soundio_device_unref(ah->ready_devices_info->input_devices.at(i)); + for (int i = 0; i < ah->ready_devices_info->output_devices.length; i += 1) + soundio_device_unref(ah->ready_devices_info->output_devices.at(i)); + destroy(ah->ready_devices_info); + ah->ready_devices_info = nullptr; } } @@ -90,8 +90,8 @@ static void destroy_pa(SoundIo *soundio) { if (ah->main_loop) pa_threaded_mainloop_stop(ah->main_loop); - destroy_current_audio_devices_info(soundio); - destroy_ready_audio_devices_info(soundio); + destroy_current_devices_info(soundio); + destroy_ready_devices_info(soundio); pa_context_disconnect(ah->pulse_context); pa_context_unref(ah->pulse_context); @@ -195,26 +195,26 @@ static void finish_device_query(SoundIo *soundio) { } // based on the default sink name, figure out the default output index - ah->current_audio_devices_info->default_output_index = -1; - ah->current_audio_devices_info->default_input_index = -1; - for (int i = 0; i < ah->current_audio_devices_info->input_devices.length; i += 1) { - SoundIoDevice *device = ah->current_audio_devices_info->input_devices.at(i); + ah->current_devices_info->default_output_index = -1; + ah->current_devices_info->default_input_index = -1; + for (int i = 0; i < ah->current_devices_info->input_devices.length; i += 1) { + SoundIoDevice *device = ah->current_devices_info->input_devices.at(i); assert(device->purpose == SoundIoDevicePurposeInput); if (strcmp(device->name, ah->default_source_name) == 0) { - ah->current_audio_devices_info->default_input_index = i; + ah->current_devices_info->default_input_index = i; } } - for (int i = 0; i < ah->current_audio_devices_info->output_devices.length; i += 1) { - SoundIoDevice *device = ah->current_audio_devices_info->output_devices.at(i); + for (int i = 0; i < ah->current_devices_info->output_devices.length; i += 1) { + SoundIoDevice *device = ah->current_devices_info->output_devices.at(i); assert(device->purpose == SoundIoDevicePurposeOutput); if (strcmp(device->name, ah->default_sink_name) == 0) { - ah->current_audio_devices_info->default_output_index = i; + ah->current_devices_info->default_output_index = i; } } - destroy_ready_audio_devices_info(soundio); - ah->ready_audio_devices_info = ah->current_audio_devices_info; - ah->current_audio_devices_info = NULL; + destroy_ready_devices_info(soundio); + ah->ready_devices_info = ah->current_devices_info; + ah->current_devices_info = NULL; ah->have_devices_flag = true; pa_threaded_mainloop_signal(ah->main_loop, 0); soundio->on_events_signal(soundio); @@ -243,7 +243,7 @@ static void sink_info_callback(pa_context *pulse_context, const pa_sink_info *in device->default_sample_rate = sample_rate_from_pulseaudio(info->sample_spec); device->purpose = SoundIoDevicePurposeOutput; - if (ah->current_audio_devices_info->output_devices.append(device)) + if (ah->current_devices_info->output_devices.append(device)) panic("out of memory"); } pa_threaded_mainloop_signal(ah->main_loop, 0); @@ -272,7 +272,7 @@ static void source_info_callback(pa_context *pulse_context, const pa_source_info device->default_sample_rate = sample_rate_from_pulseaudio(info->sample_spec); device->purpose = SoundIoDevicePurposeInput; - if (ah->current_audio_devices_info->input_devices.append(device)) + if (ah->current_devices_info->input_devices.append(device)) panic("out of memory"); } pa_threaded_mainloop_signal(ah->main_loop, 0); @@ -304,9 +304,9 @@ static void scan_devices(SoundIo *soundio) { ah->have_default_sink = false; ah->have_source_list = false; - destroy_current_audio_devices_info(soundio); - ah->current_audio_devices_info = create(); - if (!ah->current_audio_devices_info) + destroy_current_devices_info(soundio); + ah->current_devices_info = create(); + if (!ah->current_devices_info) panic("out of memory"); pa_threaded_mainloop_lock(ah->main_loop); @@ -343,10 +343,10 @@ static void flush_events(SoundIo *soundio) { pa_threaded_mainloop_lock(ah->main_loop); - if (ah->ready_audio_devices_info) { + if (ah->ready_devices_info) { old_devices_info = soundio->safe_devices_info; - soundio->safe_devices_info = ah->ready_audio_devices_info; - ah->ready_audio_devices_info = nullptr; + soundio->safe_devices_info = ah->ready_devices_info; + ah->ready_devices_info = nullptr; change = true; } @@ -357,9 +357,9 @@ static void flush_events(SoundIo *soundio) { if (old_devices_info) { for (int i = 0; i < old_devices_info->input_devices.length; i += 1) - soundio_audio_device_unref(old_devices_info->input_devices.at(i)); + soundio_device_unref(old_devices_info->input_devices.at(i)); for (int i = 0; i < old_devices_info->output_devices.length; i += 1) - soundio_audio_device_unref(old_devices_info->output_devices.at(i)); + soundio_device_unref(old_devices_info->output_devices.at(i)); destroy(old_devices_info); } } @@ -790,7 +790,7 @@ static void input_device_clear_buffer_pa(SoundIo *soundio, pa_threaded_mainloop_unlock(ah->main_loop); } -static void refresh_audio_devices(SoundIo *soundio) { +static void refresh_devices(SoundIo *soundio) { block_until_ready(soundio); soundio_flush_events(soundio); block_until_have_devices(soundio); @@ -850,7 +850,7 @@ int soundio_pulseaudio_init(SoundIo *soundio) { soundio->destroy = destroy_pa; soundio->flush_events = flush_events; - soundio->refresh_audio_devices = refresh_audio_devices; + soundio->refresh_devices = refresh_devices; soundio->output_device_init = output_device_init_pa; soundio->output_device_destroy = output_device_destroy_pa; diff --git a/src/pulseaudio.hpp b/src/pulseaudio.hpp index 258322f..9ca84a2 100644 --- a/src/pulseaudio.hpp +++ b/src/pulseaudio.hpp @@ -31,12 +31,12 @@ struct SoundIoPulseAudio { atomic_bool device_scan_queued; // the one that we're working on building - struct SoundIoDevicesInfo *current_audio_devices_info; + struct SoundIoDevicesInfo *current_devices_info; char * default_sink_name; char * default_source_name; // this one is ready to be read with flush_events. protected by mutex - struct SoundIoDevicesInfo *ready_audio_devices_info; + struct SoundIoDevicesInfo *ready_devices_info; bool have_sink_list; bool have_source_list; diff --git a/src/soundio.cpp b/src/soundio.cpp index 8fb8891..e89be11 100644 --- a/src/soundio.cpp +++ b/src/soundio.cpp @@ -120,3 +120,21 @@ int soundio_get_default_output_device_index(struct SoundIo *soundio) { assert(soundio->safe_devices_info); return soundio->safe_devices_info->default_output_index; } + +struct SoundIoDevice *soundio_get_input_device(struct SoundIo *soundio, int index) { + assert(soundio->safe_devices_info); + assert(index >= 0); + assert(index < soundio->safe_devices_info->input_devices.length); + SoundIoDevice *device = soundio->safe_devices_info->input_devices.at(index); + soundio_device_ref(device); + return device; +} + +struct SoundIoDevice *soundio_get_output_device(struct SoundIo *soundio, int index) { + assert(soundio->safe_devices_info); + assert(index >= 0); + assert(index < soundio->safe_devices_info->output_devices.length); + SoundIoDevice *device = soundio->safe_devices_info->output_devices.at(index); + soundio_device_ref(device); + return device; +} diff --git a/src/soundio.h b/src/soundio.h index 3cb3dba..4eadb99 100644 --- a/src/soundio.h +++ b/src/soundio.h @@ -156,7 +156,7 @@ struct SoundIo { void (*destroy)(struct SoundIo *); void (*flush_events)(struct SoundIo *); - void (*refresh_audio_devices)(struct SoundIo *); + void (*refresh_devices)(struct SoundIo *); int (*output_device_init)(struct SoundIo *, struct SoundIoOutputDevice *); void (*output_device_destroy)(struct SoundIo *, struct SoundIoOutputDevice *); @@ -244,7 +244,7 @@ int soundio_get_input_device_count(struct SoundIo *soundio); int soundio_get_output_device_count(struct SoundIo *soundio); // returns NULL on error -// call soundio_audio_device_unref when you no longer have a reference to the pointer. +// call soundio_device_unref when you no longer have a reference to the pointer. struct SoundIoDevice *soundio_get_input_device(struct SoundIo *soundio, int index); struct SoundIoDevice *soundio_get_output_device(struct SoundIo *soundio, int index); @@ -254,19 +254,19 @@ int soundio_get_default_input_device_index(struct SoundIo *soundio); // returns the index of the default output device, or -1 on error int soundio_get_default_output_device_index(struct SoundIo *soundio); -void soundio_audio_device_ref(struct SoundIoDevice *device); -void soundio_audio_device_unref(struct SoundIoDevice *device); +void soundio_device_ref(struct SoundIoDevice *device); +void soundio_device_unref(struct SoundIoDevice *device); // the name is the identifier for the device. UTF-8 encoded -const char *soundio_audio_device_name(const struct SoundIoDevice *device); +const char *soundio_device_name(const struct SoundIoDevice *device); // UTF-8 encoded -const char *soundio_audio_device_description(const struct SoundIoDevice *device); +const char *soundio_device_description(const struct SoundIoDevice *device); -const struct SoundIoChannelLayout *soundio_audio_device_channel_layout(const struct SoundIoDevice *device); -int soundio_audio_device_sample_rate(const struct SoundIoDevice *device); +const struct SoundIoChannelLayout *soundio_device_channel_layout(const struct SoundIoDevice *device); +int soundio_device_sample_rate(const struct SoundIoDevice *device); -bool soundio_audio_device_equal( +bool soundio_device_equal( const struct SoundIoDevice *a, const struct SoundIoDevice *b); enum SoundIoDevicePurpose soundio_device_purpose(const struct SoundIoDevice *device); @@ -275,7 +275,7 @@ enum SoundIoDevicePurpose soundio_device_purpose(const struct SoundIoDevice *dev // Output Devices -int soundio_output_device_create(struct SoundIoDevice *audio_device, +int soundio_output_device_create(struct SoundIoDevice *device, enum SoundIoSampleFormat sample_format, double latency, void *userdata, void (*write_callback)(struct SoundIoOutputDevice *, int), @@ -301,7 +301,7 @@ void soundio_output_device_clear_buffer(struct SoundIoOutputDevice *device); // Input Devices -int soundio_input_device_create(struct SoundIoDevice *audio_device, +int soundio_input_device_create(struct SoundIoDevice *device, enum SoundIoSampleFormat sample_format, double latency, void *userdata, void (*read_callback)(struct SoundIoOutputDevice *), struct SoundIoOutputDevice **out_input_device);