Skip to content

Commit

Permalink
Optimize requantizer to work in , then round
Browse files Browse the repository at this point in the history
  • Loading branch information
roderickvd committed Apr 9, 2021
1 parent 928a673 commit 175ba02
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion audio/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ macro_rules! convert_samples_to {
$samples
.iter()
.map(|sample| {
(*sample as f64 * (std::$type::MAX as f64 + 0.5) - 0.5) as $type >> $drop_bits
// Losslessly represent [-1.0, 1.0] to [$type::MIN, $type::MAX]
// while maintaining DC linearity. There is nothing to be gained
// by doing this in f64, as the significand of a f32 is 24 bits,
// just like the maximum bit depth we are converting to.
let int_value = *sample * (std::$type::MAX as f32 + 0.5) - 0.5;

// Casting floats to ints truncates by default, which results
// in larger quantization error than rounding arithmetically.
// Flooring is faster, but again with larger error.
int_value.round() as $type >> $drop_bits
})
.collect()
};
Expand Down

0 comments on commit 175ba02

Please sign in to comment.