Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / webaudio / periodicwave-normalization.html
blob7ed2702a0079d85117663cf24592af7e8d850f37
1 <!doctype html>
2 <html>
3 <head>
4 <title>Test PeriodicWave Normalization</title>
5 <script src="../resources/js-test.js"></script>
6 <script src="resources/compatibility.js"></script>
7 <script src="resources/audio-testing.js"></script>
8 </head>
10 <body>
11 <script>
12 description("Test PeriodicWave Normalization");
13 window.jsTestIsAsync = true;
15 var sampleRate = 48000;
16 var renderFrames = sampleRate;
17 var context;
18 var audit = Audit.createTaskRunner();
20 var testSet = [
21 // Test the default case where the two-arg createPeriodicWave is called. The waveform
22 // should be normalized.
24 name: "option-default/normalized",
25 // This option is treated specially; it means that the two-arg function is called.
26 option: "NONE",
27 // Somewhat arbitrary except that resulting waveform should have amplitude greater than
28 // 1. And we don't want a simple sine wave because that always has max amplitude of 1.
29 realCoef: [0, -1, 1],
30 threshold: 1e-5,
31 expectedMax: 1
35 name: "option-null/normalized",
36 option: null,
37 // Somewhat arbitrary except that resulting waveform should have amplitude greater than
38 // 1. And we don't want a simple sine wave because that always has max amplitude of 1.
39 realCoef: [0, -1, 1],
40 threshold: 1e-5,
41 expectedMax: 1
44 // Explicitly set to false. Waveform should be normalized.
46 name: "false/normalized",
47 option: {
48 disableNormalization: false
50 realCoef: [0, -1, 1],
51 threshold: 1e-5,
52 expectedMax: 1
55 // Explicitly disable normalization. Waveform is not normalized.
57 name: "true/unnormalized",
58 option: {
59 disableNormalization: true
61 // Somewhat arbitrary except that resulting waveform should have amplitude very clearly
62 // greater than 1.
63 realCoef: [0, 10],
64 threshold: 1e-5,
65 expectedMax: 10
68 // Explicitly set to some value that is not false. Waveform should NOT be normalized.
70 name: "foo/unnormalized",
71 option: {
72 disableNormalization: "foo"
74 realCoef: [0, 10],
75 threshold: 1e-5,
76 expectedMax: 10
79 // Explicitly set to null, which is the same as false. Waveform is normalized.
81 name: "null/unnormalized",
82 option: {
83 disableNormalization: null
85 realCoef: [0, 1, -1],
86 threshold: 1e-5,
87 expectedMax: 1
90 // Pass in a random dictionary not using our key. Waveform should be normalized.
92 name: "random-key-value/normalized",
93 option: {
94 randomKey: true
96 realCoef: [0, -1, 1],
97 threshold: 1e-5,
98 expectedMax: 1
101 // Set options to several random keys. Waveform must be normalized.
103 name: "more-random-keys/normalized",
104 option: {
105 key1: "value1",
106 key2: 42
108 realCoef: [0, 1, -1],
109 threshold: 1e-5,
110 expectedMax: 1
113 // Set option to include our key (set to true) amongst a bunch of others. Waveform is NOT normalized.
115 name: "true-with-random-keys/unnormalized",
116 option: {
117 key1: "value1",
118 disableNormalization: true
120 realCoef: [0, 10],
121 threshold: 1e-5,
122 expectedMax: 10
125 // Set option to include our key (set to false) amongst a bunch of others. Waveform is normalized.
127 name: "false-with-random-keys/normalized",
128 option: {
129 key1: "value1",
130 disableNormalization: false
132 realCoef: [0, 10],
133 threshold: 1e-5,
134 expectedMax: 1
137 // Set option to a non-dictionary. Waveform is normalized.
139 name: "non-dict/normalized",
140 option : [1, 2, 3],
141 realCoef: [0, 1, -1],
142 threshold: 1e-5,
143 expectedMax: 1
147 function arrayMax(array) {
148 return array.reduce(function (reducedValue, currentValue) {
149 return Math.max(reducedValue, Math.abs(currentValue));
150 }, -1);
153 function createAndRunAudioGraph(options, realCoef, imagCoef, verify) {
154 context = new OfflineAudioContext(1, renderFrames, sampleRate);
155 var osc = context.createOscillator();
157 var r = new Float32Array(realCoef);
158 var i = new Float32Array(imagCoef);
160 var wave;
162 // If options is "NONE", we want to be sure to call the two-arg createPeriodicWave to make
163 // sure the default method works as expected.
164 if (options === "NONE") {
165 //console.log("2-arg: " + options);
166 wave = context.createPeriodicWave(r, i);
167 } else {
168 //console.log("3-arg: " + options);
169 wave = context.createPeriodicWave(r, i, options);
172 osc.setPeriodicWave(wave);
173 osc.connect(context.destination);
174 osc.start();
176 return context.startRendering().then(verify);
179 // Define a test function from the given test parameter. This is used as the Audit.defineTask
180 // task function.
181 function defineTest(test) {
182 return function (done) {
183 var imagCoef = new Float32Array(test.realCoef.length);
184 createAndRunAudioGraph(test.option, test.realCoef, imagCoef, function (result) {
185 var prefix;
187 // Try to print out the test.option in a reasonably nice but explicit way.
188 if (test.option === "NONE") {
189 prefix = "";
190 } else if (Array.isArray(test.option)) {
191 prefix = "[" + test.option + "]: ";
192 } else {
193 prefix = JSON.stringify(test.option) + ": ";
196 Should(prefix + "amplitude", arrayMax(result.getChannelData(0)))
197 .beCloseTo(test.expectedMax, test.threshold);
198 }).then(done);
202 // Ensure the actual Audit test name is unique by prepending an index to the provided test
203 // name.
204 function actualTestName(name, index) {
205 return index + ":" + name;
208 function defineTasks() {
209 for (var k = 0; k < testSet.length; ++k) {
210 var test = testSet[k];
211 audit.defineTask(actualTestName(test.name, k), defineTest(test));
214 // Explicitly define the last test to finish up everything.
215 audit.defineTask(actualTestName("finish-tests", testSet.length), function (done) {
216 finishJSTest();
217 done();
221 // Run all of the tests defined in testSet.
222 function runAuditTests() {
223 var tests = testSet.map(function (value, index) {
224 return actualTestName(value.name, index);
225 }).concat(actualTestName("finish-tests", testSet.length));
226 audit.runTasks.apply(audit, tests);
229 defineTasks();
230 runAuditTests();
232 successfullyParsed = true;
233 </script>
234 </body>
235 </html>