| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 | 2 |
| 3 <html> | 3 <html> |
| 4 <head> | 4 <head> |
| 5 <script src="../../resources/js-test.js"></script> | 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharnessreport.js"></script> |
| 6 <script src="../resources/audit-util.js"></script> | 7 <script src="../resources/audit-util.js"></script> |
| 7 <script src="../resources/audio-testing.js"></script> | 8 <script src="../resources/audit.js"></script> |
| 8 <script type="text/javascript" src="../resources/buffer-loader.js"></script> | 9 <script type="text/javascript" src="../resources/buffer-loader.js"></script> |
| 9 </head> | 10 </head> |
| 10 | 11 |
| 11 <body> | 12 <body> |
| 12 | |
| 13 <div id="description"></div> | |
| 14 <div id="console"></div> | |
| 15 | |
| 16 <script> | 13 <script> |
| 17 description("Tests that WaveShaperNode applies proper non-linear distortion."); | 14 var audit = Audit.createTaskRunner(); |
| 18 | 15 |
| 19 var sampleRate = 44100; | 16 var sampleRate = 44100; |
| 20 var lengthInSeconds = 4; | 17 var lengthInSeconds = 4; |
| 21 var numberOfRenderFrames = sampleRate * lengthInSeconds; | 18 var numberOfRenderFrames = sampleRate * lengthInSeconds; |
| 22 var numberOfCurveFrames = 65536; | 19 var numberOfCurveFrames = 65536; |
| 23 var inputBuffer; | 20 var inputBuffer; |
| 24 var waveShapingCurve; | 21 var waveShapingCurve; |
| 25 | 22 |
| 26 var context; | 23 var context; |
| 27 | 24 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 53 var n2 = n / 2; | 50 var n2 = n / 2; |
| 54 | 51 |
| 55 for (var i = 0; i < n; ++i) { | 52 for (var i = 0; i < n; ++i) { |
| 56 var x = (i - n2) / n2; | 53 var x = (i - n2) / n2; |
| 57 var y = Math.atan(5 * x) / (0.5 * Math.PI); | 54 var y = Math.atan(5 * x) / (0.5 * Math.PI); |
| 58 } | 55 } |
| 59 | 56 |
| 60 return curve; | 57 return curve; |
| 61 } | 58 } |
| 62 | 59 |
| 63 function checkShapedCurve(event) { | 60 function checkShapedCurve(buffer, should) { |
| 64 var buffer = event.renderedBuffer; | |
| 65 | |
| 66 var inputData = inputBuffer.getChannelData(0); | 61 var inputData = inputBuffer.getChannelData(0); |
| 67 var outputData = buffer.getChannelData(0); | 62 var outputData = buffer.getChannelData(0); |
| 68 | 63 |
| 69 var success = true; | 64 var success = true; |
| 70 | 65 |
| 71 // Go through every sample and make sure it has been shaped exactly accordin
g to the shaping curve we gave it. | 66 // Go through every sample and make sure it has been shaped exactly accordin
g to the shaping curve we gave it. |
| 72 for (var i = 0; i < buffer.length; ++i) { | 67 for (var i = 0; i < buffer.length; ++i) { |
| 73 var input = inputData[i]; | 68 var input = inputData[i]; |
| 74 | 69 |
| 75 // Calculate an index based on input -1 -> +1 with 0 being at the center
of the curve data. | 70 // Calculate an index based on input -1 -> +1 with 0 being at the center
of the curve data. |
| 76 var index = Math.floor(numberOfCurveFrames * 0.5 * (input + 1)); | 71 var index = Math.floor(numberOfCurveFrames * 0.5 * (input + 1)); |
| 77 | 72 |
| 78 // Clip index to the input range of the curve. | 73 // Clip index to the input range of the curve. |
| 79 // This takes care of input outside of nominal range -1 -> +1 | 74 // This takes care of input outside of nominal range -1 -> +1 |
| 80 index = index < 0 ? 0 : index; | 75 index = index < 0 ? 0 : index; |
| 81 index = index > numberOfCurveFrames - 1 ? numberOfCurveFrames - 1 : inde
x; | 76 index = index > numberOfCurveFrames - 1 ? numberOfCurveFrames - 1 : inde
x; |
| 82 | 77 |
| 83 var expectedOutput = waveShapingCurve[index]; | 78 var expectedOutput = waveShapingCurve[index]; |
| 84 | 79 |
| 85 var output = outputData[i]; | 80 var output = outputData[i]; |
| 86 | 81 |
| 87 if (output != expectedOutput) { | 82 if (output != expectedOutput) { |
| 88 success = false; | 83 success = false; |
| 89 break; | 84 break; |
| 90 } | 85 } |
| 91 } | 86 } |
| 92 | 87 |
| 93 if (success) { | 88 should(success, "WaveShaperNode applied non-linear distortion correctly") |
| 94 testPassed("WaveShaperNode properly applied non-linear distortion."); | 89 .beTrue(); |
| 95 } else { | |
| 96 testFailed("WaveShaperNode did not properly apply non-linear distortion.
"); | |
| 97 } | |
| 98 | |
| 99 finishJSTest(); | |
| 100 } | 90 } |
| 101 | 91 |
| 102 function runTest() { | 92 audit.define("test", function (task, should) { |
| 103 if (window.testRunner) { | |
| 104 testRunner.dumpAsText(); | |
| 105 testRunner.waitUntilDone(); | |
| 106 } | |
| 107 | |
| 108 window.jsTestIsAsync = true; | |
| 109 | |
| 110 // Create offline audio context. | 93 // Create offline audio context. |
| 111 context = new OfflineAudioContext(1, numberOfRenderFrames, sampleRate); | 94 context = new OfflineAudioContext(1, numberOfRenderFrames, sampleRate); |
| 112 | 95 |
| 113 // source -> waveshaper -> destination | 96 // source -> waveshaper -> destination |
| 114 var source = context.createBufferSource(); | 97 var source = context.createBufferSource(); |
| 115 var waveshaper = context.createWaveShaper(); | 98 var waveshaper = context.createWaveShaper(); |
| 116 source.connect(waveshaper); | 99 source.connect(waveshaper); |
| 117 waveshaper.connect(context.destination); | 100 waveshaper.connect(context.destination); |
| 118 | 101 |
| 119 // Create an input test vector. | 102 // Create an input test vector. |
| 120 inputBuffer = generateInputBuffer(); | 103 inputBuffer = generateInputBuffer(); |
| 121 source.buffer = inputBuffer; | 104 source.buffer = inputBuffer; |
| 122 | 105 |
| 123 // We'll apply non-linear distortion according to this shaping curve. | 106 // We'll apply non-linear distortion according to this shaping curve. |
| 124 waveShapingCurve = generateWaveShapingCurve(); | 107 waveShapingCurve = generateWaveShapingCurve(); |
| 125 waveshaper.curve = waveShapingCurve; | 108 waveshaper.curve = waveShapingCurve; |
| 126 | 109 |
| 127 source.start(0); | 110 source.start(0); |
| 128 | 111 |
| 129 context.oncomplete = checkShapedCurve; | 112 context.startRendering() |
| 130 context.startRendering(); | 113 .then(buffer => checkShapedCurve(buffer, should)) |
| 131 } | 114 .then(task.done.bind(task)); |
| 115 }); |
| 132 | 116 |
| 133 runTest(); | 117 audit.run(); |
| 134 | 118 |
| 135 </script> | 119 </script> |
| 136 | 120 |
| 137 </body> | 121 </body> |
| 138 </html> | 122 </html> |
| OLD | NEW |