Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!doctype html> | 1 <!doctype html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <script src="../../resources/testharness.js"></script> | 4 <script src="../../resources/testharness.js"></script> |
| 5 <script src="../../resources/testharnessreport.js"></script> | 5 <script src="../../resources/testharnessreport.js"></script> |
| 6 <script src="../resources/audit-util.js"></script> | 6 <script src="../resources/audit-util.js"></script> |
| 7 <script src="../resources/audio-testing.js"></script> | 7 <script src="../resources/audit.js"></script> |
| 8 <title>Test OscillatorNode with Negative Frequency</title> | 8 <title>Test OscillatorNode with Negative Frequency</title> |
| 9 </head> | 9 </head> |
| 10 | 10 |
| 11 <body> | 11 <body> |
| 12 <script> | 12 <script> |
| 13 // Some arbitrary sample rate for the offline context. But it MUST be | 13 // Some arbitrary sample rate for the offline context. But it MUST be |
| 14 // at least twice the oscillator frequency that we're using for the | 14 // at least twice the oscillator frequency that we're using for the |
| 15 // test. (Currently 440 Hz.) | 15 // test. (Currently 440 Hz.) |
| 16 var sampleRate = 16000; | 16 var sampleRate = 16000; |
| 17 | 17 |
| 18 // A fairly arbitrary duration that should have at least 1-2 sample | 18 // A fairly arbitrary duration that should have at least 1-2 sample |
| 19 // periods of the oscillator (at a nominal 440 Hz). | 19 // periods of the oscillator (at a nominal 440 Hz). |
| 20 var renderDuration = 0.1; | 20 var renderDuration = 0.1; |
| 21 var renderFrames = renderDuration * sampleRate; | 21 var renderFrames = renderDuration * sampleRate; |
| 22 | 22 |
| 23 var audit = Audit.createTaskRunner(); | 23 var audit = Audit.createTaskRunner(); |
| 24 | 24 |
| 25 audit.defineTask("sine", function (done) { | 25 audit.define("sine", (task, should) => { |
| 26 runTest({ | 26 runTest(should, { |
| 27 message: "Sum of positive and negative frequency sine oscillators", | 27 message: "Sum of positive and negative frequency sine oscillators", |
| 28 type: "sine", | 28 type: "sine", |
| 29 threshold: 3.5763e-7 | 29 threshold: 3.5763e-7 |
| 30 }).then(done); | 30 }).then(() => task.done()); |
| 31 }); | 31 }); |
| 32 | 32 |
| 33 audit.defineTask("square", function (done) { | 33 audit.define("square", (task, should) => { |
| 34 runTest({ | 34 runTest(should, { |
| 35 message: "Sum of positive and negative frequency square oscillators", | 35 message: "Sum of positive and negative frequency square oscillators", |
| 36 type: "square", | 36 type: "square", |
| 37 threshold: 1.4753e-6 | 37 threshold: 1.4753e-6 |
| 38 }).then(done); | 38 }).then(() => task.done()); |
| 39 }); | 39 }); |
| 40 | 40 |
| 41 audit.defineTask("sawtooth", function (done) { | 41 audit.define("sawtooth", (task, should) => { |
| 42 runTest({ | 42 runTest(should, { |
| 43 message: "Sum of positive and negative frequency sawtooth oscillators" , | 43 message: "Sum of positive and negative frequency sawtooth oscillators" , |
| 44 type: "sawtooth", | 44 type: "sawtooth", |
| 45 threshold: 1.4753e-6 | 45 threshold: 1.4753e-6 |
| 46 }).then(done); | 46 }).then(() => task.done()); |
| 47 }); | 47 }); |
| 48 | 48 |
| 49 audit.defineTask("triangle", function (done) { | 49 audit.define("triangle", (task, should) => { |
| 50 runTest({ | 50 runTest(should, { |
| 51 message: "Sum of positive and negative frequency triangle oscillators" , | 51 message: "Sum of positive and negative frequency triangle oscillators" , |
| 52 type: "triangle", | 52 type: "triangle", |
| 53 threshold: 2.9803e-7 | 53 threshold: 2.9803e-7 |
| 54 }).then(done); | 54 }).then(() => task.done()); |
| 55 }); | 55 }); |
| 56 | 56 |
| 57 audit.defineTask("auto-sawtooth", function (done) { | 57 audit.define("auto-sawtooth", (task, should) => { |
| 58 runTest({ | 58 runTest(should, { |
| 59 message: "Sum of positive and negative frequency-ramped sawtooth oscil lators", | 59 message: "Sum of positive and negative frequency-ramped sawtooth oscil lators", |
| 60 type: "sawtooth", | 60 type: "sawtooth", |
| 61 automation: { | 61 automation: { |
| 62 type: "linearRampToValueAtTime", | 62 type: "linearRampToValueAtTime", |
| 63 startTime: 0, | 63 startTime: 0, |
| 64 endTime: renderDuration / 2, | 64 endTime: renderDuration / 2, |
| 65 startFrequency: 440, | 65 startFrequency: 440, |
| 66 endFrequency: sampleRate / 4 | 66 endFrequency: sampleRate / 4 |
| 67 }, | 67 }, |
| 68 threshold: 1.2368e-6 | 68 threshold: 1.2368e-6 |
| 69 }).then(done); | 69 }).then(() => task.done()); |
| 70 }); | 70 }); |
| 71 | 71 |
| 72 audit.defineTask("periodic-wave", function (done) { | 72 audit.define("periodic-wave", (task, should) => { |
| 73 // Test negative frequencies for a custom oscillator. Two channels are | 73 // Test negative frequencies for a custom oscillator. Two channels are |
| 74 // needed for the context; one for the expected result, and one for the | 74 // needed for the context; one for the expected result, and one for the |
| 75 // actual, as explained below. | 75 // actual, as explained below. |
| 76 var context = new OfflineAudioContext(2, renderFrames, sampleRate); | 76 var context = new OfflineAudioContext(2, renderFrames, sampleRate); |
| 77 | 77 |
| 78 var oscPositive = context.createOscillator(); | 78 var oscPositive = context.createOscillator(); |
| 79 var oscNegative = context.createOscillator(); | 79 var oscNegative = context.createOscillator(); |
| 80 | 80 |
| 81 // The Fourier coefficients for our custom oscillator. The actual value s | 81 // The Fourier coefficients for our custom oscillator. The actual value s |
| 82 // not important. The waveform for our custom oscillator is | 82 // not important. The waveform for our custom oscillator is |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 | 132 |
| 133 merger.connect(context.destination); | 133 merger.connect(context.destination); |
| 134 | 134 |
| 135 oscPositive.start(); | 135 oscPositive.start(); |
| 136 oscNegative.start(); | 136 oscNegative.start(); |
| 137 | 137 |
| 138 context.startRendering().then(function (buffer) { | 138 context.startRendering().then(function (buffer) { |
| 139 var expected = buffer.getChannelData(0); | 139 var expected = buffer.getChannelData(0); |
| 140 var actual = buffer.getChannelData(1); | 140 var actual = buffer.getChannelData(1); |
| 141 | 141 |
| 142 Should("Sum of positive and negative frequency custom oscillators", | 142 should(actual, |
| 143 actual, { | 143 "Sum of positive and negative frequency custom oscillators") |
| 144 precision: 6 | 144 .beCloseToArray(expected, { |
| 145 }) | 145 absoluteThreshold: 3.5763e-7 |
| 146 .beCloseToArray(expected, 3.5763e-7); | 146 }); |
| 147 }).then(done); | 147 }).then(() => task.done()); |
| 148 }); | 148 }); |
| 149 | 149 |
| 150 audit.runTasks(); | 150 audit.run(); |
| 151 | 151 |
| 152 function runTest(options) { | 152 function runTest(should, options) { |
| 153 // To test if negative frequencies work, create two oscillators. One | 153 // To test if negative frequencies work, create two oscillators. One |
| 154 // has a positive frequency and the other has a negative frequency. | 154 // has a positive frequency and the other has a negative frequency. |
| 155 // Sum the oscillator outputs; the output should be zero because all of | 155 // Sum the oscillator outputs; the output should be zero because all of |
| 156 // the builtin oscillator types are odd functions of frequency. | 156 // the builtin oscillator types are odd functions of frequency. |
| 157 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | 157 var context = new OfflineAudioContext(1, renderFrames, sampleRate); |
| 158 | 158 |
| 159 var oscPositive = context.createOscillator(); | 159 var oscPositive = context.createOscillator(); |
| 160 var oscNegative = context.createOscillator(); | 160 var oscNegative = context.createOscillator(); |
| 161 | 161 |
| 162 oscPositive.type = options.type; | 162 oscPositive.type = options.type; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 178 oscNegative.connect(context.destination); | 178 oscNegative.connect(context.destination); |
| 179 | 179 |
| 180 oscPositive.start(); | 180 oscPositive.start(); |
| 181 oscNegative.start(); | 181 oscNegative.start(); |
| 182 | 182 |
| 183 return context.startRendering().then(function (buffer) { | 183 return context.startRendering().then(function (buffer) { |
| 184 var result = buffer.getChannelData(0); | 184 var result = buffer.getChannelData(0); |
| 185 | 185 |
| 186 var zero = new Float32Array(result.length); | 186 var zero = new Float32Array(result.length); |
| 187 zero.fill(0); | 187 zero.fill(0); |
| 188 Should(options.message, result, { | 188 should(result, options.message) |
| 189 verbose: true | 189 .beCloseToArray(zero, { |
|
hongchan
2017/04/19 15:45:10
|zero| could be |new Float32Array(result.length).
| |
| 190 }).beCloseToArray(zero, options.threshold || 0); | 190 absoluteThreshold: options.threshold || 0 |
| 191 }); | |
| 191 }); | 192 }); |
| 192 } | 193 } |
| 193 </script> | 194 </script> |
| 194 </body> | 195 </body> |
| 195 </html> | 196 </html> |
| OLD | NEW |