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>
11 <div id=
"description"></div>
12 <div id=
"console"></div>
15 description("Tests ScriptProcessorNode.");
17 var sampleRate
= 44100.0;
18 var outputChannels
= 6;
19 var playbackTime
= 0.0;
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;
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);
35 for (var i
= 0; i
< n
; ++i
) {
43 function processAudioData(event
) {
44 playbackTime
= event
.playbackTime
;
45 var expectedTime
= context
.currentTime
+ (bufferSize
/ context
.sampleRate
);
46 var allowedTimeGap
= 0.0000001;
48 // There may be a little time gap which is from different thread operation
49 // between currentTime when main thread fires onaudioprocess() and currentTime when read in JS
50 // since currentTime is continuously increasing on audio thread.
52 shouldBeCloseTo("playbackTime", expectedTime
, allowedTimeGap
, true);
54 buffer
= event
.outputBuffer
;
55 if (buffer
.numberOfChannels
!= outputChannels
)
56 testFailed("numberOfOutputChannels doesn't match!");
58 if (buffer
.length
!= bufferSize
)
59 testFailed("numberOfOutputChannels doesn't match!");
61 buffer
= event
.inputBuffer
;
62 var bufferDataL
= buffer
.getChannelData(0);
63 var bufferDataR
= buffer
.getChannelData(1);
66 // Go through every sample and make sure it's all -1 for the left-channel, and all +1 for the right-channel.
67 for (var i
= 0; i
< buffer
.length
; ++i
) {
68 if (bufferDataL
[i
] != -1 || bufferDataR
[i
] != 1) {
75 testPassed("onaudioprocess was called with correct data.");
77 testFailed("onaudioprocess was called with wrong data.");
81 function doBufferSizeTest(size
) {
83 var jsnode
= context
.createScriptProcessor(size
, 1, 1);
84 testPassed("Successfully created ScriptProcessorNode with bufferSize = " + size
+ ".");
86 testFailed("Failed to create ScriptProcessorNode with bufferSize = " + size
+ ".");
91 if (window
.testRunner
) {
92 testRunner
.dumpAsText();
93 testRunner
.waitUntilDone();
96 window
.jsTestIsAsync
= true;
98 // Create offline audio context.
99 context
= new OfflineAudioContext(2, renderLengthInFrames
, sampleRate
);
102 var jsnode
= context
.createScriptProcessor(512, 0, 0);
103 testFailed("Exception should be thrown when both numberOfInputChannels and numberOfOutputChannels are zero.");
105 testPassed("Exception was thrown when both numberOfInputChannels and numberOfOutputChannels are zero.");
109 var jsnode
= context
.createScriptProcessor(512, 1, 0);
110 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 1 and numberOfOutputChannels = 0.");
112 testFailed("Exception should not be thrown when numberOfInputChannels = 1 and numberOfOutputChannels = 0.");
116 var jsnode
= context
.createScriptProcessor(512, 2, 0);
117 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 2 and numberOfOutputChannels = 0.");
119 testFailed("Exception should not be thrown when numberOfInputChannels = 2 and numberOfOutputChannels = 0.");
123 var jsnode
= context
.createScriptProcessor(512, 0, 1);
124 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 1.");
126 testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 1.");
130 var jsnode
= context
.createScriptProcessor(512, 0, 2);
131 testPassed("Successfully created ScriptProcessorNode with numberOfInputChannels = 0 and numberOfOutputChannels = 2.");
133 testFailed("Exception should not be thrown when numberOfInputChannels = 0 and numberOfOutputChannels = 2.");
137 var jsnode
= context
.createScriptProcessor(511, 1, 1);
138 testFailed("Exception should be thrown for illegal bufferSize.");
140 testPassed("Exception was thrown for illegal bufferSize.");
143 doBufferSizeTest(256);
144 doBufferSizeTest(512);
145 doBufferSizeTest(1024);
146 doBufferSizeTest(2048);
147 doBufferSizeTest(4096);
148 doBufferSizeTest(8192);
149 doBufferSizeTest(16384);
151 var sourceBuffer
= createBuffer(context
, renderLengthInFrames
);
153 var bufferSource
= context
.createBufferSource();
154 bufferSource
.buffer
= sourceBuffer
;
156 var jsnode
= context
.createScriptProcessor(bufferSize
, 2, outputChannels
);
158 bufferSource
.connect(jsnode
);
159 jsnode
.connect(context
.destination
);
160 jsnode
.onaudioprocess
= processAudioData
;
162 bufferSource
.start(0);
163 context
.oncomplete
= finishJSTest
;
164 context
.startRendering();