summaryrefslogtreecommitdiff
path: root/src/window_type.rs
blob: 0c6a19f5c999a9f08b225f8687c9e764dff9374f (plain)
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
use crate::{
    window_function::{
        BlackmanHarrisWindow, HammingWindow, HannWindow, RectangularWindow, Sine4Window,
        WindowFunction,
    },
    window_size::WindowSize,
};
use nice_plug::prelude::*;

#[derive(Clone, PartialEq, Enum)]
pub enum WindowType {
    Rectangular,
    Hann,
    Hamming,
    BlackmanHarris,
    Sine4,
}

impl WindowType {
    pub fn new_function(&self, window_size: &WindowSize) -> Box<dyn WindowFunction> {
        match self {
            Self::Rectangular => Box::new(RectangularWindow::new(window_size)),
            Self::Hann => Box::new(HannWindow::new(window_size)),
            Self::Hamming => Box::new(HammingWindow::new(window_size)),
            Self::BlackmanHarris => Box::new(BlackmanHarrisWindow::new(window_size)),
            Self::Sine4 => Box::new(Sine4Window::new(window_size)),
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Rectangular => "Rectangular",
            Self::Hann => "Hann",
            Self::Hamming => "Hamming",
            Self::BlackmanHarris => "Blackman-Harris",
            Self::Sine4 => "Sin^4",
        }
    }

    pub fn iter() -> impl Iterator<Item = Self> {
        [
            Self::Rectangular,
            Self::Hann,
            Self::Hamming,
            Self::BlackmanHarris,
            Self::Sine4,
        ]
        .into_iter()
    }

    pub fn as_byte(&self) -> u8 {
        match self {
            Self::Rectangular => 0,
            Self::Hann => 1,
            Self::Hamming => 2,
            Self::BlackmanHarris => 3,
            Self::Sine4 => 4,
        }
    }
}

impl From<u8> for WindowType {
    fn from(item: u8) -> Self {
        match item {
            0 => Self::Rectangular,
            1 => Self::Hann,
            2 => Self::Hamming,
            3 => Self::BlackmanHarris,
            4 => Self::Sine4,
            _ => panic!("Invalid window type"),
        }
    }
}