Skip to main content

qualia_audio/features/window/
apply.rs

1//! Elementwise window application (length-checked, in place).
2
3use crate::types::AudioError;
4
5/// Multiply `samples` in place by `window`, elementwise.
6///
7/// The two slices must have equal length. Zero-heap: delegated to
8/// [`etalon_signal::window::apply_window_in_place`].
9///
10/// Returns [`AudioError::InvalidParameter`] if the lengths differ.
11#[inline]
12pub fn apply_window(samples: &mut [f32], window: &[f32]) -> Result<(), AudioError> {
13    etalon_signal::window::apply_window_in_place(samples, window)
14        .map_err(|_| AudioError::InvalidParameter)
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn multiplies_elementwise() {
23        let mut s = [1.0f32, 2.0, 4.0, 8.0];
24        let w = [0.5f32, 0.25, 0.0, 1.0];
25        apply_window(&mut s, &w).unwrap();
26        assert_eq!(s, [0.5, 0.5, 0.0, 8.0]);
27    }
28
29    #[test]
30    fn length_mismatch_is_invalid_parameter() {
31        let mut s = [1.0f32; 4];
32        let w = [1.0f32; 3];
33        assert_eq!(apply_window(&mut s, &w), Err(AudioError::InvalidParameter));
34    }
35}