Skip to content

Commit

Permalink
Rename try_open_stream and try_default_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrGlad committed Nov 21, 2024
1 parent 678df03 commit d1d5726
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/automatic_gain_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::thread;
use std::time::Duration;

fn main() -> Result<(), Box<dyn Error>> {
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
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Duration;
use tracing;

fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let mixer = stream_handle.mixer();

let beep1 = {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// 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)
Expand Down
2 changes: 1 addition & 1 deletion examples/mix_multiple_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
fn main() -> Result<(), Box<dyn Error>> {
// Construct a dynamic controller and mixer, stream_handle, and sink.
let (controller, mixer) = mixer::mixer::<f32>(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
Expand Down
2 changes: 1 addition & 1 deletion examples/music_flac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::io::BufReader;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/music_m4a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::io::BufReader;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/music_mp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::io::BufReader;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/music_ogg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::io::BufReader;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/music_wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::io::BufReader;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/noise_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> Result<(), Box<dyn Error>> {
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);
Expand Down
2 changes: 1 addition & 1 deletion examples/reverb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::BufReader;
use std::time::Duration;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/seek_mp3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::BufReader;
use std::time::Duration;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/signal_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() -> Result<(), Box<dyn Error>> {
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);
Expand Down
2 changes: 1 addition & 1 deletion examples/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error>> {

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(
Expand Down
2 changes: 1 addition & 1 deletion examples/stereo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::error::Error;
use std::io::BufReader;

fn main() -> Result<(), Box<dyn Error>> {
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")?;
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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());
//!
Expand Down
4 changes: 2 additions & 2 deletions src/source/speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutputStream, StreamError> {
/// supported by the device.
/// If all attempts fail returns initial error.
pub fn open_stream_or_fallback(&self) -> Result<OutputStream, StreamError> {
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)? {
Expand All @@ -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<OutputStream, StreamError> {
/// 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<OutputStream, StreamError> {
Self::from_default_device()
.and_then(|x| x.open_stream())
.or_else(|original_err| {
Expand All @@ -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)
})
}
Expand Down

0 comments on commit d1d5726

Please sign in to comment.