1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! A struct that maps a range of values to the normalized range `[0.0, 1.0]` using various
//! gradients, useful for DSP applications.
//!
//! (prerelease)

#[cfg(test)]
mod tests;

use std::fmt::Debug;

mod discrete;
mod linear;
mod linear_base;
mod log2;
mod power;

pub use discrete::{DiscreteMapF32, DiscreteMapF64};
pub use linear::{LinearMapF32, LinearMapF64};
pub use log2::{Log2MapF32, Log2MapF64};
pub use power::{PowerMapF32, PowerMapF64};

/// The type of mapping to use
pub enum MapperF32 {
    /// Linear mapping
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// will be linearly mapped, not the raw amplitude.
    Lin(LinearMapF32),
    /// Exponential mapping where the normalized value is raised to the
    /// supplied exponent.
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// will be linearly mapped, not the raw amplitude.
    Pow(PowerMapF32),
    /// Logarithmic mapping using `log2`
    Log2(Log2MapF32),
    /// Discrete `isize` integer mapping
    ///
    /// A supplied enum may be used as well as long
    /// as it implements `From<isize> + Into<isize> + Copy + Clone`.
    Discrete(DiscreteMapF32),
}

/// The type of mapping to use
pub enum MapperF64 {
    /// Linear mapping
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// will be linearly mapped, not the raw amplitude.
    Lin(LinearMapF64),
    /// Exponential mapping where the normalized value is raised to the
    /// supplied exponent.
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// will be linearly mapped, not the raw amplitude.
    Pow(PowerMapF64),
    /// Logarithmic mapping using `log2`
    Log2(Log2MapF64),
    /// Discrete `isize` integer mapping
    ///
    /// A supplied enum may be used as well as long
    /// as it implements `From<isize> + Into<isize> + Copy + Clone`.
    Discrete(DiscreteMapF64),
}

#[derive(Debug)]
/// The unit to use
pub enum Unit {
    /// Generic units
    Generic,
    /// Decibel units
    Decibels,
}

/// A mapper than maps a range of values to and from the normalized
/// `f32` range `[0.0, 1.0]`.
pub struct NormalMapF32 {
    /// The current mapper in use
    pub mapper: MapperF32,
}

impl NormalMapF32 {
    /// Create a new `NormalMap` with linear mapping.
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// are what will be linearly mapped, not the raw amplitude.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range
    /// * max - the maximum of the range
    /// * unit - the type of unit
    pub fn linear(min: f32, max: f32, unit: Unit) -> Self {
        Self {
            mapper: MapperF32::Lin(LinearMapF32::new(min, max, unit)),
        }
    }

    /// Create a new `NormalMap` with an exponential mapping where the
    /// normalized value is raised to the supplied exponent.
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// are what will be mapped, not the raw amplitude.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range
    /// * max - the maximum of the range
    /// * exponent - the exponent to raise the normalized value to
    /// * unit - the type of unit
    ///
    /// # Panics
    ///
    /// * Panics when `exponent = 0.0`.
    pub fn power(min: f32, max: f32, exponent: f32, unit: Unit) -> Self {
        Self {
            mapper: MapperF32::Pow(PowerMapF32::new(min, max, exponent, unit)),
        }
    }

    /// Create a new `NormalMap` with a logarithmic mapping using `log2`.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range, must be > 0.0
    /// * max - the maximum of the range, must be > 0.0
    ///
    /// # Panics
    ///
    /// * Panics when either `min` or `max` <= 0.0.
    pub fn log2(min: f32, max: f32) -> Self {
        Self {
            mapper: MapperF32::Log2(Log2MapF32::new(min, max)),
        }
    }

    /// Create a new `NormalMap` with a discrete `isize` integer range.
    ///
    /// A supplied enum may be used as well as long
    /// as it implements `From<isize> + Into<isize> + Copy + Clone`.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range
    /// * max - the maximum of the range
    pub fn discrete<T>(min: T, max: T) -> Self
    where
        T: From<isize> + Into<isize> + Copy + Clone,
    {
        Self {
            mapper: MapperF32::Discrete(DiscreteMapF32::new(min, max)),
        }
    }

    /// Map an `f32` value to the normalized range `[0.0, 1.0]`.
    pub fn normalize(&self, value: f32) -> f32 {
        match &self.mapper {
            MapperF32::Lin(mapper) => mapper.normalize(value),
            MapperF32::Pow(mapper) => mapper.normalize(value),
            MapperF32::Log2(mapper) => mapper.normalize(value),
            MapperF32::Discrete(mapper) => mapper.normalize_f32(value),
        }
    }

    /// Map an array of `f32` values to the normalized range `[0.0, 1.0]`.
    ///
    /// Values will be processed up to the length of the shortest array.
    pub fn normalize_array(&self, in_values: &[f32], out_normalized: &mut [f32]) {
        match &self.mapper {
            MapperF32::Lin(mapper) => mapper.normalize_array(in_values, out_normalized),
            MapperF32::Pow(mapper) => mapper.normalize_array(in_values, out_normalized),
            MapperF32::Log2(mapper) => mapper.normalize_array(in_values, out_normalized),
            MapperF32::Discrete(mapper) => mapper.normalize_array_f32(in_values, out_normalized),
        }
    }

