content/media/webaudio/test/test_delayNodeCycles.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4   <title>Test the support of cycles.</title>
     5   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     6   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     7 </head>
     8 <body>
     9 <pre id="test">
    10 <script src="webaudio.js" type="text/javascript"></script>
    11 <script class="testbody" type="text/javascript">
    13 SimpleTest.waitForExplicitFinish();
    15 addLoadEvent(function() {
    16   function getSineBuffer(ctx) {
    17     var buffer = ctx.createBuffer(1, 2048, ctx.sampleRate);
    18     var b = buffer.getChannelData(0);
    19     for (var i = 0; i < 2048; i++) {
    20       b[i] = Math.sin(440 * 2 * Math.PI * i / ctx.sampleRate);
    21     }
    22     return buffer;
    23   }
    25   function createAndPlayWithCycleAndDelayNode(ctx) {
    26     var source = ctx.createBufferSource();
    27     source.loop = true;
    28     source.buffer = getSineBuffer(ctx);
    30     var gain = ctx.createGain();
    31     var delay = ctx.createDelay();
    32     delay.delayTime = 0.5;
    34     source.connect(gain);
    35     gain.connect(delay);
    36     delay.connect(ctx.destination);
    37     // cycle
    38     delay.connect(gain);
    40     source.start(0);
    41   }
    43   function createAndPlayWithCycleAndDelayNodeButNullDelayTime(ctx) {
    44     var source = ctx.createBufferSource();
    45     source.loop = true;
    46     source.buffer = getSineBuffer(ctx);
    48     var gain = ctx.createGain();
    49     var delay = ctx.createDelay();
    50     delay.delayTime = 0.0;
    52     source.connect(gain);
    53     gain.connect(delay);
    54     delay.connect(ctx.destination);
    55     // cycle
    56     delay.connect(gain);
    58     source.start(0);
    59   }
    61   function createAndPlayWithCycleAndNoDelayNode(ctx) {
    62     var source = ctx.createBufferSource();
    63     source.loop = true;
    64     source.buffer = getSineBuffer(ctx);
    66     var gain = ctx.createGain();
    67     var gain2 = ctx.createGain();
    69     source.connect(gain);
    70     gain.connect(gain2);
    71     // cycle
    72     gain2.connect(gain);
    73     gain2.connect(ctx.destination);
    75     source.start(0);
    76   }
    78   function createAndPlayWithCycleAndNoDelayNodeInCycle(ctx) {
    79     var source = ctx.createBufferSource();
    80     source.loop = true;
    81     source.buffer = getSineBuffer(ctx);
    83     var delay = ctx.createDelay();
    84     var gain = ctx.createGain();
    85     var gain2 = ctx.createGain();
    87     // Their is a cycle, a delay, but the delay is not in the cycle.
    88     source.connect(delay);
    89     delay.connect(gain);
    90     gain.connect(gain2);
    91     // cycle
    92     gain2.connect(gain);
    93     gain2.connect(ctx.destination);
    95     source.start(0);
    96   }
    98   var remainingTests = 0;
    99   function finish() {
   100     if (--remainingTests == 0) {
   101       SimpleTest.finish();
   102     }
   103   }
   105   function getOfflineContext(oncomplete) {
   106     var ctx = new OfflineAudioContext(1, 48000, 48000);
   107     ctx.oncomplete = oncomplete;
   108     return ctx;
   109   }
   111   function checkSilentBuffer(e) {
   112     var buffer = e.renderedBuffer.getChannelData(0);
   113     for (var i = 0; i < buffer.length; i++) {
   114       if (buffer[i] != 0.0) {
   115         ok(false, "buffer should be silent.");
   116         finish();
   117         return;
   118       }
   119     }
   120     ok(true, "buffer should be silent.");
   121     finish();
   122   }
   124   function checkNoisyBuffer(e) {
   125     var buffer = e.renderedBuffer.getChannelData(0);
   126     for (var i = 0; i < buffer.length; i++) {
   127       if (buffer[i] != 0.0) {
   128         ok(true, "buffer should be noisy.");
   129         finish();
   130         return true;
   131       }
   132     }
   133     ok(false, "buffer should be noisy.");
   134     finish();
   135     return false;
   136   }
   138   function expectSilentOutput(f) {
   139     remainingTests++;
   140     var ctx = getOfflineContext(checkSilentBuffer);
   141     f(ctx);
   142     ctx.startRendering();
   143   }
   145   function expectNoisyOutput(f) {
   146     remainingTests++;
   147     var ctx = getOfflineContext(checkNoisyBuffer);
   148     f(ctx);
   149     ctx.startRendering();
   150   }
   152   // This is trying to make a graph with a cycle and no DelayNode in the graph.
   153   // The cycle subgraph should be muted, in this graph the output should be silent.
   154   expectSilentOutput(createAndPlayWithCycleAndNoDelayNode);
   155   // This is trying to make a graph with a cycle and a DelayNode in the graph, but
   156   // not part of the cycle.
   157   // The cycle subgraph should be muted, in this graph the output should be silent.
   158   expectSilentOutput(createAndPlayWithCycleAndNoDelayNodeInCycle);
   159   // Those are making legal graphs, with at least one DelayNode in the cycle.
   160   // There should be some non-silent output.
   161   expectNoisyOutput(createAndPlayWithCycleAndDelayNode);
   162   // DelayNode.delayTime will be clamped to 128/ctx.sampleRate.
   163   // There should be some non-silent output.
   164   expectNoisyOutput(createAndPlayWithCycleAndDelayNodeButNullDelayTime);
   165 });
   167 </script>
   168 </pre>
   169 </body>
   170 </html>

mercurial