Skip to content

Commit 43503b4

Browse files
committed
Adds example and makes both wav_output functions own source
It does not make sense to get them by ref. Since we will not return till they are fully exhausted (or never if the source is infinite). Therefore there is no use to borrowing
1 parent 1dd9266 commit 43503b4

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,6 @@ pub use crate::stream::{play, OutputStream, OutputStreamBuilder, PlayError, Stre
199199
#[cfg(feature = "wav_output")]
200200
#[cfg_attr(docsrs, doc(cfg(feature = "wav_output")))]
201201
pub use crate::wav_output::output_to_wav;
202+
#[cfg(feature = "wav_output")]
203+
#[cfg_attr(docsrs, doc(cfg(feature = "wav_output")))]
204+
pub use crate::wav_output::collect_to_wav;

src/wav_output.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,37 @@ assert_error_traits!(ToWavError);
2424
///
2525
/// If the file already exists it will be overwritten.
2626
pub fn output_to_wav(
27-
source: &mut impl Source,
27+
source: impl Source,
2828
wav_file: impl AsRef<path::Path>,
2929
) -> Result<(), ToWavError> {
30-
let file = std::fs::File::create(wav_file)
30+
let mut file = std::fs::File::create(wav_file)
3131
.map_err(Arc::new)
3232
.map_err(ToWavError::OpenFile)?;
33-
collect_to_wav(source, file)
33+
collect_to_wav(source, &mut file)
3434
}
3535

3636
/// Saves Source's output into a writer. The output samples format is 32-bit float. This function
3737
/// is intended primarily for testing and diagnostics. It can be used to see the output without
3838
/// opening output stream to a real audio device.
39+
///
40+
/// # Example
41+
/// ```rust
42+
/// # use rodio::static_buffer::StaticSamplesBuffer;
43+
/// # use rodio::collect_to_wav;
44+
/// # const SAMPLES: [rodio::Sample; 5] = [0.0, 1.0, 2.0, 3.0, 4.0];
45+
/// # let source = StaticSamplesBuffer::new(
46+
/// # 1.try_into().unwrap(),
47+
/// # 1.try_into().unwrap(),
48+
/// # &SAMPLES
49+
/// # );
50+
/// let mut writer = std::io::Cursor::new(Vec::new());
51+
/// collect_to_wav(source, &mut writer)?;
52+
/// let wav_bytes: Vec<u8> = writer.into_inner();
53+
/// # Ok::<(), Box<dyn std::error::Error>>(())
54+
/// ```
3955
pub fn collect_to_wav(
40-
source: &mut impl Source,
41-
writer: impl io::Write + io::Seek,
56+
source: impl Source,
57+
writer: &mut (impl io::Write + io::Seek),
4258
) -> Result<(), ToWavError> {
4359
let format = WavSpec {
4460
channels: source.channels().get(),

0 commit comments

Comments
 (0)