5 <script src="../resources/js-test.js"></script>
6 <script type="text/javascript" src="resources/audio-testing.js"></script>
11 <div id="description"></div>
12 <div id="console"></div>
15 description("This test verifies that the AudioBasicInspectorNode based nodes work correctly.");
17 var sampleRate = 44100.0;
18 // We carefully arrange the renderLengthInFrames to be a multiple of the AudioNode rendering quantum (AudioNode::ProcessingSizeInFrames)
19 // so that AudioSourceNode will not feed tailing zeroes into the context and fail this test.
20 var renderLengthInFrames = 256;
23 var audioDataOne = 255; // Audio data 1.0 in Uint8 format will be 255.
24 var audioDataZero = 128; // Audio data 0 in Uint8 format will be 128.
31 function constructCommonGraph() {
32 // Create offline audio context.
33 context = new webkitOfflineAudioContext(1, renderLengthInFrames, sampleRate);
34 constantBuffer = createConstantBuffer(context, renderLengthInFrames, 1);
36 bufferSource = context.createBufferSource();
37 bufferSource.buffer = constantBuffer;
39 analyser = context.createAnalyser();
40 analyser.fftSize = fftSize;
42 bufferSource.connect(analyser);
45 function test1Finished() {
46 var timeDomainData = new Uint8Array(fftSize);
47 analyser.getByteTimeDomainData(timeDomainData);
49 if (timeDomainData[0] >= audioDataOne)
50 testPassed("RealtimeAnalyserNode got pulled when connected from upstream node but not to downstream node.");
52 testFailed("RealtimeAnalyserNode failed to get pulled when connected from upstream node but not to downstream node.");
57 // To verify the realtimeAnalyser can pull data when there is an upstream node connected to it
58 // but it's not connected to a downstream node.
60 constructCommonGraph();
62 bufferSource.start(0);
64 context.oncomplete = test1Finished;
65 context.startRendering();
68 function test2Finished() {
69 var timeDomainData = new Uint8Array(fftSize);
70 analyser.getByteTimeDomainData(timeDomainData);
72 if (timeDomainData[0] >= audioDataOne)
73 testPassed("RealtimeAnalyserNode got pulled when connected from upstream node and to destination node.");
75 testFailed("RealtimeAnalyserNode failed to be pulled when connected by upstream node and to destination node.");
80 // To verify the realtimeAnalyser can process normally when there is an upstream node connected to it
81 // and it's also connected to a downstream node which ultimately connect to audio destination.
83 constructCommonGraph();
85 analyser.connect(context.destination);
87 bufferSource.start(0);
89 context.oncomplete = test2Finished;
90 context.startRendering();
93 function test3Finished() {
94 var timeDomainData = new Uint8Array(fftSize);
95 analyser.getByteTimeDomainData(timeDomainData);
97 // If realtimeAnalyser hasn't pulled any data, the data in buffer will be 0.
98 if (timeDomainData[0] == audioDataZero)
99 testPassed("RealtimeAnalyserNode didn't get pulled when it should not.");
101 testFailed("RealtimeAnalyserNode been pulled when it should not.");
106 // To verify the realtimeAnalyser will stop pulling if it is connected to a downstream node
107 // which is not ultimatly connected to any audio destination.
109 constructCommonGraph();
111 var gain = context.createGain();
112 analyser.connect(gain);
114 bufferSource.start(0);
116 context.oncomplete = test3Finished;
117 context.startRendering();
121 if (window.testRunner) {
122 testRunner.dumpAsText();
123 testRunner.waitUntilDone();
126 window.jsTestIsAsync = true;