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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use block::Chunk;
use block::Tick;
use node::AudioNodeEngine;
use node::BlockInfo;
use node::{AudioNodeMessage, AudioNodeType, ChannelInfo};
use param::{Param, ParamType};
use smallvec::SmallVec;
use std::f64::consts::{PI, SQRT_2};

#[derive(Copy, Clone, Debug)]
pub struct BiquadFilterNodeOptions {
    pub filter: FilterType,
    pub frequency: f32,
    pub detune: f32,
    pub q: f32,
    pub gain: f32,
}

#[derive(Copy, Clone, Debug)]
pub enum FilterType {
    LowPass,
    HighPass,
    BandPass,
    LowShelf,
    HighShelf,
    Peaking,
    Notch,
    AllPass,
}

impl Default for BiquadFilterNodeOptions {
    fn default() -> Self {
        BiquadFilterNodeOptions {
            filter: FilterType::LowPass,
            frequency: 350.,
            detune: 0.,
            q: 1.,
            gain: 0.,
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub enum BiquadFilterNodeMessage {
    SetFilterType(FilterType),
}

/// The last two input and output values, per-channel
// Default sets all fields to zero
#[derive(Default, Copy, Clone, PartialEq)]
struct BiquadState {
    /// The input value from last frame
    x1: f64,
    /// The input value from two frames ago
    x2: f64,
    /// The output value from last frame
    y1: f64,
    /// The output value from two frames ago
    y2: f64,
}

impl BiquadState {
    /// Update with new input/output values from this frame
    fn update(&mut self, x: f64, y: f64) {
        self.x2 = self.x1;
        self.x1 = x;
        self.y2 = self.y1;
        self.y1 = y;
    }
}

/// https://webaudio.github.io/web-audio-api/#biquadfilternode
#[derive(AudioNodeCommon)]
pub(crate) struct BiquadFilterNode {
    channel_info: ChannelInfo,
    filter: FilterType,
    frequency: Param,
    detune: Param,
    q: Param,
    gain: Param,
    /// The computed filter parameter b0
    /// This is actually b0 / a0, we pre-divide
    /// for efficiency
    b0: f64,
    /// The computed filter parameter b1
    /// This is actually b1 / a0, we pre-divide
    /// for efficiency
    b1: f64,
    /// The computed filter parameter b2
    /// This is actually b2 / a0, we pre-divide
    /// for efficiency
    b2: f64,
    /// The computed filter parameter a1
    /// This is actually a1 / a0, we pre-divide
    /// for efficiency
    a1: f64,
    /// The computed filter parameter a2
    /// This is actually a2 / a0, we pre-divide
    /// for efficiency
    a2: f64,
    /// Stored filter state, this contains the last two
    /// frames of input and output values for every
    /// channel
    state: SmallVec<[BiquadState; 2]>,
}

impl BiquadFilterNode {
    pub fn new(
        options: BiquadFilterNodeOptions,
        channel_info: ChannelInfo,
        sample_rate: f32,
    ) -> Self {
        let mut ret = Self {
            channel_info,
            filter: options.filter,
            frequency: Param::new(options.frequency),
            gain: Param::new(options.gain),
            q: Param::new(options.q),
            detune: Param::new(options.detune),
            b0: 0.,
            b1: 0.,
            b2: 0.,
            a1: 0.,
            a2: 0.,
            state: SmallVec::new(),
        };
        ret.update_coefficients(sample_rate);
        ret
    }

    pub fn update_parameters(&mut self, info: &BlockInfo, tick: Tick) -> bool {
        let mut changed = self.frequency.update(info, tick);
        changed |= self.detune.update(info, tick);
        changed |= self.q.update(info, tick);
        changed |= self.gain.update(info, tick);

        if changed {
            self.update_coefficients(info.sample_rate);
        }
        changed
    }

    /// Set to the constant z-transform y[n] = b0 * x[n]
    fn constant_z_transform(&mut self, b0: f64) {
        self.b0 = b0;
        self.b1 = 0.;
        self.b2 = 0.;
        self.a1 = 0.;
        self.a2 = 0.;
    }

    /// Update the coefficients a1, a2, b0, b1, b2, given the sample_rate
    ///
    /// See https://webaudio.github.io/web-audio-api/#filters-characteristics
    fn update_coefficients(&mut self, fs: f32) {
        let g: f64 = self.gain.value().into();
        let q: f64 = self.q.value().into();
        let freq: f64 = self.frequency.value().into();
        let f0: f64 = freq * (2.0_f64).powf(self.detune.value() as f64 / 1200.);
        let fs: f64 = fs.into();
        // clamp to nominal range
        // https://webaudio.github.io/web-audio-api/#biquadfilternode
        let f0 = if f0 > fs / 2. || !f0.is_finite() {
            fs / 2.
        } else if f0 < 0. {
            0.
        } else {
            f0
        };

        let normalized = f0 / fs;
        let a = 10.0_f64.powf(g / 40.);

        // the boundary values sometimes need limits to
        // be taken
        match self.filter {
            FilterType::LowPass => {
                if normalized == 1. {
                    self.constant_z_transform(1.);
                    return;
                } else if normalized == 0. {
                    self.constant_z_transform(0.);
                    return;
                }
            }
            FilterType::HighPass => {
                if normalized == 1. {
                    self.constant_z_transform(0.);
                    return;
                } else if normalized == 0. {
                    self.constant_z_transform(1.);
                    return;
                }
            }
            FilterType::LowShelf => {
                if normalized == 1. {
                    self.constant_z_transform(a * a);
                    return;
                } else if normalized == 0. {
                    self.constant_z_transform(1.);
                    return;
                }
            }
            FilterType::HighShelf => {
                if normalized == 1. {
                    self.constant_z_transform(1.);
                    return;
                } else if normalized == 0. {
                    self.constant_z_transform(a * a);
                    return;
                }
            }
            FilterType::Peaking => {
                if normalized == 0. || normalized == 1. {
                    self.constant_z_transform(1.);
                    return;
                } else if q <= 0. {
                    self.constant_z_transform(a * a);
                    return;
                }
            }
            FilterType::AllPass => {
                if normalized == 0. || normalized == 1. {
                    self.constant_z_transform(1.);
                    return;
                } else if q <= 0. {
                    self.constant_z_transform(-1.);
                    return;
                }
            }
            FilterType::Notch => {
                if normalized == 0. || normalized == 1. {
                    self.constant_z_transform(1.);
                    return;
                } else if q <= 0. {
                    self.constant_z_transform(0.);
                    return;
                }
            }
            FilterType::BandPass => {
                if normalized == 0. || normalized == 1. {
                    self.constant_z_transform(0.);
                    return;
                } else if q <= 0. {
                    self.constant_z_transform(1.);
                    return;
                }
            }
        }

        let omega0 = 2. * PI * normalized;
        let sin_omega = omega0.sin();
        let cos_omega = omega0.cos();
        let alpha_q = sin_omega / (2. * q);
        let alpha_q_db = sin_omega / (2. * 10.0_f64.powf(q / 20.));
        let alpha_s = sin_omega / SQRT_2;

        // we predivide by a0
        let a0;

        match self.filter {
            FilterType::LowPass => {
                self.b0 = (1. - cos_omega) / 2.;
                self.b1 = 1. - cos_omega;
                self.b2 = self.b1 / 2.;
                a0 = 1. + alpha_q_db;
                self.a1 = -2. * cos_omega;
                self.a2 = 1. - alpha_q_db;
            }
            FilterType::HighPass => {
                self.b0 = (1. + cos_omega) / 2.;
                self.b1 = -(1. + cos_omega);
                self.b2 = -self.b1 / 2.;
                a0 = 1. + alpha_q_db;
                self.a1 = -2. * cos_omega;
                self.a2 = 1. - alpha_q_db;
            }
            FilterType::BandPass => {
                self.b0 = alpha_q;
                self.b1 = 0.;
                self.b2 = -alpha_q;
                a0 = 1. + alpha_q;
                self.a1 = -2. * cos_omega;
                self.a2 = 1. - alpha_q;
            }
            FilterType::Notch => {
                self.b0 = 1.;
                self.b1 = -2. * cos_omega;
                self.b2 = 1.;
                a0 = 1. + alpha_q;
                self.a1 = -2. * cos_omega;
                self.a2 = 1. - alpha_q;
            }
            FilterType::AllPass => {
                self.b0 = 1. - alpha_q;
                self.b1 = -2. * cos_omega;
                self.b2 = 1. + alpha_q;
                a0 = 1. + alpha_q;
                self.a1 = -2. * cos_omega;
                self.a2 = 1. - alpha_q;
            }
            FilterType::Peaking => {
                self.b0 = 1. + alpha_q * a;
                self.b1 = -2. * cos_omega;
                self.b2 = 1. - alpha_q * a;
                a0 = 1. + alpha_q / a;
                self.a1 = -2. * cos_omega;
                self.a2 = 1. - alpha_q / a;
            }
            FilterType::LowShelf => {
                let alpha_rt_a = 2. * alpha_s * a.sqrt();
                self.b0 = a * ((a + 1.) - (a - 1.) * cos_omega + alpha_rt_a);
                self.b1 = 2. * a * ((a - 1.) - (a + 1.) * cos_omega);
                self.b2 = a * ((a + 1.) - (a - 1.) * cos_omega - alpha_rt_a);
                a0 = (a + 1.) + (a - 1.) * cos_omega + alpha_rt_a;
                self.a1 = -2. * ((a - 1.) + (a + 1.) * cos_omega);
                self.a2 = (a + 1.) + (a - 1.) * cos_omega - alpha_rt_a;
            }
            FilterType::HighShelf => {
                let alpha_rt_a = 2. * alpha_s * a.sqrt();
                self.b0 = a * ((a + 1.) + (a - 1.) * cos_omega + alpha_rt_a);
                self.b1 = -2. * a * ((a - 1.) + (a + 1.) * cos_omega);
                self.b2 = a * ((a + 1.) + (a - 1.) * cos_omega - alpha_rt_a);
                a0 = (a + 1.) - (a - 1.) * cos_omega + alpha_rt_a;
                self.a1 = 2. * ((a - 1.) - (a + 1.) * cos_omega);
                self.a2 = (a + 1.) - (a - 1.) * cos_omega - alpha_rt_a;
            }
        }
        self.b0 = self.b0 / a0;
        self.b1 = self.b1 / a0;
        self.b2 = self.b2 / a0;
        self.a1 = self.a1 / a0;
        self.a2 = self.a2 / a0;
    }
}

impl AudioNodeEngine for BiquadFilterNode {
    fn node_type(&self) -> AudioNodeType {
        AudioNodeType::BiquadFilterNode
    }

    fn process(&mut self, mut inputs: Chunk, info: &BlockInfo) -> Chunk {
        debug_assert!(inputs.len() == 1);
        self.state
            .resize(inputs.blocks[0].chan_count() as usize, Default::default());
        self.update_parameters(info, Tick(0));

        // XXXManishearth this node has tail time, so even if the block is silence
        // we must still compute things on it. However, it is possible to become
        // a dumb passthrough as long as we reach a quiescent state
        //
        // see https://dxr.mozilla.org/mozilla-central/rev/87a95e1b7ec691bef7b938e722fe1b01cce68664/dom/media/webaudio/blink/Biquad.cpp#81-91

        let repeat_or_silence = inputs.blocks[0].is_silence() || inputs.blocks[0].is_repeat();

        if repeat_or_silence && !self.state.iter().all(|s| *s == self.state[0]) {
            // In case our input is repeat/silence but our states are not identical, we must
            // explicitly duplicate, since mutate_with will otherwise only operate
            // on the first channel, ignoring the states of the later ones
            inputs.blocks[0].explicit_repeat();
        } else {
            // In case the states are identical, just make any silence explicit,
            // since mutate_with can't handle silent blocks
            inputs.blocks[0].explicit_silence();
        }

        {
            let mut iter = inputs.blocks[0].iter();
            while let Some(mut frame) = iter.next() {
                self.update_parameters(info, frame.tick());
                frame.mutate_with(|sample, chan| {
                    let state = &mut self.state[chan as usize];
                    let x0 = *sample as f64;
                    let y0 = self.b0 * x0 + self.b1 * state.x1 + self.b2 * state.x2
                        - self.a1 * state.y1
                        - self.a2 * state.y2;
                    *sample = y0 as f32;
                    state.update(x0, y0);
                });
            }
        }

        if inputs.blocks[0].is_repeat() {
            let state = self.state[0];
            self.state.iter_mut().for_each(|s| *s = state);
        }

        inputs
    }

    fn get_param(&mut self, id: ParamType) -> &mut Param {
        match id {
            ParamType::Frequency => &mut self.frequency,
            ParamType::Detune => &mut self.detune,
            ParamType::Q => &mut self.q,
            ParamType::Gain => &mut self.gain,
            _ => panic!("Unknown param {:?} for BiquadFilterNode", id),
        }
    }

    fn message_specific(&mut self, message: AudioNodeMessage, sample_rate: f32) {
        match message {
            AudioNodeMessage::BiquadFilterNode(m) => match m {
                BiquadFilterNodeMessage::SetFilterType(f) => {
                    self.filter = f;
                    self.update_coefficients(sample_rate);
                }
            },
            _ => (),
        }
    }
}