riff_wave/lib.rs
1// riff-wave -- Basic support for reading and writing wave PCM files.
2// Copyright (c) 2016 Kevin Brothaler and the riff-wave project authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// A copy of the License has been included in the root of the repository.
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! Basic support for reading and writing wave PCM files.
14//!
15//! This early version of the library seeks to support the "canonical" wave PCM
16//! file format, as well as the basic features of the extended file format.
17//! Non-PCM files are not currently supported, nor is metadata. Future versions
18//! of the library may add support for some of these features.
19//!
20//! The wave file format was originally defined by Microsoft, and it stores
21//! audio wave data in a container using RIFF chunks to encode the header and
22//! data. The RIFF format also supports file metadata via a LIST INFO chunk and
23//! an associated data chunk.
24//!
25//! See also:
26//!
27//! * [Hound][1] - Another wave encoding and decoding library in Rust.
28//! * [Multimedia Programming Interface and Data Specifications 1.0][2] - Further
29//! info about the RIFF file format.
30//! * [RIFF WAVE (.WAV) file format][3] - Also has more info on the RIFF WAVE format.
31//!
32//! # The wave file format
33//!
34//! The wave file format starts with the RIFF file header:
35//!
36//! Offset | Size | Data | Description
37//! -----: | ---: | ---------- | ----------------------------------------------
38//! 0 | 4 | "RIFF" | Identifies the main chunk.
39//! 4 | 4 | chunk size | The size of the rest of the file. This should be equal to the size of the file minus 8 bytes.
40//! 8 | 4 | "WAVE" | Indicates that this is a wave file.
41//!
42//! The wave file format requires at least two subchunks which follow the main chunk:
43//!
44//! * The "fmt " subchunk. This contains additional header information.
45//! * The "data" subchunk. This contains the actual audio data.
46//!
47//! ## The "fmt " subchunk
48//!
49//! The "fmt " subchunk starts with the following fields:
50//!
51//! Offset | Size | Data | Description
52//! -----: | ---: | --------------- | -----------------------------------------
53//! 12 | 4 | "fmt " | Identifies this subchunk.
54//! 16 | 4 | subchunk size | The size of the rest of this subchunk.
55//! 20 | 2 | format (1) | The format of the wave data, which will be 1 for uncompressed PCM data.
56//! 22 | 2 | num channels | Indicates if the data is mono, stereo, or something else.
57//! 24 | 4 | sample rate | The sample rate per second.
58//! 28 | 4 | byte rate | The total byte rate per second. For 16-bit stereo at 44,100 samples per second, this would be equal to 176,000 bytes per second.
59//! 32 | 2 | block align | How many bytes are needed for each "frame", where a frame is one sample for each channel.
60//! 34 | 2 | bits per sample | The bits per sample; i.e. 16 for 16-bit audio.
61//!
62//! The `format` can take on various values, including the following codes:
63//!
64//! Value | Description
65//! ----: | -------------------------------------------------------------------
66//! 1 | Uncompressed PCM
67//! 3 | IEEE floating-point
68//! 6 | 8-bit ITU-T G.711 A-law
69//! 7 | 8-bit ITU-T G.711 ยต-law
70//! 65534 | A special marker value, indicating that this is an "extended" wave file.
71//!
72//! This library currently only supports uncompressed PCM in standard and
73//! extended wave formats. These files will usually be either 8-bit unsigned or
74//! 16-bit signed, mono or stereo.
75//!
76//! Wave files may include an additional field, usually reserved for non-PCM formats:
77//!
78//! Offset | Size | Data | Description
79//! -----: | ---: | --------------- | -----------------------------------------
80//! 36 | 2 | extra info size | For non-PCM formats, this stores the size of the additional info that follows the end of the standard header. Otherwise, it is set to 0.
81//!
82//! ### Extended wave files
83//!
84//! Some wave files may follow the extended format. In this case, the
85//! `extra info size` field will be at least 22 instead of 0.
86//!
87//! Offset | Size | Data | Description
88//! -----: | ---: | --------------- | -----------------------------------------
89//! 38 | 2 | sample info | For PCM files, this contains the valid bits for sample. For example, if this is set to 20 bits and `bits per sample` is set to 24 bits, then that means that 24 bits are being used to store the sample data, but the actual sample data should not exceed 20 bits of precision.
90//! 40 | 4 | channel mask | This specifies the assignment of channels to speaker positions.
91//! 44 | 16 | sub format | For extended wave files, `format` will be set to 0xFFFE to indicate that it's an extended wave file, with the actual format specified here as a [GUID][4]. The first two bytes are the same as specified in `format code`, and the remainder should match 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, and 0x71.
92//!
93//! The MSDN docs recommend this format for files with more than two channels
94//! or more than 16 bits per sample, but it's also possible to encounter such
95//! wave files that don't include these extra fields. In my testing, Android
96//! Marshmallow was able to play back 24-bit PCM wave files using both the
97//! standard format and the extensible format, generated using [Audacity][5].
98//!
99//! ## The "data" subchunk
100//!
101//! The "data" subchunk contains the actual audio data:
102//!
103//! Offset | Size | Data | Description
104//! -----: | ---: | --------------- | -----------------------------------------
105//! 36+ | 4 | "data" | Identifies this subchunk
106//! 40+ | 4 | subchunk size | The size of this chunk. For the simple "canonical" wave file format, this will generally be the size of the file minus 44 bytes for the header data, up to and including this field.
107//! 44+ | ... | audio data | This stores the actual audio data.
108//! ... | ... | padding byte | If the length of audio data is an odd number, then an additional padding byte should be inserted.
109//!
110//! As the subchunk size is a 32-bit value, the length of audio data cannot
111//! exceed 4 GiB, and indeed the entire file can't really exceed 4 GiB as the
112//! master RIFF chunk size field is also a 32-bit value.
113//!
114//! ## Additional meta-data
115//!
116//! Wave files may also contain other metadata, such as the LIST INFO chunks
117//! defined by RIFF or other metadata. The LIST INFO chunk is analogous to
118//! the ID3 tag in an MP3 file, and if it's present, it can often be found
119//! between the "fmt " and "data" subchunks or after the end of the "data"
120//! subchunk.
121//!
122//! See also:
123//!
124//! * [WAVEFORMATEXTENSIBLE structure][6]
125//! * [Audio File Format Specifications][7]
126//! * [WAVE PCM soundfile format][8]
127//!
128//! [1]: https://github.com/ruud-v-a/hound
129//! [2]: https://www.aelius.com/njh/wavemetatools/doc/riffmci.pdf
130//! [3]: http://www.neurophys.wisc.edu/auditory/riff-format.txt
131//! [4]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa373931(v=vs.85).aspx
132//! [5]: http://www.audacityteam.org
133//! [6]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd757714(v=vs.85).aspx
134//! [7]: http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html
135//! [8]: http://soundfile.sapp.org/doc/WaveFormat/
136
137extern crate byteorder;
138
139// This is a helper macro that helps us validate results in our tests. It has
140// to be defined before the mod definitions below so that it's visible in those
141// mods.
142// Thank you bluss and durka42!
143#[cfg(test)]
144macro_rules! assert_matches {
145 ($expected:pat $(if $guard:expr)*, $value:expr) => {
146 match $value {
147 $expected $(if $guard)* => {},
148 ref actual => {
149 panic!("assertion failed: `(left matches right)` (left: `{}`, right: `{:?}`",
150 stringify!($expected), actual);
151 },
152 }
153 };
154}
155
156mod reader;
157mod writer;
158
159pub use self::reader::{ReadError, ReadErrorKind, ReadResult, WaveReader};
160pub use self::writer::{WaveWriter, WriteError, WriteResult};
161
162pub const FORMAT_UNCOMPRESSED_PCM: u16 = 1;
163pub const FORMAT_EXTENDED: u16 = 65534;
164
165pub const MIN_I24_VALUE: i32 = -8388608;
166pub const MAX_I24_VALUE: i32 = 8388607;
167
168#[derive(Debug)]
169pub enum Format {
170 UncompressedPcm,
171 Extended,
172}
173
174#[derive(Debug)]
175pub struct PcmFormat {
176 pub num_channels: u16,
177 pub sample_rate: u32,
178 pub bits_per_sample: u16,
179}