Skip to content

Commit 4187282

Browse files
committed
refactor: use Decoder::try_from<Path> for file loading in examples and tests
1 parent 1b66519 commit 4187282

26 files changed

+84
-77
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- `with_scan_duration()` - Enable file scanning for duration computation.
2828
- All alternative decoders now support `Settings` via `new_with_settings()`.
2929
- Symphonia decoder handles multi-track containers and chained Ogg streams.
30+
- Added `Decoder::TryFrom` implementations for memory-based types:
31+
- `Vec<u8>` - Decoding from owned byte vectors
32+
- `Box<[u8]>` - Decoding from boxed byte slices
33+
- `Arc<[u8]>` - Decoding from shared byte slices
34+
- `&'static [u8]` - Decoding from embedded static data
35+
- `Cow<'static, [u8]>` - Decoding from borrowed or owned byte data
36+
- `bytes::Bytes` - Decoding from bytes crate (requires `bytes` feature)
37+
- `&Path` and `PathBuf` - Convenient file path decoding with format hints
38+
- `TryFrom<BufReader<R>>` and `TryFrom<Cursor<T>>` now use `DecoderBuilder` with optimized settings
39+
instead of basic `Decoder::new()` - enabling seeking and byte length detection where possible.
3040

3141
### Changed
3242
- `output_to_wav` renamed to `wav_to_file` and now takes ownership of the `Source`.

examples/automatic_gain_control.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rodio::source::Source;
22
use rodio::Decoder;
33
use std::error::Error;
4-
use std::fs::File;
4+
use std::path::Path;
55
use std::sync::atomic::{AtomicBool, Ordering};
66
use std::sync::Arc;
77
use std::thread;
@@ -12,8 +12,8 @@ fn main() -> Result<(), Box<dyn Error>> {
1212
let sink = rodio::Sink::connect_new(stream_handle.mixer());
1313

1414
// Decode the sound file into a source
15-
let file = File::open("assets/music.flac")?;
16-
let source = Decoder::try_from(file)?;
15+
let path = Path::new("assets/music.flac");
16+
let source = Decoder::try_from(path)?;
1717

1818
// Apply automatic gain control to the source
1919
let agc_source = source.automatic_gain_control(1.0, 4.0, 0.005, 5.0);

examples/callback_on_end.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ fn main() -> Result<(), Box<dyn Error>> {
66
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
77
let sink = rodio::Sink::connect_new(stream_handle.mixer());
88

9-
let file = std::fs::File::open("assets/music.wav")?;
10-
sink.append(rodio::Decoder::try_from(file)?);
9+
let path = std::path::Path::new("assets/music.wav");
10+
sink.append(rodio::Decoder::try_from(path)?);
1111

1212
// lets increment a number after `music.wav` has played. We are going to use atomics
1313
// however you could also use a `Mutex` or send a message through a `std::sync::mpsc`.

examples/distortion_mp3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ fn main() -> Result<(), Box<dyn Error>> {
66
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
77
let sink = rodio::Sink::connect_new(stream_handle.mixer());
88

9-
let file = std::fs::File::open("assets/music.mp3")?;
9+
let path = std::path::Path::new("assets/music.mp3");
1010
// Apply distortion effect before appending to the sink
11-
let source = rodio::Decoder::try_from(file)?.distortion(4.0, 0.3);
11+
let source = rodio::Decoder::try_from(path)?.distortion(4.0, 0.3);
1212
sink.append(source);
1313

1414
sink.sleep_until_end();

examples/distortion_wav.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ fn main() -> Result<(), Box<dyn Error>> {
66
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
77
let sink = rodio::Sink::connect_new(stream_handle.mixer());
88

9-
let file = std::fs::File::open("assets/music.wav")?;
9+
let path = std::path::Path::new("assets/music.wav");
1010
// Apply distortion effect before appending to the sink
11-
let source = rodio::Decoder::try_from(file)?.distortion(4.0, 0.3);
11+
let source = rodio::Decoder::try_from(path)?.distortion(4.0, 0.3);
1212
sink.append(source);
1313

1414
sink.sleep_until_end();

examples/distortion_wav_alternate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn main() -> Result<(), Box<dyn Error>> {
1212
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
1313
let sink = rodio::Sink::connect_new(stream_handle.mixer());
1414

15-
let file = std::fs::File::open("assets/music.wav")?;
16-
let source = rodio::Decoder::try_from(file)?;
15+
let path = std::path::Path::new("assets/music.wav");
16+
let source = rodio::Decoder::try_from(path)?;
1717

1818
// Shared flag to enable/disable distortion
1919
let distortion_enabled = Arc::new(AtomicBool::new(true));

examples/into_file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::error::Error;
55
/// This example does not use any audio devices
66
/// and can be used in build configurations without `cpal` feature enabled.
77
fn main() -> Result<(), Box<dyn Error>> {
8-
let file = std::fs::File::open("assets/music.mp3")?;
9-
let mut audio = rodio::Decoder::try_from(file)?
8+
let path = std::path::Path::new("assets/music.mp3");
9+
let mut audio = rodio::Decoder::try_from(path)?
1010
.automatic_gain_control(1.0, 4.0, 0.005, 3.0)
1111
.speed(0.8);
1212

examples/limit_wav.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ fn main() -> Result<(), Box<dyn Error>> {
55
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
66
let sink = rodio::Sink::connect_new(stream_handle.mixer());
77

8-
let file = std::fs::File::open("assets/music.wav")?;
9-
let source = rodio::Decoder::try_from(file)?
8+
let path = std::path::Path::new("assets/music.wav");
9+
let source = rodio::Decoder::try_from(path)?
1010
.amplify(3.0)
1111
.limit(LimitSettings::default());
1212

examples/low_pass.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::error::Error;
2-
use std::io::BufReader;
32

43
use rodio::Source;
54

65
fn main() -> Result<(), Box<dyn Error>> {
76
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
87
let sink = rodio::Sink::connect_new(stream_handle.mixer());
98

10-
let file = std::fs::File::open("assets/music.wav")?;
11-
let decoder = rodio::Decoder::new(BufReader::new(file))?;
9+
let path = std::path::Path::new("assets/music.wav");
10+
let decoder = rodio::Decoder::try_from(path)?;
1211
let source = decoder.low_pass(200);
1312
sink.append(source);
1413

examples/music_flac.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fn main() -> Result<(), Box<dyn Error>> {
44
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
55
let sink = rodio::Sink::connect_new(stream_handle.mixer());
66

7-
let file = std::fs::File::open("assets/music.flac")?;
8-
sink.append(rodio::Decoder::try_from(file)?);
7+
let path = std::path::Path::new("assets/music.flac");
8+
sink.append(rodio::Decoder::try_from(path)?);
99

1010
sink.sleep_until_end();
1111

0 commit comments

Comments
 (0)