wavers 1.5.1

A library for reading and writing wav files.
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Module containing functions and structs for working with Wav file headers.
use std::{
    any::TypeId,
    collections::HashMap,
    fmt::{Debug, Display},
    io::SeekFrom,
};

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

#[cfg(feature = "colored")]
use colored::Colorize;

use crate::{
    chunks::{
        fmt::{CbSize, ExtFmtChunkInfo, FMT_CB_SIZE, FMT_SIZE_BASE_SIZE, FMT_SIZE_EXTENDED_SIZE},
        read_chunk, Chunk, FmtChunk, DATA, FMT, RIFF, WAVE,
    },
    conversion::AudioSample,
    core::{ReadSeek, WavInfo},
    error::{WaversError, WaversResult},
    log,
    wav_type::{format_info_to_wav_type, FormatCode, WavType},
};

const _RIFF_SIZE: usize = 4; // do not count the size or RIFF id, only the WAVE id. The other chunks will be used for the remaining size
const HEADER_FMT_BASE_SIZE: usize = 36;
const HEADER_FMT_CB_SIZE: usize = 38;
const HEADER_FMT_EXTENDED_SIZE: usize = 60;
/// A struct used to store the offset and size of a chunk
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct HeaderChunkInfo {
    pub offset: usize,
    pub size: u32,
}

impl Display for HeaderChunkInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "(offset: {}, size: {})", self.offset, self.size)
    }
}

impl HeaderChunkInfo {
    /// Constructs a new HeaderEntryInfo struct with a given offset and size.
    pub fn new(offset: usize, size: u32) -> Self {
        HeaderChunkInfo { offset, size }
    }
}

impl Into<(usize, u32)> for HeaderChunkInfo {
    fn into(self) -> (usize, u32) {
        (self.offset, self.size)
    }
}

impl Into<(usize, u32)> for &HeaderChunkInfo {
    fn into(self) -> (usize, u32) {
        (self.offset, self.size)
    }
}

impl PartialOrd for HeaderChunkInfo {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.offset.cmp(&other.offset))
    }
}

impl Ord for HeaderChunkInfo {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.offset.cmp(&other.offset)
    }
}

#[cfg(feature = "pyo3")]
#[pyclass]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WavHeader {
    header_info: HashMap<ChunkIdentifier, HeaderChunkInfo>,
    #[pyo3(get)]
    pub fmt_chunk: FmtChunk,
    #[pyo3(get)]
    pub current_file_size: usize, // convenience field for keeping track of the current file size
}

/// A struct representing the header of a wav file. It stores the offset and size of each chunk in the header,
/// the format information and the current size of the file in bytes.
#[cfg(not(feature = "pyo3"))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WavHeader {
    pub header_info: HashMap<ChunkIdentifier, HeaderChunkInfo>,
    pub fmt_chunk: FmtChunk,
    pub current_file_size: usize, // convenience field for keeping track of the current file size
}

impl WavHeader {
    /// Constructs a new WavHeader struct using the provided header information.
    pub fn new(
        header_info: HashMap<ChunkIdentifier, HeaderChunkInfo>,
        fmt_chunk: FmtChunk,
        current_file_size: usize,
    ) -> Self {
        assert!(
            header_info.contains_key(&DATA.into()),
            "Header info must contain a DATA chunk"
        );
        log!(
            log::Level::Debug,
            "Creating new WavHeader with fmt chunk: {:?}",
            fmt_chunk
        );
        WavHeader {
            header_info,
            fmt_chunk,
            current_file_size,
        }
    }

    /// Returns the information relating to the DATA chunk.
    pub fn data(&self) -> &HeaderChunkInfo {
        self.header_info.get(&DATA.into()).unwrap() // Safe since a header cannot be created without a DATA chunk
    }

    /// Returns the information relating to the FMT chunk.
    pub fn fmt(&self) -> &HeaderChunkInfo {
        self.header_info.get(&FMT.into()).unwrap() // Safe since a header cannot be created without a FMT chunk
    }

