Skip to main content

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::pod::PodVecView;
32use fyrox_core::visitor::{Visit, VisitResult, Visitor};
33
34pub mod filters;
35
36#[derive(Debug, PartialEq, Clone)]
37struct SamplesContainer(pub Vec<f32>);
38
39impl Visit for SamplesContainer {
40    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
41        PodVecView::from_pod_vec(&mut self.0).visit(name, visitor)
42    }
43}
44
45/// See more info here <https://ccrma.stanford.edu/~jos/pasp/Delay_Lines.html>
46#[derive(Debug, PartialEq, Clone, Visit)]
47pub struct DelayLine {
48    #[visit(optional)]
49    samples: SamplesContainer,
50    last: f32,
51    pos: u32,
52}
53
54impl DelayLine {
55    /// Creates new instance of delay line of given length in samples.
56    pub fn new(len: usize) -> Self {
57        Self {
58            samples: SamplesContainer(vec![0.0; len]),
59            last: 0.0,
60            pos: 0,
61        }
62    }
63
64    /// Returns length of delay line in samples.
65    pub fn len(&self) -> usize {
66        self.samples.0.len()
67    }
68
69    /// Processes single sample.
70    pub fn feed(&mut self, sample: f32) -> f32 {
71        self.last = self.samples.0[self.pos as usize];
72        self.samples.0[self.pos as usize] = sample;
73        self.pos += 1;
74        if self.pos >= self.samples.0.len() as u32 {
75            self.pos -= self.samples.0.len() as u32
76        }
77        self.last
78    }
79
80    /// Returns last processed sample.
81    pub fn last(&self) -> f32 {
82        self.last
83    }
84}
85
86impl Default for DelayLine {
87    fn default() -> Self {
88        Self {
89            samples: SamplesContainer(vec![0.0]),
90            last: 0.0,
91            pos: 0,
92        }
93    }
94}
95
96/// Calculates single coefficient of Hamming window.
97/// <https://en.wikipedia.org/wiki/Window_function#Hamming_window>
98pub fn hamming_window(i: usize, sample_count: usize) -> f32 {
99    0.54 - 0.46 * (2.0 * std::f32::consts::PI * i as f32 / (sample_count - 1) as f32).cos()
100}
101
102/// Calculates single coefficient of Hann window.
103/// <https://en.wikipedia.org/wiki/Hann_function>
104pub fn hann_window(i: usize, sample_count: usize) -> f32 {
105    0.5 - 0.5 * (2.0 * std::f32::consts::PI * i as f32 / (sample_count - 1) as f32).cos()
106}
107
108/// Creates new window using specified window function.
109/// <https://en.wikipedia.org/wiki/Window_function>
110pub fn make_window<W: Fn(usize, usize) -> f32>(sample_count: usize, func: W) -> Vec<f32> {
111    (0..sample_count).map(|i| func(i, sample_count)).collect()
112}