1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! WAV (RIFF/WAVE) reader and writer over std I/O.
//!
//! `wav_codec` reads and writes the uncompressed WAV encodings that real
//! files carry: PCM at 8, 16, 24, and 32 bits (packed or in wider
//! containers via `WAVE_FORMAT_EXTENSIBLE`) and IEEE float at 32 and 64
//! bits. Both directions work over the std traits, so any `io::Read`
//! source decodes and any `io::Write + io::Seek` sink encodes.
//!
//! # Reading
//!
//! ```no_run
//! use wav_codec::WavReader;
//!
//! let mut reader = WavReader::open("input.wav")?;
//! let spec = reader.spec();
//! println!("{} Hz, {} channels", spec.sample_rate, spec.channels);
//! let samples: Vec<i16> = reader.samples::<i16>().collect::<Result<_, _>>()?;
//! # Ok::<(), wav_codec::Error>(())
//! ```
//!
//! Every sample item is a `Result`. A stream that ends inside a sample, or
//! short of the declared `data` size, yields exactly one `Err` and then
//! the iterator is fused, so corruption cannot pass as a short clean read.
//!
//! # Writing
//!
//! ```no_run
//! use wav_codec::{SampleFormat, WavSpec, WavWriter};
//!
//! let spec = WavSpec {
//! channels: 1,
//! sample_rate: 44100,
//! bits_per_sample: 16,
//! sample_format: SampleFormat::Int,
//! };
//! let mut writer = WavWriter::create("sine.wav", spec)?;
//! for n in 0..44100u32 {
//! let t = n as f32 / 44100.0;
//! let v = (t * 440.0 * 2.0 * std::f32::consts::PI).sin();
//! writer.write_sample((v * i16::MAX as f32) as i16)?;
//! }
//! writer.finalize()?;
//! # Ok::<(), wav_codec::Error>(())
//! ```
//!
//! `finalize` writes the pad byte for an odd `data` size and patches the
//! RIFF and `data` sizes. An incomplete last frame returns
//! [`Error::UnfinishedFrame`] after the header is patched to a whole
//! number of frames. Dropping a writer runs the same steps and discards
//! the result, so Drop cannot report `UnfinishedFrame`. Call `finalize`
//! to observe that error.
//!
//! # Sample types
//!
//! | file encoding | readable as | writable from |
//! |---------------------------|------------------------|--------------------------------|
//! | 8-bit PCM (offset binary) | `i8`, `i16`, `i32` | `i8`, `i16`, `i32` (in range) |
//! | 16-bit PCM | `i16`, `i32` | `i8`, `i16`, `i32` (in range) |
//! | 24-bit PCM, 3 or 4 bytes | `i32` | `i8`, `i16`, `i32` (in range) |
//! | 32-bit PCM | `i32` | `i8`, `i16`, `i32` |
//! | 32-bit float | `f32`, `f64` | `f32` |
//! | 64-bit float | `f64` | `f32`, `f64` |
//!
//! 8-bit files are unsigned on disk and exposed as `i8` with the value
//! `stored_byte - 128`. A read with a type too narrow for the file, or a
//! type of the wrong class, fails with [`Error::TooWide`].
//!
//! # Scope
//!
//! Out of scope, rejected with a clean error: compressed format tags
//! (ADPCM, a-law, mu-law, MP3), `RF64`, `RIFX`, `W64`, and any
//! `WAVE_FORMAT_EXTENSIBLE` GUID other than PCM and IEEE float. The crate
//! is an adapter over `std::io`, so `no_std` is unsupported.
use fmt;
use io;
pub use ;
pub use Sample;
pub use WavWriter;
pub const TAG_PCM: u16 = 0x0001;
pub const TAG_IEEE_FLOAT: u16 = 0x0003;
pub const TAG_EXTENSIBLE: u16 = 0xFFFE;
pub const GUID_PCM: = ;
pub const GUID_IEEE_FLOAT: = ;
/// How samples are encoded on disk.
/// Stream parameters: channel count, sample rate, precision, and encoding.
/// A [`WavSpec`] plus the container width, for files whose samples sit in
/// containers wider than their valid bits.
/// Errors from reading or writing a WAV stream.
/// Result alias with this crate's [`Error`].
pub type Result<T> = Result;