Rename try_open_stream and try_default_stream

This commit is contained in:
Petr Gladkikh 2024-11-21 21:43:24 +04:00
parent 678df037cb
commit d1d5726ce0
18 changed files with 31 additions and 27 deletions

View file

@ -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

View file

@ -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 = {

View file

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

View file

@ -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

View file

@ -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")?;

View file

@ -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")?;

View file

@ -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")?;

View file

@ -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")?;

View file

@ -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")?;

View file

@ -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);

View file

@ -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")?;

View file

@ -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")?;

View file

@ -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);

View file

@ -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(

View file

@ -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")?;

View file

@ -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());
//!

View file

@ -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);

View file

@ -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)? {
@ -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| {
@ -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)
})
}