fyrox_sound/dsp/
mod.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21// Clippy is being stupid here again, filters cannot be empty and there is no
22// need to define is_empty() method.
23#![allow(clippy::len_without_is_empty)]
24
25//! Digital signal processing module. Provides basic elements to process signal sample-by-sample.
26//!
27//! # Abbreviations
28//!
29//! `fc` - normalized frequency, i.e. `fc = 0.2` with `sample rate = 44100 Hz` will be `f = 8820 Hz`
30
31use fyrox_core::visitor::{PodVecView, Visit, VisitResult, Visitor};
32
33pub mod filters;
34
35#[derive(Debug, PartialEq, Clone)]
36struct SamplesContainer(pub Vec<f32>);
37
38impl Visit for SamplesContainer {
39    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
40        PodVecView::from_pod_vec(&mut self.0).visit(name, visitor)
41    }
42}
43
44/// See more info here <https://ccrma.stanford.edu/~jos/pasp/Delay_Lines.html>
45#[derive(Debug, PartialEq, Clone, Visit)]
46pub struct DelayLine {
47    #[visit(optional)]
48    samples: SamplesContainer,
49    last: f32,
50    pos: u32,
51}
52
53impl DelayLine {
54    /// Creates new instance of delay line of given length in samples.
55    pub fn new(len: usize) -> Self {
56        Self {
57            samples: SamplesContainer(vec![0.0; len]),
58            last: 0.0,
59            pos: 0,
60        }
61    }
62
63    /// Returns length of delay line in samples.
64    pub fn len(&self) -> usize {
65        self.samples.0.len()
66    }
67
68    /// Processes single sample.
69    pub fn feed(&mut self, sample: f32) -> f32 {
70        self.last = self.samples.0[self.pos as usize];
71        self.samples.0[self.pos as usize] = sample;
72        self.pos += 1;
73        if self.pos >= self.samples.0.len() as u32 {
74            self.pos -= self.samples.0.len() as u32
75        }
76        self.last
77    }
78
79    /// Returns last processed sample.
80    pub fn last(&self) -> f32 {
81        self.last
82    }
83}
84
85impl Default for DelayLine {
86    fn default() -> Self {
87        Self {
88            samples: SamplesContainer(vec![0.0]),
89            last: 0.0,
90            pos: 0,
91        }
92    }
93}
94
95/// Calculates single coefficient of Hamming window.
96/// <https://en.wikipedia.org/wiki/Window_function#Hamming_window>
97pub fn hamming_window(i: usize, sample_count: usize) -> f32 {
98    0.54 - 0.46 * (2.0 * std::f32::consts::PI * i as f32 / (sample_count - 1) as f32).cos()
99}
100
101/// Calculates single coefficient of Hann window.
102/// <https://en.wikipedia.org/wiki/Hann_function>
103pub fn hann_window(i: usize, sample_count: usize) -> f32 {
104    0.5 - 0.5 * (2.0 * std::f32::consts::PI * i as f32 / (sample_count - 1) as f32).cos()
105}
106
107/// Creates new window using specified window function.
108/// <https://en.wikipedia.org/wiki/Window_function>
109pub fn make_window<W: Fn(usize, usize) -> f32>(sample_count: usize, func: W) -> Vec<f32> {
110    (0..sample_count).map(|i| func(i, sample_count)).collect()
111}