    /// Creates a new WavHeader with the given sample rate, number of channels and number of samples.
    /// This function inserts the required fields into the WavHeader such as RIFF, WAVE, FMT and DATA.
    pub fn new_header<T>(sample_rate: i32, n_channels: u16, n_samples: usize) -> WaversResult<Self>
    where
        T: AudioSample,
    {
        log!(
            log::Level::Debug,
            "Creating new header with sample rate: {}, channels: {}, samples: {}",
            sample_rate,
            n_channels,
            n_samples
        );
        let wav_type: WavType = TypeId::of::<T>().try_into()?;

        // Calculate sizes of data chunks
        let bits_per_sample = wav_type.n_bits();
        let data_size_bytes = n_samples * (bits_per_sample / 8) as usize;
        let fmt_data_size = match wav_type {
            WavType::Pcm16 | WavType::Pcm24 | WavType::Pcm32 | WavType::Float32
            | WavType::Float64=> FMT_SIZE_BASE_SIZE,
            WavType::EPcm16
            | WavType::EPcm24
            | WavType::EPcm32
            | WavType::EFloat32
            | WavType::EFloat64 => FMT_SIZE_EXTENDED_SIZE,
        };

        let mut header_info: HashMap<ChunkIdentifier, HeaderChunkInfo> = HashMap::new();

        // The RIFF chunk size should be:
        // sizeof("WAVE") + sizeof(fmt_chunk) + sizeof(data_chunk)
        // where each chunk includes its header (identifier + size field)
        let riff_chunk_size = 4 +                     // "WAVE"
        (8 + fmt_data_size) +  // "fmt " + size + data
        (8 + data_size_bytes); // "data" + size + data

        header_info.insert(
            RIFF.into(),
            HeaderChunkInfo::new(0, (riff_chunk_size - 8) as u32),
        );
        header_info.insert(FMT.into(), HeaderChunkInfo::new(12, fmt_data_size as u32));
        header_info.insert(
            DATA.into(),
            HeaderChunkInfo::new(20 + fmt_data_size, data_size_bytes as u32),
        );

        // Total file size includes everything
        let current_file_size = 8 + riff_chunk_size - 8; // RIFF + size + (chunk_size - 8)

        // Create fmt chunk
        let (main_format, sub_format) = match wav_type {
            WavType::Pcm16 | WavType::Pcm24 | WavType::Pcm32 => {
                (FormatCode::WAV_FORMAT_PCM, FormatCode::WAV_FORMAT_PCM)
            }
            WavType::Float32 | WavType::Float64 => (
                FormatCode::WAV_FORMAT_IEEE_FLOAT,
                FormatCode::WAV_FORMAT_IEEE_FLOAT,
            ),
            WavType::EPcm16 | WavType::EPcm24 | WavType::EPcm32 => (
                FormatCode::WAVE_FORMAT_EXTENSIBLE,
                FormatCode::WAV_FORMAT_PCM,
            ),
            WavType::EFloat32 | WavType::EFloat64 => (
                FormatCode::WAVE_FORMAT_EXTENSIBLE,
                FormatCode::WAV_FORMAT_IEEE_FLOAT,
            ),
        };

        let ext_fmt_chunk = ExtFmtChunkInfo::new(CbSize::Base, bits_per_sample, 0, sub_format);

        let fmt_chunk = FmtChunk::new(
            main_format,
            n_channels,
            sample_rate,
            bits_per_sample,
            ext_fmt_chunk,
        );

        Ok(WavHeader {
            header_info,
            fmt_chunk,
            current_file_size,
        })
    }

    /// Returns the current size of the file in bytes.
    pub fn file_size(&self) -> usize {
        self.current_file_size
    }

