summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/processors/zero-output-processor.js
blob: 2d7399ca3ba68055926f5f8ee29f4e6f885b9c64 (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
/**
 * @class ZeroOutputProcessor
 * @extends AudioWorkletProcessor
 *
 * This processor accumulates the incoming buffer and send the buffered data
 * to the main thread when it reaches the specified frame length. The processor
 * only supports the single input.
 */

const kRenderQuantumFrames = 128;

class ZeroOutputProcessor extends AudioWorkletProcessor {
  constructor(options) {
    super();

    this._framesRequested = options.processorOptions.bufferLength;
    this._framesCaptured = 0;
    this._buffer = [];
    for (let i = 0; i < options.processorOptions.channeCount; ++i) {
      this._buffer[i] = new Float32Array(this._framesRequested);
    }
  }

  process(inputs) {
    let input = inputs[0];
    let startIndex = this._framesCaptured;
    let endIndex = startIndex + kRenderQuantumFrames;
    for (let i = 0; i < this._buffer.length; ++i) {
      this._buffer[i].subarray(startIndex, endIndex).set(input[i]);
    }
    this._framesCaptured = endIndex;

    if (this._framesCaptured >= this._framesRequested) {
      this.port.postMessage({ capturedBuffer: this._buffer });
      return false;
    } else {
      return true;
    }
  }
}

registerProcessor('zero-output-processor', ZeroOutputProcessor);