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
use crate::buffer::{ChannelConfig, ChannelConfigOptions};
use crate::context::{AsBaseAudioContext, AudioContextRegistration, AudioParamId};
use crate::param::{AudioParam, AudioParamOptions};
use crate::process::{AudioParamValues, AudioProcessor};
use crate::{SampleRate, BUFFER_SIZE};
use super::AudioNode;
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 {
registration: AudioContextRegistration,
delay_time: AudioParam,
channel_config: ChannelConfig,
}
impl AudioNode for DelayNode {
fn registration(&self) -> &AudioContextRegistration {
&self.registration
}
fn channel_config_raw(&self) -> &ChannelConfig {
&self.channel_config
}
fn number_of_inputs(&self) -> u32 {
1
}
fn number_of_outputs(&self) -> u32 {
1
}
}
impl DelayNode {
pub fn new<C: AsBaseAudioContext>(context: &C, options: DelayOptions) -> Self {
context.base().register(move |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, registration.id());
param.set_value_at_time(options.delay_time, 0.);
let max_samples = options.max_delay_time * context.base().sample_rate().0 as f32;
let max_quanta = (max_samples.ceil() as u32 + BUFFER_SIZE - 1) / BUFFER_SIZE;
let delay_buffer = Vec::with_capacity(max_quanta as usize);
let render = DelayRenderer {
delay_time: proc,
delay_buffer,
index: 0,
};
let node = DelayNode {
registration,
channel_config: options.channel_config.into(),
delay_time: param,
};
(node, Box::new(render))
})
}
pub fn delay_time(&self) -> &AudioParam {
&self.delay_time
}
}
struct DelayRenderer {
delay_time: AudioParamId,
delay_buffer: Vec<crate::alloc::AudioBuffer>,
index: usize,
}
unsafe impl Send for DelayRenderer {}
impl AudioProcessor for DelayRenderer {
fn process(
&mut self,
inputs: &[crate::alloc::AudioBuffer],
outputs: &mut [crate::alloc::AudioBuffer],
params: AudioParamValues,
_timestamp: f64,
sample_rate: SampleRate,
) {
let input = &inputs[0];
let output = &mut outputs[0];
let delay = params.get(&self.delay_time)[0];
let quanta = (delay * sample_rate.0 as f32) as usize / BUFFER_SIZE as usize;
if quanta == 0 {
*output = input.clone();
} else if self.delay_buffer.len() < quanta {
self.delay_buffer.push(input.clone());
output.make_silent();
} else {
*output = std::mem::replace(&mut self.delay_buffer[self.index], input.clone());
self.index = (self.index + 1) % quanta;
}
}
fn tail_time(&self) -> bool {
true
}
}