Skip to content
Open
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
150 changes: 133 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ noise = ["rand", "rand_distr"]
#
# Enable WebAssembly support for web browsers
wasm-bindgen = ["cpal/wasm-bindgen"]
# Use shared C++ stdlib on Android (reduces APK size, fixes linking issues)
cpal-shared-stdcxx = ["cpal/oboe-shared-stdcxx"]

# To decode an audio source with Rodio, you need to enable the appropriate features for *both* the
# demuxer and the decoder.
Expand Down Expand Up @@ -103,7 +101,7 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
cpal = { version = "0.16", optional = true }
cpal = { git = "https://github.com/RustAudio/cpal", rev = "e32ee65a5335301553cc9650b949b868f922d748", optional = true }
dasp_sample = "0.11.0"
claxon = { version = "0.4.2", optional = true }
hound = { version = "3.5", optional = true }
Expand Down
14 changes: 9 additions & 5 deletions examples/error_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ fn main() -> Result<(), Box<dyn Error>> {

let stream_handle = rodio::OutputStreamBuilder::from_device(default_device)?
.with_error_callback(move |err| {
// Filter for where err is a DeviceNotAvailable error.
if let cpal::StreamError::DeviceNotAvailable = err {
// Filter for where err is an actionable error.
if matches!(
err,
cpal::StreamError::DeviceNotAvailable | cpal::StreamError::StreamInvalidated
) {
if let Err(e) = tx.send(err) {
eprintln!("Error emitting StreamError: {e}");
}
Expand All @@ -31,9 +34,10 @@ fn main() -> Result<(), Box<dyn Error>> {
mixer.add(wave);

if let Ok(err) = rx.recv_timeout(Duration::from_secs(30)) {
// Here we print the error that was emitted by the error callback.
// but in a real application we may want to destroy the stream and
// try to reopen it, either with the same device or a different one.
// Here we received an error that requires action from the error callback.
// In a real application you would destroy the stream and try to reopen it,
// either with the same device (for StreamInvalidated) or a different device
// (for DeviceNotAvailable).
eprintln!("Error with stream {err}");
}

Expand Down
2 changes: 1 addition & 1 deletion examples/microphone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.prompt()?;

let input = MicrophoneBuilder::new()
.device(input)?
.device(input.into_inner())?
.default_config()?
.open_stream()?;

Expand Down
30 changes: 22 additions & 8 deletions src/microphone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
//!
//! // Use a specific device (e.g., the second one)
//! let mic = MicrophoneBuilder::new()
//! .device(inputs[1].clone())?
//! .device(inputs[1].clone().into_inner())?
//! .default_config()?
//! .open_stream()?;
//! # Ok(())
Expand Down Expand Up @@ -130,23 +130,38 @@ pub struct Input {
inner: cpal::Device,
}

impl From<Input> for cpal::Device {
fn from(val: Input) -> Self {
val.inner
impl Input {
/// Consumes the input and returns the inner device.
pub fn into_inner(self) -> cpal::Device {
self.inner
}
}

impl fmt::Debug for Input {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Device")
.field("inner", &self.inner.name().unwrap_or("unknown".to_string()))
.field(
"inner",
&self
.inner
.description()
.ok()
.map_or("unknown".to_string(), |d| d.name().to_string()),
)
.finish()
}
}

impl fmt::Display for Input {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner.name().unwrap_or("unknown".to_string()))
write!(
f,
"{}",
self.inner
.description()
.ok()
.map_or("unknown".to_string(), |d| d.name().to_string())
)
}
}

Expand Down Expand Up @@ -271,8 +286,7 @@ impl Microphone {
I64, i64;
U8, u8;
U16, u16;
// TODO: uncomment when https://github.com/RustAudio/cpal/pull/1011 is merged
// U24, cpal::U24;
U24, cpal::U24;
U32, u32;
U64, u64
)
Expand Down
Loading