Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / scriptprocessornode.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../resources/js-test.js"></script>
5 <script src="resources/compatibility.js"></script>
6 <script type="text/javascript" src="resources/audio-testing.js"></script>
7 </head>
8
9 <body>
10
11 <div id="description"></div>
12 <div id="console"></div>
13
14 <script>
15 description("Tests ScriptProcessorNode.");
16
17 var sampleRate = 44100.0;
18 var outputChannels = 6;
19 var playbackTime = 0.0;
20
21 // For the current implementation of ScriptProcessorNode, when it works with OfflineAudioContext (which runs much faster
22 // than real-time) the event.inputBuffer might be overwrite again before onaudioprocess ever get chance to be called.
23 // We carefully arrange the renderLengthInFrames and bufferSize to have exactly the same value to avoid this issue.
24 var renderLengthInFrames = 512;
25 var bufferSize = 512;
26
27 var context;
28
29 function createBuffer(context, length) {
30     var audioBuffer = context.createBuffer(2, length, sampleRate);
31     var n = audioBuffer.length;
32     var dataL = audioBuffer.getChannelData(0);
33     var dataR = audioBuffer.getChannelData(1);
34
35     for (var i = 0; i < n; ++i) {
36         dataL[i] = -1;
37         dataR[i] = 1;
38     }
39
40     return audioBuffer;
41 }
42
43 function processAudioData(event) {
44     playbackTime = event.playbackTime;
45     var expectedTime = context.currentTime + (bufferSize / context.sampleRate);
46     var allowedTimeGap = 0.0000001;
47
48     // There may be a little time gap which is from different thread operation
49     // between currentTime when main thread fires onaudioprocess() and currenTime when read in JS
50     // since currentTime is continuously increasing on audio thread.
51     shouldBeCloseTo("playbackTime", expectedTime, allowedTimeGap, true);
52
53     buffer = event.outputBuffer;
54     if (buffer.numberOfChannels != outputChannels)
55         testFailed("numberOfOutputChannels doesn't match!");
56
57     if (buffer.length != bufferSize)
58         testFailed("numberOfOutputChannels doesn't match!");
59
60     buffer = event.inputBuffer;
61     var bufferDataL = buffer.getChannelData(0);
62     var bufferDataR = buffer.getChannelData(1);
63
64     var success = true;
65     // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
66     for (var i = 0; i < buffer.length; ++i) {
67         if (bufferDataL[i] != -1 || bufferDataR[i] != 1) {
68             success = false;
69             break;
70         }
71     }
72
73     if (success) {
74         testPassed("onaudioprocess was called with correct data.");
75     } else {
76         testFailed("onaudioprocess was called with wrong data.");
77     }
78 }
79
80 function doBufferSizeTest(size) {
81     try {
82         var jsnode = context.createScriptProcessor(size, 1, 1);
83         testPassed("Successfully created ScriptProcessorNode with bufferSize = " + size + ".");
84     } catch(e) {
85         testFailed("Failed to create ScriptProcessorNode with bufferSize = " + size + ".");
86     }
87 }
88
89 function runTest() {
90     if (window.testRunner) {
91         testRunner.dumpAsText();
92         testRunner.waitUntilDone();
93     }
94
95     window.jsTestIsAsync = true;
96
97     // Create offline audio context.
98     context = new OfflineAudioContext(2, renderLengthInFrames, sampleRate);
99
100     try {
101         var jsnode = context.createScriptProcessor(512, 0, 0);
102         testFailed("Exception should be thrown when both numberOfInputChannels and numberOfOutputChannels are zero.");
103     } catch(e) {
104         testPassed("Exception was thrown when both numberOfInputChannels and numberOfOutputChannels are zero.");
105     }
106     
107     try {
108         var jsnode = context.createScriptProcessor(512, 1, 0);
109         testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 1 and numberOfOutputChannels = 0.");
110     } catch(e) {
111         testFailed("Exception should not be thrown when numberOfInputChannels = 1 and numberOfOutputChannels = 0.");
112     }
113     
114     try {
115         var jsnode = context.createScriptProcessor(512, 2, 0);
116         testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 2 and numberOfOutputChannels = 0.");
117     } catch(e) {
118         testFailed("Exception should not be thrown when numberOfInputChannels = 2 and numberOfOutputChannels = 0.");
119     }
120     
121     try {
122         var jsnode = context.createScriptProcessor(512, 0, 1);
123         testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 1.");
124     } catch(e) {
125         testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 1.");
126     }
127     
128     try {
129         var jsnode = context.createScriptProcessor(512, 0, 2);
130         testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 2.");
131     } catch(e) {
132         testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 2.");
133     }
134     
135     try {
136         var jsnode = context.createScriptProcessor(511, 1, 1);
137         testFailed("Exception should be thrown for illegal bufferSize.");
138     } catch(e) {
139         testPassed("Exception was thrown for illegal bufferSize.");
140     }
141
142     doBufferSizeTest(256);
143     doBufferSizeTest(512);
144     doBufferSizeTest(1024);
145     doBufferSizeTest(2048);
146     doBufferSizeTest(4096);
147     doBufferSizeTest(8192);
148     doBufferSizeTest(16384);
149  
150     var sourceBuffer = createBuffer(context, renderLengthInFrames);
151
152     var bufferSource = context.createBufferSource();
153     bufferSource.buffer = sourceBuffer;
154
155     var jsnode = context.createScriptProcessor(bufferSize, 2, outputChannels);
156
157     bufferSource.connect(jsnode);
158     jsnode.connect(context.destination);
159     jsnode.onaudioprocess = processAudioData;
160
161     bufferSource.start(0);
162     context.oncomplete = finishJSTest;
163     context.startRendering();
164 }
165
166 runTest();
167
168 </script>
169
170 </body>
171 </html>