use crate::common::complex::Scaler;
pub fn scale_and_round(x_hat_ltpf: &[Scaler], _bits_per_audio_sample_dec: usize, x_hat_clip: &mut [i16]) {
for (to, from) in x_hat_clip.iter_mut().zip(x_hat_ltpf) {
let tmp = if *from > 0. {
(*from + 0.5) as i32
} else {
(*from - 0.5) as i32
};
*to = tmp.min(32767).max(-32768) as i16;
}
}
#[cfg(test)]
mod tests {
extern crate std;
use super::*;
#[test]
fn scale_and_round_test() {
let x_hat_ltpf = [0.0, -0.4, -0.5, -0.6, 0.4, 0.5, 0.6, 32767.6, -32768.6];
let mut x_hat_clip = [0; 9];
scale_and_round(&x_hat_ltpf, 16, &mut x_hat_clip);
assert_eq!(x_hat_clip, [0, 0, -1, -1, 0, 1, 1, 32767, -32768])
}
}