From d1d5726ce0a8e0b8799e3ce5673e63d1a4ff0a49 Mon Sep 17 00:00:00 2001 From: Petr Gladkikh Date: Thu, 21 Nov 2024 21:43:24 +0400 Subject: [PATCH] Rename try_open_stream and try_default_stream --- examples/automatic_gain_control.rs | 2 +- examples/basic.rs | 2 +- examples/custom_config.rs | 2 +- examples/mix_multiple_sources.rs | 2 +- examples/music_flac.rs | 2 +- examples/music_m4a.rs | 2 +- examples/music_mp3.rs | 2 +- examples/music_ogg.rs | 2 +- examples/music_wav.rs | 2 +- examples/noise_generator.rs | 2 +- examples/reverb.rs | 2 +- examples/seek_mp3.rs | 2 +- examples/signal_generator.rs | 2 +- examples/spatial.rs | 2 +- examples/stereo.rs | 2 +- src/lib.rs | 6 +++--- src/source/speed.rs | 4 ++-- src/stream.rs | 18 +++++++++++------- 18 files changed, 31 insertions(+), 27 deletions(-) diff --git a/examples/automatic_gain_control.rs b/examples/automatic_gain_control.rs index 2c259d5..aae0ba6 100644 --- a/examples/automatic_gain_control.rs +++ b/examples/automatic_gain_control.rs @@ -9,7 +9,7 @@ use std::thread; use std::time::Duration; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); // Decode the sound file into a source diff --git a/examples/basic.rs b/examples/basic.rs index e45580f..fa3ef3e 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -8,7 +8,7 @@ use std::time::Duration; use tracing; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let mixer = stream_handle.mixer(); let beep1 = { diff --git a/examples/custom_config.rs b/examples/custom_config.rs index 07b2f8d..40b6ca4 100644 --- a/examples/custom_config.rs +++ b/examples/custom_config.rs @@ -20,7 +20,7 @@ fn main() -> Result<(), Box> { // Note that the function below still tries alternative configs if the specified one fails. // If you need to only use the exact specified configuration, // then use OutputStreamBuilder::open_stream() instead. - .try_open_stream()?; + .open_stream_or_fallback()?; let mixer = stream_handle.mixer(); let wave = SineWave::new(740.0) diff --git a/examples/mix_multiple_sources.rs b/examples/mix_multiple_sources.rs index a2f760f..55c3fb0 100644 --- a/examples/mix_multiple_sources.rs +++ b/examples/mix_multiple_sources.rs @@ -6,7 +6,7 @@ use std::time::Duration; fn main() -> Result<(), Box> { // Construct a dynamic controller and mixer, stream_handle, and sink. let (controller, mixer) = mixer::mixer::(2, 44_100); - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); // Create four unique sources. The frequencies used here correspond diff --git a/examples/music_flac.rs b/examples/music_flac.rs index fd79d24..13013ba 100644 --- a/examples/music_flac.rs +++ b/examples/music_flac.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::io::BufReader; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.flac")?; diff --git a/examples/music_m4a.rs b/examples/music_m4a.rs index ff4d52d..15b5843 100644 --- a/examples/music_m4a.rs +++ b/examples/music_m4a.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::io::BufReader; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.m4a")?; diff --git a/examples/music_mp3.rs b/examples/music_mp3.rs index 1bd4a40..cb94159 100644 --- a/examples/music_mp3.rs +++ b/examples/music_mp3.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::io::BufReader; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.mp3")?; diff --git a/examples/music_ogg.rs b/examples/music_ogg.rs index 10b9e49..74c3622 100644 --- a/examples/music_ogg.rs +++ b/examples/music_ogg.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::io::BufReader; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.ogg")?; diff --git a/examples/music_wav.rs b/examples/music_wav.rs index 2e582ab..a29d929 100644 --- a/examples/music_wav.rs +++ b/examples/music_wav.rs @@ -2,7 +2,7 @@ use std::error::Error; use std::io::BufReader; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.wav")?; diff --git a/examples/noise_generator.rs b/examples/noise_generator.rs index b9ff905..577f9d0 100644 --- a/examples/noise_generator.rs +++ b/examples/noise_generator.rs @@ -8,7 +8,7 @@ fn main() -> Result<(), Box> { use std::thread; use std::time::Duration; - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let noise_duration = Duration::from_millis(1000); let interval_duration = Duration::from_millis(1500); diff --git a/examples/reverb.rs b/examples/reverb.rs index c77c379..dc74e71 100644 --- a/examples/reverb.rs +++ b/examples/reverb.rs @@ -4,7 +4,7 @@ use std::io::BufReader; use std::time::Duration; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.ogg")?; diff --git a/examples/seek_mp3.rs b/examples/seek_mp3.rs index 3be311f..a78238b 100644 --- a/examples/seek_mp3.rs +++ b/examples/seek_mp3.rs @@ -3,7 +3,7 @@ use std::io::BufReader; use std::time::Duration; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/music.mp3")?; diff --git a/examples/signal_generator.rs b/examples/signal_generator.rs index c73731b..e746692 100644 --- a/examples/signal_generator.rs +++ b/examples/signal_generator.rs @@ -7,7 +7,7 @@ fn main() -> Result<(), Box> { use std::thread; use std::time::Duration; - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let test_signal_duration = Duration::from_millis(1000); let interval_duration = Duration::from_millis(1500); diff --git a/examples/spatial.rs b/examples/spatial.rs index 485829e..31505ec 100644 --- a/examples/spatial.rs +++ b/examples/spatial.rs @@ -19,7 +19,7 @@ fn main() -> Result<(), Box> { let total_duration = iter_duration * 2 * repeats; - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let mut positions = ([0., 0., 0.], [-1., 0., 0.], [1., 0., 0.]); let sink = rodio::SpatialSink::connect_new( diff --git a/examples/stereo.rs b/examples/stereo.rs index a908894..5b5ec50 100644 --- a/examples/stereo.rs +++ b/examples/stereo.rs @@ -5,7 +5,7 @@ use std::error::Error; use std::io::BufReader; fn main() -> Result<(), Box> { - let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?; + let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?; let sink = rodio::Sink::connect_new(&stream_handle.mixer()); let file = std::fs::File::open("assets/RL.ogg")?; diff --git a/src/lib.rs b/src/lib.rs index 606359e..c46b506 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ //! //! // Get an output stream handle to the default physical sound device. //! // Note that no sound will be played if _stream is dropped -//! let stream_handle = rodio::OutputStreamBuilder::try_default_stream() +//! let stream_handle = rodio::OutputStreamBuilder::open_default_stream() //! .expect("open default audio stream"); //! let sink = rodio::Sink::connect_new(&stream_handle.mixer()); //! // Load a sound from a file, using a path relative to Cargo.toml @@ -46,7 +46,7 @@ //! //! // Get an output stream handle to the default physical sound device. //! // Note that no sound will be played if _stream is dropped -//! let stream_handle = rodio::OutputStreamBuilder::try_default_stream() +//! let stream_handle = rodio::OutputStreamBuilder::open_default_stream() //! .expect("open default audio stream"); //! //! // Load a sound from a file, using a path relative to Cargo.toml @@ -78,7 +78,7 @@ //! use rodio::source::{SineWave, Source}; //! //! // _stream must live as long as the sink -//! let stream_handle = rodio::OutputStreamBuilder::try_default_stream() +//! let stream_handle = rodio::OutputStreamBuilder::open_default_stream() //! .expect("open default audio stream"); //! let sink = rodio::Sink::connect_new(&stream_handle.mixer()); //! diff --git a/src/source/speed.rs b/src/source/speed.rs index 4b7bac8..8e278e9 100644 --- a/src/source/speed.rs +++ b/src/source/speed.rs @@ -18,7 +18,7 @@ //! //! // Get an output stream handle to the default physical sound device. //! // Note that no sound will be played if the _stream is dropped. -//! let stream_handle = rodio::OutputStreamBuilder::try_default_stream() +//! let stream_handle = rodio::OutputStreamBuilder::open_default_stream() //! .expect("open default audio stream"); //! // Load a sound from a file, using a path relative to `Cargo.toml` //! let file = BufReader::new(File::open("examples/music.ogg").unwrap()); @@ -34,7 +34,7 @@ //! let source = SineWave::new(440.0) //! .take_duration(std::time::Duration::from_secs_f32(20.25)) //! .amplify(0.20); -//! let stream_handle = rodio::OutputStreamBuilder::try_default_stream() +//! let stream_handle = rodio::OutputStreamBuilder::open_default_stream() //! .expect("open default audio stream"); //! let sink = rodio::Sink::connect_new(&stream_handle.mixer()); //! sink.set_speed(2.0); diff --git a/src/stream.rs b/src/stream.rs index 57209b2..8b31882 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -146,9 +146,9 @@ impl OutputStreamBuilder { /// Try opening a new output stream with the builder's current stream configuration. /// Failing that attempt to open stream with other available configurations - /// provided by the device. - /// If all attempts did not succeed returns initial error. - pub fn try_open_stream(&self) -> Result { + /// supported by the device. + /// If all attempts fail returns initial error. + pub fn open_stream_or_fallback(&self) -> Result { let device = self.device.as_ref().expect("output device specified"); OutputStream::open(device, &self.config).or_else(|err| { for supported_config in supported_output_configs(device)? { @@ -166,9 +166,9 @@ impl OutputStreamBuilder { /// Try to open a new output stream for the default output device with its default configuration. /// Failing that attempt to open output stream with alternative configuration and/or non default - /// output devices. Returns stream for first tried configuration that succeeds. - /// If all attempts have not succeeded return the initial error. - pub fn try_default_stream() -> Result { + /// output devices. Returns stream for first of the tried configurations that succeeds. + /// If all attempts fail return the initial error. + pub fn open_default_stream() -> Result { Self::from_default_device() .and_then(|x| x.open_stream()) .or_else(|original_err| { @@ -183,7 +183,11 @@ impl OutputStreamBuilder { } }; devices - .find_map(|d| Self::from_device(d).and_then(|x| x.try_open_stream()).ok()) + .find_map(|d| { + Self::from_device(d) + .and_then(|x| x.open_stream_or_fallback()) + .ok() + }) .ok_or(original_err) }) }