Skip to content

Commit 12c3aba

Browse files
committed
flush buffer, changelog entry, rename wav output fns
1 parent 43503b4 commit 12c3aba

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Adds a new input source: Microphone.
2020
- Adds a new method on source: record which collects all samples into a
2121
SamplesBuffer.
22+
- Adds `wav_to_writer` which writes a `Source` to a writer.
2223

2324
### Fixed
2425
- docs.rs will now document all features, including those that are optional.
@@ -27,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2728
- `PeriodicAccess` is slightly more accurate for 44.1 kHz sample rate families.
2829

2930
### Changed
31+
- `output_to_wav` renamed to `wav_to_file` and now takes ownership of the `Source`.
3032
- `Blue` noise generator uses uniform instead of Gaussian noise for better performance.
3133
- `Gaussian` noise generator has standard deviation of 0.6 for perceptual equivalence.
3234

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub use crate::spatial_sink::SpatialSink;
198198
pub use crate::stream::{play, OutputStream, OutputStreamBuilder, PlayError, StreamError};
199199
#[cfg(feature = "wav_output")]
200200
#[cfg_attr(docsrs, doc(cfg(feature = "wav_output")))]
201-
pub use crate::wav_output::output_to_wav;
201+
pub use crate::wav_output::wav_to_file;
202202
#[cfg(feature = "wav_output")]
203203
#[cfg_attr(docsrs, doc(cfg(feature = "wav_output")))]
204-
pub use crate::wav_output::collect_to_wav;
204+
pub use crate::wav_output::wav_to_writer;

src/wav_output.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::common::assert_error_traits;
22
use crate::Source;
33
use hound::{SampleFormat, WavSpec};
4+
use std::io::{self, Write};
45
use std::path;
5-
use std::io;
66
use std::sync::Arc;
77

88
#[derive(Debug, thiserror::Error, Clone)]
@@ -15,6 +15,8 @@ pub enum ToWavError {
1515
Writing(#[source] Arc<hound::Error>),
1616
#[error("Failed to update the wav header")]
1717
Finishing(#[source] Arc<hound::Error>),
18+
#[error("Failed to flush all bytes to writer")]
19+
Flushing(#[source] Arc<std::io::Error>),
1820
}
1921
assert_error_traits!(ToWavError);
2022

@@ -23,14 +25,17 @@ assert_error_traits!(ToWavError);
2325
/// the output without opening output stream to a real audio device.
2426
///
2527
/// If the file already exists it will be overwritten.
26-
pub fn output_to_wav(
28+
///
29+
/// # Note
30+
/// This is a convenience wrapper around `wav_to_writer`
31+
pub fn wav_to_file(
2732
source: impl Source,
2833
wav_file: impl AsRef<path::Path>,
2934
) -> Result<(), ToWavError> {
3035
let mut file = std::fs::File::create(wav_file)
3136
.map_err(Arc::new)
3237
.map_err(ToWavError::OpenFile)?;
33-
collect_to_wav(source, &mut file)
38+
wav_to_writer(source, &mut file)
3439
}
3540

3641
/// Saves Source's output into a writer. The output samples format is 32-bit float. This function
@@ -43,16 +48,16 @@ pub fn output_to_wav(
4348
/// # use rodio::collect_to_wav;
4449
/// # const SAMPLES: [rodio::Sample; 5] = [0.0, 1.0, 2.0, 3.0, 4.0];
4550
/// # let source = StaticSamplesBuffer::new(
46-
/// # 1.try_into().unwrap(),
47-
/// # 1.try_into().unwrap(),
51+
/// # 1.try_into().unwrap(),
52+
/// # 1.try_into().unwrap(),
4853
/// # &SAMPLES
4954
/// # );
5055
/// let mut writer = std::io::Cursor::new(Vec::new());
51-
/// collect_to_wav(source, &mut writer)?;
56+
/// wav_to_writer(source, &mut writer)?;
5257
/// let wav_bytes: Vec<u8> = writer.into_inner();
5358
/// # Ok::<(), Box<dyn std::error::Error>>(())
5459
/// ```
55-
pub fn collect_to_wav(
60+
pub fn wav_to_writer(
5661
source: impl Source,
5762
writer: &mut (impl io::Write + io::Seek),
5863
) -> Result<(), ToWavError> {
@@ -62,20 +67,26 @@ pub fn collect_to_wav(
6267
bits_per_sample: 32,
6368
sample_format: SampleFormat::Float,
6469
};
65-
let writer = io::BufWriter::new(writer);
66-
let mut writer = hound::WavWriter::new(writer, format)
67-
.map_err(Arc::new)
68-
.map_err(ToWavError::Creating)?;
69-
for sample in source {
70+
let mut writer = io::BufWriter::new(writer);
71+
{
72+
let mut writer = hound::WavWriter::new(&mut writer, format)
73+
.map_err(Arc::new)
74+
.map_err(ToWavError::Creating)?;
75+
for sample in source {
76+
writer
77+
.write_sample(sample)
78+
.map_err(Arc::new)
79+
.map_err(ToWavError::Writing)?;
80+
}
7081
writer
71-
.write_sample(sample)
82+
.finalize()
7283
.map_err(Arc::new)
73-
.map_err(ToWavError::Writing)?;
84+
.map_err(ToWavError::Finishing)?;
7485
}
7586
writer
76-
.finalize()
87+
.flush()
7788
.map_err(Arc::new)
78-
.map_err(ToWavError::Finishing)?;
89+
.map_err(ToWavError::Flushing)?;
7990
Ok(())
8091
}
8192

0 commit comments

Comments
 (0)