| OLD | NEW |
| 1 // Notes about generated waveforms: | 1 // Notes about generated waveforms: |
| 2 // | 2 // |
| 3 // QUESTION: Why does the wave shape not look like the exact shape (sharp edges)
? | 3 // QUESTION: Why does the wave shape not look like the exact shape (sharp edges)
? |
| 4 // ANSWER: Because a shape with sharp edges has infinitely high frequency conten
t. | 4 // ANSWER: Because a shape with sharp edges has infinitely high frequency conten
t. |
| 5 // Since a digital audio signal must be band-limited based on the nyquist freque
ncy (half the sample-rate) | 5 // Since a digital audio signal must be band-limited based on the nyquist freque
ncy (half the sample-rate) |
| 6 // in order to avoid aliasing, this creates more rounded edges and "ringing" in
the | 6 // in order to avoid aliasing, this creates more rounded edges and "ringing" in
the |
| 7 // appearance of the waveform. See Nyquist-Shannon sampling theorem: | 7 // appearance of the waveform. See Nyquist-Shannon sampling theorem: |
| 8 // http://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem | 8 // http://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem |
| 9 // | 9 // |
| 10 // QUESTION: Why does the very end of the generated signal appear to get slightl
y weaker? | 10 // QUESTION: Why does the very end of the generated signal appear to get slightl
y weaker? |
| 11 // ANSWER: This is an artifact of the algorithm to avoid aliasing. | 11 // ANSWER: This is an artifact of the algorithm to avoid aliasing. |
| 12 // | 12 // |
| 13 // QUESTION: Since the tests compare the actual result with an expected referenc
e file, how are the | 13 // QUESTION: Since the tests compare the actual result with an expected referenc
e file, how are the |
| 14 // reference files created? | 14 // reference files created? |
| 15 // ANSWER: Create an html with the following contents in the webaudio directory.
Then run a layout | 15 // ANSWER: Run the test in a browser. When the test completes, a |
| 16 // test on this file. A new file names "<file>-actual.wav" is created that cont
ains the new result | 16 // generated reference file with the name "<file>-actual.wav" is |
| 17 // that can be used as the new expected reference file. Replace the "sine" belo
w with the | 17 // automatically downloaded. Use this as the new reference, after |
| 18 // oscillator type that you want to use. | 18 // carefully inspecting to see if this is correct. |
| 19 // | 19 // |
| 20 // <!doctype html> | |
| 21 // <html> | |
| 22 // <head> | |
| 23 // <script src="../resources/testharness.js"></script> | |
| 24 // <script src="resources/oscillator-testing.js"></script> | |
| 25 // <script src="resources/audit-util.js"></script> | |
| 26 // </head> | |
| 27 // <body> | |
| 28 // <script> | |
| 29 // OscillatorTestingUtils.createNewReference("sine"); | |
| 30 // </script> | |
| 31 // </body> | |
| 32 // </html> | |
| 33 | 20 |
| 34 OscillatorTestingUtils = (function () { | 21 OscillatorTestingUtils = (function () { |
| 35 | 22 |
| 36 var sampleRate = 44100.0; | 23 var sampleRate = 44100.0; |
| 37 var nyquist = 0.5 * sampleRate; | 24 var nyquist = 0.5 * sampleRate; |
| 38 var lengthInSeconds = 4; | 25 var lengthInSeconds = 4; |
| 39 var lowFrequency = 10; | 26 var lowFrequency = 10; |
| 40 var highFrequency = nyquist + 2000; // go slightly higher than nyquist to make s
ure we generate silence there | 27 var highFrequency = nyquist + 2000; // go slightly higher than nyquist to make s
ure we generate silence there |
| 41 var context = 0; | 28 var context = 0; |
| 42 | 29 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 92 } |
| 106 | 93 |
| 107 function loadReferenceAndRunTest(context, oscType, task, should) { | 94 function loadReferenceAndRunTest(context, oscType, task, should) { |
| 108 var bufferLoader = new BufferLoader( | 95 var bufferLoader = new BufferLoader( |
| 109 context, | 96 context, |
| 110 [ "../Oscillator/oscillator-" + oscType + "-expected.wav" ], | 97 [ "../Oscillator/oscillator-" + oscType + "-expected.wav" ], |
| 111 function (bufferList) { | 98 function (bufferList) { |
| 112 reference = bufferList[0].getChannelData(0); | 99 reference = bufferList[0].getChannelData(0); |
| 113 generateExponentialOscillatorSweep(context, oscType); | 100 generateExponentialOscillatorSweep(context, oscType); |
| 114 context.oncomplete = () => { | 101 context.oncomplete = () => { |
| 115 checkResult(event, should); | 102 checkResult(event, should, oscType); |
| 116 task.done(); | 103 task.done(); |
| 117 }; | 104 }; |
| 118 context.startRendering(); | 105 context.startRendering(); |
| 119 }); | 106 }); |
| 120 | 107 |
| 121 bufferLoader.load(); | 108 bufferLoader.load(); |
| 122 } | 109 } |
| 123 | 110 |
| 124 function checkResult (event, should) { | 111 function checkResult (event, should, oscType) { |
| 125 let renderedData = event.renderedBuffer.getChannelData(0); | 112 let renderedData = event.renderedBuffer.getChannelData(0); |
| 126 // Compute signal to noise ratio between the result and the reference. Also
keep track | 113 // Compute signal to noise ratio between the result and the reference. Also
keep track |
| 127 // of the max difference (and position). | 114 // of the max difference (and position). |
| 128 | 115 |
| 129 var maxError = -1; | 116 var maxError = -1; |
| 130 var errorPosition = -1; | 117 var errorPosition = -1; |
| 131 var diffCount = 0; | 118 var diffCount = 0; |
| 132 | 119 |
| 133 for (var k = 0; k < renderedData.length; ++k) { | 120 for (var k = 0; k < renderedData.length; ++k) { |
| 134 var diff = renderedData[k] - reference[k]; | 121 var diff = renderedData[k] - reference[k]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 var snr = calculateSNR(signalPower, noisePower); | 135 var snr = calculateSNR(signalPower, noisePower); |
| 149 should(snr, "SNR") | 136 should(snr, "SNR") |
| 150 .beGreaterThanOrEqualTo(thresholdSNR); | 137 .beGreaterThanOrEqualTo(thresholdSNR); |
| 151 should(maxError * waveScaleFactor, "Maximum difference in ulp (16-bits)") | 138 should(maxError * waveScaleFactor, "Maximum difference in ulp (16-bits)") |
| 152 .beLessThanOrEqualTo(thresholdDiff * waveScaleFactor); | 139 .beLessThanOrEqualTo(thresholdDiff * waveScaleFactor); |
| 153 | 140 |
| 154 should(diffCount, | 141 should(diffCount, |
| 155 "Number of differences between actual and expected result out of " | 142 "Number of differences between actual and expected result out of " |
| 156 + renderedData.length + " frames") | 143 + renderedData.length + " frames") |
| 157 .beLessThanOrEqualTo(thresholdDiffCount); | 144 .beLessThanOrEqualTo(thresholdDiffCount); |
| 145 |
| 146 var filename = "oscillator-" + oscType + "-actual.wav"; |
| 147 if (downloadAudioBuffer(event.renderedBuffer, filename)) |
| 148 should(true, "Saved reference file").message(filename, ""); |
| 158 } | 149 } |
| 159 | 150 |
| 160 function setThresholds(thresholds) { | 151 function setThresholds(thresholds) { |
| 161 thresholdSNR = thresholds.snr; | 152 thresholdSNR = thresholds.snr; |
| 162 thresholdDiff = thresholds.maxDiff / waveScaleFactor; | 153 thresholdDiff = thresholds.maxDiff / waveScaleFactor; |
| 163 thresholdDiffCount = thresholds.diffCount; | 154 thresholdDiffCount = thresholds.diffCount; |
| 164 } | 155 } |
| 165 | 156 |
| 166 function runTest(context, oscType, description, task, should) { | 157 function runTest(context, oscType, description, task, should) { |
| 167 loadReferenceAndRunTest(context, oscType, task, should); | 158 loadReferenceAndRunTest(context, oscType, task, should); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 186 thresholdSNR: thresholdSNR, | 177 thresholdSNR: thresholdSNR, |
| 187 thresholdDiff: thresholdDiff, | 178 thresholdDiff: thresholdDiff, |
| 188 thresholdDiffCount: thresholdDiffCount, | 179 thresholdDiffCount: thresholdDiffCount, |
| 189 waveScaleFactor: waveScaleFactor, | 180 waveScaleFactor: waveScaleFactor, |
| 190 setThresholds: setThresholds, | 181 setThresholds: setThresholds, |
| 191 runTest: runTest, | 182 runTest: runTest, |
| 192 createNewReference: createNewReference, | 183 createNewReference: createNewReference, |
| 193 }; | 184 }; |
| 194 | 185 |
| 195 }()); | 186 }()); |
| OLD | NEW |