libflo_audio/core/
audio_constants.rs

1/// Maximum positive value for 16-bit signed integer (2^15 - 1)
2pub const I16_MAX_F32: f32 = 32767.0;
3
4/// Minimum value for 16-bit signed integer (-2^15)
5pub const I16_MIN_F32: f32 = -32768.0;
6
7/// Maximum absolute value for 16-bit signed integer as f64
8pub const I16_MAX_F64: f64 = 32767.0;
9
10/// Inverse of I16_MAX_F32, used for int→float conversion (1/32767)
11pub const I16_TO_F32_SCALE: f32 = 1.0 / 32767.0;
12
13/// Inverse of I16_MIN_F32 absolute value, used for alternate int→float (1/32768)
14pub const I16_TO_F32_SCALE_ALT: f32 = 1.0 / 32768.0;
15
16/// Convert f32 sample to i32 for processing
17#[inline]
18pub fn f32_to_i32(sample: f32) -> i32 {
19    (sample * I16_MAX_F32).clamp(I16_MIN_F32, I16_MAX_F32) as i32
20}
21
22/// Convert i32 sample to f32
23#[inline]
24pub fn i32_to_f32(sample: i32) -> f32 {
25    sample as f32 * I16_TO_F32_SCALE
26}
27
28/// Convert f32 sample to i16
29#[inline]
30pub fn f32_to_i16(sample: f32) -> i16 {
31    (sample * I16_MAX_F32).clamp(I16_MIN_F32, I16_MAX_F32) as i16
32}
33
34/// Convert i16 sample to f32
35#[inline]
36pub fn i16_to_f32(sample: i16) -> f32 {
37    sample as f32 * I16_TO_F32_SCALE
38}