From 9a5f37d5d943cbf76313f7fd87b496f6de1f65a7 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Mon, 12 Oct 2020 18:40:28 +0200 Subject: [PATCH] core/soundcvt: fix convertsion of certain sample types --- nihav-core/src/soundcvt/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nihav-core/src/soundcvt/mod.rs b/nihav-core/src/soundcvt/mod.rs index b218e4a..f107925 100644 --- a/nihav-core/src/soundcvt/mod.rs +++ b/nihav-core/src/soundcvt/mod.rs @@ -97,10 +97,10 @@ impl FromFmt for u8 { fn cvt_from(val: u8) -> u8 { val } } impl FromFmt for i16 { - fn cvt_from(val: u8) -> i16 { (i16::from(val) - 128).wrapping_mul(0x101) } + fn cvt_from(val: u8) -> i16 { u16::from(val ^ 0x80).wrapping_mul(0x101) as i16} } impl FromFmt for i32 { - fn cvt_from(val: u8) -> i32 { (i32::from(val) - 128).wrapping_mul(0x01010101) } + fn cvt_from(val: u8) -> i32 { u32::from(val ^ 0x80).wrapping_mul(0x01010101) as i32 } } impl FromFmt for f32 { fn cvt_from(val: u8) -> f32 { (f32::from(val) - 128.0) / 128.0 } @@ -113,7 +113,7 @@ impl FromFmt for i16 { fn cvt_from(val: i16) -> i16 { val } } impl FromFmt for i32 { - fn cvt_from(val: i16) -> i32 { i32::from(val).wrapping_mul(0x10001) } + fn cvt_from(val: i16) -> i32 { (i32::from(val) & 0xFFFF) | (i32::from(val) << 16) } } impl FromFmt for f32 { fn cvt_from(val: i16) -> f32 { f32::from(val) / 32768.0 } @@ -136,7 +136,7 @@ impl FromFmt for u8 { fn cvt_from(val: f32) -> u8 { ((val * 128.0) + 128.0).min(255.0).max(0.0) as u8 } } impl FromFmt for i16 { - fn cvt_from(val: f32) -> i16 { (val * 32768.0).min(16383.0).max(-16384.0) as 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 } -- 2.39.5