    /// Un-map a normalized value to the corresponding `f32` value.
    pub fn denormalize(&self, normalized: f32) -> f32 {
        match &self.mapper {
            MapperF32::Lin(mapper) => mapper.denormalize(normalized),
            MapperF32::Pow(mapper) => mapper.denormalize(normalized),
            MapperF32::Log2(mapper) => mapper.denormalize(normalized),
            MapperF32::Discrete(mapper) => mapper.denormalize_f32(normalized),
        }
    }

    /// Un-map an array of normalized values to the corresponding `f32` value.
    ///
    /// Values will be processed up to the length of the shortest array.
    pub fn denormalize_array(&self, in_normalized: &[f32], out_values: &mut [f32]) {
        match &self.mapper {
            MapperF32::Lin(mapper) => mapper.denormalize_array(in_normalized, out_values),
            MapperF32::Pow(mapper) => mapper.denormalize_array(in_normalized, out_values),
            MapperF32::Log2(mapper) => mapper.denormalize_array(in_normalized, out_values),
            MapperF32::Discrete(mapper) => mapper.denormalize_array_f32(in_normalized, out_values),
        }
    }
}

/// A mapper than maps a range of values to and from the normalized
/// `f64` range `[0.0, 1.0]`.
pub struct NormalMapF64 {
    /// The current mapper in use
    pub mapper: MapperF64,
}

impl NormalMapF64 {
    /// Create a new `NormalMap` with linear mapping.
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// are what will be linearly mapped, not the raw amplitude.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range
    /// * max - the maximum of the range
    /// * unit - the type of unit
    pub fn linear(min: f64, max: f64, unit: Unit) -> Self {
        Self {
            mapper: MapperF64::Lin(LinearMapF64::new(min, max, unit)),
        }
    }

    /// Create a new `NormalMap` with an exponential mapping where the
    /// normalized value is raised to the supplied exponent.
    ///
    /// Please note if you use `Unit::Decibels`, then the decibels
    /// are what will be mapped, not the raw amplitude.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range
    /// * max - the maximum of the range
    /// * exponent - the exponent to raise the normalized value to
    /// * unit - the type of unit
    ///
    /// # Panics
    ///
    /// * Panics when `exponent = 0.0`.
    pub fn power(min: f64, max: f64, exponent: f64, unit: Unit) -> Self {
        Self {
            mapper: MapperF64::Pow(PowerMapF64::new(min, max, exponent, unit)),
        }
    }

    /// Create a new `NormalMap` with a logarithmic mapping using `log2`.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range, must be > 0.0
    /// * max - the maximum of the range, must be > 0.0
    ///
    /// # Panics
    ///
    /// * Panics when either `min` or `max` <= 0.0.
    pub fn log2(min: f64, max: f64) -> Self {
        Self {
            mapper: MapperF64::Log2(Log2MapF64::new(min, max)),
        }
    }

    /// Create a new `NormalMap` with a discrete `isize` integer range.
    ///
    /// A supplied enum may be used as well as long
    /// as it implements `From<isize> + Into<isize> + Copy + Clone`.
    ///
    /// # Arguments
    ///
    /// * min - the minimum of the range
    /// * max - the maximum of the range
    pub fn discrete<T>(min: T, max: T) -> Self
    where
        T: From<isize> + Into<isize> + Copy + Clone,
    {
        Self {
            mapper: MapperF64::Discrete(DiscreteMapF64::new(min, max)),
        }
    }

    /// Map an `f64` value to the normalized range `[0.0, 1.0]`.
    pub fn normalize(&self, value: f64) -> f64 {
        match &self.mapper {
            MapperF64::Lin(mapper) => mapper.normalize(value),
            MapperF64::Pow(mapper) => mapper.normalize(value),
            MapperF64::Log2(mapper) => mapper.normalize(value),
            MapperF64::Discrete(mapper) => mapper.normalize_f64(value),
        }
    }

    /// Map an array of `f64` values to the normalized range `[0.0, 1.0]`.
    ///
    /// Values will be processed up to the length of the shortest array.
    pub fn normalize_array(&self, in_values: &[f64], out_normalized: &mut [f64]) {
        match &self.mapper {
            MapperF64::Lin(mapper) => mapper.normalize_array(in_values, out_normalized),
            MapperF64::Pow(mapper) => mapper.normalize_array(in_values, out_normalized),
            MapperF64::Log2(mapper) => mapper.normalize_array(in_values, out_normalized),
            MapperF64::Discrete(mapper) => mapper.normalize_array_f64(in_values, out_normalized),
        }
    }

    /// Un-map a normalized value to the corresponding `f64` value.
    pub fn denormalize(&self, normalized: f64) -> f64 {
        match &self.mapper {
            MapperF64::Lin(mapper) => mapper.denormalize(normalized),
            MapperF64::Pow(mapper) => mapper.denormalize(normalized),
            MapperF64::Log2(mapper) => mapper.denormalize(normalized),
            MapperF64::Discrete(mapper) => mapper.denormalize_f64(normalized),
        }
    }

    /// Un-map an array of normalized values to the corresponding `f64` value.
    ///
    /// Values will be processed up to the length of the shortest array.
    pub fn denormalize_array(&self, in_normalized: &[f64], out_values: &mut [f64]) {
        match &self.mapper {
            MapperF64::Lin(mapper) => mapper.denormalize_array(in_normalized, out_values),
            MapperF64::Pow(mapper) => mapper.denormalize_array(in_normalized, out_values),
            MapperF64::Log2(mapper) => mapper.denormalize_array(in_normalized, out_values),
            MapperF64::Discrete(mapper) => mapper.denormalize_array_f64(in_normalized, out_values),
        }
    }
}