summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/processors/active-processing.js
blob: ef497733cafb471f74ace33045c4b4d43f3ce4f2 (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
50
51
52
53
54
/**
 * @class ActiveProcessingTester
 * @extends AudioWorkletProcessor
 *
 * This processor class sends a message to its AudioWorkletNodew whenever the
 * number of channels on the input changes.  The message includes the actual
 * number of channels, the context time at which this occurred, and whether
 * we're done processing or not.
 */
class ActiveProcessingTester extends AudioWorkletProcessor {
  constructor(options) {
    super(options);
    this._lastChannelCount = 0;

    // See if user specified a value for test duration.
    if (options.hasOwnProperty('processorOptions') &&
        options.processorOptions.hasOwnProperty('testDuration')) {
      this._testDuration = options.processorOptions.testDuration;
    } else {
      this._testDuration = 5;
    }

    // Time at which we'll signal we're done, based on the requested
    // |testDuration|
    this._endTime = currentTime + this._testDuration;
  }

  process(inputs, outputs) {
    const input = inputs[0];
    const output = outputs[0];
    const inputChannelCount = input.length;
    const isFinished = currentTime > this._endTime;

    // Send a message if we're done or the count changed.
    if (isFinished || (inputChannelCount != this._lastChannelCount)) {
      this.port.postMessage({
        channelCount: inputChannelCount,
        finished: isFinished,
        time: currentTime
      });
      this._lastChannelCount = inputChannelCount;
    }

    // Just copy the input to the output for no particular reason.
    for (let channel = 0; channel < input.length; ++channel) {
      output[channel].set(input[channel]);
    }

    // When we're finished, this method no longer needs to be called.
    return !isFinished;
  }
}

registerProcessor('active-processing-tester', ActiveProcessingTester);