OLD | NEW |
1 <!doctype html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <title>Test PeriodicWave Normalization</title> | 4 <title> |
| 5 Test PeriodicWave Normalization |
| 6 </title> |
5 <script src="../../resources/testharness.js"></script> | 7 <script src="../../resources/testharness.js"></script> |
6 <script src="../../resources/testharnessreport.js"></script> | 8 <script src="../../resources/testharnessreport.js"></script> |
7 <script src="../resources/audit-util.js"></script> | 9 <script src="../resources/audit-util.js"></script> |
8 <script src="../resources/audit.js"></script> | 10 <script src="../resources/audit.js"></script> |
9 </head> | 11 </head> |
| 12 <body> |
| 13 <script id="layout-test-code"> |
| 14 let sampleRate = 48000; |
| 15 let renderFrames = 6000; |
| 16 let context; |
| 17 let audit = Audit.createTaskRunner(); |
10 | 18 |
11 <body> | 19 let testSet = [ |
12 <script> | 20 // Test the default case where the two-arg createPeriodicWave is called. |
13 var sampleRate = 48000; | 21 // The waveform should be normalized. |
14 var renderFrames = 6000; | |
15 var context; | |
16 var audit = Audit.createTaskRunner(); | |
17 | |
18 var testSet = [ | |
19 // Test the default case where the two-arg createPeriodicWave is called.
The waveform | |
20 // should be normalized. | |
21 { | 22 { |
22 name: "option-default/normalized", | 23 name: 'option-default/normalized', |
23 // This option is treated specially; it means that the two-arg functio
n is called. | 24 // This option is treated specially; it means that the two-arg |
24 option: "NONE", | 25 // function is called. |
25 // Somewhat arbitrary except that resulting waveform should have ampli
tude greater than | 26 option: 'NONE', |
26 // 1. And we don't want a simple sine wave because that always has ma
x amplitude of 1. | 27 // Somewhat arbitrary except that resulting waveform should have |
| 28 // amplitude greater than 1. And we don't want a simple sine wave |
| 29 // because that always has max amplitude of 1. |
27 realCoef: [0, -1, 1], | 30 realCoef: [0, -1, 1], |
28 threshold: 1e-5, | 31 threshold: 1e-5, |
29 expectedMax: 1 | 32 expectedMax: 1 |
30 }, | 33 }, |
31 | 34 |
32 { | 35 { |
33 name: "option-null/normalized", | 36 name: 'option-null/normalized', |
34 option: null, | 37 option: null, |
35 // Somewhat arbitrary except that resulting waveform should have ampli
tude greater than | 38 // Somewhat arbitrary except that resulting waveform should have |
36 // 1. And we don't want a simple sine wave because that always has ma
x amplitude of 1. | 39 // amplitude greater than 1. And we don't want a simple sine wave |
| 40 // because that always has max amplitude of 1. |
37 realCoef: [0, -1, 1], | 41 realCoef: [0, -1, 1], |
38 threshold: 1e-5, | 42 threshold: 1e-5, |
39 expectedMax: 1 | 43 expectedMax: 1 |
40 }, | 44 }, |
41 | 45 |
42 // Explicitly set to false. Waveform should be normalized. | 46 // Explicitly set to false. Waveform should be normalized. |
43 { | 47 { |
44 name: "false/normalized", | 48 name: 'false/normalized', |
45 option: { | 49 option: {disableNormalization: false}, |
46 disableNormalization: false | |
47 }, | |
48 realCoef: [0, -1, 1], | 50 realCoef: [0, -1, 1], |
49 threshold: 1e-5, | 51 threshold: 1e-5, |
50 expectedMax: 1 | 52 expectedMax: 1 |
51 }, | 53 }, |
52 | 54 |
53 // Explicitly disable normalization. Waveform is not normalized. | 55 // Explicitly disable normalization. Waveform is not normalized. |
54 { | 56 { |
55 name: "true/unnormalized", | 57 name: 'true/unnormalized', |
56 option: { | 58 option: {disableNormalization: true}, |
57 disableNormalization: true | 59 // Somewhat arbitrary except that resulting waveform should have |
58 }, | 60 // amplitude very clearly greater than 1. |
59 // Somewhat arbitrary except that resulting waveform should have ampli
tude very clearly | |
60 // greater than 1. | |
61 realCoef: [0, 10], | 61 realCoef: [0, 10], |
62 threshold: 1e-5, | 62 threshold: 1e-5, |
63 expectedMax: 10 | 63 expectedMax: 10 |
64 }, | 64 }, |
65 | 65 |
66 // Explicitly set to some value that is not false. Waveform should NOT
be normalized. | 66 // Explicitly set to some value that is not false. Waveform should NOT |
| 67 // be normalized. |
67 { | 68 { |
68 name: "foo/unnormalized", | 69 name: 'foo/unnormalized', |
69 option: { | 70 option: {disableNormalization: 'foo'}, |
70 disableNormalization: "foo" | |
71 }, | |
72 realCoef: [0, 10], | 71 realCoef: [0, 10], |
73 threshold: 1e-5, | 72 threshold: 1e-5, |
74 expectedMax: 10 | 73 expectedMax: 10 |
75 }, | 74 }, |
76 | 75 |
77 // Explicitly set to null, which is the same as false. Waveform is norm
alized. | 76 // Explicitly set to null, which is the same as false. Waveform is |
| 77 // normalized. |
78 { | 78 { |
79 name: "null/unnormalized", | 79 name: 'null/unnormalized', |
80 option: { | 80 option: {disableNormalization: null}, |
81 disableNormalization: null | |
82 }, | |
83 realCoef: [0, 1, -1], | 81 realCoef: [0, 1, -1], |
84 threshold: 1e-5, | 82 threshold: 1e-5, |
85 expectedMax: 1 | 83 expectedMax: 1 |
86 }, | 84 }, |
87 | 85 |
88 // Pass in a random dictionary not using our key. Waveform should be no
rmalized. | 86 // Pass in a random dictionary not using our key. Waveform should be |
| 87 // normalized. |
89 { | 88 { |
90 name: "random-key-value/normalized", | 89 name: 'random-key-value/normalized', |
91 option: { | 90 option: {randomKey: true}, |
92 randomKey: true | |
93 }, | |
94 realCoef: [0, -1, 1], | 91 realCoef: [0, -1, 1], |
95 threshold: 1e-5, | 92 threshold: 1e-5, |
96 expectedMax: 1 | 93 expectedMax: 1 |
97 }, | 94 }, |
98 | 95 |
99 // Set options to several random keys. Waveform must be normalized. | 96 // Set options to several random keys. Waveform must be normalized. |
100 { | 97 { |
101 name: "more-random-keys/normalized", | 98 name: 'more-random-keys/normalized', |
102 option: { | 99 option: {key1: 'value1', key2: 42}, |
103 key1: "value1", | |
104 key2: 42 | |
105 }, | |
106 realCoef: [0, 1, -1], | 100 realCoef: [0, 1, -1], |
107 threshold: 1e-5, | 101 threshold: 1e-5, |
108 expectedMax: 1 | 102 expectedMax: 1 |
109 }, | 103 }, |
110 | 104 |
111 // Set option to include our key (set to true) amongst a bunch of others
. Waveform is NOT normalized. | 105 // Set option to include our key (set to true) amongst a bunch of |
| 106 // others. Waveform is NOT normalized. |
112 { | 107 { |
113 name: "true-with-random-keys/unnormalized", | 108 name: 'true-with-random-keys/unnormalized', |
114 option: { | 109 option: {key1: 'value1', disableNormalization: true}, |
115 key1: "value1", | |
116 disableNormalization: true | |
117 }, | |
118 realCoef: [0, 10], | 110 realCoef: [0, 10], |
119 threshold: 1e-5, | 111 threshold: 1e-5, |
120 expectedMax: 10 | 112 expectedMax: 10 |
121 }, | 113 }, |
122 | 114 |
123 // Set option to include our key (set to false) amongst a bunch of other
s. Waveform is normalized. | 115 // Set option to include our key (set to false) amongst a bunch of |
| 116 // others. Waveform is normalized. |
124 { | 117 { |
125 name: "false-with-random-keys/normalized", | 118 name: 'false-with-random-keys/normalized', |
126 option: { | 119 option: {key1: 'value1', disableNormalization: false}, |
127 key1: "value1", | |
128 disableNormalization: false | |
129 }, | |
130 realCoef: [0, 10], | 120 realCoef: [0, 10], |
131 threshold: 1e-5, | 121 threshold: 1e-5, |
132 expectedMax: 1 | 122 expectedMax: 1 |
133 }, | 123 }, |
134 | 124 |
135 // Set option to a non-dictionary. Waveform is normalized. | 125 // Set option to a non-dictionary. Waveform is normalized. |
136 { | 126 { |
137 name: "non-dict/normalized", | 127 name: 'non-dict/normalized', |
138 option : [1, 2, 3], | 128 option: [1, 2, 3], |
139 realCoef: [0, 1, -1], | 129 realCoef: [0, 1, -1], |
140 threshold: 1e-5, | 130 threshold: 1e-5, |
141 expectedMax: 1 | 131 expectedMax: 1 |
142 }, | 132 }, |
143 ]; | 133 ]; |
144 | 134 |
145 function arrayMax(array) { | 135 function arrayMax(array) { |
146 return array.reduce(function (reducedValue, currentValue) { | 136 return array.reduce(function(reducedValue, currentValue) { |
147 return Math.max(reducedValue, Math.abs(currentValue)); | 137 return Math.max(reducedValue, Math.abs(currentValue)); |
148 }, -1); | 138 }, -1); |
149 } | 139 } |
150 | 140 |
151 function createAndRunAudioGraph(options, realCoef, imagCoef, verify) { | 141 function createAndRunAudioGraph(options, realCoef, imagCoef, verify) { |
152 context = new OfflineAudioContext(1, renderFrames, sampleRate); | 142 context = new OfflineAudioContext(1, renderFrames, sampleRate); |
153 var osc = context.createOscillator(); | 143 let osc = context.createOscillator(); |
154 | 144 |
155 var r = new Float32Array(realCoef); | 145 let r = new Float32Array(realCoef); |
156 var i = new Float32Array(imagCoef); | 146 let i = new Float32Array(imagCoef); |
157 | 147 |
158 var wave; | 148 let wave; |
159 | 149 |
160 // If options is "NONE", we want to be sure to call the two-arg createPe
riodicWave to make | 150 // If options is "NONE", we want to be sure to call the two-arg |
161 // sure the default method works as expected. | 151 // createPeriodicWave to make sure the default method works as expected. |
162 if (options === "NONE") { | 152 if (options === 'NONE') { |
163 //console.log("2-arg: " + options); | 153 // console.log("2-arg: " + options); |
164 wave = context.createPeriodicWave(r, i); | 154 wave = context.createPeriodicWave(r, i); |
165 } else { | 155 } else { |
166 //console.log("3-arg: " + options); | 156 // console.log("3-arg: " + options); |
167 wave = context.createPeriodicWave(r, i, options); | 157 wave = context.createPeriodicWave(r, i, options); |
168 } | 158 } |
169 | 159 |
170 osc.setPeriodicWave(wave); | 160 osc.setPeriodicWave(wave); |
171 osc.connect(context.destination); | 161 osc.connect(context.destination); |
172 osc.start(); | 162 osc.start(); |
173 | 163 |
174 return context.startRendering().then(verify); | 164 return context.startRendering().then(verify); |
175 } | 165 } |
176 | 166 |
177 // Define a test function from the given test parameter. This is used as
the Audit.defineTask | 167 // Define a test function from the given test parameter. This is used as |
178 // task function. | 168 // the Audit.defineTask task function. |
179 function defineTest(testInfo) { | 169 function defineTest(testInfo) { |
180 return (task, should) => { | 170 return (task, should) => { |
181 var imagCoef = new Float32Array(testInfo.realCoef.length); | 171 let imagCoef = new Float32Array(testInfo.realCoef.length); |
182 createAndRunAudioGraph(testInfo.option, testInfo.realCoef, imagCoef, f
unction (result) { | 172 createAndRunAudioGraph( |
183 var prefix; | 173 testInfo.option, testInfo.realCoef, imagCoef, |
| 174 function(result) { |
| 175 let prefix; |
184 | 176 |
185 // Try to print out the test.option in a reasonably nice but explici
t way. | 177 // Try to print out the test.option in a reasonably nice but |
186 if (testInfo.option === "NONE") { | 178 // explicit way. |
187 prefix = ""; | 179 if (testInfo.option === 'NONE') { |
188 } else if (Array.isArray(testInfo.option)) { | 180 prefix = ''; |
189 prefix = "[" + testInfo.option + "]: "; | 181 } else if (Array.isArray(testInfo.option)) { |
190 } else { | 182 prefix = '[' + testInfo.option + ']: '; |
191 prefix = JSON.stringify(testInfo.option) + ": "; | 183 } else { |
192 } | 184 prefix = JSON.stringify(testInfo.option) + ': '; |
193 | 185 } |
194 should(arrayMax(result.getChannelData(0)), prefix + "amplitude") | 186 |
195 .beCloseTo(testInfo.expectedMax, {threshold: testInfo.threshold}); | 187 should(arrayMax(result.getChannelData(0)), prefix + 'amplitude') |
196 }).then(() => task.done()); | 188 .beCloseTo( |
| 189 testInfo.expectedMax, {threshold: testInfo.threshold}); |
| 190 }) |
| 191 .then(() => task.done()); |
197 }; | 192 }; |
198 } | 193 } |
199 | 194 |
200 // Ensure the actual Audit test name is unique by prepending an index to t
he provided test | 195 // Ensure the actual Audit test name is unique by prepending an index to |
201 // name. | 196 // the provided test name. |
202 function actualTestName(name, index) { | 197 function actualTestName(name, index) { |
203 return index + ":" + name; | 198 return index + ':' + name; |
204 } | 199 } |
205 | 200 |
206 function defineTasks() { | 201 function defineTasks() { |
207 for (var k = 0; k < testSet.length; ++k) { | 202 for (let k = 0; k < testSet.length; ++k) { |
208 var testInfo = testSet[k]; | 203 let testInfo = testSet[k]; |
209 audit.define(actualTestName(test.name, k), defineTest(testInfo)); | 204 audit.define(actualTestName(test.name, k), defineTest(testInfo)); |
210 } | 205 } |
211 } | 206 } |
212 | 207 |
213 defineTasks(); | 208 defineTasks(); |
214 audit.run(); | 209 audit.run(); |
215 | 210 |
216 successfullyParsed = true; | 211 successfullyParsed = true; |
217 </script> | 212 </script> |
218 </body> | 213 </body> |
219 </html> | 214 </html> |
OLD | NEW |