content/media/webaudio/test/test_scriptProcessorNode.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/test/test_scriptProcessorNode.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test ScriptProcessorNode</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <script type="text/javascript" src="webaudio.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body>
    1.13 +<pre id="test">
    1.14 +<script class="testbody" type="text/javascript">
    1.15 +
    1.16 +// We do not use our generic graph test framework here because
    1.17 +// the testing logic here is sort of complicated, and would
    1.18 +// not be easy to map to OfflineAudioContext, as ScriptProcessorNodes
    1.19 +// can experience delays.
    1.20 +
    1.21 +SimpleTest.waitForExplicitFinish();
    1.22 +addLoadEvent(function() {
    1.23 +  var context = new AudioContext();
    1.24 +  var buffer = null;
    1.25 +
    1.26 +  var sourceSP = context.createScriptProcessor(2048);
    1.27 +  sourceSP.addEventListener("audioprocess", function(e) {
    1.28 +    // generate the audio
    1.29 +    for (var i = 0; i < 2048; ++i) {
    1.30 +      // Make sure our first sample won't be zero
    1.31 +      e.outputBuffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * (i + 1) / context.sampleRate);
    1.32 +      e.outputBuffer.getChannelData(0)[i] = Math.sin(880 * 2 * Math.PI * (i + 1) / context.sampleRate);
    1.33 +    }
    1.34 +    // Remember our generated audio
    1.35 +    buffer = e.outputBuffer;
    1.36 +
    1.37 +    sourceSP.removeEventListener("audioprocess", arguments.callee);
    1.38 +  }, false);
    1.39 +
    1.40 +  expectException(function() {
    1.41 +    context.createScriptProcessor(1);
    1.42 +  }, DOMException.INDEX_SIZE_ERR);
    1.43 +  expectException(function() {
    1.44 +    context.createScriptProcessor(2);
    1.45 +  }, DOMException.INDEX_SIZE_ERR);
    1.46 +  expectException(function() {
    1.47 +    context.createScriptProcessor(128);
    1.48 +  }, DOMException.INDEX_SIZE_ERR);
    1.49 +  expectException(function() {
    1.50 +    context.createScriptProcessor(255);
    1.51 +  }, DOMException.INDEX_SIZE_ERR);
    1.52 +
    1.53 +  is(sourceSP.channelCount, 2, "script processor node has 2 input channels by default");
    1.54 +  is(sourceSP.channelCountMode, "explicit", "Correct channelCountMode for the script processor node");
    1.55 +  is(sourceSP.channelInterpretation, "speakers", "Correct channelCountInterpretation for the script processor node");
    1.56 +
    1.57 +  function findFirstNonZeroSample(buffer) {
    1.58 +    for (var i = 0; i < buffer.length; ++i) {
    1.59 +      if (buffer.getChannelData(0)[i] != 0) {
    1.60 +        return i;
    1.61 +      }
    1.62 +    }
    1.63 +    return buffer.length;
    1.64 +  }
    1.65 +
    1.66 +  var sp = context.createScriptProcessor(2048);
    1.67 +  sourceSP.connect(sp);
    1.68 +  sp.connect(context.destination);
    1.69 +  var lastPlaybackTime = 0;
    1.70 +  sp.onaudioprocess = function(e) {
    1.71 +    isnot(buffer, null, "The audioprocess handler for sourceSP must be run at this point");
    1.72 +    is(e.target, sp, "Correct event target");
    1.73 +    ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
    1.74 +    lastPlaybackTime = e.playbackTime;
    1.75 +    is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
    1.76 +    is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
    1.77 +    is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
    1.78 +    is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
    1.79 +    is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
    1.80 +    is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
    1.81 +
    1.82 +    // Because of the initial latency added by the second script processor node,
    1.83 +    // we will never see any generated audio frames in the first callback.
    1.84 +    var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);
    1.85 +    compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
    1.86 +    compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
    1.87 +    compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
    1.88 +    compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
    1.89 +
    1.90 +    sp.onaudioprocess = function(e) {
    1.91 +      is(e.target, sp, "Correct event target");
    1.92 +      ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
    1.93 +      lastPlaybackTime = e.playbackTime;
    1.94 +      is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
    1.95 +      is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
    1.96 +      is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
    1.97 +      is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
    1.98 +      is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
    1.99 +      is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
   1.100 +
   1.101 +      var firstNonZero = findFirstNonZeroSample(e.inputBuffer);
   1.102 +      ok(firstNonZero <= 2048, "First non-zero sample within range");
   1.103 +
   1.104 +      compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), firstNonZero);
   1.105 +      compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), firstNonZero);
   1.106 +      compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), 2048 - firstNonZero, firstNonZero, 0);
   1.107 +      compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), 2048 - firstNonZero, firstNonZero, 0);
   1.108 +      compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
   1.109 +      compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
   1.110 +
   1.111 +      if (firstNonZero == 0) {
   1.112 +        // If we did not experience any delays, the test is done!
   1.113 +        sp.onaudioprocess = null;
   1.114 +
   1.115 +        SimpleTest.finish();
   1.116 +      } else if (firstNonZero != 2048) {
   1.117 +        // In case we just saw a zero buffer this time, wait one more round
   1.118 +        sp.onaudioprocess = function(e) {
   1.119 +          is(e.target, sp, "Correct event target");
   1.120 +          ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
   1.121 +          lastPlaybackTime = e.playbackTime;
   1.122 +          is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
   1.123 +          is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
   1.124 +          is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
   1.125 +          is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
   1.126 +          is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
   1.127 +          is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
   1.128 +
   1.129 +          compareChannels(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), firstNonZero, 0, 2048 - firstNonZero);
   1.130 +          compareChannels(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), firstNonZero, 0, 2048 - firstNonZero);
   1.131 +          compareChannels(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), undefined, firstNonZero);
   1.132 +          compareChannels(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), undefined, firstNonZero);
   1.133 +          compareChannels(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
   1.134 +          compareChannels(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
   1.135 +
   1.136 +          sp.onaudioprocess = null;
   1.137 +
   1.138 +          SimpleTest.finish();
   1.139 +        };
   1.140 +      }
   1.141 +    };
   1.142 +  };
   1.143 +});
   1.144 +
   1.145 +</script>
   1.146 +</pre>
   1.147 +</body>
   1.148 +</html>

mercurial