From: Kostya Shishkov Date: Wed, 23 Dec 2020 10:41:45 +0000 (+0100) Subject: core/soundcvt: clip output in f32->i32 conversion X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=c151de2653d4d12d9310c00dbe04553e20f4807c;p=nihav.git core/soundcvt: clip output in f32->i32 conversion --- diff --git a/nihav-core/src/soundcvt/mod.rs b/nihav-core/src/soundcvt/mod.rs index 154b59f..17d7709 100644 --- a/nihav-core/src/soundcvt/mod.rs +++ b/nihav-core/src/soundcvt/mod.rs @@ -139,7 +139,16 @@ impl FromFmt for i16 { fn cvt_from(val: f32) -> i16 { (val * 32768.0).min(32767.0).max(-32768.0) as i16 } } impl FromFmt for i32 { - fn cvt_from(val: f32) -> i32 { (val * 31.0f32.exp2()) as i32 } + fn cvt_from(val: f32) -> i32 { + if val >= 1.0 { + std::i32::MAX + } else if val <= -1.0 { + std::i32::MIN + } else { + let scale = (1u32 << 31) as f32; + (val * scale) as i32 + } + } } impl FromFmt for f32 { fn cvt_from(val: f32) -> f32 { val }