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