OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <title>Test Constructor: PeriodicWave</title> |
| 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharnessreport.js"></script> |
| 7 <script src="../resources/audio-testing.js"></script> |
| 8 <script src="audionodeoptions.js"></script> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 var context; |
| 14 |
| 15 var audit = Audit.createTaskRunner(); |
| 16 |
| 17 audit.defineTask("initialize", function (taskDone) { |
| 18 Should("context = new OfflineAudioContext(...)", function () { |
| 19 context = new OfflineAudioContext(1, 1, 48000); |
| 20 }).notThrow(); |
| 21 |
| 22 taskDone(); |
| 23 }); |
| 24 |
| 25 audit.defineTask("invalid constructor", function (taskDone) { |
| 26 var node; |
| 27 var success = true; |
| 28 |
| 29 success = Should("new PeriodicWave()", function () { |
| 30 node = new PeriodicWave(); |
| 31 }).throw("TypeError"); |
| 32 success = Should("new PeriodicWave(1)", function () { |
| 33 node = new PeriodicWave(1) && success; |
| 34 }).throw("TypeError"); |
| 35 success = Should("new PeriodicWave(context, 42)", function () { |
| 36 node = new PeriodicWave(context, 42) && success; |
| 37 }).throw("TypeError"); |
| 38 |
| 39 Should("Invalid constructors", success) |
| 40 .summarize( |
| 41 "correctly threw errors", |
| 42 "did not throw errors in all cases"); |
| 43 |
| 44 taskDone(); |
| 45 }); |
| 46 |
| 47 audit.defineTask("default constructor", function (taskDone) { |
| 48 var node; |
| 49 var success = true; |
| 50 |
| 51 success = Should("node = new PeriodicWave(context)", function () { |
| 52 node = new PeriodicWave(context); |
| 53 }).throw("InvalidStateError"); |
| 54 |
| 55 taskDone(); |
| 56 }); |
| 57 |
| 58 audit.defineTask("constructor with options", function (taskDone) { |
| 59 var node; |
| 60 var success = true; |
| 61 |
| 62 var options = { |
| 63 real: [1, 1] |
| 64 }; |
| 65 success = Should("node = new PeriodicWave(context, " + JSON.stringify(op
tions) + ")", |
| 66 function () { |
| 67 node = new PeriodicWave(context, options); |
| 68 }).notThrow(); |
| 69 success = Should("node instanceof PeriodicWave", node instanceof Periodi
cWave) |
| 70 .beEqualTo(true) && success; |
| 71 |
| 72 options = { |
| 73 imag: [1, 1] |
| 74 }; |
| 75 success = Should("node = new PeriodicWave(context, " + JSON.stringify(op
tions) + ")", |
| 76 function () { |
| 77 node = new PeriodicWave(context, options); |
| 78 }).notThrow(); |
| 79 success = Should("node instanceof PeriodicWave", node instanceof Periodi
cWave) |
| 80 .beEqualTo(true) && success; |
| 81 |
| 82 options = { |
| 83 real: [1, 2], |
| 84 imag: [1, 1] |
| 85 }; |
| 86 success = Should("node = new PeriodicWave(context, " + JSON.stringify(op
tions) + ")", |
| 87 function () { |
| 88 node = new PeriodicWave(context, options); |
| 89 }).notThrow(); |
| 90 success = Should("node instanceof PeriodicWave", node instanceof Periodi
cWave) |
| 91 .beEqualTo(true) && success; |
| 92 |
| 93 Should("new PeriodicWave() with options", success) |
| 94 .summarize( |
| 95 "constructed with correct attributes", |
| 96 "was not constructed correctly"); |
| 97 |
| 98 taskDone(); |
| 99 }); |
| 100 |
| 101 // The following test that the correct waveforms are produced when various |
| 102 // possible PeriodicWave options are used. These are needed because it's |
| 103 // the only way to tell if the various options were correctly applied. |
| 104 |
| 105 audit.defineTask("1: real periodicwave test", function (taskDone) { |
| 106 waveTest({ |
| 107 real: [0, 2] |
| 108 }, function (length, freq, sampleRate) { |
| 109 var expected = new Float32Array(length); |
| 110 var omega = 2 * Math.PI * freq / sampleRate; |
| 111 var normalizationFactor = 0.5; |
| 112 for (var k = 0; k < length; ++k) { |
| 113 expected[k] = Math.cos(omega * k); |
| 114 } |
| 115 return expected; |
| 116 }, |
| 117 2.7106e-5).then(taskDone); |
| 118 }); |
| 119 |
| 120 audit.defineTask("2: real periodicwave test", function (taskDone) { |
| 121 waveTest({ |
| 122 real: [0, 2], |
| 123 disableNormalization: false |
| 124 }, function (length, freq, sampleRate) { |
| 125 var expected = new Float32Array(length); |
| 126 var omega = 2 * Math.PI * freq / sampleRate; |
| 127 for (var k = 0; k < length; ++k) { |
| 128 expected[k] = Math.cos(omega * k); |
| 129 } |
| 130 return expected; |
| 131 }, |
| 132 2.7106e-5).then(taskDone); |
| 133 }), |
| 134 |
| 135 audit.defineTask("3: real periodicwave test", function (taskDone) { |
| 136 |
| 137 waveTest({ |
| 138 real: [0, 2], |
| 139 disableNormalization: true |
| 140 }, function (length, freq, sampleRate) { |
| 141 var expected = new Float32Array(length); |
| 142 var omega = 2 * Math.PI * freq / sampleRate; |
| 143 for (var k = 0; k < length; ++k) { |
| 144 expected[k] = 2 * Math.cos(omega * k); |
| 145 } |
| 146 return expected; |
| 147 }, |
| 148 5.4211e-5).then(taskDone); |
| 149 }); |
| 150 |
| 151 audit.defineTask("1: imag periodicwave test", function (taskDone) { |
| 152 waveTest({ |
| 153 imag: [0, 2] |
| 154 }, function (length, freq, sampleRate) { |
| 155 var expected = new Float32Array(length); |
| 156 var omega = 2 * Math.PI * freq / sampleRate; |
| 157 for (var k = 0; k < length; ++k) { |
| 158 expected[k] = Math.sin(omega * k); |
| 159 } |
| 160 return expected; |
| 161 }, |
| 162 2.7232e-5).then(taskDone); |
| 163 }); |
| 164 |
| 165 audit.defineTask("2: imag periodicwave test", function (taskDone) { |
| 166 waveTest({ |
| 167 imag: [0, 2], |
| 168 disableNormalization: false |
| 169 }, function (length, freq, sampleRate) { |
| 170 var expected = new Float32Array(length); |
| 171 var omega = 2 * Math.PI * freq / sampleRate; |
| 172 for (var k = 0; k < length; ++k) { |
| 173 expected[k] = Math.sin(omega * k); |
| 174 } |
| 175 return expected; |
| 176 }, |
| 177 2.7232e-5).then(taskDone); |
| 178 }); |
| 179 |
| 180 audit.defineTask("3: imag periodicwave test", function (taskDone) { |
| 181 waveTest({ |
| 182 imag: [0, 2], |
| 183 disableNormalization: true |
| 184 }, function (length, freq, sampleRate) { |
| 185 var expected = new Float32Array(length); |
| 186 var omega = 2 * Math.PI * freq / sampleRate; |
| 187 for (var k = 0; k < length; ++k) { |
| 188 expected[k] = 2 * Math.sin(omega * k); |
| 189 } |
| 190 return expected; |
| 191 }, |
| 192 5.4464e-5).then(taskDone); |
| 193 }); |
| 194 |
| 195 audit.defineTask("1: real/imag periodicwave test", function (taskDone) { |
| 196 waveTest({ |
| 197 real: [0, 1], |
| 198 imag: [0, 1], |
| 199 }, function (length, freq, sampleRate) { |
| 200 var expected = new Float32Array(length); |
| 201 var omega = 2 * Math.PI * freq / sampleRate; |
| 202 var normalizationFactor = Math.SQRT1_2; |
| 203 for (var k = 0; k < length; ++k) { |
| 204 expected[k] = normalizationFactor * (Math.sin(omega * k) + Math.co
s(omega * k)); |
| 205 } |
| 206 return expected; |
| 207 }, |
| 208 3.8371e-5).then(taskDone); |
| 209 }); |
| 210 |
| 211 audit.defineTask("2: real/imag periodicwave test", function (taskDone) { |
| 212 waveTest({ |
| 213 real: [0, 1], |
| 214 imag: [0, 1], |
| 215 disableNormalization: false |
| 216 }, function (length, freq, sampleRate) { |
| 217 var expected = new Float32Array(length); |
| 218 var omega = 2 * Math.PI * freq / sampleRate; |
| 219 var normalizationFactor = Math.SQRT1_2; |
| 220 for (var k = 0; k < length; ++k) { |
| 221 expected[k] = normalizationFactor * (Math.sin(omega * k) + Math.co
s(omega * k)); |
| 222 } |
| 223 return expected; |
| 224 }, |
| 225 2.7165e-5).then(taskDone); |
| 226 }); |
| 227 |
| 228 audit.defineTask("3: real/imag periodicwave test", function (taskDone) { |
| 229 waveTest({ |
| 230 real: [0, 1], |
| 231 imag: [0, 1], |
| 232 disableNormalization: true |
| 233 }, function (length, freq, sampleRate) { |
| 234 var expected = new Float32Array(length); |
| 235 var omega = 2 * Math.PI * freq / sampleRate; |
| 236 for (var k = 0; k < length; ++k) { |
| 237 expected[k] = Math.sin(omega * k) + Math.cos(omega * k); |
| 238 } |
| 239 return expected; |
| 240 }, |
| 241 3.8416e-5).then(taskDone); |
| 242 }); |
| 243 |
| 244 function waveTest(waveOptions, expectedFunction, threshold) { |
| 245 var node; |
| 246 var success = true; |
| 247 |
| 248 // Rather arbitrary sample rate and render length. Length doesn't have |
| 249 // to be very long. |
| 250 var sampleRate = 48000; |
| 251 var renderLength = 0.25; |
| 252 var testContext = new OfflineAudioContext(1, renderLength * sampleRate,
sampleRate); |
| 253 |
| 254 var options = { |
| 255 periodicWave: new PeriodicWave(testContext, waveOptions) |
| 256 }; |
| 257 node = new OscillatorNode(testContext, options); |
| 258 |
| 259 // Create the graph |
| 260 node.connect(testContext.destination); |
| 261 node.start(); |
| 262 |
| 263 return testContext.startRendering().then(function (resultBuffer) { |
| 264 var actual = resultBuffer.getChannelData(0); |
| 265 var expected = expectedFunction(actual.length, |
| 266 node.frequency.value, |
| 267 testContext.sampleRate); |
| 268 // Actual must match expected to within the (experimentally) |
| 269 // determined threshold. |
| 270 var message = ""; |
| 271 if (waveOptions.disableNormalization != undefined) |
| 272 message = "disableNormalization: " + waveOptions.disableNormalizatio
n; |
| 273 if (waveOptions.real) { |
| 274 if (message.length > 0) |
| 275 message += ", " |
| 276 message += "real: [" + waveOptions.real + "]"; |
| 277 } |
| 278 if (waveOptions.imag) { |
| 279 if (message.length > 0) |
| 280 message += ", " |
| 281 message += "imag: [" + waveOptions.imag + "]"; |
| 282 } |
| 283 Should("Oscillator with periodicWave {" + message + "}", actual) |
| 284 .beCloseToArray(expected, threshold); |
| 285 }); |
| 286 } |
| 287 |
| 288 audit.runTasks(); |
| 289 </script> |
| 290 </body> |
| 291 </html> |
OLD | NEW |