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
use crate::sound::NextSample;
use crate::Sound;
use qoaudio::{DecodeError, QoaDecoder as RawDecoder, QoaItem};
use std::io::Read;

/// Decoder for the [QOA](https://qoaformat.org/) format.
pub struct QoaDecoder<R>
where
    R: Read + Send,
{
    raw_decoder: RawDecoder<R>,
    sample_rate: u32,
    channel_count: u16,
}

impl<R> QoaDecoder<R>
where
    R: Read + Send,
{
    /// Attempts to decode the data as QOA audio.
    pub fn new(data: R) -> Result<QoaDecoder<R>, DecodeError> {
        let mut raw_decoder = RawDecoder::new(data)?;

        let QoaItem::FrameHeader(first_frame) = raw_decoder
            .next()
            .ok_or(DecodeError::InvalidFrameHeader)??
        else {
            return Err(DecodeError::InvalidFrameHeader);
        };
        let sample_rate = first_frame.sample_rate;
        let channel_count = first_frame.num_channels as u16;

        Ok(QoaDecoder {
            raw_decoder,
            sample_rate,
            channel_count,
        })
    }

    /// Return the wrapped Reader
    pub fn into_inner(self) -> R {
        self.raw_decoder.into_inner()
    }
}

impl<R> Sound for QoaDecoder<R>
where
    R: Read + Send,
{
    fn channel_count(&self) -> u16 {
        self.channel_count
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn next_sample(&mut self) -> Result<NextSample, crate::Error> {
        loop {
            let Some(next_sample) = self.raw_decoder.next() else {
                return Ok(NextSample::Finished);
            };
            let next_sample = next_sample?;

            match next_sample {
                QoaItem::Sample(s) => return Ok(NextSample::Sample(s)),
                QoaItem::FrameHeader(f) => {
                    if f.num_channels as u16 != self.channel_count
                        || f.sample_rate != self.sample_rate
                    {
                        self.channel_count = f.num_channels.into();
                        self.sample_rate = f.sample_rate;
                        return Ok(NextSample::MetadataChanged);
                    }
                    // No metadata change. Continue and read next sample
                    continue;
                }
            }
        }
    }

    fn on_start_of_batch(&mut self) {}
}

impl From<DecodeError> for crate::Error {
    fn from(value: DecodeError) -> Self {
        match value {
            DecodeError::IoError(e) => e.into(),
            DecodeError::NotQoaFile
            | DecodeError::NoSamples
            | DecodeError::InvalidFrameHeader
            | DecodeError::IncompatibleFrame => crate::Error::FormatError(Box::new(value)),
        }
    }
}

#[cfg(test)]
#[path = "./tests/qoa.rs"]
mod tests;