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>
12 description("Test PeriodicWave Normalization");
13 window
.jsTestIsAsync
= true;
15 var sampleRate
= 48000;
16 var renderFrames
= sampleRate
;
18 var audit
= Audit
.createTaskRunner();
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.
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.
35 name
: "option-null/normalized",
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.
44 // Explicitly set to false. Waveform should be normalized.
46 name
: "false/normalized",
48 disableNormalization
: false
55 // Explicitly disable normalization. Waveform is not normalized.
57 name
: "true/unnormalized",
59 disableNormalization
: true
61 // Somewhat arbitrary except that resulting waveform should have amplitude very clearly
68 // Explicitly set to some value that is not false. Waveform should NOT be normalized.
70 name
: "foo/unnormalized",
72 disableNormalization
: "foo"
79 // Explicitly set to null, which is the same as false. Waveform is normalized.
81 name
: "null/unnormalized",
83 disableNormalization
: null
90 // Pass in a random dictionary not using our key. Waveform should be normalized.
92 name
: "random-key-value/normalized",
101 // Set options to several random keys. Waveform must be normalized.
103 name
: "more-random-keys/normalized",
108 realCoef
: [0, 1, -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",
118 disableNormalization
: true
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",
130 disableNormalization
: false
137 // Set option to a non-dictionary. Waveform is normalized.
139 name
: "non-dict/normalized",
141 realCoef
: [0, 1, -1],
147 function arrayMax(array
) {
148 return array
.reduce(function (reducedValue
, currentValue
) {
149 return Math
.max(reducedValue
, Math
.abs(currentValue
));
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
);
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
);
168 //console.log("3-arg: " + options);
169 wave
= context
.createPeriodicWave(r
, i
, options
);
172 osc
.setPeriodicWave(wave
);
173 osc
.connect(context
.destination
);
176 return context
.startRendering().then(verify
);
179 // Define a test function from the given test parameter. This is used as the Audit.defineTask
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
) {
187 // Try to print out the test.option in a reasonably nice but explicit way.
188 if (test
.option
=== "NONE") {
190 } else if (Array
.isArray(test
.option
)) {
191 prefix
= "[" + test
.option
+ "]: ";
193 prefix
= JSON
.stringify(test
.option
) + ": ";
196 Should(prefix
+ "amplitude", arrayMax(result
.getChannelData(0)))
197 .beCloseTo(test
.expectedMax
, test
.threshold
);
202 // Ensure the actual Audit test name is unique by prepending an index to the provided test
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
) {
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
);
232 successfullyParsed
= true;