    /// Returns the header in bytes, assuming that the FmtChunk is in the base format.
    pub fn as_base_bytes(&self) -> [u8; HEADER_FMT_BASE_SIZE] {
        let mut bytes = [0; HEADER_FMT_BASE_SIZE];
        bytes[0..4].copy_from_slice(&RIFF);
        let size = self.file_size() as u32;
        bytes[4..8].copy_from_slice(&size.to_ne_bytes());
        bytes[8..12].copy_from_slice(&WAVE);

        let fmt_bytes = self.fmt_chunk.as_bytes();
        debug_assert!(
            fmt_bytes.len() == FMT_SIZE_BASE_SIZE + 8,
            "Fmt bytes length is not equal to the expected length: {} vs {}",
            fmt_bytes.len(),
            FMT_SIZE_BASE_SIZE + 8
        );
        bytes[12..12 + fmt_bytes.len()].copy_from_slice(&fmt_bytes);
        bytes
    }

    /// Returns the header in bytes, assuming that the FmtChunk is in the cb (has cb field but no other extensible format fields) format.
    pub fn as_cb_bytes(&self) -> [u8; HEADER_FMT_CB_SIZE] {
        let mut bytes = [0; HEADER_FMT_CB_SIZE];
        bytes[0..4].copy_from_slice(&RIFF);
        let size = self.file_size() as u32;
        bytes[4..8].copy_from_slice(&size.to_ne_bytes());
        bytes[8..12].copy_from_slice(&WAVE);

        let fmt_bytes = self.fmt_chunk.as_bytes();
        debug_assert!(
            fmt_bytes.len() == FMT_CB_SIZE + 8,
            "Fmt bytes length is not equal to the expected length: {} vs {}",
            fmt_bytes.len(),
            FMT_CB_SIZE + 8
        );
        bytes[12..12 + fmt_bytes.len()].copy_from_slice(&fmt_bytes);

        bytes
    }

    /// Returns the header in bytes, assuming that the FmtChunk is in the extensible format.
    pub fn as_extended_bytes(&self) -> [u8; HEADER_FMT_EXTENDED_SIZE] {
        let mut bytes = [0; HEADER_FMT_EXTENDED_SIZE];
        bytes[0..4].copy_from_slice(&RIFF);
        let size = self.file_size() as u32;
        bytes[4..8].copy_from_slice(&size.to_ne_bytes());
        bytes[8..12].copy_from_slice(&WAVE);
        bytes[12..16].copy_from_slice(&FMT);
        bytes[16..20].copy_from_slice(&(FMT_SIZE_EXTENDED_SIZE as u32).to_ne_bytes());
        let fmt_bytes: [u8; FMT_SIZE_EXTENDED_SIZE] = self.fmt_chunk.extended_bytes();
        bytes[20..60].copy_from_slice(&fmt_bytes);
        bytes
    }

    /// Attempt to get some chunk information from the header. Returns None if the chunk is not found.
    pub fn get_chunk_info(&self, chunk_identifier: ChunkIdentifier) -> Option<&HeaderChunkInfo> {
        self.header_info.get(&chunk_identifier)
    }
}

#[cfg(feature = "colored")]
impl Display for WavHeader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut header_info_string = String::new();
        header_info_string.push_str(
            "Header Info:\n"
                .white()
                .bold()
                .underline()
                .to_string()
                .as_str(),
        );
        let mut sorted_info: Vec<(&ChunkIdentifier, &HeaderChunkInfo)> =
            self.header_info.iter().collect();

        sorted_info.sort_by(|a, b| a.1.cmp(b.1));

        for (chunk_id, chunk_info) in sorted_info {
            let k = format!("{:?}", chunk_id).green().bold();
            let v = format!("{:?}", chunk_info).white();
            header_info_string.push_str(&format!("\t{}: {}\n", k, v));
        }

        let current_file_size = format!("Current file size: ").white().bold().underline();
        let current_size = format!("{}", self.current_file_size).white();
        write!(
            f,
            "{}\n{}\n{}{}",
            header_info_string, self.fmt_chunk, current_file_size, current_size
        )
    }
}

