content/media/webaudio/test/test_delayNodeCycles.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:3e83555b1d95
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">
12
13 SimpleTest.waitForExplicitFinish();
14
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 }
24
25 function createAndPlayWithCycleAndDelayNode(ctx) {
26 var source = ctx.createBufferSource();
27 source.loop = true;
28 source.buffer = getSineBuffer(ctx);
29
30 var gain = ctx.createGain();
31 var delay = ctx.createDelay();
32 delay.delayTime = 0.5;
33
34 source.connect(gain);
35 gain.connect(delay);
36 delay.connect(ctx.destination);
37 // cycle
38 delay.connect(gain);
39
40 source.start(0);
41 }
42
43 function createAndPlayWithCycleAndDelayNodeButNullDelayTime(ctx) {
44 var source = ctx.createBufferSource();
45 source.loop = true;
46 source.buffer = getSineBuffer(ctx);
47
48 var gain = ctx.createGain();
49 var delay = ctx.createDelay();
50 delay.delayTime = 0.0;
51
52 source.connect(gain);
53 gain.connect(delay);
54 delay.connect(ctx.destination);
55 // cycle
56 delay.connect(gain);
57
58 source.start(0);
59 }
60
61 function createAndPlayWithCycleAndNoDelayNode(ctx) {
62 var source = ctx.createBufferSource();
63 source.loop = true;
64 source.buffer = getSineBuffer(ctx);
65
66 var gain = ctx.createGain();
67 var gain2 = ctx.createGain();
68
69 source.connect(gain);
70 gain.connect(gain2);
71 // cycle
72 gain2.connect(gain);
73 gain2.connect(ctx.destination);
74
75 source.start(0);
76 }
77
78 function createAndPlayWithCycleAndNoDelayNodeInCycle(ctx) {
79 var source = ctx.createBufferSource();
80 source.loop = true;
81 source.buffer = getSineBuffer(ctx);
82
83 var delay = ctx.createDelay();
84 var gain = ctx.createGain();
85 var gain2 = ctx.createGain();
86
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);
94
95 source.start(0);
96 }
97
98 var remainingTests = 0;
99 function finish() {
100 if (--remainingTests == 0) {
101 SimpleTest.finish();
102 }
103 }
104
105 function getOfflineContext(oncomplete) {
106 var ctx = new OfflineAudioContext(1, 48000, 48000);
107 ctx.oncomplete = oncomplete;
108 return ctx;
109 }
110
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 }
123
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 }
137
138 function expectSilentOutput(f) {
139 remainingTests++;
140 var ctx = getOfflineContext(checkSilentBuffer);
141 f(ctx);
142 ctx.startRendering();
143 }
144
145 function expectNoisyOutput(f) {
146 remainingTests++;
147 var ctx = getOfflineContext(checkNoisyBuffer);
148 f(ctx);
149 ctx.startRendering();
150 }
151
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 });
166
167 </script>
168 </pre>
169 </body>
170 </html>

mercurial