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
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>Test tail time lifetime of DelayNode after input is disconnected</title>
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6 <script type="text/javascript" src="webaudio.js"></script>
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
8 </head>
9 <body>
10 <pre id="test">
11 <script class="testbody" type="text/javascript">
13 // Web Audio doesn't provide a means to precisely time disconnect()s but we
14 // can test that the output of delay nodes matches the output from their
15 // sources before they are disconnected.
17 SimpleTest.waitForExplicitFinish();
19 const signalLength = 128;
20 const bufferSize = 4096;
21 const sourceCount = bufferSize / signalLength;
22 // Delay should be long enough to allow CC to run
23 var delayBufferCount = 20;
24 const delayLength = delayBufferCount * bufferSize;
26 var sourceOutput = new Float32Array(bufferSize);
27 var delayOutputCount = 0;
28 var sources = [];
30 function onDelayOutput(e) {
31 if (delayOutputCount < delayBufferCount) {
32 delayOutputCount++;
33 return;
34 }
36 compareChannels(e.inputBuffer.getChannelData(0), sourceOutput);
37 e.target.onaudioprocess = null;
38 SimpleTest.finish();
39 }
41 function onSourceOutput(e) {
42 // Record the first buffer
43 e.inputBuffer.copyFromChannel(sourceOutput, 0);
44 e.target.onaudioprocess = null;
45 }
47 function disconnectSources() {
48 for (var i = 0; i < sourceCount; ++i) {
49 sources[i].disconnect();
50 }
52 SpecialPowers.forceGC();
53 SpecialPowers.forceCC();
54 }
56 function startTest() {
57 var ctx = new AudioContext();
59 var sourceProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
60 sourceProcessor.onaudioprocess = onSourceOutput;
61 // Keep audioprocess events going after source disconnect.
62 sourceProcessor.connect(ctx.destination);
64 var delayProcessor = ctx.createScriptProcessor(bufferSize, 1, 0);
65 delayProcessor.onaudioprocess = onDelayOutput;
66 // Work around bug 916387.
67 delayProcessor.connect(ctx.destination);
69 var delayDuration = delayLength / ctx.sampleRate;
70 for (var i = 0; i < sourceCount; ++i) {
71 var delay = ctx.createDelay(delayDuration);
72 delay.delayTime.value = delayDuration;
73 delay.connect(delayProcessor);
75 var source = ctx.createOscillator();
76 source.frequency.value = 440 + 10 * i
77 source.start(i * signalLength / ctx.sampleRate);
78 source.stop((i + 1) * signalLength / ctx.sampleRate);
79 source.connect(delay);
80 source.connect(sourceProcessor);
82 sources[i] = source;
83 }
85 // Assuming the above Web Audio operations have already scheduled an event
86 // to run in stable state and start the graph thread, schedule a subsequent
87 // event to disconnect the sources, which will remove main thread connection
88 // references before it knows the graph thread has started using the source
89 // streams.
90 SimpleTest.executeSoon(disconnectSources);
91 };
93 startTest();
94 </script>
95 </pre>
96 </body>
97 </html>