2022-03-04 09:30:29 +11:00
|
|
|
//! Utilities to handle smoothing parameter changes over time.
|
|
|
|
|
2022-02-06 13:38:59 +11:00
|
|
|
use atomic_float::AtomicF32;
|
2022-02-14 04:32:59 +11:00
|
|
|
use std::sync::atomic::{AtomicI32, Ordering};
|
2022-02-06 11:33:19 +11:00
|
|
|
|
2022-02-03 07:08:23 +11:00
|
|
|
/// Controls if and how parameters gets smoothed.
|
2022-05-29 22:21:41 +10:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-02-03 07:08:23 +11:00
|
|
|
pub enum SmoothingStyle {
|
|
|
|
/// No smoothing is applied. The parameter's `value` field contains the latest sample value
|
|
|
|
/// available for the parameters.
|
|
|
|
None,
|
2022-02-03 09:00:17 +11:00
|
|
|
/// Smooth parameter changes so the current value approaches the target value at a constant
|
2022-05-12 09:33:58 +10:00
|
|
|
/// rate. The target value will be reached in exactly this many milliseconds.
|
2022-02-03 08:00:00 +11:00
|
|
|
Linear(f32),
|
2022-05-12 09:33:58 +10:00
|
|
|
/// Smooth parameter changes such that the rate matches the curve of a logarithmic function,
|
|
|
|
/// starting out slow and then constantly increasing the slope until the value is reached. The
|
|
|
|
/// target value will be reached in exactly this many milliseconds. This is useful for smoothing
|
|
|
|
/// things like frequencies and decibel gain value. **The caveat is that the value may never
|
|
|
|
/// reach 0**, or you will end up multiplying and dividing things by zero. Make sure your value
|
|
|
|
/// ranges don't include 0.
|
2022-02-03 09:00:17 +11:00
|
|
|
Logarithmic(f32),
|
2022-05-12 09:33:58 +10:00
|
|
|
/// Smooth parameter changes such that the rate matches the curve of an exponential function,
|
2022-05-12 10:22:12 +10:00
|
|
|
/// starting out fast and then tapering off until the end. This is a single-pole IIR filter
|
|
|
|
/// under the hood, while the other smoothing options are FIR filters. This means that the exact
|
2022-05-12 21:21:32 +10:00
|
|
|
/// value would never be reached. Instead, this reaches 99.99% of the value target value in the
|
2022-05-12 10:22:12 +10:00
|
|
|
/// specified number of milliseconds, and it then snaps to the target value in the last step.
|
|
|
|
/// This results in a smoother transition, with the caveat being that there will be a tiny jump
|
|
|
|
/// at the end. Unlike the `Logarithmic` option, this does support crossing the zero value.
|
|
|
|
Exponential(f32),
|
2022-02-03 07:08:23 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A smoother, providing a smoothed value for each sample.
|
2022-02-06 11:33:19 +11:00
|
|
|
//
|
|
|
|
// TODO: We need to use atomics here so we can share the params object with the GUI. Is there a
|
|
|
|
// better alternative to allow the process function to mutate these smoothers?
|
2022-07-06 22:32:18 +10:00
|
|
|
#[derive(Debug)]
|
2022-02-03 07:08:23 +11:00
|
|
|
pub struct Smoother<T> {
|
|
|
|
/// The kind of snoothing that needs to be applied, if any.
|
2022-07-07 04:14:29 +10:00
|
|
|
pub style: SmoothingStyle,
|
2022-02-03 07:08:23 +11:00
|
|
|
/// The number of steps of smoothing left to take.
|
2022-02-14 04:32:59 +11:00
|
|
|
///
|
|
|
|
// This is a signed integer because we can skip multiple steps, which would otherwise make it
|
|
|
|
// possible to get an underflow here.
|
|
|
|
steps_left: AtomicI32,
|
2022-02-03 07:08:23 +11:00
|
|
|
/// The amount we should adjust the current value each sample to be able to reach the target in
|
|
|
|
/// the specified tiem frame. This is also a floating point number to keep the smoothing
|
|
|
|
/// uniform.
|
2022-05-12 09:33:58 +10:00
|
|
|
///
|
2022-05-12 10:22:12 +10:00
|
|
|
/// In the case of the `Exponential` smoothing style this is the coefficient `x` that the
|
2022-05-12 09:33:58 +10:00
|
|
|
/// previous sample is multplied by.
|
2022-02-03 07:08:23 +11:00
|
|
|
step_size: f32,
|
|
|
|
/// The value for the current sample. Always stored as floating point for obvious reasons.
|
2022-02-06 13:38:59 +11:00
|
|
|
current: AtomicF32,
|
2022-02-03 07:08:23 +11:00
|
|
|
/// The value we're smoothing towards
|
|
|
|
target: T,
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:51:51 +10:00
|
|
|
/// An iterator that continuously produces smoothed values. Can be used as an alternative to the
|
2022-07-06 22:26:43 +10:00
|
|
|
/// block-based smoothing API. Since the iterator itself is infinite, you can use
|
2022-04-05 01:51:51 +10:00
|
|
|
/// [`Smoother::is_smoothing()`] and [`Smoother::steps_left()`] to get information on the current
|
|
|
|
/// smoothing status.
|
|
|
|
pub struct SmootherIter<'a, T> {
|
|
|
|
smoother: &'a Smoother<T>,
|
|
|
|
}
|
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
/// A type that can be smoothed. This exists just to avoid duplicate explicit implementations for
|
|
|
|
/// the smoothers.
|
|
|
|
pub trait Smoothable: Default + Copy {
|
|
|
|
fn to_f32(self) -> f32;
|
|
|
|
fn from_f32(value: f32) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Smoothable> Default for Smoother<T> {
|
2022-02-03 07:08:23 +11:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
style: SmoothingStyle::None,
|
2022-02-14 04:32:59 +11:00
|
|
|
steps_left: AtomicI32::new(0),
|
2022-02-03 07:08:23 +11:00
|
|
|
step_size: Default::default(),
|
2022-02-06 13:38:59 +11:00
|
|
|
current: AtomicF32::new(0.0),
|
2022-02-03 07:08:23 +11:00
|
|
|
target: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
impl<T: Smoothable> Iterator for SmootherIter<'_, T> {
|
|
|
|
type Item = T;
|
2022-04-05 01:51:51 +10:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
Some(self.smoother.next())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:32:18 +10:00
|
|
|
impl<T: Clone> Clone for Smoother<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
// We can't derive clone because of the atomics, but these atomics are only here to allow
|
|
|
|
// Send+Sync interior mutability
|
|
|
|
Self {
|
2022-07-07 02:55:42 +10:00
|
|
|
style: self.style,
|
2022-07-06 22:32:18 +10:00
|
|
|
steps_left: AtomicI32::new(self.steps_left.load(Ordering::Relaxed)),
|
2022-07-07 02:55:42 +10:00
|
|
|
step_size: self.step_size,
|
2022-07-06 22:32:18 +10:00
|
|
|
current: AtomicF32::new(self.current.load(Ordering::Relaxed)),
|
|
|
|
target: self.target.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
impl<T: Smoothable> Smoother<T> {
|
2022-02-03 07:08:23 +11:00
|
|
|
/// Use the specified style for the smoothing.
|
|
|
|
pub fn new(style: SmoothingStyle) -> Self {
|
|
|
|
Self {
|
|
|
|
style,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for not applying any smoothing at all. Same as `Smoother::default`.
|
|
|
|
pub fn none() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
2022-02-04 02:51:35 +11:00
|
|
|
|
2022-03-24 02:50:09 +11:00
|
|
|
/// The number of steps left until calling [`next()`][Self::next()] will stop yielding new
|
|
|
|
/// values.
|
|
|
|
#[inline]
|
|
|
|
pub fn steps_left(&self) -> i32 {
|
|
|
|
self.steps_left.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Whether calling [`next()`][Self::next()] will yield a new value or an old value. Useful if
|
|
|
|
/// you need to recompute something wheenver this parameter changes.
|
2022-03-24 02:50:09 +11:00
|
|
|
#[inline]
|
2022-02-04 02:51:35 +11:00
|
|
|
pub fn is_smoothing(&self) -> bool {
|
2022-03-24 02:50:09 +11:00
|
|
|
self.steps_left() > 0
|
2022-02-04 02:51:35 +11:00
|
|
|
}
|
2022-03-02 03:07:03 +11:00
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
/// Produce an iterator that yields smoothed values. These are not iterators already for the
|
|
|
|
/// sole reason that this will always yield a value, and needing to unwrap all of those options
|
|
|
|
/// is not going to be very fun.
|
2022-04-05 01:51:51 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn iter(&self) -> SmootherIter<T> {
|
|
|
|
SmootherIter { smoother: self }
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:14:47 +11:00
|
|
|
/// Reset the smoother the specified value.
|
2022-05-12 21:34:02 +10:00
|
|
|
pub fn reset(&mut self, value: T) {
|
2022-02-05 01:14:47 +11:00
|
|
|
self.target = value;
|
2022-05-12 21:34:02 +10:00
|
|
|
self.current.store(value.to_f32(), Ordering::Relaxed);
|
2022-02-06 11:33:19 +11:00
|
|
|
self.steps_left.store(0, Ordering::Relaxed);
|
2022-02-05 01:14:47 +11:00
|
|
|
}
|
|
|
|
|
2022-02-03 07:08:23 +11:00
|
|
|
/// Set the target value.
|
2022-05-12 21:34:02 +10:00
|
|
|
pub fn set_target(&mut self, sample_rate: f32, target: T) {
|
2022-02-03 07:08:23 +11:00
|
|
|
self.target = target;
|
2022-02-06 11:33:19 +11:00
|
|
|
|
|
|
|
let steps_left = match self.style {
|
2022-02-05 01:14:47 +11:00
|
|
|
SmoothingStyle::None => 1,
|
2022-05-12 09:33:58 +10:00
|
|
|
SmoothingStyle::Linear(time)
|
|
|
|
| SmoothingStyle::Logarithmic(time)
|
2022-05-12 10:22:12 +10:00
|
|
|
| SmoothingStyle::Exponential(time) => (sample_rate * time / 1000.0).round() as i32,
|
2022-02-05 01:14:47 +11:00
|
|
|
};
|
2022-02-06 11:33:19 +11:00
|
|
|
self.steps_left.store(steps_left, Ordering::Relaxed);
|
|
|
|
|
2022-02-06 13:38:59 +11:00
|
|
|
let current = self.current.load(Ordering::Relaxed);
|
2022-02-05 01:14:47 +11:00
|
|
|
self.step_size = match self.style {
|
|
|
|
SmoothingStyle::None => 0.0,
|
2022-05-12 21:34:02 +10:00
|
|
|
SmoothingStyle::Linear(_) => (self.target.to_f32() - current) / steps_left as f32,
|
2022-02-05 01:14:47 +11:00
|
|
|
SmoothingStyle::Logarithmic(_) => {
|
|
|
|
// We need to solve `current * (step_size ^ steps_left) = target` for
|
|
|
|
// `step_size`
|
2022-02-06 11:33:19 +11:00
|
|
|
nih_debug_assert_ne!(current, 0.0);
|
2022-05-12 21:50:45 +10:00
|
|
|
((self.target.to_f32() / current) as f64).powf((steps_left as f64).recip()) as f32
|
2022-02-05 01:14:47 +11:00
|
|
|
}
|
2022-05-12 09:33:58 +10:00
|
|
|
// In this case the step size value is the coefficient the current value will be
|
|
|
|
// multiplied by, while the target value is multipled by one minus the coefficient. This
|
2022-05-12 21:21:32 +10:00
|
|
|
// reaches 99.99% of the target value after `steps_left`. The smoother will snap to the
|
2022-05-12 09:33:58 +10:00
|
|
|
// target value after that point.
|
2022-05-12 21:50:45 +10:00
|
|
|
SmoothingStyle::Exponential(_) => 0.0001f64.powf(1.0 / steps_left as f64) as f32,
|
2022-02-05 01:14:47 +11:00
|
|
|
};
|
2022-02-03 07:08:23 +11:00
|
|
|
}
|
|
|
|
|
2022-02-14 04:32:59 +11:00
|
|
|
/// Get the next value from this smoother. The value will be equal to the previous value once
|
2022-03-02 03:29:09 +11:00
|
|
|
/// the smoothing period is over. This should be called exactly once per sample.
|
2022-02-03 07:08:23 +11:00
|
|
|
// Yes, Clippy, like I said, this was intentional
|
|
|
|
#[allow(clippy::should_implement_trait)]
|
2022-03-02 02:55:30 +11:00
|
|
|
#[inline]
|
2022-05-12 21:34:02 +10:00
|
|
|
pub fn next(&self) -> T {
|
2022-02-14 04:32:59 +11:00
|
|
|
self.next_step(1)
|
|
|
|
}
|
|
|
|
|
2022-03-04 09:05:01 +11:00
|
|
|
/// [`next()`][Self::next()], but with the ability to skip forward in the smoother.
|
|
|
|
/// [`next()`][Self::next()] is equivalent to calling this function with a `steps` value of 1.
|
|
|
|
/// Calling this function with a `steps` value of `n` means will cause you to skip the next `n -
|
|
|
|
/// 1` values and return the `n`th value.
|
2022-03-02 02:55:30 +11:00
|
|
|
#[inline]
|
2022-05-12 21:34:02 +10:00
|
|
|
pub fn next_step(&self, steps: u32) -> T {
|
2022-02-14 04:32:59 +11:00
|
|
|
nih_debug_assert_ne!(steps, 0);
|
|
|
|
|
2022-02-13 06:54:03 +11:00
|
|
|
if self.steps_left.load(Ordering::Relaxed) > 0 {
|
2022-02-06 13:38:59 +11:00
|
|
|
let current = self.current.load(Ordering::Relaxed);
|
2022-05-12 21:34:02 +10:00
|
|
|
let target = self.target.to_f32();
|
2022-02-06 11:33:19 +11:00
|
|
|
|
2022-02-14 04:32:59 +11:00
|
|
|
// The number of steps usually won't fit exactly, so make sure we don't end up with
|
|
|
|
// quantization errors on overshoots or undershoots. We also need to account for the
|
2022-05-12 09:33:58 +10:00
|
|
|
// possibility that we only have `n < steps` steps left. This is especially important
|
2022-05-12 10:22:12 +10:00
|
|
|
// for the `Exponential` smoothing style, since that won't reach the target value
|
2022-05-12 09:33:58 +10:00
|
|
|
// exactly.
|
2022-02-14 04:32:59 +11:00
|
|
|
let old_steps_left = self.steps_left.fetch_sub(steps as i32, Ordering::Relaxed);
|
|
|
|
let new = if old_steps_left <= steps as i32 {
|
|
|
|
self.steps_left.store(0, Ordering::Relaxed);
|
2022-05-12 21:34:02 +10:00
|
|
|
target
|
2022-02-03 07:08:23 +11:00
|
|
|
} else {
|
|
|
|
match &self.style {
|
2022-05-12 21:34:02 +10:00
|
|
|
SmoothingStyle::None => target,
|
2022-02-14 04:32:59 +11:00
|
|
|
SmoothingStyle::Linear(_) => current + (self.step_size * steps as f32),
|
|
|
|
SmoothingStyle::Logarithmic(_) => current * (self.step_size.powi(steps as i32)),
|
2022-05-12 10:22:12 +10:00
|
|
|
SmoothingStyle::Exponential(_) => {
|
2022-05-12 09:56:07 +10:00
|
|
|
// This is the same as calculating `current = (current * step_size) +
|
2022-05-12 21:21:32 +10:00
|
|
|
// (target * (1 - step_size))` in a loop since the target value won't change
|
2022-05-12 09:56:07 +10:00
|
|
|
let coefficient = self.step_size.powi(steps as i32);
|
2022-05-12 21:34:02 +10:00
|
|
|
(current * coefficient) + (target * (1.0 - coefficient))
|
2022-05-12 09:33:58 +10:00
|
|
|
}
|
2022-02-06 11:33:19 +11:00
|
|
|
}
|
|
|
|
};
|
2022-02-06 13:38:59 +11:00
|
|
|
self.current.store(new, Ordering::Relaxed);
|
2022-02-06 11:33:19 +11:00
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
T::from_f32(new)
|
2022-02-03 07:08:23 +11:00
|
|
|
} else {
|
|
|
|
self.target
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 04:32:59 +11:00
|
|
|
|
2022-04-28 01:43:40 +10:00
|
|
|
/// Get previous value returned by this smoother. This may be useful to save some boilerplate
|
|
|
|
/// when [`is_smoothing()`][Self::is_smoothing()] is used to determine whether an expensive
|
|
|
|
/// calculation should take place, and [`next()`][Self::next()] gets called as part of that
|
|
|
|
/// calculation.
|
2022-05-12 21:34:02 +10:00
|
|
|
pub fn previous_value(&self) -> T {
|
|
|
|
T::from_f32(self.current.load(Ordering::Relaxed))
|
2022-04-28 01:43:40 +10:00
|
|
|
}
|
|
|
|
|
2022-07-06 22:26:43 +10:00
|
|
|
/// Produce smoothed values for an entire block of audio. This is useful when iterating the same
|
|
|
|
/// block of audio multiple times. For instance when summing voices for a synthesizer.
|
|
|
|
/// `block_values[..block_len]` will be filled with the smoothed values. This is simply a
|
|
|
|
/// convenient function for [`next_block_exact()`][Self::next_block_exact()] when iterating over
|
|
|
|
/// variable length blocks with a known maximum size.
|
2022-03-02 06:22:42 +11:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2022-07-06 22:26:43 +10:00
|
|
|
/// Panics if `block_len > block_values.len()`.
|
|
|
|
pub fn next_block(&self, block_values: &mut [T], block_len: usize) {
|
|
|
|
self.next_block_exact_mapped(&mut block_values[..block_len], |x| x)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as [`next_block()`][Self::next_block()], but filling the entire slice.
|
|
|
|
pub fn next_block_exact(&self, block_values: &mut [T]) {
|
|
|
|
self.next_block_exact_mapped(block_values, |x| x)
|
2022-04-05 01:44:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as [`next_block()`][Self::next_block()], but with a function applied to each
|
|
|
|
/// produced value. Useful when applying modulation to a smoothed parameter.
|
2022-07-06 22:26:43 +10:00
|
|
|
pub fn next_block_mapped(&self, block_values: &mut [T], block_len: usize, f: impl Fn(T) -> T) {
|
|
|
|
self.next_block_exact_mapped(&mut block_values[..block_len], f)
|
|
|
|
}
|
2022-03-02 03:29:09 +11:00
|
|
|
|
2022-07-06 22:26:43 +10:00
|
|
|
/// The same as [`next_block_exact()`][Self::next_block()], but with a function applied to each
|
|
|
|
/// produced value. Useful when applying modulation to a smoothed parameter.
|
|
|
|
pub fn next_block_exact_mapped(&self, block_values: &mut [T], f: impl Fn(T) -> T) {
|
|
|
|
// `self.next()` will yield the current value if the parameter is no longer smoothing, but
|
|
|
|
// it's a bit of a waste to continuesly call that if only the first couple or none of the
|
|
|
|
// values in `block_values` would require smoothing and the rest don't. Instead, we'll just
|
|
|
|
// smooth the values as necessary, and then reuse the target value for the rest of the
|
|
|
|
// block.
|
|
|
|
let num_smoothed_values = block_values
|
|
|
|
.len()
|
|
|
|
.min(self.steps_left.load(Ordering::Relaxed) as usize);
|
|
|
|
|
|
|
|
block_values[..num_smoothed_values].fill_with(|| f(self.next()));
|
|
|
|
block_values[num_smoothed_values..].fill(self.target);
|
2022-03-02 03:29:09 +11:00
|
|
|
}
|
2022-05-12 21:34:02 +10:00
|
|
|
}
|
2022-03-02 03:29:09 +11:00
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
impl Smoothable for f32 {
|
|
|
|
#[inline]
|
|
|
|
fn to_f32(self) -> f32 {
|
|
|
|
self
|
|
|
|
}
|
2022-02-14 04:32:59 +11:00
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
#[inline]
|
|
|
|
fn from_f32(value: f32) -> Self {
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 11:33:19 +11:00
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
impl Smoothable for i32 {
|
|
|
|
#[inline]
|
|
|
|
fn to_f32(self) -> f32 {
|
|
|
|
self as f32
|
|
|
|
}
|
2022-02-06 11:33:19 +11:00
|
|
|
|
2022-05-12 21:34:02 +10:00
|
|
|
#[inline]
|
|
|
|
fn from_f32(value: f32) -> Self {
|
|
|
|
value.round() as i32
|
2022-02-03 07:26:20 +11:00
|
|
|
}
|
|
|
|
}
|
2022-02-03 08:34:29 +11:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn linear_f32_smoothing() {
|
|
|
|
let mut smoother: Smoother<f32> = Smoother::new(SmoothingStyle::Linear(100.0));
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.reset(10.0);
|
2022-02-03 08:34:29 +11:00
|
|
|
assert_eq!(smoother.next(), 10.0);
|
|
|
|
|
|
|
|
// Instead of testing the actual values, we'll make sure that we reach the target values at
|
|
|
|
// the expected time.
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.set_target(100.0, 20.0);
|
2022-02-03 08:34:29 +11:00
|
|
|
for _ in 0..(10 - 2) {
|
2022-02-11 09:40:18 +11:00
|
|
|
smoother.next();
|
2022-02-03 08:34:29 +11:00
|
|
|
}
|
|
|
|
assert_ne!(smoother.next(), 20.0);
|
|
|
|
assert_eq!(smoother.next(), 20.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn linear_i32_smoothing() {
|
|
|
|
let mut smoother: Smoother<i32> = Smoother::new(SmoothingStyle::Linear(100.0));
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.reset(10);
|
2022-02-03 08:34:29 +11:00
|
|
|
assert_eq!(smoother.next(), 10);
|
|
|
|
|
|
|
|
// Integers are rounded, but with these values we can still test this
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.set_target(100.0, 20);
|
2022-02-03 08:34:29 +11:00
|
|
|
for _ in 0..(10 - 2) {
|
2022-02-11 09:40:18 +11:00
|
|
|
smoother.next();
|
2022-02-03 08:34:29 +11:00
|
|
|
}
|
|
|
|
assert_ne!(smoother.next(), 20);
|
|
|
|
assert_eq!(smoother.next(), 20);
|
|
|
|
}
|
2022-02-03 09:00:17 +11:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn logarithmic_f32_smoothing() {
|
|
|
|
let mut smoother: Smoother<f32> = Smoother::new(SmoothingStyle::Logarithmic(100.0));
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.reset(10.0);
|
2022-02-03 09:00:17 +11:00
|
|
|
assert_eq!(smoother.next(), 10.0);
|
|
|
|
|
|
|
|
// Instead of testing the actual values, we'll make sure that we reach the target values at
|
|
|
|
// the expected time.
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.set_target(100.0, 20.0);
|
2022-02-03 09:00:17 +11:00
|
|
|
for _ in 0..(10 - 2) {
|
2022-02-11 09:40:18 +11:00
|
|
|
smoother.next();
|
2022-02-03 09:00:17 +11:00
|
|
|
}
|
|
|
|
assert_ne!(smoother.next(), 20.0);
|
|
|
|
assert_eq!(smoother.next(), 20.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn logarithmic_i32_smoothing() {
|
|
|
|
let mut smoother: Smoother<i32> = Smoother::new(SmoothingStyle::Logarithmic(100.0));
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.reset(10);
|
2022-02-03 09:00:17 +11:00
|
|
|
assert_eq!(smoother.next(), 10);
|
|
|
|
|
|
|
|
// Integers are rounded, but with these values we can still test this
|
2022-02-05 01:14:47 +11:00
|
|
|
smoother.set_target(100.0, 20);
|
2022-02-03 09:00:17 +11:00
|
|
|
for _ in 0..(10 - 2) {
|
2022-02-11 09:40:18 +11:00
|
|
|
smoother.next();
|
2022-02-03 09:00:17 +11:00
|
|
|
}
|
|
|
|
assert_ne!(smoother.next(), 20);
|
|
|
|
assert_eq!(smoother.next(), 20);
|
|
|
|
}
|
2022-02-14 04:32:59 +11:00
|
|
|
|
|
|
|
/// Same as [linear_f32_smoothing], but skipping steps instead.
|
|
|
|
#[test]
|
|
|
|
fn skipping_linear_f32_smoothing() {
|
|
|
|
let mut smoother: Smoother<f32> = Smoother::new(SmoothingStyle::Linear(100.0));
|
|
|
|
smoother.reset(10.0);
|
|
|
|
assert_eq!(smoother.next(), 10.0);
|
|
|
|
|
|
|
|
smoother.set_target(100.0, 20.0);
|
|
|
|
smoother.next_step(8);
|
|
|
|
assert_ne!(smoother.next(), 20.0);
|
|
|
|
assert_eq!(smoother.next(), 20.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as [linear_i32_smoothing], but skipping steps instead.
|
|
|
|
#[test]
|
|
|
|
fn skipping_linear_i32_smoothing() {
|
|
|
|
let mut smoother: Smoother<i32> = Smoother::new(SmoothingStyle::Linear(100.0));
|
|
|
|
smoother.reset(10);
|
|
|
|
assert_eq!(smoother.next(), 10);
|
|
|
|
|
|
|
|
smoother.set_target(100.0, 20);
|
|
|
|
smoother.next_step(8);
|
|
|
|
assert_ne!(smoother.next(), 20);
|
|
|
|
assert_eq!(smoother.next(), 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as [logarithmic_f32_smoothing], but skipping steps instead.
|
|
|
|
#[test]
|
|
|
|
fn skipping_logarithmic_f32_smoothing() {
|
|
|
|
let mut smoother: Smoother<f32> = Smoother::new(SmoothingStyle::Logarithmic(100.0));
|
|
|
|
smoother.reset(10.0);
|
|
|
|
assert_eq!(smoother.next(), 10.0);
|
|
|
|
|
|
|
|
smoother.set_target(100.0, 20.0);
|
|
|
|
smoother.next_step(8);
|
|
|
|
assert_ne!(smoother.next(), 20.0);
|
|
|
|
assert_eq!(smoother.next(), 20.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as [logarithmic_i32_smoothing], but skipping steps instead.
|
|
|
|
#[test]
|
|
|
|
fn skipping_logarithmic_i32_smoothing() {
|
|
|
|
let mut smoother: Smoother<i32> = Smoother::new(SmoothingStyle::Logarithmic(100.0));
|
|
|
|
smoother.reset(10);
|
|
|
|
assert_eq!(smoother.next(), 10);
|
|
|
|
|
|
|
|
smoother.set_target(100.0, 20);
|
|
|
|
smoother.next_step(8);
|
|
|
|
assert_ne!(smoother.next(), 20);
|
|
|
|
assert_eq!(smoother.next(), 20);
|
|
|
|
}
|
2022-05-12 21:44:01 +10:00
|
|
|
|
|
|
|
// TODO: Tests for the exponential smoothing
|
2022-02-03 08:34:29 +11:00
|
|
|
}
|