11use crate :: common:: assert_error_traits;
22use crate :: Source ;
33use hound:: { SampleFormat , WavSpec } ;
4+ use std:: io:: { self , Write } ;
45use std:: path;
5- use std:: io;
66use 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}
1921assert_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