.clamp(min, max)
, instead of .max(min).min(max)
or .min(max).max(min)
RS-W1069.clamp(min, max)
, instead of .max(min).min(max)
or .min(max).max(min)
12 let mut i16_audio = Vec::with_capacity(f32_audio.len());
13 for sample in f32_audio {
14 // clamp the sample to [-1.0, 1.0]
15 let sample = sample.max(-1.0).min(1.0);16 i16_audio.push((sample * i16::MAX as f32) as i16);
17 }
18
Chained Ord::min
and Ord::max
functions can be replaced with Ord::clamp
, which is also a more readable solution.
fn get_in_range(value: u32, min: u32, max: u32) -> u32 {
value.max(min).min(max)
}
fn get_in_range(value: u32, min: u32, max: u32) -> u32 {
value.clamp(min, max)
}