From 582e93c41d6073df5d5da871989c5fd581ea04b8 Mon Sep 17 00:00:00 2001 From: Jesper Nordenberg Date: Sun, 3 Nov 2024 15:09:27 +0100 Subject: [PATCH] Added supports_input/output methods to DeviceTrait (#920) Time to get list of WASAPI devices on my machine (5 devices in total) before change: Input devices: 12 ms, output devices: 187 ms After change: Input devices: 1 ms, output devices: 1 ms --- src/host/wasapi/device.rs | 8 ++++++++ src/platform/mod.rs | 18 ++++++++++++++++++ src/traits.rs | 30 ++++++++++++++++-------------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/host/wasapi/device.rs b/src/host/wasapi/device.rs index ac12ee8..d660aa8 100644 --- a/src/host/wasapi/device.rs +++ b/src/host/wasapi/device.rs @@ -58,6 +58,14 @@ impl DeviceTrait for Device { Device::name(self) } + fn supports_input(&self) -> bool { + self.data_flow() == Audio::eCapture + } + + fn supports_output(&self) -> bool { + self.data_flow() == Audio::eRender + } + fn supported_input_configs( &self, ) -> Result { diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 4cb4d5a..65d77ca 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -315,6 +315,24 @@ macro_rules! impl_platform_host { } } + fn supports_input(&self) -> bool { + match self.0 { + $( + $(#[cfg($feat)])? + DeviceInner::$HostVariant(ref d) => d.supports_input(), + )* + } + } + + fn supports_output(&self) -> bool { + match self.0 { + $( + $(#[cfg($feat)])? + DeviceInner::$HostVariant(ref d) => d.supports_output(), + )* + } + } + fn supported_input_configs(&self) -> Result { match self.0 { $( diff --git a/src/traits.rs b/src/traits.rs index 6f5ff45..2f1bd34 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -59,13 +59,7 @@ pub trait HostTrait { /// /// Can be empty if the system does not support audio input. fn input_devices(&self) -> Result, DevicesError> { - fn supports_input(device: &D) -> bool { - device - .supported_input_configs() - .map(|mut iter| iter.next().is_some()) - .unwrap_or(false) - } - Ok(self.devices()?.filter(supports_input::)) + Ok(self.devices()?.filter(DeviceTrait::supports_input)) } /// An iterator yielding all `Device`s currently available to the system that support one or more @@ -73,13 +67,7 @@ pub trait HostTrait { /// /// Can be empty if the system does not support audio output. fn output_devices(&self) -> Result, DevicesError> { - fn supports_output(device: &D) -> bool { - device - .supported_output_configs() - .map(|mut iter| iter.next().is_some()) - .unwrap_or(false) - } - Ok(self.devices()?.filter(supports_output::)) + Ok(self.devices()?.filter(DeviceTrait::supports_output)) } } @@ -101,6 +89,20 @@ pub trait DeviceTrait { /// The human-readable name of the device. fn name(&self) -> Result; + /// True if the device supports audio input, otherwise false + fn supports_input(&self) -> bool { + self.supported_input_configs() + .map(|mut iter| iter.next().is_some()) + .unwrap_or(false) + } + + /// True if the device supports audio output, otherwise false + fn supports_output(&self) -> bool { + self.supported_output_configs() + .map(|mut iter| iter.next().is_some()) + .unwrap_or(false) + } + /// An iterator yielding formats that are supported by the backend. /// /// Can return an error if the device is no longer valid (e.g. it has been disconnected).