#[cfg(not(feature = "colored"))]
impl Display for WavHeader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut header_info_string = String::new();
        header_info_string.push_str("Header Info:\n");

        let mut sorted_info: Vec<(&ChunkIdentifier, &HeaderChunkInfo)> =
            self.header_info.iter().collect();

        sorted_info.sort_by(|a, b| a.1.cmp(b.1));

        for (chunk_id, chunk_info) in sorted_info {
            let k = format!("{:?}", chunk_id);
            let v = format!("{:?}", chunk_info);
            header_info_string.push_str(&format!("\t{}: {}\n", k, v));
        }

        let current_file_size = format!("Current file size: ");
        let current_size = format!("{}", self.current_file_size);

        write!(
            f,
            "{}\n{}\n{}{}",
            header_info_string, self.fmt_chunk, current_file_size, current_size
        )
    }
}

/// Wrapper around a 4 byte buffer. Used for storing and displaying/debugging the chunk identifier of a chunk.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct ChunkIdentifier {
    identifier: [u8; 4],
}

impl ChunkIdentifier {
    pub fn new(identifier: [u8; 4]) -> Self {
        ChunkIdentifier { identifier }
    }
}

impl Into<[u8; 4]> for ChunkIdentifier {
    fn into(self) -> [u8; 4] {
        self.identifier
    }
}

impl Into<ChunkIdentifier> for [u8; 4] {
    fn into(self) -> ChunkIdentifier {
        ChunkIdentifier::new(self)
    }
}

impl AsRef<[u8; 4]> for ChunkIdentifier {
    fn as_ref(&self) -> &[u8; 4] {
        &self.identifier
    }
}

impl Display for ChunkIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let as_str: &str = match std::str::from_utf8(&self.identifier) {
            Ok(s) => s,
            Err(_) => "Invalid identifier",
        };
        write!(f, "{:?}", as_str)
    }
}

impl Debug for ChunkIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let out_str = match std::str::from_utf8(&self.identifier) {
            Ok(s) => s,
            Err(_) => "Invalid identifier",
        };
        write!(f, "{}", out_str)
    }
}

/// Reads the header of a wav file and returns a tuple containing the header information and the wav encoding.
/// Mostly for convenience, but can also be used to inspect a wav file without reading the data.
pub(crate) fn read_header(readable: &mut Box<dyn ReadSeek>) -> WaversResult<WavInfo> {
    // reset the buffer reader to the start of the file
    readable.seek(SeekFrom::Start(0))?;

    let header_info: HashMap<ChunkIdentifier, HeaderChunkInfo> =
        discover_all_header_chunks(readable)?;

    match header_info.contains_key(&FMT.into()) {
        true => (),
        false => {
            return Err(WaversError::from(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "File does not contain a fmt chunk",
            )));
        }
    }

    let fmt_entry = header_info.get(&FMT.into()).unwrap(); // Safe since we just checked that the key exists
    let fmt_chunk: FmtChunk = read_chunk::<FmtChunk>(readable, fmt_entry)?;

    let wav_type = format_info_to_wav_type((
        fmt_chunk.format,
        fmt_chunk.bits_per_sample,
        fmt_chunk.format(),
    ))?;

    let total_size = header_info.get(&RIFF.into()).unwrap().size as usize + 8;
    let wav_header = WavHeader::new(header_info, fmt_chunk, total_size);

    Ok(WavInfo {
        wav_type,
        wav_header,
    })
}

