mbelib_rs/
error.rs

1// SPDX-FileCopyrightText: 2026 Swift Raccoon
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4//! Error types for the AMBE 3600×2400 decoder.
5//!
6//! The AMBE codec uses Golay(23,12) and Hamming(15,11) forward error
7//! correction to protect the parameter bits. When the channel
8//! introduces more errors than the FEC can correct, the decoder
9//! detects this via syndrome analysis and reports it here.
10
11use core::fmt;
12
13/// Errors that can occur during AMBE frame decoding.
14///
15/// These errors represent conditions where the codec cannot produce
16/// reliable audio output. The decoder handles them internally by
17/// repeating the previous frame's parameters (up to 3 times) and
18/// then muting to silence. Callers generally do not need to inspect
19/// these — [`AmbeDecoder::decode_frame`](crate::AmbeDecoder::decode_frame)
20/// always returns a valid PCM buffer, using silence as the fallback.
21#[derive(Debug, Clone, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum DecodeError {
24    /// Too many bit errors for the FEC to correct.
25    ///
26    /// The Golay and Hamming decoders detected more errors than their
27    /// correction capacity (3 bits for Golay, 1 bit for Hamming). The
28    /// decoded parameters are unreliable and should not be used for
29    /// synthesis.
30    ExcessiveErrors {
31        /// Errors detected in the C0 codeword (Golay-protected).
32        ///
33        /// C0 carries the fundamental frequency index (b0), the most
34        /// critical parameter. Even 1 error here can shift the pitch
35        /// drastically.
36        c0_errors: u32,
37        /// Total errors across all four codewords (C0–C3).
38        total_errors: u32,
39    },
40}
41
42impl fmt::Display for DecodeError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            Self::ExcessiveErrors {
46                c0_errors,
47                total_errors,
48            } => {
49                write!(
50                    f,
51                    "excessive AMBE bit errors: {c0_errors} in C0, {total_errors} total"
52                )
53            }
54        }
55    }
56}
57
58impl std::error::Error for DecodeError {}