summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/processors/one-pole-processor.js
blob: 0bcc43f6f0850f0afc0a596e90f9d626bfd8daed (plain)
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
/**
 * @class OnePoleFilter
 * @extends AudioWorkletProcessor
 *
 * A simple One-pole filter.
 */

class OnePoleFilter extends AudioWorkletProcessor {

  // This gets evaluated as soon as the global scope is created.
  static get parameterDescriptors() {
    return [{
      name: 'frequency',
      defaultValue: 250,
      minValue: 0,
      maxValue: 0.5 * sampleRate
    }];
  }

  constructor() {
    super();
    this.updateCoefficientsWithFrequency_(250);
  }

  updateCoefficientsWithFrequency_(frequency) {
    this.b1_ = Math.exp(-2 * Math.PI * frequency / sampleRate);
    this.a0_ = 1.0 - this.b1_;
    this.z1_ = 0;
  }

  process(inputs, outputs, parameters) {
    let input = inputs[0];
    let output = outputs[0];
    let frequency = parameters.frequency;
    for (let channel = 0; channel < output.length; ++channel) {
      let inputChannel = input[channel];
      let outputChannel = output[channel];
      for (let i = 0; i < outputChannel.length; ++i) {
        this.updateCoefficientsWithFrequency_(frequency[i]);
        this.z1_ = inputChannel[i] * this.a0_ + this.z1_ * this.b1_;
        outputChannel[i] = this.z1_;
      }
    }

    return true;
  }
}

registerProcessor('one-pole-filter', OnePoleFilter);