| OLD | NEW |
| 1 <!doctype html> | 1 <!doctype html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <title>Test Constructor: PeriodicWave</title> | 4 <title>Test Constructor: PeriodicWave</title> |
| 5 <script src="../../resources/testharness.js"></script> | 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharnessreport.js"></script> | 6 <script src="../../resources/testharnessreport.js"></script> |
| 7 <script src="../resources/audit-util.js"></script> | 7 <script src="../resources/audit-util.js"></script> |
| 8 <script src="../resources/audio-testing.js"></script> | 8 <script src="../resources/audit.js"></script> |
| 9 <script src="audionodeoptions.js"></script> | 9 <script src="new-audionodeoptions.js"></script> |
| 10 </head> | 10 </head> |
| 11 | 11 |
| 12 <body> | 12 <body> |
| 13 <script> | 13 <script> |
| 14 var context; | 14 var context; |
| 15 | 15 |
| 16 var audit = Audit.createTaskRunner(); | 16 var audit = Audit.createTaskRunner(); |
| 17 | 17 |
| 18 audit.defineTask("initialize", function (taskDone) { | 18 audit.define('initialize', (task, should) => { |
| 19 Should("context = new OfflineAudioContext(...)", function () { | 19 context = initializeContext(should); |
| 20 context = new OfflineAudioContext(1, 1, 48000); | 20 task.done(); |
| 21 }).notThrow(); | |
| 22 | |
| 23 taskDone(); | |
| 24 }); | 21 }); |
| 25 | 22 |
| 26 audit.defineTask("invalid constructor", function (taskDone) { | 23 audit.define('invalid constructor', (task, should) => { |
| 27 var node; | 24 testInvalidConstructor(should, 'PeriodicWave', context); |
| 28 var success = true; | 25 task.done(); |
| 29 | |
| 30 success = Should("new PeriodicWave()", function () { | |
| 31 node = new PeriodicWave(); | |
| 32 }).throw("TypeError"); | |
| 33 success = Should("new PeriodicWave(1)", function () { | |
| 34 node = new PeriodicWave(1) && success; | |
| 35 }).throw("TypeError"); | |
| 36 success = Should("new PeriodicWave(context, 42)", function () { | |
| 37 node = new PeriodicWave(context, 42) && success; | |
| 38 }).throw("TypeError"); | |
| 39 | |
| 40 Should("Invalid constructors", success) | |
| 41 .summarize( | |
| 42 "correctly threw errors", | |
| 43 "did not throw errors in all cases"); | |
| 44 | |
| 45 taskDone(); | |
| 46 }); | 26 }); |
| 47 | 27 |
| 48 audit.defineTask("default constructor", function (taskDone) { | 28 audit.define('default constructor', (task, should) => { |
| 49 var node; | 29 should(() => { |
| 50 var success = true; | 30 node = new PeriodicWave(context); |
| 31 }, 'node = new PeriodicWave(context)').notThrow(); |
| 51 | 32 |
| 52 success = Should("node = new PeriodicWave(context)", function () { | 33 task.done(); |
| 53 node = new PeriodicWave(context); | |
| 54 }).notThrow(); | |
| 55 | |
| 56 taskDone(); | |
| 57 }); | 34 }); |
| 58 | 35 |
| 59 audit.defineTask("constructor with options", function (taskDone) { | 36 audit.define('constructor with options', (task, should) => { |
| 60 var node1; | 37 var node1; |
| 61 var success = true; | 38 var options = {real: [1, 1]}; |
| 62 | 39 should( |
| 63 var options = { | 40 () => { |
| 64 real: [1, 1] | 41 node1 = new PeriodicWave(context, options); |
| 65 }; | 42 }, |
| 66 success = Should("node = new PeriodicWave(context, " + JSON.stringify(op
tions) + ")", | 43 'node = new PeriodicWave(context, ' + JSON.stringify(options) + ')') |
| 67 function () { | 44 .notThrow(); |
| 68 node1 = new PeriodicWave(context, options); | 45 should(node1 instanceof PeriodicWave, 'node1 instanceof PeriodicWave') |
| 69 }).notThrow(); | 46 .beEqualTo(true); |
| 70 success = Should("node1 instanceof PeriodicWave", node1 instanceof Perio
dicWave) | |
| 71 .beEqualTo(true) && success; | |
| 72 | 47 |
| 73 var node2; | 48 var node2; |
| 74 options = { | 49 options = {imag: [1, 1]}; |
| 75 imag: [1, 1] | 50 should( |
| 76 }; | 51 () => { |
| 77 success = Should("node2 = new PeriodicWave(context, " + JSON.stringify(o
ptions) + ")", | 52 node2 = new PeriodicWave(context, options); |
| 78 function () { | 53 }, |
| 79 node2 = new PeriodicWave(context, options); | 54 'node2 = new PeriodicWave(context, ' + JSON.stringify(options) + |
| 80 }).notThrow(); | 55 ')') |
| 81 success = Should("node2 instanceof PeriodicWave", node2 instanceof Perio
dicWave) | 56 .notThrow(); |
| 82 .beEqualTo(true) && success; | 57 should(node2 instanceof PeriodicWave, 'node2 instanceof PeriodicWave') |
| 58 .beEqualTo(true); |
| 83 | 59 |
| 84 var node3; | 60 var node3; |
| 85 options = { | 61 options = {real: [1, 2], imag: [1, 1]}; |
| 86 real: [1, 2], | 62 should( |
| 87 imag: [1, 1] | 63 () => { |
| 88 }; | 64 node3 = new PeriodicWave(context, options); |
| 89 success = Should("node3 = new PeriodicWave(context, " + JSON.stringify(o
ptions) + ")", | 65 }, |
| 90 function () { | 66 'node3 = new PeriodicWave(context, ' + JSON.stringify(options) + |
| 91 node3 = new PeriodicWave(context, options); | 67 ')') |
| 92 }).notThrow(); | 68 .notThrow(); |
| 93 success = Should("node3 instanceof PeriodicWave", node3 instanceof Perio
dicWave) | 69 should(node3 instanceof PeriodicWave, 'node3 instanceof PeriodicWave') |
| 94 .beEqualTo(true) && success; | 70 .beEqualTo(true); |
| 95 | 71 |
| 96 Should("new PeriodicWave() with options", success) | 72 task.done(); |
| 97 .summarize( | 73 }); |
| 98 "constructed with correct attributes", | |
| 99 "was not constructed correctly"); | |
| 100 | 74 |
| 101 taskDone(); | |
| 102 }); | |
| 103 | |
| 104 // The following test that the correct waveforms are produced when various | 75 // The following test that the correct waveforms are produced when various |
| 105 // possible PeriodicWave options are used. These are needed because it's | 76 // possible PeriodicWave options are used. These are needed because it's |
| 106 // the only way to tell if the various options were correctly applied. | 77 // the only way to tell if the various options were correctly applied. |
| 107 | 78 |
| 108 // TODO(rtoy): These functionality tests should be moved out to a separate | 79 // TODO(rtoy): These functionality tests should be moved out to a separate |
| 109 // file. | 80 // file. |
| 110 audit.defineTask("1: real periodicwave test", function (taskDone) { | 81 audit.define('1: real periodicwave test', (task, should) => { |
| 111 waveTest({ | 82 verifyPeriodicWaveOutput( |
| 112 real: [0, 2] | 83 should, {real: [0, 2]}, generateReference(Math.cos), 2.7143e-5) |
| 113 }, function (length, freq, sampleRate) { | 84 .then(() => task.done()); |
| 114 var expected = new Float32Array(length); | |
| 115 var omega = 2 * Math.PI * freq / sampleRate; | |
| 116 var normalizationFactor = 0.5; | |
| 117 for (var k = 0; k < length; ++k) { | |
| 118 expected[k] = Math.cos(omega * k); | |
| 119 } | |
| 120 return expected; | |
| 121 }, | |
| 122 2.7143e-5).then(taskDone); | |
| 123 }); | 85 }); |
| 124 | 86 |
| 125 audit.defineTask("2: real periodicwave test", function (taskDone) { | 87 audit.define('2: real periodicwave test', (task, should) => { |
| 126 waveTest({ | 88 verifyPeriodicWaveOutput( |
| 127 real: [0, 2], | 89 should, {real: [0, 2], disableNormalization: false}, |
| 128 disableNormalization: false | 90 generateReference(Math.cos), 2.7143e-5) |
| 129 }, function (length, freq, sampleRate) { | 91 .then(() => task.done()); |
| 130 var expected = new Float32Array(length); | 92 }); |
| 131 var omega = 2 * Math.PI * freq / sampleRate; | |
| 132 for (var k = 0; k < length; ++k) { | |
| 133 expected[k] = Math.cos(omega * k); | |
| 134 } | |
| 135 return expected; | |
| 136 }, | |
| 137 2.7143e-5).then(taskDone); | |
| 138 }), | |
| 139 | 93 |
| 140 audit.defineTask("3: real periodicwave test", function (taskDone) { | 94 audit.define('3: real periodicwave test', (task, should) => { |
| 95 verifyPeriodicWaveOutput( |
| 96 should, {real: [0, 2], disableNormalization: true}, |
| 97 generateReference(x => 2 * Math.cos(x)), 5.4285e-5) |
| 98 .then(() => task.done()); |
| 99 }); |
| 141 | 100 |
| 142 waveTest({ | 101 audit.define('1: imag periodicwave test', (task, should) => { |
| 143 real: [0, 2], | 102 verifyPeriodicWaveOutput( |
| 144 disableNormalization: true | 103 should, {imag: [0, 2]}, generateReference(Math.sin), 2.7232e-5) |
| 145 }, function (length, freq, sampleRate) { | 104 .then(() => task.done()); |
| 105 }); |
| 106 |
| 107 audit.define('2: imag periodicwave test', (task, should) => { |
| 108 verifyPeriodicWaveOutput( |
| 109 should, {imag: [0, 2], disableNormalization: false}, |
| 110 generateReference(Math.sin), 2.7232e-5) |
| 111 .then(() => task.done()); |
| 112 }); |
| 113 |
| 114 audit.define('3: imag periodicwave test', (task, should) => { |
| 115 verifyPeriodicWaveOutput( |
| 116 should, {imag: [0, 2], disableNormalization: true}, |
| 117 generateReference(x => 2 * Math.sin(x)), 5.4464e-5) |
| 118 .then(() => task.done()); |
| 119 }); |
| 120 |
| 121 audit.define('1: real/imag periodicwave test', (task, should) => { |
| 122 verifyPeriodicWaveOutput( |
| 123 should, { |
| 124 real: [0, 1], |
| 125 imag: [0, 1], |
| 126 }, |
| 127 generateReference(x => Math.SQRT1_2 * (Math.sin(x) + Math.cos(x))), |
| 128 3.8371e-5) |
| 129 .then(() => task.done()); |
| 130 }); |
| 131 |
| 132 audit.define('2: real/imag periodicwave test', (task, should) => { |
| 133 verifyPeriodicWaveOutput( |
| 134 should, {real: [0, 1], imag: [0, 1], disableNormalization: false}, |
| 135 generateReference(x => Math.SQRT1_2 * (Math.sin(x) + Math.cos(x))), |
| 136 2.7165e-5) |
| 137 .then(() => task.done()); |
| 138 }); |
| 139 |
| 140 audit.define('3: real/imag periodicwave test', (task, should) => { |
| 141 verifyPeriodicWaveOutput( |
| 142 should, {real: [0, 1], imag: [0, 1], disableNormalization: true}, |
| 143 generateReference(x => Math.sin(x) + Math.cos(x)), 3.8416e-5) |
| 144 .then(() => task.done()); |
| 145 }); |
| 146 |
| 147 // Returns a function that generates the expected reference array where |
| 148 // the samples are generated by the function |gen|. |
| 149 function generateReference(gen) { |
| 150 return (length, freq, sampleRate) => { |
| 146 var expected = new Float32Array(length); | 151 var expected = new Float32Array(length); |
| 147 var omega = 2 * Math.PI * freq / sampleRate; | 152 var omega = 2 * Math.PI * freq / sampleRate; |
| 148 for (var k = 0; k < length; ++k) { | 153 for (var k = 0; k < length; ++k) { |
| 149 expected[k] = 2 * Math.cos(omega * k); | 154 expected[k] = gen(omega * k); |
| 150 } | 155 } |
| 151 return expected; | 156 return expected; |
| 152 }, | 157 }; |
| 153 5.4285e-5).then(taskDone); | 158 } |
| 154 }); | |
| 155 | 159 |
| 156 audit.defineTask("1: imag periodicwave test", function (taskDone) { | 160 // Verify that an oscillator constructed from the given periodic wave |
| 157 waveTest({ | 161 // produces the expected result. |
| 158 imag: [0, 2] | 162 function verifyPeriodicWaveOutput( |
| 159 }, function (length, freq, sampleRate) { | 163 should, waveOptions, expectedFunction, threshold) { |
| 160 var expected = new Float32Array(length); | |
| 161 var omega = 2 * Math.PI * freq / sampleRate; | |
| 162 for (var k = 0; k < length; ++k) { | |
| 163 expected[k] = Math.sin(omega * k); | |
| 164 } | |
| 165 return expected; | |
| 166 }, | |
| 167 2.7232e-5).then(taskDone); | |
| 168 }); | |
| 169 | |
| 170 audit.defineTask("2: imag periodicwave test", function (taskDone) { | |
| 171 waveTest({ | |
| 172 imag: [0, 2], | |
| 173 disableNormalization: false | |
| 174 }, function (length, freq, sampleRate) { | |
| 175 var expected = new Float32Array(length); | |
| 176 var omega = 2 * Math.PI * freq / sampleRate; | |
| 177 for (var k = 0; k < length; ++k) { | |
| 178 expected[k] = Math.sin(omega * k); | |
| 179 } | |
| 180 return expected; | |
| 181 }, | |
| 182 2.7232e-5).then(taskDone); | |
| 183 }); | |
| 184 | |
| 185 audit.defineTask("3: imag periodicwave test", function (taskDone) { | |
| 186 waveTest({ | |
| 187 imag: [0, 2], | |
| 188 disableNormalization: true | |
| 189 }, function (length, freq, sampleRate) { | |
| 190 var expected = new Float32Array(length); | |
| 191 var omega = 2 * Math.PI * freq / sampleRate; | |
| 192 for (var k = 0; k < length; ++k) { | |
| 193 expected[k] = 2 * Math.sin(omega * k); | |
| 194 } | |
| 195 return expected; | |
| 196 }, | |
| 197 5.4464e-5).then(taskDone); | |
| 198 }); | |
| 199 | |
| 200 audit.defineTask("1: real/imag periodicwave test", function (taskDone) { | |
| 201 waveTest({ | |
| 202 real: [0, 1], | |
| 203 imag: [0, 1], | |
| 204 }, function (length, freq, sampleRate) { | |
| 205 var expected = new Float32Array(length); | |
| 206 var omega = 2 * Math.PI * freq / sampleRate; | |
| 207 var normalizationFactor = Math.SQRT1_2; | |
| 208 for (var k = 0; k < length; ++k) { | |
| 209 expected[k] = normalizationFactor * (Math.sin(omega * k) + Math.co
s(omega * k)); | |
| 210 } | |
| 211 return expected; | |
| 212 }, | |
| 213 3.8371e-5).then(taskDone); | |
| 214 }); | |
| 215 | |
| 216 audit.defineTask("2: real/imag periodicwave test", function (taskDone) { | |
| 217 waveTest({ | |
| 218 real: [0, 1], | |
| 219 imag: [0, 1], | |
| 220 disableNormalization: false | |
| 221 }, function (length, freq, sampleRate) { | |
| 222 var expected = new Float32Array(length); | |
| 223 var omega = 2 * Math.PI * freq / sampleRate; | |
| 224 var normalizationFactor = Math.SQRT1_2; | |
| 225 for (var k = 0; k < length; ++k) { | |
| 226 expected[k] = normalizationFactor * (Math.sin(omega * k) + Math.co
s(omega * k)); | |
| 227 } | |
| 228 return expected; | |
| 229 }, | |
| 230 2.7165e-5).then(taskDone); | |
| 231 }); | |
| 232 | |
| 233 audit.defineTask("3: real/imag periodicwave test", function (taskDone) { | |
| 234 waveTest({ | |
| 235 real: [0, 1], | |
| 236 imag: [0, 1], | |
| 237 disableNormalization: true | |
| 238 }, function (length, freq, sampleRate) { | |
| 239 var expected = new Float32Array(length); | |
| 240 var omega = 2 * Math.PI * freq / sampleRate; | |
| 241 for (var k = 0; k < length; ++k) { | |
| 242 expected[k] = Math.sin(omega * k) + Math.cos(omega * k); | |
| 243 } | |
| 244 return expected; | |
| 245 }, | |
| 246 3.8416e-5).then(taskDone); | |
| 247 }); | |
| 248 | |
| 249 function waveTest(waveOptions, expectedFunction, threshold) { | |
| 250 var node; | 164 var node; |
| 251 var success = true; | |
| 252 | |
| 253 // Rather arbitrary sample rate and render length. Length doesn't have | 165 // Rather arbitrary sample rate and render length. Length doesn't have |
| 254 // to be very long. | 166 // to be very long. |
| 255 var sampleRate = 48000; | 167 var sampleRate = 48000; |
| 256 var renderLength = 0.25; | 168 var renderLength = 0.25; |
| 257 var testContext = new OfflineAudioContext(1, renderLength * sampleRate,
sampleRate); | 169 var testContext = |
| 170 new OfflineAudioContext(1, renderLength * sampleRate, sampleRate); |
| 258 | 171 |
| 259 var options = { | 172 var options = { |
| 260 periodicWave: new PeriodicWave(testContext, waveOptions) | 173 periodicWave: new PeriodicWave(testContext, waveOptions) |
| 261 }; | 174 }; |
| 262 node = new OscillatorNode(testContext, options); | 175 node = new OscillatorNode(testContext, options); |
| 263 | 176 |
| 264 // Create the graph | 177 // Create the graph |
| 265 node.connect(testContext.destination); | 178 node.connect(testContext.destination); |
| 266 node.start(); | 179 node.start(); |
| 267 | 180 |
| 268 return testContext.startRendering().then(function (resultBuffer) { | 181 return testContext.startRendering().then(function(resultBuffer) { |
| 269 var actual = resultBuffer.getChannelData(0); | 182 var actual = resultBuffer.getChannelData(0); |
| 270 var expected = expectedFunction(actual.length, | 183 var expected = expectedFunction( |
| 271 node.frequency.value, | 184 actual.length, node.frequency.value, testContext.sampleRate); |
| 272 testContext.sampleRate); | |
| 273 // Actual must match expected to within the (experimentally) | 185 // Actual must match expected to within the (experimentally) |
| 274 // determined threshold. | 186 // determined threshold. |
| 275 var message = ""; | 187 var message = ''; |
| 276 if (waveOptions.disableNormalization != undefined) | 188 if (waveOptions.disableNormalization != undefined) |
| 277 message = "disableNormalization: " + waveOptions.disableNormalizatio
n; | 189 message = |
| 190 'disableNormalization: ' + waveOptions.disableNormalization; |
| 278 if (waveOptions.real) { | 191 if (waveOptions.real) { |
| 279 if (message.length > 0) | 192 if (message.length > 0) |
| 280 message += ", " | 193 message += ', ' |
| 281 message += "real: [" + waveOptions.real + "]"; | 194 message += 'real: [' + waveOptions.real + ']'; |
| 282 } | 195 } |
| 283 if (waveOptions.imag) { | 196 if (waveOptions.imag) { |
| 284 if (message.length > 0) | 197 if (message.length > 0) |
| 285 message += ", " | 198 message += ', ' |
| 286 message += "imag: [" + waveOptions.imag + "]"; | 199 message += 'imag: [' + waveOptions.imag + ']'; |
| 287 } | 200 } |
| 288 Should("Oscillator with periodicWave {" + message + "}", actual) | 201 should(actual, 'Oscillator with periodicWave {' + message + '}') |
| 289 .beCloseToArray(expected, threshold); | 202 .beCloseToArray(expected, {absoluteThreshold: threshold}); |
| 290 }); | 203 }); |
| 291 } | 204 } |
| 292 | 205 |
| 293 function sineWaveTest(waveFun, message) { | 206 // Verify that the default PeriodicWave produces a sine wave. Use a |
| 294 // Verify that the default PeriodicWave produces a sine wave. Use a | 207 // 2-channel context to verify this. |
| 295 // 2-channel context to verify this. Channel 0 is the output from the | 208 function sineWaveTest(should, waveFun, message) { |
| 296 // PeriodicWave, and channel 1 is the reference oscillator output. | 209 // Channel 0 is the output from the PeriodicWave, and channel 1 is the |
| 210 // reference oscillator output. |
| 297 let context = new OfflineAudioContext(2, 40000, 40000); | 211 let context = new OfflineAudioContext(2, 40000, 40000); |
| 298 let oscRef = | 212 let oscRef = |
| 299 new OscillatorNode(context, {type: 'sine', frequency: 440}); | 213 new OscillatorNode(context, {type: 'sine', frequency: 440}); |
| 300 let wave = waveFun(context); | 214 let wave = waveFun(context); |
| 301 let oscTest = | 215 let oscTest = |
| 302 new OscillatorNode(context, {frequency: 440, periodicWave: wave}); | 216 new OscillatorNode(context, {frequency: 440, periodicWave: wave}); |
| 303 | 217 |
| 304 let merger = new ChannelMergerNode(context, {numberOfInputs: 2}); | 218 let merger = new ChannelMergerNode(context, {numberOfInputs: 2}); |
| 305 | 219 |
| 306 oscRef.connect(merger, 0, 1); | 220 oscRef.connect(merger, 0, 1); |
| 307 oscTest.connect(merger, 0, 0); | 221 oscTest.connect(merger, 0, 0); |
| 308 | 222 |
| 309 merger.connect(context.destination); | 223 merger.connect(context.destination); |
| 310 | 224 |
| 311 oscRef.start(); | 225 oscRef.start(); |
| 312 oscTest.start(); | 226 oscTest.start(); |
| 313 | 227 |
| 314 return context.startRendering().then(output => { | 228 return context.startRendering().then(output => { |
| 315 // The output from the two channels MUST match exactly. | 229 // The output from the two channels MUST match exactly. |
| 316 let actual = output.getChannelData(0); | 230 let actual = output.getChannelData(0); |
| 317 let ref = output.getChannelData(1); | 231 let ref = output.getChannelData(1); |
| 318 | 232 |
| 319 Should(message, actual).beEqualToArray(ref); | 233 should(actual, message).beEqualToArray(ref); |
| 320 }); | 234 }); |
| 321 } | 235 } |
| 322 | 236 |
| 323 audit.defineTask('default wave', function(taskDone) { | 237 audit.define('default wave', (task, should) => { |
| 324 // Verify that the default PeriodicWave produces a sine wave. | 238 // Verify that the default PeriodicWave produces a sine wave. |
| 325 sineWaveTest( | 239 sineWaveTest( |
| 326 (context) => new PeriodicWave(context), | 240 should, (context) => new PeriodicWave(context), |
| 327 'new PeriodicWave(context) output') | 241 'new PeriodicWave(context) output') |
| 328 .then(taskDone); | 242 .then(() => task.done()); |
| 329 }); | 243 }); |
| 330 | 244 |
| 331 audit.defineTask('default wave (with dict)', function(taskDone) { | 245 audit.define('default wave (with dict)', (task, should) => { |
| 332 // Verify that the default PeriodicWave produces a sine wave when the | 246 // Verify that the default PeriodicWave produces a sine wave when the |
| 333 // PeriodicWaveOptions dictionary is given, but real and imag members | 247 // PeriodicWaveOptions dictionary is given, but real and imag members |
| 334 // are not set. | 248 // are not set. |
| 335 sineWaveTest( | 249 sineWaveTest( |
| 336 (context) => new PeriodicWave(context, {foo: 42}), | 250 should, (context) => new PeriodicWave(context, {foo: 42}), |
| 337 'new PeriodicWave(context, {foo: 42}) output') | 251 'new PeriodicWave(context, {foo: 42}) output') |
| 338 .then(taskDone); | 252 .then(() => task.done()); |
| 339 }); | 253 }); |
| 340 | 254 |
| 341 audit.runTasks(); | 255 audit.run(); |
| 342 </script> | 256 </script> |
| 343 </body> | 257 </body> |
| 344 </html> | 258 </html> |
| OLD | NEW |