| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>ScriptProcessorNode on OfflineAudioContext</title> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="resources/audio-testing.js"></script> |
| </head> |
| <body> |
| <script> |
| var audit = Audit.createTaskRunner(); |
| |
| |
| // Fill the output of script processor with a constant value. |
| audit.defineTask('simple-output', function (taskDone) { |
| var sampleRate = 44100; |
| var scriptBufferSize = 256; |
| var renderLength = 1; |
| var PI = Math.fround(Math.PI); |
| |
| var context = new OfflineAudioContext( |
| 1, renderLength * sampleRate, sampleRate); |
| |
| var scriptNode = context.createScriptProcessor(scriptBufferSize, 1, 1); |
| scriptNode.onaudioprocess = function (event) { |
| var outputChannel = event.outputBuffer.getChannelData(0); |
| outputChannel.fill(PI); |
| }; |
| scriptNode.connect(context.destination); |
| |
| context.startRendering().then(function (buffer) { |
| var channel = buffer.getChannelData(0); |
| var initialDelay = channel.subarray(0, 2 * scriptBufferSize); |
| var actualContent = channel.subarray(2 * scriptBufferSize); |
| |
| // There is the initial delay (2 x buffer size) which is silent. |
| Should('The initial delay contains zeros.', initialDelay) |
| .beConstantValueOf(0); |
| |
| // After the initial delay, we must get |PI|. |
| Should('The actual content contains ' + PI, actualContent) |
| .beConstantValueOf(PI); |
| |
| taskDone(); |
| }); |
| }); |
| |
| |
| // Pass through an oscillator via a script processor. Sum with the |
| // phase-inverted oscillator with the delayed start time. Verify the |
| // rendered buffer is completely silent. |
| audit.defineTask('oscillator-output', function (taskDone) { |
| var sampleRate = 44100; |
| var scriptBufferSize = 256; |
| var renderLength = 1; |
| |
| var context = new OfflineAudioContext( |
| 1, renderLength * sampleRate, sampleRate); |
| |
| var osc1 = context.createOscillator(); |
| var osc2 = context.createOscillator(); |
| var inverter = context.createGain(); |
| var scriptNode = context.createScriptProcessor(scriptBufferSize, 1, 1); |
| scriptNode.onaudioprocess = function (event) { |
| var inputChannel = event.inputBuffer.getChannelData(0); |
| var outputChannel = event.outputBuffer.getChannelData(0); |
| outputChannel.set(inputChannel); |
| }; |
| |
| inverter.gain.value = -1; |
| |
| osc1.connect(inverter).connect(context.destination); |
| osc2.connect(scriptNode).connect(context.destination); |
| |
| // The delayed start for |osc1|. |
| osc1.start((2 * scriptBufferSize) / sampleRate); |
| osc2.start(); |
| |
| context.startRendering().then(function (buffer) { |
| var channel = buffer.getChannelData(0); |
| |
| // The rendered buffer must be silent. |
| Should('The rendered buffer', channel) |
| .beConstantValueOf(0); |
| |
| taskDone(); |
| }); |
| }); |
| |
| |
| audit.runTasks(); |
| </script> |
| </body> |
| </html> |