From ba65bdb131fae63f3699a9db6fb69652f1d6995f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 12 Aug 2015 18:57:34 -0700 Subject: [PATCH] WASAPI: shared mode devices support any sample rate --- README.md | 3 +-- soundio/soundio.h | 1 + src/dummy.cpp | 4 ++-- src/pulseaudio.cpp | 4 ++-- src/soundio.hpp | 2 ++ src/wasapi.cpp | 8 +++++--- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c8d60c1..7b42460 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ behavior on every platform. * Supports optimal usage of each supported backend. The same API does the right thing whether the backend has a fixed buffer size, such as on JACK and CoreAudio, or whether it allows directly managing the buffer, such as on - ALSA or PulseAudio. + ALSA, PulseAudio, and WASAPI. * C library. Depends only on the respective backend API libraries and libc. Does *not* depend on libstdc++, and does *not* have exceptions, run-time type information, or [setjmp](http://latentcontent.net/2007/12/05/libpng-worst-api-ever/). @@ -251,7 +251,6 @@ view `coverage/index.html` in a browser. 0. implement WASAPI (Windows) backend, get examples working - list devices - - buffer duration - raw mode - watching - sine wave diff --git a/soundio/soundio.h b/soundio/soundio.h index bdffd50..dc7e9f5 100644 --- a/soundio/soundio.h +++ b/soundio/soundio.h @@ -358,6 +358,7 @@ struct SoundIoDevice { // are used. You may check that the current backend is PulseAudio and // ignore these min/max values. // For JACK and CoreAudio, buffer duration and period duration are the same. + // For WASAPI, buffer duration is unknown. double buffer_duration_min; double buffer_duration_max; double buffer_duration_current; diff --git a/src/dummy.cpp b/src/dummy.cpp index 51a25ac..d0c83f4 100644 --- a/src/dummy.cpp +++ b/src/dummy.cpp @@ -367,8 +367,8 @@ static void set_all_device_sample_rates(SoundIoDevice *device) { SoundIoDevicePrivate *dev = (SoundIoDevicePrivate *)device; device->sample_rate_count = 1; device->sample_rates = &dev->prealloc_sample_rate_range; - device->sample_rates[0].min = 8000; - device->sample_rates[0].max = 5644800; + device->sample_rates[0].min = SOUNDIO_MIN_SAMPLE_RATE; + device->sample_rates[0].max = SOUNDIO_MAX_SAMPLE_RATE; } static int set_all_device_channel_layouts(SoundIoDevice *device) { diff --git a/src/pulseaudio.cpp b/src/pulseaudio.cpp index 371bbc4..db10c4f 100644 --- a/src/pulseaudio.cpp +++ b/src/pulseaudio.cpp @@ -296,8 +296,8 @@ static void sink_info_callback(pa_context *pulse_context, const pa_sink_info *in // some reasonable min and max values. device->sample_rate_count = 1; device->sample_rates = &dev->prealloc_sample_rate_range; - device->sample_rates[0].min = min(8000, device->sample_rate_current); - device->sample_rates[0].max = max(5644800, device->sample_rate_current); + device->sample_rates[0].min = min(SOUNDIO_MIN_SAMPLE_RATE, device->sample_rate_current); + device->sample_rates[0].max = max(SOUNDIO_MAX_SAMPLE_RATE, device->sample_rate_current); device->current_format = from_pulseaudio_format(info->sample_spec); // PulseAudio performs sample format conversion, so any PulseAudio diff --git a/src/soundio.hpp b/src/soundio.hpp index 88908c3..40c6ea4 100644 --- a/src/soundio.hpp +++ b/src/soundio.hpp @@ -169,5 +169,7 @@ struct SoundIoDevicePrivate { void soundio_destroy_devices_info(struct SoundIoDevicesInfo *devices_info); +static const int SOUNDIO_MIN_SAMPLE_RATE = 8000; +static const int SOUNDIO_MAX_SAMPLE_RATE = 5644800; #endif diff --git a/src/wasapi.cpp b/src/wasapi.cpp index bf693e2..de48dc7 100644 --- a/src/wasapi.cpp +++ b/src/wasapi.cpp @@ -353,11 +353,13 @@ static int refresh_devices(SoundIoPrivate *si) { deinit_refresh_devices(&rd); return SoundIoErrorOpeningDevice; } + rd.device->sample_rate_current = rd.wave_format->Format.nSamplesPerSec; + // WASAPI performs resampling in shared mode, so any value is valid. + // Let's pick some reasonable min and max values. rd.device->sample_rate_count = 1; rd.device->sample_rates = &dev->prealloc_sample_rate_range; - rd.device->sample_rate_current = rd.wave_format->Format.nSamplesPerSec; - rd.device->sample_rates[0].min = rd.device->sample_rate_current; - rd.device->sample_rates[0].max = rd.device->sample_rate_current; + rd.device->sample_rates[0].min = min(SOUNDIO_MIN_SAMPLE_RATE, rd.device->sample_rate_current); + rd.device->sample_rates[0].max = max(SOUNDIO_MAX_SAMPLE_RATE, rd.device->sample_rate_current); rd.device->current_format = from_wave_format_format(rd.wave_format); rd.device->format_count = 1;