Skip to content

Commit

Permalink
Remove unwrap and expect from example code
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrGlad committed Nov 19, 2024
1 parent 77c50cd commit 4101ae9
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 75 deletions.
11 changes: 6 additions & 5 deletions examples/automatic_gain_control.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use rodio::source::Source;
use rodio::Decoder;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

// Decode the sound file into a source
let file = BufReader::new(File::open("assets/music.flac").unwrap());
let source = Decoder::new(file).unwrap();
let file = BufReader::new(File::open("assets/music.flac")?);
let source = Decoder::new(file)?;

// Apply automatic gain control to the source
let agc_source = source.automatic_gain_control(1.0, 4.0, 0.005, 5.0);
Expand Down Expand Up @@ -45,4 +45,5 @@ fn main() {

// Keep the program running until the playback is complete.
sink.sleep_until_end();
Ok(())
}
16 changes: 9 additions & 7 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use rodio::source::SineWave;
use rodio::Source;
use std::error::Error;
use std::io::BufReader;
use std::thread;
use std::time::Duration;
#[cfg(feature = "tracing")]
use tracing;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let mixer = stream_handle.mixer();

let beep1 = {
// Play a WAV file.
let file = std::fs::File::open("assets/beep.wav").unwrap();
let sink = rodio::play(&mixer, BufReader::new(file)).unwrap();
let file = std::fs::File::open("assets/beep.wav")?;
let sink = rodio::play(&mixer, BufReader::new(file))?;
sink.set_volume(0.2);
sink
};
Expand All @@ -33,8 +33,8 @@ fn main() {

let beep3 = {
// Play an OGG file.
let file = std::fs::File::open("assets/beep3.ogg").unwrap();
let sink = rodio::play(&mixer, BufReader::new(file)).unwrap();
let file = std::fs::File::open("assets/beep3.ogg")?;
let sink = rodio::play(&mixer, BufReader::new(file))?;
sink.set_volume(0.2);
sink
};
Expand All @@ -49,4 +49,6 @@ fn main() {
println!("Stopped beep3");

thread::sleep(Duration::from_millis(1500));

Ok(())
}
8 changes: 5 additions & 3 deletions examples/mix_multiple_sources.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use rodio::mixer;
use rodio::source::{SineWave, Source};
use std::error::Error;
use std::time::Duration;

fn main() {
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().expect("open default audio stream");
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

// Create four unique sources. The frequencies used here correspond
Expand Down Expand Up @@ -36,4 +36,6 @@ fn main() {

// Sleep the thread until sink is empty.
sink.sleep_until_end();

Ok(())
}
12 changes: 7 additions & 5 deletions examples/music_flac.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::error::Error;
use std::io::BufReader;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.flac").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let file = std::fs::File::open("assets/music.flac")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);

sink.sleep_until_end();

Ok(())
}
12 changes: 7 additions & 5 deletions examples/music_m4a.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::error::Error;
use std::io::BufReader;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.m4a").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let file = std::fs::File::open("assets/music.m4a")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);

sink.sleep_until_end();

Ok(())
}
12 changes: 7 additions & 5 deletions examples/music_mp3.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::error::Error;
use std::io::BufReader;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.mp3").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let file = std::fs::File::open("assets/music.mp3")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);

sink.sleep_until_end();

Ok(())
}
12 changes: 7 additions & 5 deletions examples/music_ogg.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::error::Error;
use std::io::BufReader;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.ogg").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let file = std::fs::File::open("assets/music.ogg")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);

sink.sleep_until_end();

Ok(())
}
12 changes: 7 additions & 5 deletions examples/music_wav.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::error::Error;
use std::io::BufReader;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.wav").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let file = std::fs::File::open("assets/music.wav")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);

sink.sleep_until_end();

Ok(())
}
9 changes: 6 additions & 3 deletions examples/noise_generator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Noise generator example. Use the "noise" feature to enable the noise generator sources.

use std::error::Error;

