pub struct Decoder<const MAX_CHANNELS: usize> { /* private fields */ }Expand description
A chunk-based QOA audio decoder.
The Decoder uses an internal state machine to process audio data incrementally.
This design allows for seamless streaming of data and operation in no_std
environments without dynamic allocation.
§Configuration
The constant MAX_CHANNELS defines the maximum number of channels permitted
by this instance. It determines the size of internal buffer constants at
compile-time. If a source contains more channels than MAX_CHANNELS,
DecoderError::TooManyChannels will be returned.
§Buffer Requirements
These minimum sizes are required to ensure the decoder can navigate its internal state machine correctly:
- Input Buffer: A minimum of
16 * MAX_CHANNELSbytes is required to guarantee that the decoder has enough context to complete transitions between different states (e.g., header parsing or LMS parameter updates) in a single call. - Sample Buffer: A minimum capacity of
20 * MAX_CHANNELSis required. If the provided buffer is smaller, then no samples will be produced for the current call as the sampling logic cannot be entered.
§Execution Model
Because audio data can be delivered in chunks or across multiple segments,
decode may not consume all available input or produce a full frame in a
single call.
Each call returns a DecoderResult:
consumed: The number of bytes read from the input.produced: The number of samples written to the target buffer.
A return of result.consumed == 0 indicates that the input provided was
insufficient to advance the internal state machine (e.g., the input
did not contain enough bytes to finish the current header or metadata step).
§Example
use qoa::Decoder;
let mut decoder = Decoder::<2>::new();
let qoa_data = [0u8; 32]; // Minimum: 16 * MAX_CHANNELS for expected transitions
let mut sample_buffer = [0i16; 40]; // Capacity must be >= 20 * MAX_CHANNELS
if let Ok(result) = decoder.decode(&qoa_data, &mut sample_buffer) {
// Result reflects the actual progress made in this call.
}Implementations§
Source§impl<const MAX_CHANNELS: usize> Decoder<MAX_CHANNELS>
impl<const MAX_CHANNELS: usize> Decoder<MAX_CHANNELS>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, ready-to-use decoder.
A fresh decoder starts in the file-header state with its internal state
reset: the per-channel LMS state, the frame header, and all counters are
zeroed. It takes no arguments — you specify the supported channel count at
the type level when you write Decoder::<2>::new().
After construction nothing has been produced yet: a fresh decoder must
first read its 8-byte file header and then 8 bytes per channel of
frame header before it writes samples. So begin by feeding the first chunk
to Self::decode, then continue feeding further chunks on each call
until one reports consumed == 0.
Do not call Self::new to resume a partial frame — that resets the
entire decoder. Instead, keep the same instance and re-supply the bytes it
could not consume during a partial frame.
Sourcepub fn decode(
&mut self,
input: &[u8],
samples: &mut [i16],
) -> Result<DecoderResult, DecoderError>
pub fn decode( &mut self, input: &[u8], samples: &mut [i16], ) -> Result<DecoderResult, DecoderError>
Decode audio data from the input buffer.
Processes input bytes through the internal state machine, returning how many
samples were produced into the provided sample buffer and how much input was consumed.
The input slice should be at least 16 * MAX_CHANNELS bytes to allow the decoder
to complete state transitions for header parsing and LMS updates. The samples
buffer must be able to hold at least 20 * MAX_CHANNELS entries, otherwise no
samples will be produced. See Decoder for full buffer requirements. Consume any
unconsumed input bytes by including them in subsequent calls with new data until
finished or result.consumed == 0.
§Returns
Ok(DecoderResult)with updated counters on each call.DecoderErrorif magic bytes don’t match or channels exceedMAX_CHANNELS.