conformal_component/audio/compare.rs
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
//! Utilities for comparing audio samples, slices, and buffers
use super::{Buffer, channels};
use itertools::{EitherOrBoth, Itertools};
/// Checks if two `f32` values `a` and `b` are within `e` of each other.
///
/// This is useful for comparing floating point values, while allowing for
/// some rounding errors.
///
/// # Examples
///
/// ```
/// # use conformal_component::audio::approx_eq;
/// assert_eq!(approx_eq(1.0, 1.01, 0.1), true);
/// assert_eq!(approx_eq(1.0, 1.3, 0.1), false);
/// ```
#[must_use]
pub fn approx_eq(a: f32, b: f32, e: f32) -> bool {
(a - b).abs() < e
}
/// Checks if all the values from two iterators of f32 values are within `e` of each other.
///
/// This is useful for comparing ranges of floating point values, while allowing for
/// some rounding errors.
///
/// # Examples
///
/// ```
/// # use conformal_component::audio::all_approx_eq;
/// assert!(all_approx_eq([1.0, 2.0, 3.0], [1.01, 2.01, 3.01], 0.1));
/// assert!(!all_approx_eq([1.0, 2.0, 3.0], [1.01, 2.2, 3.01], 0.1));
/// assert!(!all_approx_eq([1.0, 2.0, 3.0], [1.0, 2.0], 0.1));
/// ```
#[must_use]
pub fn all_approx_eq<L: IntoIterator<Item = f32>, R: IntoIterator<Item = f32>>(
lhs: L,
rhs: R,
e: f32,
) -> bool {
lhs.into_iter().zip_longest(rhs).all(|x| match x {
EitherOrBoth::Both(l, r) => approx_eq(l, r, e),
_ => false,
})
}
/// Checks two buffers are equal to within a tolerance `e`.
///
/// Note that buffers will only count as equal if they have
/// the same channel layout and length, and if all samples
/// are within `e` of each other.
///
/// # Examples
///
/// ```
/// # use conformal_component::audio::{BufferData, Buffer, buffer_approx_eq};
/// assert!(buffer_approx_eq(
/// &BufferData::new_mono(vec![1.0, 2.0, 3.0]),
/// &BufferData::new_mono(vec![1.01, 2.01, 3.01]),
/// 0.1));
/// assert!(!buffer_approx_eq(
/// &BufferData::new_mono(vec![1.0, 2.0, 3.0]),
/// &BufferData::new_mono(vec![1.01, 2.2, 3.01]),
/// 0.1));
/// assert!(!buffer_approx_eq(
/// &BufferData::new_mono(vec![1.0, 2.0, 3.0]),
/// &BufferData::new_mono(vec![1.0, 2.0]),
/// 0.1));
/// assert!(!buffer_approx_eq(
/// &BufferData::new_stereo(vec![1.0, 2.0], vec![3.0, 4.0]),
/// &BufferData::new_mono(vec![1.0, 2.0, 3.0, 4.0]),
/// 0.1));
/// ```
#[must_use]
pub fn buffer_approx_eq<A: Buffer, B: Buffer>(a: &A, b: &B, e: f32) -> bool {
a.channel_layout() == b.channel_layout()
&& all_approx_eq(
channels(a).flatten().copied(),
channels(b).flatten().copied(),
e,
)
}