content/media/webaudio/test/test_scriptProcessorNode.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>Test ScriptProcessorNode</title>
michael@0 5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <script type="text/javascript" src="webaudio.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 8 </head>
michael@0 9 <body>
michael@0 10 <pre id="test">
michael@0 11 <script class="testbody" type="text/javascript">
michael@0 12
michael@0 13 // We do not use our generic graph test framework here because
michael@0 14 // the testing logic here is sort of complicated, and would
michael@0 15 // not be easy to map to OfflineAudioContext, as ScriptProcessorNodes
michael@0 16 // can experience delays.
michael@0 17
michael@0 18 SimpleTest.waitForExplicitFinish();
michael@0 19 addLoadEvent(function() {
michael@0 20 var context = new AudioContext();
michael@0 21 var buffer = null;
michael@0 22
michael@0 23 var sourceSP = context.createScriptProcessor(2048);
michael@0 24 sourceSP.addEventListener("audioprocess", function(e) {
michael@0 25 // generate the audio
michael@0 26 for (var i = 0; i < 2048; ++i) {
michael@0 27 // Make sure our first sample won't be zero
michael@0 28 e.outputBuffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * (i + 1) / context.sampleRate);
michael@0 29 e.outputBuffer.getChannelData(0)[i] = Math.sin(880 * 2 * Math.PI * (i + 1) / context.sampleRate);
michael@0 30 }
michael@0 31 // Remember our generated audio
michael@0 32 buffer = e.outputBuffer;
michael@0 33
michael@0 34 sourceSP.removeEventListener("audioprocess", arguments.callee);
michael@0 35 }, false);
michael@0 36
michael@0 37 expectException(function() {
michael@0 38 context.createScriptProcessor(1);
michael@0 39 }, DOMException.INDEX_SIZE_ERR);
michael@0 40 expectException(function() {
michael@0 41 context.createScriptProcessor(2);
michael@0 42 }, DOMException.INDEX_SIZE_ERR);
michael@0 43 expectException(function() {
michael@0 44 context.createScriptProcessor(128);
michael@0 45 }, DOMException.INDEX_SIZE_ERR);
michael@0 46 expectException(function() {
michael@0 47 context.createScriptProcessor(255);
michael@0 48 }, DOMException.INDEX_SIZE_ERR);
michael@0 49
michael@0 50 is(sourceSP.channelCount, 2, "script processor node has 2 input channels by default");
michael@0 51 is(sourceSP.channelCountMode, "explicit", "Correct channelCountMode for the script processor node");
michael@0 52 is(sourceSP.channelInterpretation, "speakers", "Correct channelCountInterpretation for the script processor node");
michael@0 53
michael@0 54 function findFirstNonZeroSample(buffer) {
michael@0 55 for (var i = 0; i < buffer.length; ++i) {
michael@0 56 if (buffer.getChannelData(0)[i] != 0) {
michael@0 57 return i;
michael@0 58 }
michael@0 59 }
michael@0 60 return buffer.length;
michael@0 61 }
michael@0 62
michael@0 63 var sp = context.createScriptProcessor(2048);
michael@0 64 sourceSP.connect(sp);
michael@0 65 sp.connect(context.destination);
michael@0 66 var lastPlaybackTime = 0;
michael@0 67 sp.onaudioprocess = function(e) {
michael@0 68 isnot(buffer, null, "The audioprocess handler for sourceSP must be run at this point");
michael@0 69 is(e.target, sp, "Correct event target");
michael@0 70 ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
michael@0 71 lastPlaybackTime = e.playbackTime;
michael@0 72 is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
michael@0 73 is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
michael@0 74 is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
michael@0 75 is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
michael@0 76 is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
michael@0 77 is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
michael@0 78
michael@0 79 // Because of the initial latency added by the second script processor node,
michael@0 80 // we will never see any generated audio frames in the first callback.
michael@0 81 var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);
michael@0 82 compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
michael@0 83 compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
michael@0 84 compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
michael@0 85 compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
michael@0 86
michael@0 87 sp.onaudioprocess = function(e) {
michael@0 88 is(e.target, sp, "Correct event target");
michael@0 89 ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
michael@0 90 lastPlaybackTime = e.playbackTime;
michael@0 91 is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
michael@0 92 is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
michael@0 93 is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
michael@0 94 is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
michael@0 95 is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
michael@0 96 is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
michael@0 97
michael@0 98 var firstNonZero = findFirstNonZeroSample(e.inputBuffer);
michael@0 99 ok(firstNonZero <= 2048, "First non-zero sample within range");
michael@0 100
michael@0 101 compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), firstNonZero);
michael@0 102 compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), firstNonZero);
michael@0 103 compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), 2048 - firstNonZero, firstNonZero, 0);
michael@0 104 compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), 2048 - firstNonZero, firstNonZero, 0);
michael@0 105 compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
michael@0 106 compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
michael@0 107
michael@0 108 if (firstNonZero == 0) {
michael@0 109 // If we did not experience any delays, the test is done!
michael@0 110 sp.onaudioprocess = null;
michael@0 111
michael@0 112 SimpleTest.finish();
michael@0 113 } else if (firstNonZero != 2048) {
michael@0 114 // In case we just saw a zero buffer this time, wait one more round
michael@0 115 sp.onaudioprocess = function(e) {
michael@0 116 is(e.target, sp, "Correct event target");
michael@0 117 ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
michael@0 118 lastPlaybackTime = e.playbackTime;
michael@0 119 is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
michael@0 120 is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
michael@0 121 is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
michael@0 122 is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
michael@0 123 is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
michael@0 124 is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
michael@0 125
michael@0 126 compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), firstNonZero, 0, 2048 - firstNonZero);
michael@0 127 compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), firstNonZero, 0, 2048 - firstNonZero);
michael@0 128 compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), undefined, firstNonZero);
michael@0 129 compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), undefined, firstNonZero);
michael@0 130 compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
michael@0 131 compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
michael@0 132
michael@0 133 sp.onaudioprocess = null;
michael@0 134
michael@0 135 SimpleTest.finish();
michael@0 136 };
michael@0 137 }
michael@0 138 };
michael@0 139 };
michael@0 140 });
michael@0 141
michael@0 142 </script>
michael@0 143 </pre>
michael@0 144 </body>
michael@0 145 </html>

mercurial