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 success = Should("node = new PeriodicWave(context, {real: [1, 1]})", |
| 63 function () { |
| 64 node = new PeriodicWave(context, { |
| 65 real: [1, 1] |
| 66 }); |
| 67 }).notThrow(); |
| 68 success = Should("node instanceof PeriodicWave", node instanceof Periodi
cWave) |
| 69 .beEqualTo(true) && success; |
| 70 |
| 71 success = Should("node = new PeriodicWave(context, {imag: [1, 1]})", |
| 72 function () { |
| 73 node = new PeriodicWave(context, { |
| 74 imag: [1, 1] |
| 75 }); |
| 76 }).notThrow(); |
| 77 success = Should("node instanceof PeriodicWave", node instanceof Periodi
cWave) |
| 78 .beEqualTo(true) && success; |
| 79 |
| 80 success = Should("node = new PeriodicWave(context, {real: [1, 2], imag:
[1, 1]})", |
| 81 function () { |
| 82 node = new PeriodicWave(context, { |
| 83 real: [1, 2], |
| 84 imag: [1, 1] |
| 85 }); |
| 86 }).notThrow(); |
| 87 success = Should("node instanceof PeriodicWave", node instanceof Periodi
cWave) |
| 88 .beEqualTo(true) && success; |
| 89 |
| 90 Should("new PeriodicWave() with options", success) |
| 91 .summarize( |
| 92 "constructed with correct attributes", |
| 93 "was not constructed correctly"); |
| 94 |
| 95 taskDone(); |
| 96 }); |
| 97 |
| 98 // The following test that the correct waveforms are produced when various |
| 99 // possible PeriodicWave options are used. These are needed because it's |
| 100 // the only way to tell if the various options were correctly applied. |
| 101 |
| 102 audit.defineTask("1: real periodicwave test", function (taskDone) { |
| 103 waveTest({ |
| 104 real: [0, 2] |
| 105 }, function (length, freq, sampleRate) { |
| 106 var expected = new Float32Array(length); |
| 107 var omega = 2 * Math.PI * freq / sampleRate; |
| 108 var normalizationFactor = 0.5; |
| 109 for (var k = 0; k < length; ++k) { |
| 110 expected[k] = Math.cos(omega * k); |
| 111 } |
| 112 return expected; |
| 113 }, |
| 114 2.7106e-5).then(taskDone); |
| 115 }); |
| 116 |
| 117 audit.defineTask("2: real periodicwave test", function (taskDone) { |
| 118 waveTest({ |
| 119 real: [0, 2], |
| 120 disableNormalization: false |
| 121 }, function (length, freq, sampleRate) { |
| 122 var expected = new Float32Array(length); |
| 123 var omega = 2 * Math.PI * freq / sampleRate; |
| 124 for (var k = 0; k < length; ++k) { |
| 125 expected[k] = Math.cos(omega * k); |
| 126 } |
| 127 return expected; |
| 128 }, |
| 129 2.7106e-5).then(taskDone); |
| 130 }), |
| 131 |
| 132 audit.defineTask("3: real periodicwave test", function (taskDone) { |
| 133 |
| 134 waveTest({ |
| 135 real: [0, 2], |
| 136 disableNormalization: true |
| 137 }, function (length, freq, sampleRate) { |
| 138 var expected = new Float32Array(length); |
| 139 var omega = 2 * Math.PI * freq / sampleRate; |
| 140 for (var k = 0; k < length; ++k) { |
| 141 expected[k] = 2 * Math.cos(omega * k); |
| 142 } |
| 143 return expected; |
| 144 }, |
| 145 5.4211e-5).then(taskDone); |
| 146 }); |
| 147 |
| 148 audit.defineTask("1: imag periodicwave test", function (taskDone) { |
| 149 waveTest({ |
| 150 imag: [0, 2] |
| 151 }, function (length, freq, sampleRate) { |
| 152 var expected = new Float32Array(length); |
| 153 var omega = 2 * Math.PI * freq / sampleRate; |
| 154 for (var k = 0; k < length; ++k) { |
| 155 expected[k] = Math.sin(omega * k); |
| 156 } |
| 157 return expected; |
| 158 }, |
| 159 2.7232e-5).then(taskDone); |
| 160 }); |
| 161 |
| 162 audit.defineTask("2: imag periodicwave test", function (taskDone) { |
| 163 waveTest({ |
| 164 imag: [0, 2], |
| 165 disableNormalization: false |
| 166 }, function (length, freq, sampleRate) { |
| 167 var expected = new Float32Array(length); |
| 168 var omega = 2 * Math.PI * freq / sampleRate; |
| 169 for (var k = 0; k < length; ++k) { |
| 170 expected[k] = Math.sin(omega * k); |
| 171 } |
| 172 return expected; |
| 173 }, |
| 174 2.7232e-5).then(taskDone); |
| 175 }); |
| 176 |
| 177 audit.defineTask("3: imag periodicwave test", function (taskDone) { |
| 178 waveTest({ |
| 179 imag: [0, 2], |
| 180 disableNormalization: true |
| 181 }, function (length, freq, sampleRate) { |
| 182 var expected = new Float32Array(length); |
| 183 var omega = 2 * Math.PI * freq / sampleRate; |
| 184 for (var k = 0; k < length; ++k) { |
| 185 expected[k] = 2 * Math.sin(omega * k); |
| 186 } |
| 187 return expected; |
| 188 }, |
| 189 5.4464e-5).then(taskDone); |
| 190 }); |
| 191 |
| 192 audit.defineTask("1: real/imag periodicwave test", function (taskDone) { |
| 193 waveTest({ |
| 194 real: [0, 1], |
| 195 imag: [0, 1], |
| 196 }, function (length, freq, sampleRate) { |
| 197 var expected = new Float32Array(length); |
| 198 var omega = 2 * Math.PI * freq / sampleRate; |
| 199 var normalizationFactor = Math.SQRT1_2; |
| 200 for (var k = 0; k < length; ++k) { |
| 201 expected[k] = normalizationFactor * (Math.sin(omega * k) + Math.co
s(omega * k)); |
| 202 } |
| 203 return expected; |
| 204 }, |
| 205 3.8371e-5).then(taskDone); |
| 206 }); |
| 207 |
| 208 audit.defineTask("2: real/imag periodicwave test", function (taskDone) { |
| 209 waveTest({ |
| 210 real: [0, 1], |
| 211 imag: [0, 1], |
| 212 disableNormalization: false |
| 213 }, function (length, freq, sampleRate) { |
| 214 var expected = new Float32Array(length); |
| 215 var omega = 2 * Math.PI * freq / sampleRate; |
| 216 var normalizationFactor = Math.SQRT1_2; |
| 217 for (var k = 0; k < length; ++k) { |
| 218 expected[k] = normalizationFactor * (Math.sin(omega * k) + Math.co
s(omega * k)); |
| 219 } |
| 220 return expected; |
| 221 }, |
| 222 2.7165e-5).then(taskDone); |
| 223 }); |
| 224 |
| 225 audit.defineTask("3: real/imag periodicwave test", function (taskDone) { |
| 226 waveTest({ |
| 227 real: [0, 1], |
| 228 imag: [0, 1], |
| 229 disableNormalization: true |
| 230 }, function (length, freq, sampleRate) { |
| 231 var expected = new Float32Array(length); |
| 232 var omega = 2 * Math.PI * freq / sampleRate; |
| 233 for (var k = 0; k < length; ++k) { |
| 234 expected[k] = Math.sin(omega * k) + Math.cos(omega * k); |
| 235 } |
| 236 return expected; |
| 237 }, |
| 238 3.8416e-5).then(taskDone); |
| 239 }); |
| 240 |
| 241 function waveTest(waveOptions, expectedFunction, threshold) { |
| 242 var node; |
| 243 var success = true; |
| 244 |
| 245 // Rather arbitrary sample rate and render length. Length doesn't have |
| 246 // to be very long. |
| 247 var sampleRate = 48000; |
| 248 var renderLength = 0.25; |
| 249 var testContext = new OfflineAudioContext(1, renderLength * sampleRate,
sampleRate); |
| 250 |
| 251 var options = { |
| 252 periodicWave: new PeriodicWave(testContext, waveOptions) |
| 253 }; |
| 254 node = new OscillatorNode(testContext, options); |
| 255 |
| 256 // Create the graph |
| 257 node.connect(testContext.destination); |
| 258 node.start(); |
| 259 |
| 260 return testContext.startRendering().then(function (resultBuffer) { |
| 261 var actual = resultBuffer.getChannelData(0); |
| 262 var expected = expectedFunction(actual.length, |
| 263 node.frequency.value, |
| 264 testContext.sampleRate); |
| 265 // Actual must match expected to within the (experimentally) |
| 266 // determined threshold. |
| 267 var message = ""; |
| 268 if (waveOptions.disableNormalization != undefined) |
| 269 message = "disableNormalization: " + waveOptions.disableNormalizatio
n; |
| 270 if (waveOptions.real) { |
| 271 if (message.length > 0) |
| 272 message += ", " |
| 273 message += "real: [" + waveOptions.real + "]"; |
| 274 } |
| 275 if (waveOptions.imag) { |
| 276 if (message.length > 0) |
| 277 message += ", " |
| 278 message += "imag: [" + waveOptions.imag + "]"; |
| 279 } |
| 280 Should("Oscillator with periodicWave {" + message + "}", actual) |
| 281 .beCloseToArray(expected, threshold); |
| 282 }); |
| 283 } |
| 284 |
| 285 audit.runTasks(); |
| 286 </script> |
| 287 </body> |
| 288 </html> |
OLD | NEW |