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