dasp_window/hanning/mod.rs
1use crate::Window;
2use dasp_sample::Sample;
3use ops::f64::cos;
4
5mod ops;
6
7/// A type of window function, also known as the "raised cosine window".
8///
9/// [Wiki entry](https://en.wikipedia.org/wiki/Window_function#Hann_.28Hanning.29_window).
10///
11/// ### Required Features
12///
13/// - When using `dasp_window`, this item requires the **hanning** feature to be enabled.
14/// - When using `dasp`, this item requires the **window-hanning** feature to be enabled.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct Hanning;
17
18impl<S> Window<S> for Hanning
19where
20 S: Sample,
21{
22 type Output = S;
23 fn window(phase: S) -> Self::Output {
24 const PI_2: f64 = core::f64::consts::PI * 2.0;
25 let v = phase.to_float_sample().to_sample::<f64>() * PI_2;
26 (0.5 * (1.0 - cos(v)))
27 .to_sample::<S::Float>()
28 .to_sample::<S>()
29 }
30}