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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use oxcable::types::{SAMPLE_RATE, AudioDevice, MessageReceiver, Sample, Time};
use oxcable::utils::ringbuffer::RingBuffer;
#[derive(Clone, Copy, Debug)]
pub enum Message {
SetDelay(f32),
SetFeedback(f32),
SetWetness(f32)
}
pub use self::Message::*;
pub struct Delay {
num_channels: usize,
delay_buffers: Vec<RingBuffer<Sample>>,
feedback: f32,
wetness: f32
}
impl Delay {
pub fn new(delay: f32, feedback: f32, wetness: f32,
num_channels: usize) -> Self {
let delay_samples = (delay * SAMPLE_RATE as f32) as usize;
let init = vec![0.0; delay_samples];
Delay {
num_channels: num_channels,
delay_buffers: vec![RingBuffer::from(&init[..]); num_channels],
feedback: feedback,
wetness: wetness
}
}
}
impl MessageReceiver for Delay {
type Msg = Message;
fn handle_message(&mut self, msg: Message) {
match msg {
SetDelay(delay) => {
let delay_samples = (delay * SAMPLE_RATE as f32) as usize;
for rb in self.delay_buffers.iter_mut() {
rb.resize(delay_samples);
}
},
SetFeedback(feedback) => {
self.feedback = feedback;
},
SetWetness(wetness) => {
self.wetness = wetness;
}
}
}
}
impl AudioDevice for Delay {
fn num_inputs(&self) -> usize {
self.num_channels
}
fn num_outputs(&self) -> usize {
self.num_channels
}
fn tick(&mut self, t: Time, inputs: &[Sample], outputs: &mut[Sample]) {
for (i,s) in inputs.iter().enumerate() {
let delayed = self.delay_buffers[i][t];
self.delay_buffers[i].push(s + self.feedback*delayed);
outputs[i] = self.wetness*delayed + (1.0-self.wetness)*s;
}
}
}