Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Side by Side Diff: third_party/WebKit/LayoutTests/webaudio/resources/oscillator-testing.js

Issue 2737653007: Allow saving reference file as a float32 WAV file (Closed)
Patch Set: Address review comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/resources/audit-util.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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?
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 gainNode.connect(context.destination); 78 gainNode.connect(context.destination);
79 79
80 osc.start(0); 80 osc.start(0);
81 81
82 osc.frequency.setValueAtTime(10, 0); 82 osc.frequency.setValueAtTime(10, 0);
83 osc.frequency.exponentialRampToValueAtTime(highFrequency, lengthInSeconds); 83 osc.frequency.exponentialRampToValueAtTime(highFrequency, lengthInSeconds);
84 } 84 }
85 85
86 function calculateSNR(sPower, nPower) 86 function calculateSNR(sPower, nPower)
87 { 87 {
88 if (nPower == 0 && sPower > 0) {
89 return 1000;
90 }
91 return 10 * Math.log10(sPower / nPower); 88 return 10 * Math.log10(sPower / nPower);
92 } 89 }
93 90
94 function loadReferenceAndRunTest(context, oscType, task, should) { 91 function loadReferenceAndRunTest(context, oscType, task, should) {
95 var bufferLoader = new BufferLoader( 92 Audit
96 context, 93 .loadFileFromUrl(
97 [ "../Oscillator/oscillator-" + oscType + "-expected.wav" ], 94 '../Oscillator/oscillator-' + oscType + '-expected.wav')
98 function (bufferList) { 95 .then(response => {
99 reference = bufferList[0].getChannelData(0); 96 return context.decodeAudioData(response);
97 })
98 .then(audioBuffer => {
99 reference = audioBuffer.getChannelData(0);
100 generateExponentialOscillatorSweep(context, oscType); 100 generateExponentialOscillatorSweep(context, oscType);
101 context.oncomplete = () => { 101 return context.startRendering();
102 checkResult(event, should, oscType); 102 })
103 task.done(); 103 .then(resultBuffer => {
104 }; 104 checkResult(resultBuffer, should, oscType);
105 context.startRendering(); 105 })
106 }); 106 .then(() => task.done());
107
108 bufferLoader.load();
109 } 107 }
110 108
111 function checkResult (event, should, oscType) { 109 function checkResult (renderedBuffer, should, oscType) {
112 let renderedData = event.renderedBuffer.getChannelData(0); 110 let renderedData = renderedBuffer.getChannelData(0);
113 // Compute signal to noise ratio between the result and the reference. Also keep track 111 // Compute signal to noise ratio between the result and the reference. Also keep track
114 // of the max difference (and position). 112 // of the max difference (and position).
115 113
116 var maxError = -1; 114 var maxError = -1;
117 var errorPosition = -1; 115 var errorPosition = -1;
118 var diffCount = 0; 116 var diffCount = 0;
119 117
120 for (var k = 0; k < renderedData.length; ++k) { 118 for (var k = 0; k < renderedData.length; ++k) {
121 var diff = renderedData[k] - reference[k]; 119 var diff = renderedData[k] - reference[k];
122 noisePower += diff * diff; 120 noisePower += diff * diff;
123 signalPower += reference[k] * reference[k]; 121 signalPower += reference[k] * reference[k];
124 if (Math.abs(diff) > maxError) { 122 if (Math.abs(diff) > maxError) {
125 maxError = Math.abs(diff); 123 maxError = Math.abs(diff);
126 errorPosition = k; 124 errorPosition = k;
127 } 125 }
128 // The reference file is a 16-bit WAV file, so we will almost never get an exact match 126 // The reference file is a 16-bit WAV file, so we will almost never get an exact match
129 // between it and the actual floating-point result. 127 // between it and the actual floating-point result.
130 if (diff > 1/waveScaleFactor) { 128 if (diff > 0) {
131 diffCount++; 129 diffCount++;
132 } 130 }
133 } 131 }
134 132
135 var snr = calculateSNR(signalPower, noisePower); 133 var snr = calculateSNR(signalPower, noisePower);
136 should(snr, "SNR") 134 should(snr, "SNR")
137 .beGreaterThanOrEqualTo(thresholdSNR); 135 .beGreaterThanOrEqualTo(thresholdSNR);
138 should(maxError * waveScaleFactor, "Maximum difference in ulp (16-bits)") 136 should(maxError, "Maximum difference")
139 .beLessThanOrEqualTo(thresholdDiff * waveScaleFactor); 137 .beLessThanOrEqualTo(thresholdDiff);
140 138
141 should(diffCount, 139 should(diffCount,
142 "Number of differences between actual and expected result out of " 140 "Number of differences between actual and expected result out of "
143 + renderedData.length + " frames") 141 + renderedData.length + " frames")
144 .beLessThanOrEqualTo(thresholdDiffCount); 142 .beLessThanOrEqualTo(thresholdDiffCount);
145 143
146 var filename = "oscillator-" + oscType + "-actual.wav"; 144 var filename = "oscillator-" + oscType + "-actual.wav";
147 if (downloadAudioBuffer(event.renderedBuffer, filename)) 145 if (downloadAudioBuffer(renderedBuffer, filename, true))
148 should(true, "Saved reference file").message(filename, ""); 146 should(true, "Saved reference file").message(filename, "");
149 } 147 }
150 148
151 function setThresholds(thresholds) { 149 function setThresholds(thresholds) {
152 thresholdSNR = thresholds.snr; 150 thresholdSNR = thresholds.snr;
153 thresholdDiff = thresholds.maxDiff / waveScaleFactor; 151 thresholdDiff = thresholds.maxDiff;
154 thresholdDiffCount = thresholds.diffCount; 152 thresholdDiffCount = thresholds.diffCount;
155 } 153 }
156 154
157 function runTest(context, oscType, description, task, should) { 155 function runTest(context, oscType, description, task, should) {
158 loadReferenceAndRunTest(context, oscType, task, should); 156 loadReferenceAndRunTest(context, oscType, task, should);
159 } 157 }
160 158
161 function createNewReference(oscType) {
162 if (!window.testRunner)
163 return;
164
165 context = new OfflineAudioContext(1, sampleRate * lengthInSeconds, sampleRa te);
166 generateExponentialOscillatorSweep(context, oscType);
167
168 context.oncomplete = finishAudioTest;
169 context.startRendering();
170
171 testRunner.waitUntilDone();
172 }
173
174 return { 159 return {
175 sampleRate: sampleRate, 160 sampleRate: sampleRate,
176 lengthInSeconds: lengthInSeconds, 161 lengthInSeconds: lengthInSeconds,
177 thresholdSNR: thresholdSNR, 162 thresholdSNR: thresholdSNR,
178 thresholdDiff: thresholdDiff, 163 thresholdDiff: thresholdDiff,
179 thresholdDiffCount: thresholdDiffCount, 164 thresholdDiffCount: thresholdDiffCount,
180 waveScaleFactor: waveScaleFactor, 165 waveScaleFactor: waveScaleFactor,
181 setThresholds: setThresholds, 166 setThresholds: setThresholds,
182 runTest: runTest, 167 runTest: runTest,
183 createNewReference: createNewReference,
184 }; 168 };
185 169
186 }()); 170 }());
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/resources/audit-util.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698