diff --git a/example/sio_list_devices.c b/example/sio_list_devices.c index fc1f8a7..72a7f23 100644 --- a/example/sio_list_devices.c +++ b/example/sio_list_devices.c @@ -14,7 +14,7 @@ // list or keep a watch on audio devices static int usage(char *exe) { - fprintf(stderr, "Usage: %s [--watch] [--dummy] [--alsa] [--pulseaudio] [--jack]\n", exe); + fprintf(stderr, "Usage: %s [--watch] [--backend dummy|alsa|pulseaudio|jack|coreaudio|wasapi]\n", exe); return 1; } @@ -114,14 +114,30 @@ int main(int argc, char **argv) { char *arg = argv[i]; if (strcmp("--watch", arg) == 0) { watch = true; - } else if (strcmp("--dummy", arg) == 0) { - backend = SoundIoBackendDummy; - } else if (strcmp("--alsa", arg) == 0) { - backend = SoundIoBackendAlsa; - } else if (strcmp("--pulseaudio", arg) == 0) { - backend = SoundIoBackendPulseAudio; - } else if (strcmp("--jack", arg) == 0) { - backend = SoundIoBackendJack; + } else if (arg[0] == '-' && arg[1] == '-') { + i += 1; + if (i >= argc) { + return usage(exe); + } else if (strcmp(arg, "--backend") == 0) { + if (strcmp("-dummy", argv[i]) == 0) { + backend = SoundIoBackendDummy; + } else if (strcmp("alsa", argv[i]) == 0) { + backend = SoundIoBackendAlsa; + } else if (strcmp("pulseaudio", argv[i]) == 0) { + backend = SoundIoBackendPulseAudio; + } else if (strcmp("jack", argv[i]) == 0) { + backend = SoundIoBackendJack; + } else if (strcmp("coreaudio", argv[i]) == 0) { + backend = SoundIoBackendCoreAudio; + } else if (strcmp("wasapi", argv[i]) == 0) { + backend = SoundIoBackendWasapi; + } else { + fprintf(stderr, "Invalid backend: %s\n", argv[i]); + return 1; + } + } else { + return usage(exe); + } } else { return usage(exe); } diff --git a/src/soundio.cpp b/src/soundio.cpp index f3cea3e..cafab58 100644 --- a/src/soundio.cpp +++ b/src/soundio.cpp @@ -375,8 +375,12 @@ void soundio_device_unref(struct SoundIoDevice *device) { free(device->id); free(device->name); - if (device->sample_rates != &dev->prealloc_sample_rate_range) + if (device->sample_rates != &dev->prealloc_sample_rate_range && + device->sample_rates != dev->sample_rates.items) + { free(device->sample_rates); + } + dev->sample_rates.deinit(); if (device->formats != &dev->prealloc_format) free(device->formats); diff --git a/src/soundio.hpp b/src/soundio.hpp index 7378d93..a894ebd 100644 --- a/src/soundio.hpp +++ b/src/soundio.hpp @@ -164,6 +164,7 @@ struct SoundIoDevicePrivate { SoundIoDeviceBackendData backend_data; void (*destruct)(SoundIoDevicePrivate *); SoundIoSampleRateRange prealloc_sample_rate_range; + SoundIoList sample_rates; SoundIoFormat prealloc_format; }; diff --git a/src/wasapi.cpp b/src/wasapi.cpp index 49bb14b..f389477 100644 --- a/src/wasapi.cpp +++ b/src/wasapi.cpp @@ -295,7 +295,6 @@ static void destruct_device(SoundIoDevicePrivate *dev) { SoundIoDeviceWasapi *dw = &dev->backend_data.wasapi; if (dw->mm_device) IMMDevice_Release(dw->mm_device); - dw->sample_rates.deinit(); } struct RefreshDevices { @@ -438,7 +437,6 @@ static int add_sample_rate(SoundIoList *sample_rates, in static int do_sample_rate_test(RefreshDevices *rd, SoundIoDevicePrivate *dev, WAVEFORMATEXTENSIBLE *wave_format, int test_sample_rate, AUDCLNT_SHAREMODE share_mode, int *current_min, int *last_success_rate) { - SoundIoDeviceWasapi *dw = &dev->backend_data.wasapi; WAVEFORMATEX *closest_match = nullptr; int err; @@ -456,7 +454,7 @@ static int do_sample_rate_test(RefreshDevices *rd, SoundIoDevicePrivate *dev, WA *last_success_rate = test_sample_rate; } else if (hr == AUDCLNT_E_UNSUPPORTED_FORMAT || hr == S_FALSE || hr == E_INVALIDARG) { if (*current_min != -1) { - if ((err = add_sample_rate(&dw->sample_rates, current_min, *last_success_rate))) + if ((err = add_sample_rate(&dev->sample_rates, current_min, *last_success_rate))) return err; *current_min = -1; } @@ -470,12 +468,11 @@ static int do_sample_rate_test(RefreshDevices *rd, SoundIoDevicePrivate *dev, WA static int detect_valid_sample_rates(RefreshDevices *rd, WAVEFORMATEXTENSIBLE *wave_format, SoundIoDevicePrivate *dev, AUDCLNT_SHAREMODE share_mode) { - SoundIoDeviceWasapi *dw = &dev->backend_data.wasapi; int err; DWORD orig_sample_rate = wave_format->Format.nSamplesPerSec; - assert(dw->sample_rates.length == 0); + assert(dev->sample_rates.length == 0); int current_min = -1; int last_success_rate = -1; @@ -492,7 +489,7 @@ static int detect_valid_sample_rates(RefreshDevices *rd, WAVEFORMATEXTENSIBLE *w } if (current_min != -1) { - if ((err = add_sample_rate(&dw->sample_rates, ¤t_min, last_success_rate))) { + if ((err = add_sample_rate(&dev->sample_rates, ¤t_min, last_success_rate))) { wave_format->Format.nSamplesPerSec = orig_sample_rate; return err; } @@ -500,8 +497,8 @@ static int detect_valid_sample_rates(RefreshDevices *rd, WAVEFORMATEXTENSIBLE *w SoundIoDevice *device = &dev->pub; - device->sample_rate_count = dw->sample_rates.length; - device->sample_rates = dw->sample_rates.items; + device->sample_rate_count = dev->sample_rates.length; + device->sample_rates = dev->sample_rates.items; wave_format->Format.nSamplesPerSec = orig_sample_rate; return 0; diff --git a/src/wasapi.hpp b/src/wasapi.hpp index 003ed47..910ed19 100644 --- a/src/wasapi.hpp +++ b/src/wasapi.hpp @@ -27,7 +27,6 @@ int soundio_wasapi_init(struct SoundIoPrivate *si); struct SoundIoDeviceWasapi { - SoundIoList sample_rates; double period_duration; IMMDevice *mm_device; };