conformal_component/audio/compare.rs
1//! Utilities for comparing audio samples, slices, and buffers
2
3use super::{Buffer, channels};
4use itertools::{EitherOrBoth, Itertools};
5
6/// Checks if two `f32` values `a` and `b` are within `e` of each other.
7///
8/// This is useful for comparing floating point values, while allowing for
9/// some rounding errors.
10///
11/// # Examples
12///
13/// ```
14/// # use conformal_component::audio::approx_eq;
15/// assert_eq!(approx_eq(1.0, 1.01, 0.1), true);
16/// assert_eq!(approx_eq(1.0, 1.3, 0.1), false);
17/// ```
18#[must_use]
19pub fn approx_eq(a: f32, b: f32, e: f32) -> bool {
20 (a - b).abs() < e
21}
22
23/// Checks if all the values from two iterators of f32 values are within `e` of each other.
24///
25/// This is useful for comparing ranges of floating point values, while allowing for
26/// some rounding errors.
27///
28/// # Examples
29///
30/// ```
31/// # use conformal_component::audio::all_approx_eq;
32/// assert!(all_approx_eq([1.0, 2.0, 3.0], [1.01, 2.01, 3.01], 0.1));
33/// assert!(!all_approx_eq([1.0, 2.0, 3.0], [1.01, 2.2, 3.01], 0.1));
34/// assert!(!all_approx_eq([1.0, 2.0, 3.0], [1.0, 2.0], 0.1));
35/// ```
36#[must_use]
37pub fn all_approx_eq<L: IntoIterator<Item = f32>, R: IntoIterator<Item = f32>>(
38 lhs: L,
39 rhs: R,
40 e: f32,
41) -> bool {
42 lhs.into_iter().zip_longest(rhs).all(|x| match x {
43 EitherOrBoth::Both(l, r) => approx_eq(l, r, e),
44 _ => false,
45 })
46}
47
48/// Checks two buffers are equal to within a tolerance `e`.
49///
50/// Note that buffers will only count as equal if they have
51/// the same channel layout and length, and if all samples
52/// are within `e` of each other.
53///
54/// # Examples
55///
56/// ```
57/// # use conformal_component::audio::{BufferData, Buffer, buffer_approx_eq};
58/// assert!(buffer_approx_eq(
59/// &BufferData::new_mono(vec![1.0, 2.0, 3.0]),
60/// &BufferData::new_mono(vec![1.01, 2.01, 3.01]),
61/// 0.1));
62/// assert!(!buffer_approx_eq(
63/// &BufferData::new_mono(vec![1.0, 2.0, 3.0]),
64/// &BufferData::new_mono(vec![1.01, 2.2, 3.01]),
65/// 0.1));
66/// assert!(!buffer_approx_eq(
67/// &BufferData::new_mono(vec![1.0, 2.0, 3.0]),
68/// &BufferData::new_mono(vec![1.0, 2.0]),
69/// 0.1));
70/// assert!(!buffer_approx_eq(
71/// &BufferData::new_stereo(vec![1.0, 2.0], vec![3.0, 4.0]),
72/// &BufferData::new_mono(vec![1.0, 2.0, 3.0, 4.0]),
73/// 0.1));
74/// ```
75#[must_use]
76pub fn buffer_approx_eq<A: Buffer, B: Buffer>(a: &A, b: &B, e: f32) -> bool {
77 a.channel_layout() == b.channel_layout()
78 && all_approx_eq(
79 channels(a).flatten().copied(),
80 channels(b).flatten().copied(),
81 e,
82 )
83}