Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
422 changes: 189 additions & 233 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/error_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_error_callback(move |err| {
// Filter for where err is an actionable error.
if matches!(
err,
cpal::StreamError::DeviceNotAvailable | cpal::StreamError::StreamInvalidated
err.kind(),
cpal::ErrorKind::DeviceNotAvailable | cpal::ErrorKind::StreamInvalidated
) {
if let Err(e) = tx.send(err) {
eprintln!("Error emitting StreamError: {e}");
eprintln!("Error emitting stream error: {e}");
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@

#[cfg(feature = "playback")]
pub use cpal::{
self, traits::DeviceTrait, Device, Devices, DevicesError, InputDevices, OutputDevices,
SupportedStreamConfig,
self, traits::DeviceTrait, Device, Devices, Error as CpalError, ErrorKind as CpalErrorKind,
InputDevices, OutputDevices, SupportedStreamConfig,
};

mod common;
Expand Down
8 changes: 4 additions & 4 deletions src/microphone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ use rtrb::RingBuffer;
/// Error that can occur when we can not list the input devices
#[derive(Debug, thiserror::Error, Clone)]
#[error("Could not list input devices")]
pub struct ListError(#[source] cpal::DevicesError);
pub struct ListError(#[source] cpal::Error);
assert_error_traits! {ListError}

/// An input device
Expand Down Expand Up @@ -248,21 +248,21 @@ impl Iterator for Microphone {
pub enum OpenError {
/// Failed to build the input stream.
#[error("Could not open microphone")]
BuildStream(#[source] cpal::BuildStreamError),
BuildStream(#[source] cpal::Error),
/// This is a bug please report it
#[error("This is a bug, please report it")]
UnsupportedSampleFormat,
/// Failed to start the input stream.
#[error("Could not start the input stream")]
Play(#[source] cpal::PlayStreamError),
Play(#[source] cpal::Error),
}
assert_error_traits! {OpenError}

impl Microphone {
fn open(
device: Device,
config: InputConfig,
mut error_callback: impl FnMut(cpal::StreamError) + Send + 'static,
mut error_callback: impl FnMut(cpal::Error) + Send + 'static,
) -> Result<Self, OpenError> {
let timeout = Some(Duration::from_millis(100));
let hundred_ms_of_samples =
Expand Down
26 changes: 13 additions & 13 deletions src/microphone/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pub enum Error {
#[error("Could not get default input configuration for input device: '{device_name}'")]
DefaultInputConfig {
#[source]
source: cpal::DefaultStreamConfigError,
source: cpal::Error,
device_name: String,
},
/// Failed to get the supported input configurations for the device.
#[error("Could not get supported input configurations for input device: '{device_name}'")]
InputConfigs {
#[source]
source: cpal::SupportedStreamConfigsError,
source: cpal::Error,
device_name: String,
},
/// The requested input configuration is not supported by the device.
Expand All @@ -56,9 +56,9 @@ pub struct DeviceNotSet;

/// Builder for configuring and opening microphone input streams.
#[must_use]
pub struct MicrophoneBuilder<Device, Config, E = fn(cpal::StreamError)>
pub struct MicrophoneBuilder<Device, Config, E = fn(cpal::Error)>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
device: Option<(cpal::Device, Vec<SupportedStreamConfigRange>)>,
config: Option<super::config::InputConfig>,
Expand All @@ -70,7 +70,7 @@ where

impl<Device, Config, E> Debug for MicrophoneBuilder<Device, Config, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MicrophoneBuilder")
Expand Down Expand Up @@ -100,28 +100,28 @@ impl Default for MicrophoneBuilder<DeviceNotSet, ConfigNotSet> {
}
}

fn default_error_callback(err: cpal::StreamError) {
fn default_error_callback(err: cpal::Error) {
#[cfg(feature = "tracing")]
tracing::error!("audio stream error: {err}");
#[cfg(not(feature = "tracing"))]
eprintln!("audio stream error: {err}");
}

impl MicrophoneBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::StreamError)> {
impl MicrophoneBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::Error)> {
/// Creates a new microphone builder.
///
/// # Example
/// ```no_run
/// let builder = rodio::microphone::MicrophoneBuilder::new();
/// ```
pub fn new() -> MicrophoneBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::StreamError)> {
pub fn new() -> MicrophoneBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::Error)> {
Self::default()
}
}

impl<Device, Config, E> MicrophoneBuilder<Device, Config, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Sets the input device to use.
///
Expand Down Expand Up @@ -190,7 +190,7 @@ where

impl<Config, E> MicrophoneBuilder<DeviceIsSet, Config, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Uses the device's default input configuration.
///
Expand Down Expand Up @@ -287,7 +287,7 @@ where

impl<E> MicrophoneBuilder<DeviceIsSet, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Sets the sample rate for input.
///
Expand Down Expand Up @@ -509,7 +509,7 @@ where

impl<Device, E> MicrophoneBuilder<Device, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Returns the current input configuration.
///
Expand All @@ -531,7 +531,7 @@ where

impl<E> MicrophoneBuilder<DeviceIsSet, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Opens the microphone input stream.
///
Expand Down
2 changes: 1 addition & 1 deletion src/speakers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub use config::{BufferSize, OutputConfig};
/// Error that can occur when we can not list the output devices
#[derive(Debug, thiserror::Error, Clone)]
#[error("Could not list output devices")]
pub struct ListError(#[source] cpal::DevicesError);
pub struct ListError(#[source] cpal::Error);
assert_error_traits! {ListError}

/// An output device
Expand Down
26 changes: 13 additions & 13 deletions src/speakers/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ pub enum Error {
#[error("Could not get default output configuration for output device: '{device_name}'")]
DefaultOutputConfig {
#[source]
source: cpal::DefaultStreamConfigError,
source: cpal::Error,
device_name: String,
},
/// Failed to get the supported output configurations for the device.
#[error("Could not get supported output configurations for output device: '{device_name}'")]
OutputConfigs {
#[source]
source: cpal::SupportedStreamConfigsError,
source: cpal::Error,
device_name: String,
},
/// The requested output configuration is not supported by the device.
Expand All @@ -59,9 +59,9 @@ pub struct DeviceNotSet;

/// Builder for configuring and opening an OS-Sink, usually a speaker or headphone.
#[must_use]
pub struct SpeakersBuilder<Device, Config, E = fn(cpal::StreamError)>
pub struct SpeakersBuilder<Device, Config, E = fn(cpal::Error)>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
device: Option<(cpal::Device, Vec<SupportedStreamConfigRange>)>,
config: Option<super::config::OutputConfig>,
Expand All @@ -73,7 +73,7 @@ where

impl<Device, Config, E> Debug for SpeakersBuilder<Device, Config, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SpeakersBuilder")
Expand Down Expand Up @@ -102,28 +102,28 @@ impl Default for SpeakersBuilder<DeviceNotSet, ConfigNotSet> {
}
}

fn default_error_callback(err: cpal::StreamError) {
fn default_error_callback(err: cpal::Error) {
#[cfg(feature = "tracing")]
tracing::error!("audio stream error: {err}");
#[cfg(not(feature = "tracing"))]
eprintln!("audio stream error: {err}");
}

impl SpeakersBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::StreamError)> {
impl SpeakersBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::Error)> {
/// Creates a new speakers builder.
///
/// # Example
/// ```no_run
/// let builder = rodio::speakers::SpeakersBuilder::new();
/// ```
pub fn new() -> SpeakersBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::StreamError)> {
pub fn new() -> SpeakersBuilder<DeviceNotSet, ConfigNotSet, fn(cpal::Error)> {
Self::default()
}
}

impl<Device, Config, E> SpeakersBuilder<Device, Config, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Sets the output device to use.
///
Expand Down Expand Up @@ -190,7 +190,7 @@ where

impl<Config, E> SpeakersBuilder<DeviceIsSet, Config, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Uses the device's default output configuration.
///
Expand Down Expand Up @@ -285,7 +285,7 @@ where

impl<E> SpeakersBuilder<DeviceIsSet, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Sets the sample rate for output.
///
Expand Down Expand Up @@ -511,7 +511,7 @@ where

impl<Device, E> SpeakersBuilder<Device, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Returns the current output configuration.
///
Expand All @@ -533,7 +533,7 @@ where

impl<E> SpeakersBuilder<DeviceIsSet, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Opens the OS-Sink and provide a mixer for playing sources on it.
///
Expand Down
2 changes: 1 addition & 1 deletion src/speakers/builder/buffer_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{ConfigIsSet, DeviceIsSet};

impl<E> SpeakersBuilder<DeviceIsSet, ConfigIsSet, E>
where
E: FnMut(cpal::StreamError) + Send + Clone + 'static,
E: FnMut(cpal::Error) + Send + Clone + 'static,
{
/// Sets the buffer duration for the output. The buffer size is calculated
/// from this and the sample rate and channel count when we build the
Expand Down
33 changes: 15 additions & 18 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl core::fmt::Debug for DeviceSinkBuilder {
}
}

fn default_error_callback(err: cpal::StreamError) {
fn default_error_callback(err: cpal::Error) {
#[cfg(feature = "tracing")]
tracing::error!("audio stream error: {err}");
#[cfg(not(feature = "tracing"))]
Expand All @@ -190,9 +190,9 @@ fn default_error_callback(err: cpal::StreamError) {
///
/// <div class="warning">When the DeviceSink is dropped playback will end, and the associated
/// OS-Sink will be disposed</div>
pub struct DeviceSinkBuilder<E = fn(cpal::StreamError)>
pub struct DeviceSinkBuilder<E = fn(cpal::Error)>
where
E: FnMut(cpal::StreamError) + Send + 'static,
E: FnMut(cpal::Error) + Send + 'static,
{
device: Option<cpal::Device>,
config: DeviceSinkConfig,
Expand Down Expand Up @@ -278,7 +278,7 @@ impl DeviceSinkBuilder {

impl<E> DeviceSinkBuilder<E>
where
E: FnMut(cpal::StreamError) + Send + 'static,
E: FnMut(cpal::Error) + Send + 'static,
{
/// Sets output audio device keeping all existing stream parameters intact.
/// This method is useful if you want to set other parameters yourself.
Expand Down Expand Up @@ -383,7 +383,7 @@ where
/// Set a callback that will be called when an error occurs with the stream
pub fn with_error_callback<F>(self, callback: F) -> DeviceSinkBuilder<F>
where
F: FnMut(cpal::StreamError) + Send + 'static,
F: FnMut(cpal::Error) + Send + 'static,
{
DeviceSinkBuilder {
device: self.device,
Expand Down Expand Up @@ -470,21 +470,18 @@ assert_error_traits!(PlayError);
/// Errors that might occur when interfacing with audio output.
#[derive(Debug, thiserror::Error)]
pub enum DeviceSinkError {
/// Could not start playing the sink, see [cpal::PlayStreamError] for
/// details.
/// Could not start playing the sink.
#[error("Could not start playing the stream")]
PlayError(#[source] cpal::PlayStreamError),
/// Failed to get the stream config for the given device. See
/// [cpal::DefaultStreamConfigError] for details.
PlayError(#[source] cpal::Error),
/// Failed to get the stream config for the given device.
#[error("Failed to get the config for the given device")]
DefaultSinkConfigError(#[source] cpal::DefaultStreamConfigError),
/// Error opening sink with OS. See [cpal::BuildStreamError] for details.
DefaultSinkConfigError(#[source] cpal::Error),
/// Error opening sink with OS.
#[error("Error opening the stream with the OS")]
BuildError(#[source] cpal::BuildStreamError),
/// Could not list supported configs for the device. Maybe it
/// disconnected. For details see: [cpal::SupportedStreamConfigsError].
BuildError(#[source] cpal::Error),
/// Could not list supported configs for the device. Maybe it disconnected.
#[error("Could not list supported configs for the device. Maybe its disconnected?")]
SupportedConfigsError(#[source] cpal::SupportedStreamConfigsError),
SupportedConfigsError(#[source] cpal::Error),
/// Could not find any output device
#[error("Could not find any output device")]
NoDevice,
Expand All @@ -507,7 +504,7 @@ impl MixerDeviceSink {
error_callback: E,
) -> Result<MixerDeviceSink, DeviceSinkError>
where
E: FnMut(cpal::StreamError) + Send + 'static,
E: FnMut(cpal::Error) + Send + 'static,
{
Self::validate_config(config);
let (controller, source) = mixer(config.channel_count, config.sample_rate);
Expand All @@ -530,7 +527,7 @@ impl MixerDeviceSink {
) -> Result<cpal::Stream, DeviceSinkError>
where
S: Source + Send + 'static,
E: FnMut(cpal::StreamError) + Send + 'static,
E: FnMut(cpal::Error) + Send + 'static,
{
let cpal_config = config.into();

Expand Down
Loading