- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / resources / javascriptaudionode-testing.js
1 // For the current implementation of JavaScriptAudioNode, when it works with OfflineAudioContext (which runs much faster
2 // than real-time) the event.inputBuffer might be overwrite again before onaudioprocess ever get chance to be called.
3 // We carefully arrange the renderLengthInFrames and bufferSize to have exactly the same value to avoid this issue.
4 var renderLengthInFrames = 512;
5 var bufferSize = 512;
6
7 var context;
8
9 function createBuffer(context, numberOfChannels, length) {
10     var audioBuffer = context.createBuffer(numberOfChannels, length, sampleRate);
11
12     fillData(audioBuffer, numberOfChannels, audioBuffer.length);
13     return audioBuffer;
14 }
15
16 function processAudioData(event) {
17     buffer = event.outputBuffer;
18     if (buffer.numberOfChannels != outputChannels)
19         testFailed("numberOfOutputChannels doesn't match!");
20
21     if (buffer.length != bufferSize)
22         testFailed("length of buffer doesn't match!");
23
24     buffer = event.inputBuffer;
25     
26     var success = checkStereoOnlyData(buffer, inputChannels, buffer.length);
27
28     if (success) {
29         testPassed("onaudioprocess was called with correct input data.");
30     } else {
31         testFailed("onaudioprocess was called with wrong input data.");
32     }
33 }
34
35 function fillData(buffer, numberOfChannels, length) {
36     for (var i = 0; i < numberOfChannels; ++i) {
37         var data = buffer.getChannelData(i);
38
39         for (var j = 0; j < length; ++j)
40             if (i < 2)
41                 data[j] = i * 2 - 1;
42             else
43                 data[j] = 0;
44     }
45 }
46
47 // Both 2 to 8 upmix and 8 to 2 downmix are just directly copy the first two channels and left channels are zeroed.
48 function checkStereoOnlyData(buffer, numberOfChannels, length) {
49     for (var i = 0; i < numberOfChannels; ++i) {
50         var data = buffer.getChannelData(i);
51
52         for (var j = 0; j < length; ++j) {
53             if (i < 2) {
54                 if (data[j] != i * 2 - 1)
55                     return false;
56             } else {
57                 if (data[j] != 0)
58                     return false;
59             }
60         }
61     }
62     return true;
63 }
64
65 function runJSNodeTest()
66 {
67     // Create offline audio context.
68     context = new webkitOfflineAudioContext(2, renderLengthInFrames, sampleRate);
69
70     var sourceBuffer = createBuffer(context, sourceChannels, renderLengthInFrames);
71
72     var bufferSource = context.createBufferSource();
73     bufferSource.buffer = sourceBuffer;
74
75     var jsnode = context.createJavaScriptNode(bufferSize, inputChannels, outputChannels);
76
77     bufferSource.connect(jsnode);
78     jsnode.connect(context.destination);
79     jsnode.onaudioprocess = processAudioData;
80
81     bufferSource.noteOn(0);
82     context.oncomplete = finishJSTest;
83     context.startRendering();
84 }