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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::f32::consts::PI;
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
use std::sync::Arc;
use crate::context::{AudioContextRegistration, AudioNodeId, ConcreteBaseAudioContext};
use crate::media::MediaStream;
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum};
use crate::SampleRate;
use lazy_static::lazy_static;
mod analyser;
pub use analyser::*;
mod audio_buffer_source;
pub use audio_buffer_source::*;
mod biquad_filter;
pub use biquad_filter::*;
mod channel_merger;
pub use channel_merger::*;
mod channel_splitter;
pub use channel_splitter::*;
mod constant_source;
pub use constant_source::*;
mod delay;
pub use delay::*;
mod destination;
pub use destination::*;
mod gain;
pub use gain::*;
mod iir_filter;
pub use iir_filter::*;
mod media_stream_destination;
pub use media_stream_destination::*;
mod media_stream_source;
pub use media_stream_source::*;
mod oscillator;
pub use oscillator::*;
mod panner;
pub use panner::*;
mod stereo_panner;
pub use stereo_panner::*;
mod waveshaper;
pub use waveshaper::*;
pub(crate) const TABLE_LENGTH_USIZE: usize = 8192;
pub(crate) const TABLE_LENGTH_BY_4_USIZE: usize = TABLE_LENGTH_USIZE / 4;
pub(crate) const TABLE_LENGTH_F32: f32 = TABLE_LENGTH_USIZE as f32;
pub(crate) const TABLE_LENGTH_BY_4_F32: f32 = TABLE_LENGTH_BY_4_USIZE as f32;
lazy_static! {
pub(crate) static ref SINETABLE: Vec<f32> = {
let table: Vec<f32> = (0..TABLE_LENGTH_USIZE)
.map(|x| ((x as f32) * 2.0 * PI * (1. / (TABLE_LENGTH_F32))).sin())
.collect();
table
};
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ChannelCountMode {
Max,
ClampedMax,
Explicit,
}
impl From<u32> for ChannelCountMode {
fn from(i: u32) -> Self {
use ChannelCountMode::*;
match i {
0 => Max,
1 => ClampedMax,
2 => Explicit,
_ => unreachable!(),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ChannelInterpretation {
Speakers,
Discrete,
}
impl From<u32> for ChannelInterpretation {
fn from(i: u32) -> Self {
use ChannelInterpretation::*;
match i {
0 => Speakers,
1 => Discrete,
_ => unreachable!(),
}
}
}
#[derive(Clone, Debug)]
pub struct ChannelConfigOptions {
pub count: usize,
pub mode: ChannelCountMode,
pub interpretation: ChannelInterpretation,
}
impl Default for ChannelConfigOptions {
fn default() -> Self {
Self {
count: 2,
mode: ChannelCountMode::Max,
interpretation: ChannelInterpretation::Speakers,
}
}
}
#[derive(Clone, Debug)]
pub struct ChannelConfig {
count: Arc<AtomicUsize>,
mode: Arc<AtomicU32>,
interpretation: Arc<AtomicU32>,
}
impl ChannelConfig {
pub fn count_mode(&self) -> ChannelCountMode {
self.mode.load(Ordering::SeqCst).into()
}
pub fn set_count_mode(&self, v: ChannelCountMode) {
self.mode.store(v as u32, Ordering::SeqCst)
}
pub fn interpretation(&self) -> ChannelInterpretation {
self.interpretation.load(Ordering::SeqCst).into()
}
pub fn set_interpretation(&self, v: ChannelInterpretation) {
self.interpretation.store(v as u32, Ordering::SeqCst)
}
pub fn count(&self) -> usize {
self.count.load(Ordering::SeqCst)
}
pub fn set_count(&self, v: usize) {
self.count.store(v, Ordering::SeqCst)
}
}
impl From<ChannelConfigOptions> for ChannelConfig {
fn from(opts: ChannelConfigOptions) -> Self {
ChannelConfig {
count: Arc::new(AtomicUsize::from(opts.count)),
mode: Arc::new(AtomicU32::from(opts.mode as u32)),
interpretation: Arc::new(AtomicU32::from(opts.interpretation as u32)),
}
}
}
pub trait AudioNode {
fn registration(&self) -> &AudioContextRegistration;
fn id(&self) -> &AudioNodeId {
self.registration().id()
}
fn channel_config_raw(&self) -> &ChannelConfig;
fn channel_config_cloned(&self) -> ChannelConfig {
self.channel_config_raw().clone()
}
fn context(&self) -> &ConcreteBaseAudioContext {
self.registration().context()
}
fn connect<'a>(&self, dest: &'a dyn AudioNode) -> &'a dyn AudioNode {
self.connect_at(dest, 0, 0)
}
fn connect_at<'a>(
&self,
dest: &'a dyn AudioNode,
output: u32,
input: u32,
) -> &'a dyn AudioNode {
if self.context() != dest.context() {
panic!("InvalidAccessError: Attempting to connect nodes from different contexts");
}
if self.number_of_outputs() <= output {
panic!("IndexSizeError: output port {} is out of bounds", output);
}
if dest.number_of_inputs() <= input {
panic!("IndexSizeError: input port {} is out of bounds", input);
}
self.context().connect(self.id(), dest.id(), output, input);
dest
}
fn disconnect_from<'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_from(self.id(), dest.id());
dest
}
fn disconnect(&self) {
self.context().disconnect(self.id());
}
fn number_of_inputs(&self) -> u32;
fn number_of_outputs(&self) -> u32;
fn channel_count_mode(&self) -> ChannelCountMode {
self.channel_config_raw().count_mode()
}
fn set_channel_count_mode(&self, v: ChannelCountMode) {
self.channel_config_raw().set_count_mode(v)
}
fn channel_interpretation(&self) -> ChannelInterpretation {
self.channel_config_raw().interpretation()
}
fn set_channel_interpretation(&self, v: ChannelInterpretation) {
self.channel_config_raw().set_interpretation(v)
}
fn channel_count(&self) -> usize {
self.channel_config_raw().count()
}
fn set_channel_count(&self, v: usize) {
self.channel_config_raw().set_count(v)
}
}
pub trait AudioScheduledSourceNode {
fn start(&self);
fn start_at(&self, when: f64);
fn stop(&self);
fn stop_at(&self, when: f64);
}
struct MediaStreamRenderer<R> {
stream: R,
finished: bool,
}
impl<R> MediaStreamRenderer<R> {
fn new(stream: R) -> Self {
Self {
stream,
finished: false,
}
}
}
impl<R: MediaStream> AudioProcessor for MediaStreamRenderer<R> {
fn process(
&mut self,
_inputs: &[AudioRenderQuantum],
outputs: &mut [AudioRenderQuantum],
_params: AudioParamValues,
_timestamp: f64,
_sample_rate: SampleRate,
) -> bool {
let output = &mut outputs[0];
match self.stream.next() {
Some(Ok(buffer)) => {
let channels = buffer.number_of_channels();
output.set_number_of_channels(channels);
output
.channels_mut()
.iter_mut()
.zip(buffer.channels())
.for_each(|(o, i)| o.copy_from_slice(i.as_slice()));
}
Some(Err(e)) => {
log::warn!("Error playing audio stream: {}", e);
self.finished = true;
output.make_silent()
}
None => {
if !self.finished {
log::debug!("Stream finished");
self.finished = true;
}
output.make_silent()
}
}
!self.finished
}
}