OLD | NEW |
(Empty) | |
| 1 var context; |
| 2 var lengthInSeconds = 2; |
| 3 |
| 4 // Skip this many frames before comparing against reference to allow |
| 5 // a steady-state to be reached in the up-sampling filters. |
| 6 var filterStabilizeSkipFrames = 2048; |
| 7 |
| 8 var numberOfCurveFrames = 65536; |
| 9 var waveShapingCurve; |
| 10 |
| 11 var waveshaper; |
| 12 |
| 13 // FIXME: test at more frequencies. |
| 14 // When using the up-sampling filters (2x, 4x) any significant aliasing componen
ts |
| 15 // should be at very high frequencies near Nyquist. These tests could be improv
ed |
| 16 // to allow for a higher acceptable amount of aliasing near Nyquist, but then |
| 17 // become more stringent for lower frequencies. |
| 18 |
| 19 // These test parameters are set in runWaveShaperOversamplingTest(). |
| 20 var sampleRate; |
| 21 var nyquist; |
| 22 var oversample; |
| 23 var fundamentalFrequency; |
| 24 var acceptableAliasingThresholdDecibels; |
| 25 |
| 26 var kScale = 0.25; |
| 27 |
| 28 // Chebyshev Polynomials. |
| 29 // Given an input sinusoid, returns an output sinusoid of the given frequency mu
ltiple. |
| 30 function T0(x) { return 1; } |
| 31 function T1(x) { return x; } |
| 32 function T2(x) { return 2*x*x - 1; } |
| 33 function T3(x) { return 4*x*x*x - 3*x; } |
| 34 function T4(x) { return 8*x*x*x*x - 8*x*x + 1; } |
| 35 |
| 36 function generateWaveShapingCurve() { |
| 37 var n = 65536; |
| 38 var n2 = n / 2; |
| 39 var curve = new Float32Array(n); |
| 40 |
| 41 // The shaping curve uses Chebyshev Polynomial such that an input sinusoid |
| 42 // at frequency f will generate an output of four sinusoids of frequencies: |
| 43 // f, 2*f, 3*f, 4*f |
| 44 // each of which is scaled. |
| 45 for (var i = 0; i < n; ++i) { |
| 46 var x = (i - n2) / n2; |
| 47 var y = kScale * (T1(x) + T2(x) + T3(x) + T4(x)); |
| 48 curve[i] = y; |
| 49 } |
| 50 |
| 51 return curve; |
| 52 } |
| 53 |
| 54 function checkShapedCurve(event) { |
| 55 var buffer = event.renderedBuffer; |
| 56 |
| 57 var outputData = buffer.getChannelData(0); |
| 58 var n = buffer.length; |
| 59 |
| 60 // The WaveShaperNode will have a processing latency if oversampling is used
, |
| 61 // so we should account for it. |
| 62 |
| 63 // FIXME: .latency should be exposed as an attribute of the node |
| 64 // var waveShaperLatencyFrames = waveshaper.latency * sampleRate; |
| 65 // But for now we'll use the hard-coded values corresponding to the actual l
atencies: |
| 66 var waveShaperLatencyFrames = 0; |
| 67 if (oversample == "2x") |
| 68 waveShaperLatencyFrames = 128; |
| 69 else if (oversample == "4x") |
| 70 waveShaperLatencyFrames = 192; |
| 71 |
| 72 var waveShaperLatencyFrames = waveshaper.latency * sampleRate; |
| 73 |
| 74 var worstDeltaInDecibels = -1000; |
| 75 |
| 76 for (var i = waveShaperLatencyFrames; i < n; ++i) { |
| 77 var actual = outputData[i]; |
| 78 |
| 79 // Account for the expected processing latency. |
| 80 var j = i - waveShaperLatencyFrames; |
| 81 |
| 82 // Compute reference sinusoids. |
| 83 var phaseInc = 2 * Math.PI * fundamentalFrequency / sampleRate; |
| 84 |
| 85 // Generate an idealized reference based on the four generated frequenci
es truncated |
| 86 // to the Nyquist rate. Ideally, we'd like the waveshaper's oversamplin
g to perfectly |
| 87 // remove all frequencies above Nyquist to avoid aliasing. In reality t
he oversampling filters are not |
| 88 // quite perfect, so there will be a (hopefully small) amount of aliasin
g. We should |
| 89 // be close to the ideal. |
| 90 var reference = 0; |
| 91 |
| 92 // Sum in fundamental frequency. |
| 93 if (fundamentalFrequency < nyquist) |
| 94 reference += Math.sin(phaseInc * j); |
| 95 |
| 96 // Note that the phase of each of the expected generated harmonics is di
fferent. |
| 97 if (fundamentalFrequency * 2 < nyquist) |
| 98 reference += -Math.cos(phaseInc * j * 2); |
| 99 if (fundamentalFrequency * 3 < nyquist) |
| 100 reference += -Math.sin(phaseInc * j * 3); |
| 101 if (fundamentalFrequency * 4 < nyquist) |
| 102 reference += Math.cos(phaseInc * j * 4); |
| 103 |
| 104 // Scale the reference the same as the waveshaping curve itself. |
| 105 reference *= kScale; |
| 106 |
| 107 var delta = Math.abs(actual - reference); |
| 108 var deltaInDecibels = delta > 0 ? 20 * Math.log(delta)/Math.log(10) : -2
00; |
| 109 |
| 110 if (j >= filterStabilizeSkipFrames) { |
| 111 if (deltaInDecibels > worstDeltaInDecibels) { |
| 112 worstDeltaInDecibels = deltaInDecibels; |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 // console.log("worstDeltaInDecibels: " + worstDeltaInDecibels); |
| 118 |
| 119 var success = worstDeltaInDecibels < acceptableAliasingThresholdDecibels; |
| 120 |
| 121 if (success) { |
| 122 testPassed(oversample + " WaveShaperNode oversampling within acceptable
tolerance."); |
| 123 } else { |
| 124 testFailed(oversample + " WaveShaperNode oversampling not within accepta
ble tolerance."); |
| 125 } |
| 126 |
| 127 finishJSTest(); |
| 128 } |
| 129 |
| 130 function createImpulseBuffer(context, sampleFrameLength) { |
| 131 var audioBuffer = context.createBuffer(1, sampleFrameLength, context.sampleR
ate); |
| 132 var n = audioBuffer.length; |
| 133 var dataL = audioBuffer.getChannelData(0); |
| 134 |
| 135 for (var k = 0; k < n; ++k) |
| 136 dataL[k] = 0; |
| 137 |
| 138 dataL[0] = 1; |
| 139 |
| 140 return audioBuffer; |
| 141 } |
| 142 |
| 143 function runWaveShaperOversamplingTest(testParams) { |
| 144 sampleRate = testParams.sampleRate; |
| 145 nyquist = 0.5 * sampleRate; |
| 146 oversample = testParams.oversample; |
| 147 fundamentalFrequency = testParams.fundamentalFrequency; |
| 148 acceptableAliasingThresholdDecibels = testParams.acceptableAliasingThreshold
Decibels; |
| 149 |
| 150 if (window.testRunner) { |
| 151 testRunner.dumpAsText(); |
| 152 testRunner.waitUntilDone(); |
| 153 } |
| 154 |
| 155 window.jsTestIsAsync = true; |
| 156 |
| 157 // Create offline audio context. |
| 158 var numberOfRenderFrames = sampleRate * lengthInSeconds; |
| 159 context = new webkitOfflineAudioContext(1, numberOfRenderFrames, sampleRate)
; |
| 160 |
| 161 // source -> waveshaper -> destination |
| 162 var source = context.createBufferSource(); |
| 163 source.buffer = createToneBuffer(context, fundamentalFrequency, lengthInSeco
nds, 1); |
| 164 |
| 165 // Apply a non-linear distortion curve. |
| 166 waveshaper = context.createWaveShaper(); |
| 167 waveshaper.curve = generateWaveShapingCurve(); |
| 168 waveshaper.oversample = oversample; |
| 169 |
| 170 source.connect(waveshaper); |
| 171 waveshaper.connect(context.destination); |
| 172 |
| 173 source.start(0); |
| 174 |
| 175 context.oncomplete = checkShapedCurve; |
| 176 context.startRendering(); |
| 177 } |
OLD | NEW |