#[cfg(feature = "noise")]
fn main() {
fn main() -> Result<(), Box<dyn Error>> {
use rodio::source::{pink, white, Source};
use std::thread;
use std::time::Duration;

let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;

let noise_duration = Duration::from_millis(1000);
let interval_duration = Duration::from_millis(1500);
Expand All @@ -29,6 +30,8 @@ fn main() {
println!("Playing pink noise");

thread::sleep(interval_duration);

Ok(())
}

#[cfg(not(feature = "noise"))]
Expand Down
12 changes: 7 additions & 5 deletions examples/reverb.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use rodio::Source;
use std::error::Error;
use std::io::BufReader;
use std::time::Duration;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.ogg").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
let file = std::fs::File::open("assets/music.ogg")?;
let source = rodio::Decoder::new(BufReader::new(file))?;
let with_reverb = source.buffered().reverb(Duration::from_millis(40), 0.7);
sink.append(with_reverb);

sink.sleep_until_end();

Ok(())
}
18 changes: 10 additions & 8 deletions examples/seek_mp3.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::error::Error;
use std::io::BufReader;
use std::time::Duration;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/music.mp3").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let file = std::fs::File::open("assets/music.mp3")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);

std::thread::sleep(std::time::Duration::from_secs(2));
sink.try_seek(Duration::from_secs(0)).unwrap();
sink.try_seek(Duration::from_secs(0))?;

std::thread::sleep(std::time::Duration::from_secs(2));
sink.try_seek(Duration::from_secs(4)).unwrap();
sink.try_seek(Duration::from_secs(4))?;

sink.sleep_until_end();

// This doesn't do anything since the sound has ended already.
sink.try_seek(Duration::from_secs(5)).unwrap();
sink.try_seek(Duration::from_secs(5))?;
println!("seek example ended");

Ok(())
}
9 changes: 6 additions & 3 deletions examples/signal_generator.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Test signal generator example.

fn main() {
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
use rodio::source::{chirp, Function, SignalGenerator, Source};
use std::thread;
use std::time::Duration;

let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;

let test_signal_duration = Duration::from_millis(1000);
let interval_duration = Duration::from_millis(1500);
Expand Down Expand Up @@ -65,4 +66,6 @@ fn main() {
);

thread::sleep(interval_duration);

Ok(())
}
13 changes: 7 additions & 6 deletions examples/spatial.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::error::Error;
use std::io::BufReader;
use std::thread;
use std::time::Duration;

use rodio::Source;

fn main() {
fn main() -> Result<(), Box<dyn Error>> {
let iter_duration = Duration::from_secs(5);
let iter_distance = 5.;

Expand All @@ -18,8 +19,7 @@ fn main() {

let total_duration = iter_duration * 2 * repeats;

let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;

let mut positions = ([0., 0., 0.], [-1., 0., 0.], [1., 0., 0.]);
let sink = rodio::SpatialSink::connect_new(
Expand All @@ -29,9 +29,8 @@ fn main() {
positions.2,
);

let file = std::fs::File::open("assets/music.ogg").unwrap();
let source = rodio::Decoder::new(BufReader::new(file))
.unwrap()
let file = std::fs::File::open("assets/music.ogg")?;
let source = rodio::Decoder::new(BufReader::new(file))?
.repeat_infinite()
.take_duration(total_duration);
sink.append(source);
Expand All @@ -57,4 +56,6 @@ fn main() {
}
}
sink.sleep_until_end();

Ok(())
}
17 changes: 8 additions & 9 deletions examples/stereo.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
//! Plays a tone alternating between right and left ears, with right being first.

use rodio::Source;
use std::error::Error;
use std::io::BufReader;

fn main() {
let stream_handle =
rodio::OutputStreamBuilder::try_default_stream().expect("open default audio stream");
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

let file = std::fs::File::open("assets/RL.ogg").unwrap();
sink.append(
rodio::Decoder::new(BufReader::new(file))
.unwrap()
.amplify(0.2),
);
let file = std::fs::File::open("assets/RL.ogg")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?.amplify(0.2));

sink.sleep_until_end();

Ok(())
}
2 changes: 1 addition & 1 deletion src/source/speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! std::thread::sleep(std::time::Duration::from_secs(5));
//! ```
//! here is how you would do it using the sink
//!```
//!```no_run
//! use rodio::source::{Source, SineWave};
//! let source = SineWave::new(440.0)
//! .take_duration(std::time::Duration::from_secs_f32(20.25))
Expand Down

0 comments on commit 4101ae9

Please sign in to comment.