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
use crate::wave::{Error, Header, Samples};
use alloc::vec;
use alloc::vec::Vec;
use core::convert::TryInto;

// @TODO: https://exiftool.org/TagNames/RIFF.html#Info
#[derive(Debug, PartialEq, Clone)]
pub enum ChunkID {
    RIFF,
    FMT,
    LIST,
    DATA,
    JUNK,
    WAVE,
}

impl ChunkID {
    fn from_bytes(bytes: &[u8; 4]) -> Result<ChunkID, Error> {
        match bytes {
            [b'R', b'I', b'F', b'F'] => Ok(ChunkID::RIFF),
            [b'f', b'm', b't', b' '] => Ok(ChunkID::FMT),
            [b'L', b'I', b'S', b'T'] => Ok(ChunkID::LIST),
            [b'd', b'a', b't', b'a'] => Ok(ChunkID::DATA),
            [b'J', b'U', b'N', b'K'] => Ok(ChunkID::JUNK),
            [b'W', b'A', b'V', b'E'] => Ok(ChunkID::WAVE),
            _ => Err(Error::UnknownChunkID(bytes.clone())),
        }
    }
}

#[derive(Debug)]
pub enum Chunk {
    FMT(Header),
    DATA(Samples),
    Unknown(ChunkID, Vec<u8>),
}

impl Chunk {
    fn from_bytes_with_id_and_header(
        header: &Header,
        id: &ChunkID,
        bytes: &[u8],
    ) -> Result<Option<Chunk>, Error> {
        let chunk = match id {
            ChunkID::DATA => {
                let samples = Samples::from_bytes(header, bytes)?;
                Some(Chunk::DATA(samples))
            }
            _ => None,
        };

        Ok(chunk)
    }

    fn from_bytes_with_id(id: &ChunkID, bytes: &[u8]) -> Result<Option<Chunk>, Error> {
        let chunk = match id {
            ChunkID::FMT => {
                let header = Header::from_bytes(bytes)?;
                Some(Chunk::FMT(header))
            }
            _ => None,
        };

        Ok(chunk)
    }
}

fn parse_chunk(bytes: &[u8]) -> Result<(ChunkID, &[u8], &[u8]), Error> {
    let chunk_id = bytes[0..4]
        .try_into()
        .map_err(|e| Error::CantParseSliceInto(e))
        .and_then(ChunkID::from_bytes)?;

    let chunk_size = bytes[4..8]
        .try_into()
        .map_err(|e| Error::CantParseSliceInto(e))
        .map(|b| u32::from_le_bytes(b))?;

    let (start, end) = match chunk_id {
        ChunkID::RIFF => (12, 8 + chunk_size as usize),
        _ => (8, 8 + chunk_size as usize),
    };

    Ok((chunk_id, &bytes[start..end], &bytes[end..]))
}

pub fn parse_chunks(bytes: &[u8]) -> Result<(Vec<Chunk>, Option<Vec<Chunk>>), Error> {
    let (chunk_id, wave_data, _) = parse_chunk(bytes)?;

    if chunk_id != ChunkID::RIFF {
        return Err(Error::NoRiffChunkFound);
    }

    let (chunk_id, fmt_data, tail) = parse_chunk(wave_data)?;

    let first_chunk = Chunk::from_bytes_with_id(&chunk_id, fmt_data)?
        .ok_or(Error::CantParseChunk(chunk_id.clone()))?;

    let header = match &first_chunk {
        Chunk::FMT(header) => Ok(header.clone()),
        _ => return Err(Error::NoRiffChunkFound),
    }?;

    let mut chunks = vec![first_chunk];
    let mut unknown_chunks: Option<Vec<Chunk>> = None;
    let mut tail = tail;

    loop {
        if tail.len() == 0 {
            break;
        }

        let (chunk_id, chunk_bytes, new_tail) = parse_chunk(tail)?;
        tail = new_tail;

        match Chunk::from_bytes_with_id_and_header(&header, &chunk_id, chunk_bytes)? {
            Some(chunk) => chunks.push(chunk),
            None => {
                let chunk = Chunk::Unknown(chunk_id.clone(), chunk_bytes.into());
                match unknown_chunks.as_mut() {
                    Some(v) => v.push(chunk),
                    None => {
                        unknown_chunks.replace(vec![chunk]);
                    }
                }
            }
        }
    }

    Ok((chunks, unknown_chunks))
}