mcelp 1.0.1

Mitsubishi CELP speech codec: a 3.6 kbit/s speech encoder and decoder
Documentation
//! Values shared by the two fixed-codebook search strategies.

use crate::fixed::{acc, hi};

/// Everything the fixed-codebook search needs from the rest of the subframe.
///
/// Re-exported from [`crate::pulses`] as part of that module's existing API.
pub struct Search<'a> {
    /// The weighted target, and the adaptive codebook's filtered contribution.
    pub target: &'a [i16],
    pub contribution: &'a [i16],
    /// The weighted synthesis filter's impulse response.
    pub impulse: &'a [i16],
    /// The lag the adaptive codebook settled on, and its fraction.
    pub lag: i16,
    pub fraction: i16,
    /// The two extension gains carried between subframes.
    pub extension: (i16, i16),
    /// How the subframe is being coded, and which row of the weight table the
    /// line spectrum's own mode selects.
    pub coding: i16,
    pub weights: usize,
}

/// Keep a candidate when its squared-correlation/energy ratio takes the lead.
pub(crate) fn takes_lead(best: &mut (i64, i64), sum: i64, energy: i64) -> bool {
    if acc(sum) <= 0 {
        return false;
    }
    let squared = hi(acc((hi(sum) as i64) * (hi(sum) as i64) * 2)) as i64;
    if acc(best.0 * squared * 2 - energy * best.1 * 2) <= 0 {
        return false;
    }
    *best = (energy, squared);
    true
}