// This shouldn't cause too many performance issues. Would wager than there is only ever the core header chunks and maybe a handful more.
// Each iteration is simply just a read of 8 (4+4) bytes.
fn discover_all_header_chunks(
    reader: &mut Box<dyn ReadSeek>,
) -> WaversResult<HashMap<ChunkIdentifier, HeaderChunkInfo>> {
    let mut entries: HashMap<ChunkIdentifier, HeaderChunkInfo> = HashMap::new();

    // create a reusable buffer for reading header chunks
    let mut buf: [u8; 4] = [0; 4];
    // The first 4 bytes of the file should be the RIFF chunk
    reader.read_exact(&mut buf)?;
    match buf_eq(&RIFF, &buf) {
        true => (),
        false => {
            return Err(WaversError::from(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "File is not a valid RIFF file",
            )));
        }
    }

    reader.read_exact(&mut buf)?; // read the next 4 bytes which should be the size of the file

    let file_size: u32 =
        buf[0] as u32 | (buf[1] as u32) << 8 | (buf[2] as u32) << 16 | (buf[3] as u32) << 24;
    entries.insert(RIFF.into(), HeaderChunkInfo::new(0, file_size as u32));

    // The next 4 bytes should be the RIFF type id
    reader.read_exact(&mut buf)?;
    let _: ChunkIdentifier = buf.into();

    while let Ok(_) = reader.read_exact(&mut buf) {
        let chunk_identifier: ChunkIdentifier = buf.into();
        reader.read_exact(&mut buf)?;
        let chunk_size: u32 =
            buf[0] as u32 | (buf[1] as u32) << 8 | (buf[2] as u32) << 16 | (buf[3] as u32) << 24;
        entries.insert(
            chunk_identifier,
            HeaderChunkInfo::new(reader.stream_position()? as usize - 8, chunk_size),
        );

        reader.seek(SeekFrom::Current(chunk_size as i64))?;
    }

    Ok(entries)
}

#[inline(always)]
fn buf_eq(buf: &[u8; 4], chunk_id: &[u8; 4]) -> bool {
    buf[0] == chunk_id[0] && buf[1] == chunk_id[1] && buf[2] == chunk_id[2] && buf[3] == chunk_id[3]
}

#[cfg(test)]
mod header_tests {
    use super::*;
    use std::fs::File;

    const TEST_FILE: &str = "./test_resources/one_channel_i16.wav";
    const TWO_CHANNEL_TEST_FILE: &str = "./test_resources/two_channel_i16.wav";

    const ONE_CHANNEL_FMT_CHUNK: FmtChunk = FmtChunk {
        format: FormatCode::WAV_FORMAT_PCM,
        channels: 1,
        sample_rate: 16000,
        byte_rate: 16000 * 2 * 1,
        block_align: 2,
        bits_per_sample: 16,
        ext_fmt_chunk: ExtFmtChunkInfo::new(CbSize::Base, 16, 0, FormatCode::WAV_FORMAT_PCM),
    };

    #[test]
    fn can_read_header() {
        let file = File::open(TEST_FILE).unwrap();
        let mut file = Box::new(file) as Box<dyn ReadSeek>;
        let wav_info = read_header(&mut file).expect("Failed to read header");
        assert_eq!(
            wav_info.wav_header.fmt_chunk, ONE_CHANNEL_FMT_CHUNK,
            "Fmt chunk does not match"
        );
    }

    #[test]
    fn can_convert_to_and_from_bytes() {
        let file = File::open(TEST_FILE).unwrap();
        let mut file = Box::new(file) as Box<dyn ReadSeek>;
        let wav_info = read_header(&mut file).expect("Failed to read header");
        let fmt_bytes = wav_info.wav_header.fmt_chunk.base_bytes();
        let new_fmt = FmtChunk::from_bytes(&fmt_bytes);
        assert_eq!(
            wav_info.wav_header.fmt_chunk, new_fmt,
            "Fmt chunk does not match"
        );
    }

    #[test]
    fn test_printing() {
        let file = File::open(TEST_FILE).unwrap();
        let mut file = Box::new(file) as Box<dyn ReadSeek>;
        let wav_info = read_header(&mut file).expect("Failed to read header");
        println!("{}", wav_info.wav_header);
    }

    #[test]
    fn test_size() {
        let file = File::open(TEST_FILE).unwrap();
        let mut file = Box::new(file) as Box<dyn ReadSeek>;
        let wav_info = read_header(&mut file).expect("Failed to read header");
        assert_eq!(
            wav_info.wav_header.file_size(),
            320044,
            "File size does not match"
        );

        let file = File::open(TWO_CHANNEL_TEST_FILE).unwrap();
        let mut file = Box::new(file) as Box<dyn ReadSeek>;
        let wav_info = read_header(&mut file).expect("Failed to read header");
        assert_eq!(
            wav_info.wav_header.file_size(),
            640044,
            "File size does not match"
        );
    }
}