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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use crate::context::{AsBaseAudioContext, AudioContextRegistration, AudioParamId};
use crate::param::{AudioParam, AudioParamOptions};
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum};
use crate::{SampleRate, RENDER_QUANTUM_SIZE};
use super::{AudioNode, ChannelConfig, ChannelConfigOptions};
use std::cell::RefCell;
use std::rc::Rc;
pub struct DelayOptions {
pub max_delay_time: f32,
pub delay_time: f32,
pub channel_config: ChannelConfigOptions,
}
impl Default for DelayOptions {
fn default() -> Self {
Self {
max_delay_time: 1.,
delay_time: 0.,
channel_config: ChannelConfigOptions::default(),
}
}
}
pub struct DelayNode {
reader_registration: AudioContextRegistration,
writer_registration: AudioContextRegistration,
delay_time: AudioParam,
channel_config: ChannelConfig,
}
impl AudioNode for DelayNode {
fn registration(&self) -> &AudioContextRegistration {
&self.writer_registration
}
fn channel_config_raw(&self) -> &ChannelConfig {
&self.channel_config
}
fn number_of_inputs(&self) -> u32 {
1
}
fn number_of_outputs(&self) -> u32 {
1
}
fn connect_at<'a>(
&self,
dest: &'a dyn AudioNode,
output: u32,
input: u32,
) -> Result<&'a dyn AudioNode, crate::IndexSizeError> {
if self.context() != dest.context() {
panic!("attempting to connect nodes from different contexts");
}
if self.number_of_outputs() <= output || dest.number_of_inputs() <= input {
return Err(crate::IndexSizeError {});
}
self.context()
.connect(self.reader_registration.id(), dest.id(), output, input);
Ok(dest)
}
fn disconnect<'a>(&self, dest: &'a dyn AudioNode) -> &'a dyn AudioNode {
if self.context() != dest.context() {
panic!("attempting to disconnect nodes from different contexts");
}
self.context()
.disconnect(self.reader_registration.id(), dest.id());
dest
}
fn disconnect_all(&self) {
self.context().disconnect_all(self.reader_registration.id());
}
}
impl DelayNode {
pub fn new<C: AsBaseAudioContext>(context: &C, options: DelayOptions) -> Self {
let max_samples = options.max_delay_time * context.base().sample_rate().0 as f32;
let max_quanta =
(max_samples.ceil() as usize + RENDER_QUANTUM_SIZE - 1) / RENDER_QUANTUM_SIZE;
let delay_buffer = Vec::with_capacity(max_quanta);
let shared_buffer = Rc::new(RefCell::new(delay_buffer));
let shared_buffer_clone = shared_buffer.clone();
context.base().register(move |writer_registration| {
let node = context.base().register(move |reader_registration| {
let param_opts = AudioParamOptions {
min_value: 0.,
max_value: options.max_delay_time,
default_value: 0.,
automation_rate: crate::param::AutomationRate::A,
};
let (param, proc) = context
.base()
.create_audio_param(param_opts, reader_registration.id());
param.set_value_at_time(options.delay_time, 0.);
let reader_render = DelayReader {
delay_time: proc,
delay_buffer: shared_buffer_clone,
index: 0,
};
let node = DelayNode {
reader_registration,
writer_registration,
channel_config: options.channel_config.into(),
delay_time: param,
};
(node, Box::new(reader_render))
});
let writer_render = DelayWriter {
delay_buffer: shared_buffer,
index: 0,
};
(node, Box::new(writer_render))
})
}
pub fn delay_time(&self) -> &AudioParam {
&self.delay_time
}
}
struct DelayReader {
delay_time: AudioParamId,
delay_buffer: Rc<RefCell<Vec<AudioRenderQuantum>>>,
index: usize,
}
struct DelayWriter {
delay_buffer: Rc<RefCell<Vec<AudioRenderQuantum>>>,
index: usize,
}
unsafe impl Send for DelayReader {}
unsafe impl Send for DelayWriter {}
impl AudioProcessor for DelayWriter {
fn process(
&mut self,
inputs: &[AudioRenderQuantum],
outputs: &mut [AudioRenderQuantum],
_params: AudioParamValues,
_timestamp: f64,
_sample_rate: SampleRate,
) -> bool {
let input = inputs[0].clone();
let output = &mut outputs[0];
let mut buffer = self.delay_buffer.borrow_mut();
if buffer.len() < buffer.capacity() {
buffer.push(input);
} else {
buffer[self.index] = input;
}
self.index = (self.index + 1) % buffer.capacity();
output.make_silent();
true
}
}
impl AudioProcessor for DelayReader {
fn process(
&mut self,
_inputs: &[AudioRenderQuantum],
outputs: &mut [AudioRenderQuantum],
params: AudioParamValues,
_timestamp: f64,
sample_rate: SampleRate,
) -> bool {
let output = &mut outputs[0];
let delay = params.get(&self.delay_time)[0];
let quanta = (delay * sample_rate.0 as f32) as usize / RENDER_QUANTUM_SIZE;
let quanta = quanta.max(1);
let buffer = self.delay_buffer.borrow_mut();
let delayed_index = (self.index + buffer.capacity() - quanta) % buffer.capacity();
if delayed_index >= buffer.len() {
output.make_silent();
} else {
*output = buffer[delayed_index].clone();
}
self.index = (self.index + 1) % buffer.capacity();
true
}
}