diff options
author | Kostya Shishkov <kostya.shishkov@gmail.com> | 2020-12-23 11:41:45 +0100 |
---|---|---|
committer | Kostya Shishkov <kostya.shishkov@gmail.com> | 2020-12-23 11:41:45 +0100 |
commit | c151de2653d4d12d9310c00dbe04553e20f4807c (patch) | |
tree | 15778600da905c8ab1b228c6a8575025c26c9ccf | |
parent | 6fc1b07379bcbe390a8eaf18387f4d4e56885b7f (diff) | |
download | nihav-c151de2653d4d12d9310c00dbe04553e20f4807c.tar.gz |
core/soundcvt: clip output in f32->i32 conversion
-rw-r--r-- | nihav-core/src/soundcvt/mod.rs | 11 |
1 files changed, 10 insertions, 1 deletions
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<f32> for i16 { fn cvt_from(val: f32) -> i16 { (val * 32768.0).min(32767.0).max(-32768.0) as i16 } } impl FromFmt<f32> 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<f32> for f32 { fn cvt_from(val: f32) -> f32 { val } |