Skip to main content

DeviceTrait

Trait DeviceTrait 

Source
pub trait DeviceTrait {
    type SupportedInputConfigs: Iterator<Item = SupportedStreamConfigRange>;
    type SupportedOutputConfigs: Iterator<Item = SupportedStreamConfigRange>;
    type Stream: StreamTrait;

Show 13 methods // Required methods fn description(&self) -> Result<DeviceDescription, DeviceNameError>; fn id(&self) -> Result<DeviceId, DeviceIdError>; fn supported_input_configs( &self, ) -> Result<Self::SupportedInputConfigs, SupportedStreamConfigsError>; fn supported_output_configs( &self, ) -> Result<Self::SupportedOutputConfigs, SupportedStreamConfigsError>; fn default_input_config( &self, ) -> Result<SupportedStreamConfig, DefaultStreamConfigError>; fn default_output_config( &self, ) -> Result<SupportedStreamConfig, DefaultStreamConfigError>; fn build_input_stream_raw<D, E>( &self, config: &StreamConfig, sample_format: SampleFormat, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError> where D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static; fn build_output_stream_raw<D, E>( &self, config: &StreamConfig, sample_format: SampleFormat, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError> where D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static; // Provided methods fn name(&self) -> Result<String, DeviceNameError> { ... } fn supports_input(&self) -> bool { ... } fn supports_output(&self) -> bool { ... } fn build_input_stream<T, D, E>( &self, config: &StreamConfig, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError> where T: SizedSample, D: FnMut(&[T], &InputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static { ... } fn build_output_stream<T, D, E>( &self, config: &StreamConfig, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError> where T: SizedSample, D: FnMut(&mut [T], &OutputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static { ... }
}
Expand description

A device that is capable of audio input and/or output.

Please note that Devices may become invalid if they get disconnected. Therefore, all the methods that involve a device return a Result allowing the user to handle this case.

Required Associated Types§

Source

type SupportedInputConfigs: Iterator<Item = SupportedStreamConfigRange>

The iterator type yielding supported input stream formats.

Source

type SupportedOutputConfigs: Iterator<Item = SupportedStreamConfigRange>

The iterator type yielding supported output stream formats.

Source

type Stream: StreamTrait

The stream type created by build_input_stream_raw and build_output_stream_raw.

Required Methods§

Source

fn description(&self) -> Result<DeviceDescription, DeviceNameError>

Structured description of the device with metadata.

This returns a DeviceDescription containing structured information about the device, including name, manufacturer (if available), device type, bus type, and other platform-specific metadata.

For simple string representation, use device.description().to_string() or device.description().name().

Source

fn id(&self) -> Result<DeviceId, DeviceIdError>

The ID of the device.

This ID uniquely identifies the device on the host. It should be stable across program runs, device disconnections, and system reboots where possible.

Source

fn supported_input_configs( &self, ) -> Result<Self::SupportedInputConfigs, SupportedStreamConfigsError>

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).

Source

fn supported_output_configs( &self, ) -> Result<Self::SupportedOutputConfigs, SupportedStreamConfigsError>

An iterator yielding output stream formats that are supported by the device.

Can return an error if the device is no longer valid (e.g. it has been disconnected).

Source

fn default_input_config( &self, ) -> Result<SupportedStreamConfig, DefaultStreamConfigError>

The default input stream format for the device.

Source

fn default_output_config( &self, ) -> Result<SupportedStreamConfig, DefaultStreamConfigError>

The default output stream format for the device.

Source

fn build_input_stream_raw<D, E>( &self, config: &StreamConfig, sample_format: SampleFormat, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError>
where D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static,

Create a dynamically typed input stream.

This method allows working with sample data as raw bytes, useful when the sample format is determined at runtime. For compile-time known formats, prefer build_input_stream.

§Parameters
  • config - The stream configuration including sample rate, channels, and buffer size.
  • sample_format - The sample format of the audio data.
  • data_callback - Called periodically with captured audio data as a Data buffer.
  • error_callback - Called when a stream error occurs (e.g., device disconnected).
  • timeout - Optional timeout for backend operations. None indicates blocking behavior, Some(duration) sets a maximum wait time. Not all backends support timeouts.
Source

fn build_output_stream_raw<D, E>( &self, config: &StreamConfig, sample_format: SampleFormat, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError>
where D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static,

Create a dynamically typed output stream.

This method allows working with sample data as raw bytes, useful when the sample format is determined at runtime. For compile-time known formats, prefer build_output_stream.

§Parameters
  • config - The stream configuration including sample rate, channels, and buffer size.
  • sample_format - The sample format of the audio data.
  • data_callback - Called periodically to fill the output buffer with audio data as a mutable Data buffer.
  • error_callback - Called when a stream error occurs (e.g., device disconnected).
  • timeout - Optional timeout for backend operations. None indicates blocking behavior, Some(duration) sets a maximum wait time. Not all backends support timeouts.

Provided Methods§

Source

fn name(&self) -> Result<String, DeviceNameError>

👎Deprecated since 0.17.0:

Use description() for comprehensive device information including name, manufacturer, and device type. Use id() for a unique, stable device identifier that persists across reboots and reconnections.

The human-readable name of the device.

Source

fn supports_input(&self) -> bool

True if the device supports audio input, otherwise false

Source

fn supports_output(&self) -> bool

True if the device supports audio output, otherwise false

Source

fn build_input_stream<T, D, E>( &self, config: &StreamConfig, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError>
where T: SizedSample, D: FnMut(&[T], &InputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static,

Create an input stream.

§Parameters
  • config - The stream configuration including sample rate, channels, and buffer size.
  • data_callback - Called periodically with captured audio data. The callback receives a slice of samples in the format T and timing information.
  • error_callback - Called when a stream error occurs (e.g., device disconnected).
  • timeout - Optional timeout for backend operations. None indicates blocking behavior, Some(duration) sets a maximum wait time. Not all backends support timeouts.
Source

fn build_output_stream<T, D, E>( &self, config: &StreamConfig, data_callback: D, error_callback: E, timeout: Option<Duration>, ) -> Result<Self::Stream, BuildStreamError>
where T: SizedSample, D: FnMut(&mut [T], &OutputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static,

Create an output stream.

§Parameters
  • config - The stream configuration including sample rate, channels, and buffer size.
  • data_callback - Called periodically to fill the output buffer. The callback receives a mutable slice of samples in the format T to be filled with audio data, along with timing information.
  • error_callback - Called when a stream error occurs (e.g., device disconnected).
  • timeout - Optional timeout for backend operations. None indicates blocking behavior, Some(duration) sets a maximum wait time. Not all backends support timeouts.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§