| OLD | NEW |
| (Empty) | |
| 1 // For the current implementation of JavaScriptAudioNode, when it works with |
| 2 // OfflineAudioContext (which runs much faster than real-time) the |
| 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. |
| 6 let renderLengthInFrames = 512; |
| 7 let bufferSize = 512; |
| 8 |
| 9 let context; |
| 10 |
| 11 function createBuffer(context, numberOfChannels, length) { |
| 12 let audioBuffer = context.createBuffer(numberOfChannels, length, sampleRate); |
| 13 |
| 14 fillData(audioBuffer, numberOfChannels, audioBuffer.length); |
| 15 return audioBuffer; |
| 16 } |
| 17 |
| 18 function processAudioData(event, should) { |
| 19 buffer = event.outputBuffer; |
| 20 |
| 21 should(buffer.numberOfChannels, 'Number of channels in output buffer') |
| 22 .beEqualTo(outputChannels); |
| 23 should(buffer.length, 'Length of output buffer').beEqualTo(bufferSize); |
| 24 |
| 25 buffer = event.inputBuffer; |
| 26 |
| 27 let success = checkStereoOnlyData(buffer, inputChannels, buffer.length); |
| 28 |
| 29 should(success, 'onaudioprocess was called with the correct input data') |
| 30 .beTrue(); |
| 31 } |
| 32 |
| 33 function fillData(buffer, numberOfChannels, length) { |
| 34 for (let i = 0; i < numberOfChannels; ++i) { |
| 35 let data = buffer.getChannelData(i); |
| 36 |
| 37 for (let j = 0; j < length; ++j) |
| 38 if (i < 2) |
| 39 data[j] = i * 2 - 1; |
| 40 else |
| 41 data[j] = 0; |
| 42 } |
| 43 } |
| 44 |
| 45 // Both 2 to 8 upmix and 8 to 2 downmix are just directly copy the first two |
| 46 // channels and left channels are zeroed. |
| 47 function checkStereoOnlyData(buffer, numberOfChannels, length) { |
| 48 for (let i = 0; i < numberOfChannels; ++i) { |
| 49 let data = buffer.getChannelData(i); |
| 50 |
| 51 for (let j = 0; j < length; ++j) { |
| 52 if (i < 2) { |
| 53 if (data[j] != i * 2 - 1) |
| 54 return false; |
| 55 } else { |
| 56 if (data[j] != 0) |
| 57 return false; |
| 58 } |
| 59 } |
| 60 } |
| 61 return true; |
| 62 } |
| 63 |
| 64 function runJSNodeTest(should) { |
| 65 // Create offline audio context. |
| 66 context = new OfflineAudioContext(2, renderLengthInFrames, sampleRate); |
| 67 |
| 68 let sourceBuffer = |
| 69 createBuffer(context, sourceChannels, renderLengthInFrames); |
| 70 |
| 71 let bufferSource = context.createBufferSource(); |
| 72 bufferSource.buffer = sourceBuffer; |
| 73 |
| 74 let scriptNode = |
| 75 context.createScriptProcessor(bufferSize, inputChannels, outputChannels); |
| 76 |
| 77 bufferSource.connect(scriptNode); |
| 78 scriptNode.connect(context.destination); |
| 79 scriptNode.onaudioprocess = event => { |
| 80 processAudioData(event, should); |
| 81 }; |
| 82 |
| 83 bufferSource.start(0); |
| 84 return context.startRendering(); |
| 85 } |
| OLD | NEW |