From 56e3b02de7f67da4b94bbcb504b05c407502b76c Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 29 Sep 2024 21:15:31 +0200 Subject: Apply asin to input samples --- src/main.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e35062e..8519158 100644 --- a/src/main.rs +++ b/src/main.rs @@ -270,10 +270,20 @@ fn main() { } } - // Downmix stereo to mono, apply (v/2 + i16::MAX/2), and normalise to [-1.0; 1.0] + // Downmix stereo to mono, apply (v/2 + i16::MAX/2), + // normalise to [0.0; 1.0], + // take the arcsine to compensate for the phasing, + // and divide by PI/2 to normalise to [0.0; 1.0] + use std::f32::consts::FRAC_2_PI; let buf: Vec = buf .chunks_exact(2) - .map(|v| ((v[0]/2 + v[1]/2)/2 + i16::MAX/2) as f32 / 32768.0) + .map(|v| { + let samp = ((v[0]/2 + v[1]/2)/2 + i16::MAX/2) as f32 / 32768.0; + if samp < -1.0 || samp > 1.0 { + panic!("sample normalistion broken") + } + f32::asin(samp) * FRAC_2_PI + }) .collect(); if let Some(w) = &mut in_debug { -- cgit v1.2.3