OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <title>Test Sampling of Oscillator Start Times</title> |
| 5 <script src="../../resources/testharness.js"></script> |
| 6 <script src="../../resources/testharnessreport.js"></script> |
| 7 <script src="../resources/audit-util.js"></script> |
| 8 <script src="../resources/audit.js"></script> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 // Experimentation indicates that this sample rate with a 440 Hz |
| 14 // oscillator makes for a large difference in the difference signal if the |
| 15 // oscillator start isn't sampled correctly. |
| 16 let defaultSampleRate = 24000; |
| 17 let renderDuration = 1; |
| 18 let renderFrames = renderDuration * defaultSampleRate; |
| 19 |
| 20 let audit = Audit.createTaskRunner(); |
| 21 |
| 22 audit.define("basic test small", function (task, should) { |
| 23 task.describe("Start oscillator slightly past a sample frame") |
| 24 testStartSampling(should, 1.25, { |
| 25 error: 1.0842e-4, |
| 26 snrThreshold: 84.054 |
| 27 }) |
| 28 .then(task.done.bind(task)); |
| 29 }); |
| 30 |
| 31 audit.define("basic test big", function (task, should) { |
| 32 task.describe("Start oscillator slightly before a sample frame") |
| 33 testStartSampling(should, 1.75, { |
| 34 error: 1.0839e-4, |
| 35 snrThreshold: 84.056 |
| 36 }) |
| 37 .then(task.done.bind(task)); |
| 38 }); |
| 39 |
| 40 audit.define("diff big offset", function (task, should) { |
| 41 task.describe( |
| 42 "Test sampling with start offset greater than 1/2 sampling frame" |
| 43 ); |
| 44 // With a sample rate of 24000 Hz, and an oscillator frequency of 440 Hz |
| 45 // (the default), a quarter wave delay is 13.636363... frames. This |
| 46 // tests the case where the starting time is more than 1/2 frame from |
| 47 // the preceding sampling frame. This tests one path of the internal |
| 48 // implementation. |
| 49 testStartWithGain(should, defaultSampleRate, { |
| 50 error: 4.1724e-7, |
| 51 snrThreshold: 137.536 |
| 52 }) |
| 53 .then(task.done.bind(task)); |
| 54 }); |
| 55 |
| 56 audit.define("diff small offset", function (task, should) { |
| 57 task.describe( |
| 58 "Test sampling with start offset less than 1/2 sampling frame"); |
| 59 // With a sample rate of 48000 Hz, and an oscillator frequency of 440 Hz |
| 60 // (the default), a quarter wave delay is 27.2727... frames. This tests |
| 61 // the case where the starting time is less than 1/2 frame from the |
| 62 // preceding sampling frame. This tests one path of the internal |
| 63 // implementation. |
| 64 testStartWithGain(should, 48000, { |
| 65 error: 4.1724e-7, |
| 66 snrThreshold: 137.536 |
| 67 }) |
| 68 .then(task.done.bind(task)); |
| 69 }); |
| 70 |
| 71 function testStartSampling(should, startFrame, thresholds) { |
| 72 // Start the oscillator in the middle of a sample frame and compare |
| 73 // against the theoretical result. |
| 74 let context = new OfflineAudioContext(1, renderFrames, |
| 75 defaultSampleRate); |
| 76 let osc = context.createOscillator(); |
| 77 osc.connect(context.destination); |
| 78 osc.start(startFrame / context.sampleRate); |
| 79 |
| 80 return context.startRendering().then(function (result) { |
| 81 let actual = result.getChannelData(0); |
| 82 let expected = new Array(actual.length); |
| 83 expected.fill(0); |
| 84 |
| 85 // The expected curve is |
| 86 // |
| 87 // sin(2*pi*f*(t-t0)) |
| 88 // |
| 89 // where f is the oscillator frequency and t0 is the start time. |
| 90 let actualStart = Math.ceil(startFrame); |
| 91 let omega = 2 * Math.PI * osc.frequency.value / context.sampleRate; |
| 92 for (let k = actualStart; k < actual.length; ++k) { |
| 93 expected[k] = Math.sin(omega * (k - startFrame)); |
| 94 } |
| 95 |
| 96 should(actual, "Oscillator.start(" + startFrame + " frames)") |
| 97 .beCloseToArray(expected, { |
| 98 absoluteThreshold: thresholds.error |
| 99 }); |
| 100 let snr = 10 * Math.log10(computeSNR(actual, expected)); |
| 101 should(snr, "SNR (dB)") |
| 102 .beGreaterThanOrEqualTo(thresholds.snrThreshold); |
| 103 }) |
| 104 } |
| 105 |
| 106 function testStartWithGain(should, sampleRate, thresholds) { |
| 107 // Test consists of starting a cosine wave with a quarter wavelength |
| 108 // delay and comparing that with a sine wave that has the initial |
| 109 // quarter wavelength zeroed out. These should be equal. |
| 110 |
| 111 let context = new OfflineAudioContext(3, renderFrames, sampleRate); |
| 112 let osc = context.createOscillator(); |
| 113 |
| 114 let merger = context.createChannelMerger(3); |
| 115 merger.connect(context.destination); |
| 116 |
| 117 // Start the cosine oscillator at this time. This means the wave starts |
| 118 // at frame 13.636363.... |
| 119 let quarterWaveTime = (1 / 4) / osc.frequency.value; |
| 120 |
| 121 // Sine wave oscillator with gain term to zero out the initial quarter |
| 122 // wave length of the output. |
| 123 let g = context.createGain(); |
| 124 g.gain.setValueAtTime(0, 0); |
| 125 g.gain.setValueAtTime(1, quarterWaveTime); |
| 126 osc.connect(g); |
| 127 g.connect(merger, 0, 2); |
| 128 g.connect(merger, 0, 0); |
| 129 |
| 130 // Cosine wave oscillator with starting after a quarter wave length. |
| 131 let osc2 = context.createOscillator(); |
| 132 // Creates a cosine wave. |
| 133 let wave = context.createPeriodicWave( |
| 134 Float32Array.from([0, 1]), |
| 135 Float32Array.from([0, 0])); |
| 136 osc2.setPeriodicWave(wave); |
| 137 |
| 138 osc2.connect(merger, 0, 1); |
| 139 |
| 140 // A gain inverter so subtract the two waveforms. |
| 141 let inverter = context.createGain(); |
| 142 inverter.gain.value = -1; |
| 143 osc2.connect(inverter); |
| 144 inverter.connect(merger, 0, 0); |
| 145 |
| 146 osc.start(); |
| 147 osc2.start(quarterWaveTime); |
| 148 |
| 149 return context.startRendering().then(function (result) { |
| 150 // Channel 0 = diff |
| 151 // Channel 1 = osc with start |
| 152 // Channel 2 = osc with gain |
| 153 |
| 154 // Channel 0 should be very close to 0. |
| 155 // Channel 1 should match channel 2 very closely. |
| 156 let diff = result.getChannelData(0); |
| 157 let oscStart = result.getChannelData(1); |
| 158 let oscGain = result.getChannelData(2); |
| 159 let snr = 10 * Math.log10(computeSNR(oscStart, oscGain)); |
| 160 |
| 161 should(oscStart, "Delayed cosine oscillator at sample rate " + sampleR
ate) |
| 162 .beCloseToArray(oscGain, { |
| 163 absoluteThreshold: thresholds.error |
| 164 }); |
| 165 should(snr, "SNR (dB)") |
| 166 .beGreaterThanOrEqualTo(thresholds.snrThreshold); |
| 167 }); |
| 168 } |
| 169 |
| 170 audit.run(); |
| 171 </script> |
| 172 </body> |
| 173 </html> |
OLD | NEW |