Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // For the current implementation of JavaScriptAudioNode, when it works with Off lineAudioContext (which runs much faster | |
|
hongchan
2017/02/27 18:16:12
This file is newly created. Perhaps apply clang-fo
Raymond Toy
2017/02/27 19:07:34
Done, and also changed var to let.
| |
| 2 // than real-time) the event.inputBuffer might be overwrite again before onaudio process 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, should) { | |
| 17 buffer = event.outputBuffer; | |
| 18 | |
| 19 should(buffer.numberOfChannels, | |
| 20 "Number of channels in output buffer") | |
| 21 .beEqualTo(outputChannels); | |
| 22 should(buffer.length, | |
| 23 "Length of output buffer") | |
| 24 .beEqualTo(bufferSize); | |
| 25 | |
| 26 buffer = event.inputBuffer; | |
| 27 | |
| 28 var success = checkStereoOnlyData(buffer, inputChannels, buffer.length); | |
| 29 | |
| 30 should(success, "onaudioprocess was called with the correct input data") | |
| 31 .beTrue(); | |
| 32 } | |
| 33 | |
| 34 function fillData(buffer, numberOfChannels, length) { | |
| 35 for (var i = 0; i < numberOfChannels; ++i) { | |
| 36 var data = buffer.getChannelData(i); | |
| 37 | |
| 38 for (var j = 0; j < length; ++j) | |
| 39 if (i < 2) | |
| 40 data[j] = i * 2 - 1; | |
| 41 else | |
| 42 data[j] = 0; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 // Both 2 to 8 upmix and 8 to 2 downmix are just directly copy the first two cha nnels and left channels are zeroed. | |
| 47 function checkStereoOnlyData(buffer, numberOfChannels, length) { | |
| 48 for (var i = 0; i < numberOfChannels; ++i) { | |
| 49 var data = buffer.getChannelData(i); | |
| 50 | |
| 51 for (var 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 { | |
| 66 // Create offline audio context. | |
| 67 context = new OfflineAudioContext(2, renderLengthInFrames, sampleRate); | |
| 68 | |
| 69 var sourceBuffer = createBuffer(context, sourceChannels, renderLengthInFrame s); | |
| 70 | |
| 71 var bufferSource = context.createBufferSource(); | |
| 72 bufferSource.buffer = sourceBuffer; | |
| 73 | |
| 74 var scriptNode = context.createScriptProcessor(bufferSize, inputChannels, ou tputChannels); | |
| 75 | |
| 76 bufferSource.connect(scriptNode); | |
| 77 scriptNode.connect(context.destination); | |
| 78 scriptNode.onaudioprocess = event => { | |
| 79 processAudioData(event, should); | |
| 80 }; | |
| 81 | |
| 82 bufferSource.start(0); | |
| 83 return context.startRendering(); | |
| 84 } | |
| OLD | NEW |