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