active-processing.js (1785B)
1 /** 2 * @class ActiveProcessingTester 3 * @extends AudioWorkletProcessor 4 * 5 * This processor class sends a message to its AudioWorkletNodew whenever the 6 * number of channels on the input changes. The message includes the actual 7 * number of channels, the context time at which this occurred, and whether 8 * we're done processing or not. 9 */ 10 class ActiveProcessingTester extends AudioWorkletProcessor { 11 constructor(options) { 12 super(options); 13 this._lastChannelCount = 0; 14 15 // See if user specified a value for test duration. 16 if (options.hasOwnProperty('processorOptions') && 17 options.processorOptions.hasOwnProperty('testDuration')) { 18 this._testDuration = options.processorOptions.testDuration; 19 } else { 20 this._testDuration = 5; 21 } 22 23 // Time at which we'll signal we're done, based on the requested 24 // |testDuration| 25 this._endTime = currentTime + this._testDuration; 26 } 27 28 process(inputs, outputs) { 29 const input = inputs[0]; 30 const output = outputs[0]; 31 const inputChannelCount = input.length; 32 const isFinished = currentTime > this._endTime; 33 34 // Send a message if we're done or the count changed. 35 if (isFinished || (inputChannelCount != this._lastChannelCount)) { 36 this.port.postMessage({ 37 channelCount: inputChannelCount, 38 finished: isFinished, 39 time: currentTime 40 }); 41 this._lastChannelCount = inputChannelCount; 42 } 43 44 // Just copy the input to the output for no particular reason. 45 for (let channel = 0; channel < input.length; ++channel) { 46 output[channel].set(input[channel]); 47 } 48 49 // When we're finished, this method no longer needs to be called. 50 return !isFinished; 51 } 52 } 53 54 registerProcessor('active-processing-tester', ActiveProcessingTester);