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;
9 function createBuffer(context
, numberOfChannels
, length
) {
10 var audioBuffer
= context
.createBuffer(numberOfChannels
, length
, sampleRate
);
12 fillData(audioBuffer
, numberOfChannels
, audioBuffer
.length
);
16 function processAudioData(event
) {
17 buffer
= event
.outputBuffer
;
18 if (buffer
.numberOfChannels
!= outputChannels
)
19 testFailed("numberOfOutputChannels doesn't match!");
21 if (buffer
.length
!= bufferSize
)
22 testFailed("length of buffer doesn't match!");
24 buffer
= event
.inputBuffer
;
26 var success
= checkStereoOnlyData(buffer
, inputChannels
, buffer
.length
);
29 testPassed("onaudioprocess was called with correct input data.");
31 testFailed("onaudioprocess was called with wrong input data.");
35 function fillData(buffer
, numberOfChannels
, length
) {
36 for (var i
= 0; i
< numberOfChannels
; ++i
) {
37 var data
= buffer
.getChannelData(i
);
39 for (var j
= 0; j
< length
; ++j
)
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
);
52 for (var j
= 0; j
< length
; ++j
) {
54 if (data
[j
] != i
* 2 - 1)
65 function runJSNodeTest()
67 // Create offline audio context.
68 context
= new OfflineAudioContext(2, renderLengthInFrames
, sampleRate
);
70 var sourceBuffer
= createBuffer(context
, sourceChannels
, renderLengthInFrames
);
72 var bufferSource
= context
.createBufferSource();
73 bufferSource
.buffer
= sourceBuffer
;
75 var scriptNode
= context
.createScriptProcessor(bufferSize
, inputChannels
, outputChannels
);
77 bufferSource
.connect(scriptNode
);
78 scriptNode
.connect(context
.destination
);
79 scriptNode
.onaudioprocess
= processAudioData
;
81 bufferSource
.start(0);
82 context
.oncomplete
= finishJSTest
;
83 context
.startRendering();