Skip to main content

bae_rs/modifiers/
delay.rs

1//! # Delay
2
3use super::*;
4use std::time::Duration;
5use std::collections::VecDeque;
6
7/// Delay modifier, delays a signal by a given amount of time rounded to the
8/// nearest sample.
9pub struct Delay {
10    delay: VecDeque<SampleT>,
11}
12
13impl Delay {
14    /// Creates a new Delay object from the given duration rounded to the
15    /// nearest sample.
16    pub fn new(d: Duration) -> Self {
17        let mut v = VecDeque::new();
18
19        for _ in 0..((d.as_secs_f64()*SAMPLE_RATE as MathT).round() as usize) {
20            v.push_back(SampleT::default());
21        }
22
23        Delay {
24            delay:v
25        }
26    }
27
28    /// Returns the delay of the Modifier in a Duration value.
29    pub fn get_delay(&self) -> Duration {
30        Duration::from_secs_f64(self.delay.len() as MathT / SAMPLE_RATE as MathT)
31    }
32}
33
34impl Modifier for Delay {
35    fn process(&mut self, x: SampleT) -> SampleT {
36        self.delay.push_back(x);
37
38        self.delay.pop_front().unwrap()
39    }
40}
41
42impl Clone for Delay {
43    fn clone(&self) -> Self {
44        Delay {
45            delay: {
46                let mut v = VecDeque::new();
47
48                for _ in 0..(self.delay.len() * SAMPLE_RATE as usize) {
49                    v.push_back(SampleT::default());
50                }
51
52                v
53            }
54        }
55    }
56}