From d36493e1fee477e95b07ac6e76e60f55abde036b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 1 Jul 2015 01:20:26 -0700 Subject: [PATCH] implement getting audio device counts --- src/dummy.cpp | 19 +++++++++++---- src/dummy.hpp | 2 +- src/pulseaudio.cpp | 60 ++++++++++++++++++++++++++-------------------- src/soundio.cpp | 12 +++++++++- src/soundio.hpp | 3 ++- 5 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/dummy.cpp b/src/dummy.cpp index 166628b..02ae8aa 100644 --- a/src/dummy.cpp +++ b/src/dummy.cpp @@ -54,7 +54,10 @@ static void recording_thread_run(void *arg) { } */ -static void destroy_audio_hardware_dummy(SoundIo *soundio) { } +static void destroy_dummy(SoundIo *soundio) { + destroy(soundio->safe_devices_info); + soundio->safe_devices_info = nullptr; +} static void flush_events(SoundIo *soundio) { } @@ -197,8 +200,14 @@ static void input_device_clear_buffer_dummy(SoundIo *soundio, // TODO } -int audio_hardware_init_dummy(SoundIo *soundio) { +int soundio_dummy_init(SoundIo *soundio) { + assert(!soundio->safe_devices_info); soundio->safe_devices_info = create(); + if (!soundio->safe_devices_info) { + destroy_dummy(soundio); + return SoundIoErrorNoMem; + } + soundio->safe_devices_info->default_input_index = 0; soundio->safe_devices_info->default_output_index = 0; @@ -223,7 +232,7 @@ int audio_hardware_init_dummy(SoundIo *soundio) { device->default_sample_rate = 48000; device->purpose = SoundIoDevicePurposeOutput; - if (soundio->safe_devices_info->devices.append(device)) { + if (soundio->safe_devices_info->output_devices.append(device)) { soundio_audio_device_unref(device); return SoundIoErrorNoMem; } @@ -250,14 +259,14 @@ int audio_hardware_init_dummy(SoundIo *soundio) { device->default_sample_rate = 48000; device->purpose = SoundIoDevicePurposeInput; - if (soundio->safe_devices_info->devices.append(device)) { + if (soundio->safe_devices_info->input_devices.append(device)) { soundio_audio_device_unref(device); return SoundIoErrorNoMem; } } - soundio->destroy = destroy_audio_hardware_dummy; + soundio->destroy = destroy_dummy; soundio->flush_events = flush_events; soundio->refresh_audio_devices = refresh_audio_devices; diff --git a/src/dummy.hpp b/src/dummy.hpp index 9998bd6..5777976 100644 --- a/src/dummy.hpp +++ b/src/dummy.hpp @@ -24,7 +24,7 @@ struct SoundIoOutputDeviceDummy { }; struct SoundIoInputDeviceDummy { - + // TODO }; int soundio_dummy_init(struct SoundIo *soundio); diff --git a/src/pulseaudio.cpp b/src/pulseaudio.cpp index 35908c8..4768189 100644 --- a/src/pulseaudio.cpp +++ b/src/pulseaudio.cpp @@ -62,8 +62,10 @@ static void context_state_callback(pa_context *context, void *userdata) { static void destroy_current_audio_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->devices.length; i += 1) - soundio_audio_device_unref(ah->current_audio_devices_info->devices.at(i)); + 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)); destroy(ah->current_audio_devices_info); ah->current_audio_devices_info = nullptr; @@ -73,15 +75,17 @@ static void destroy_current_audio_devices_info(SoundIo *soundio) { static void destroy_ready_audio_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->devices.length; i += 1) - soundio_audio_device_unref(ah->ready_audio_devices_info->devices.at(i)); + 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; } } -static void destroy_audio_hardware_pa(SoundIo *soundio) { +static void destroy_pa(SoundIo *soundio) { SoundIoPulseAudio *ah = (SoundIoPulseAudio *)soundio->backend_data; if (ah->main_loop) pa_threaded_mainloop_stop(ah->main_loop); @@ -193,18 +197,20 @@ 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->devices.length; i += 1) { - SoundIoDevice *device = ah->current_audio_devices_info->devices.at(i); - if (device->purpose == SoundIoDevicePurposeOutput && - strcmp(device->name, ah->default_sink_name) == 0) - { - ah->current_audio_devices_info->default_output_index = i; - } else if (device->purpose == SoundIoDevicePurposeInput && - strcmp(device->name, ah->default_source_name) == 0) - { + 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); + assert(device->purpose == SoundIoDevicePurposeInput); + if (strcmp(device->name, ah->default_source_name) == 0) { ah->current_audio_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); + assert(device->purpose == SoundIoDevicePurposeOutput); + if (strcmp(device->name, ah->default_sink_name) == 0) { + ah->current_audio_devices_info->default_output_index = i; + } + } destroy_ready_audio_devices_info(soundio); ah->ready_audio_devices_info = ah->current_audio_devices_info; @@ -237,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->devices.append(device)) + if (ah->current_audio_devices_info->output_devices.append(device)) panic("out of memory"); } pa_threaded_mainloop_signal(ah->main_loop, 0); @@ -266,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->devices.append(device)) + if (ah->current_audio_devices_info->input_devices.append(device)) panic("out of memory"); } pa_threaded_mainloop_signal(ah->main_loop, 0); @@ -350,8 +356,10 @@ static void flush_events(SoundIo *soundio) { soundio->on_devices_change(soundio); if (old_devices_info) { - for (int i = 0; i < old_devices_info->devices.length; i += 1) - soundio_audio_device_unref(old_devices_info->devices.at(i)); + for (int i = 0; i < old_devices_info->input_devices.length; i += 1) + soundio_audio_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)); destroy(old_devices_info); } } @@ -788,7 +796,7 @@ static void refresh_audio_devices(SoundIo *soundio) { block_until_have_devices(soundio); } -int audio_hardware_init_pulseaudio(SoundIo *soundio) { +int soundio_pulseaudio_init(SoundIo *soundio) { SoundIoPulseAudio *ah = (SoundIoPulseAudio *)soundio->backend_data; ah->connection_refused = false; @@ -798,7 +806,7 @@ int audio_hardware_init_pulseaudio(SoundIo *soundio) { ah->main_loop = pa_threaded_mainloop_new(); if (!ah->main_loop) { - destroy_audio_hardware_pa(soundio); + destroy_pa(soundio); return SoundIoErrorNoMem; } @@ -806,7 +814,7 @@ int audio_hardware_init_pulseaudio(SoundIo *soundio) { ah->props = pa_proplist_new(); if (!ah->props) { - destroy_audio_hardware_pa(soundio); + destroy_pa(soundio); return SoundIoErrorNoMem; } @@ -817,7 +825,7 @@ int audio_hardware_init_pulseaudio(SoundIo *soundio) { ah->pulse_context = pa_context_new_with_proplist(main_loop_api, "SoundIo", ah->props); if (!ah->pulse_context) { - destroy_audio_hardware_pa(soundio); + destroy_pa(soundio); return SoundIoErrorNoMem; } @@ -826,21 +834,21 @@ int audio_hardware_init_pulseaudio(SoundIo *soundio) { int err = pa_context_connect(ah->pulse_context, NULL, (pa_context_flags_t)0, NULL); if (err) { - destroy_audio_hardware_pa(soundio); + destroy_pa(soundio); return SoundIoErrorInitAudioBackend; } if (ah->connection_refused) { - destroy_audio_hardware_pa(soundio); + destroy_pa(soundio); return SoundIoErrorInitAudioBackend; } if (pa_threaded_mainloop_start(ah->main_loop)) { - destroy_audio_hardware_pa(soundio); + destroy_pa(soundio); return SoundIoErrorNoMem; } - soundio->destroy = destroy_audio_hardware_pa; + soundio->destroy = destroy_pa; soundio->flush_events = flush_events; soundio->refresh_audio_devices = refresh_audio_devices; diff --git a/src/soundio.cpp b/src/soundio.cpp index 65bf3de..a572ad9 100644 --- a/src/soundio.cpp +++ b/src/soundio.cpp @@ -5,7 +5,7 @@ * See http://opensource.org/licenses/MIT */ -#include "soundio.h" +#include "soundio.hpp" #include "util.hpp" #include "dummy.hpp" #include "pulseaudio.hpp" @@ -472,3 +472,13 @@ void soundio_flush_events(struct SoundIo *soundio) { if (soundio->flush_events) soundio->flush_events(soundio); } + +int soundio_get_input_device_count(struct SoundIo *soundio) { + assert(soundio->safe_devices_info); + return soundio->safe_devices_info->input_devices.length; +} + +int soundio_get_output_device_count(struct SoundIo *soundio) { + assert(soundio->safe_devices_info); + return soundio->safe_devices_info->output_devices.length; +} diff --git a/src/soundio.hpp b/src/soundio.hpp index c8a78d7..a398d90 100644 --- a/src/soundio.hpp +++ b/src/soundio.hpp @@ -12,7 +12,8 @@ #include "list.hpp" struct SoundIoDevicesInfo { - SoundIoList devices; + SoundIoList input_devices; + SoundIoList output_devices; // can be -1 when default device is unknown int default_output_index; int default_input_index;