Bug 952860 - Leaky integrate sawtooth and square waves in the OscillatorNode to prevent bad DC offset when changing frequency. r=karlt

--HG--
extra : rebase_source : f526506e80a4d7db553fe54cc115d8bf1c7be9d2
This commit is contained in:
Paul Adenot 2014-01-10 12:40:05 +01:00
parent a4a302efbb
commit 676bc4620d

View File

@ -23,7 +23,8 @@ NS_INTERFACE_MAP_END_INHERITING(AudioNode)
NS_IMPL_ADDREF_INHERITED(OscillatorNode, AudioNode)
NS_IMPL_RELEASE_INHERITED(OscillatorNode, AudioNode)
static const float sLeak = 0.995f;
static const float sLeakTriangle = 0.995f;
static const float sLeak = 0.999f;
class DCBlocker
{
@ -320,7 +321,7 @@ public:
UpdateParametersIfNeeded(ticks, i);
// Integration to get us a square. It turns out we can have a
// pure integrator here.
mSquare += BipolarBLIT();
mSquare = mSquare * sLeak + BipolarBLIT();
aOutput[i] = mSquare;
// maybe we want to apply a gain, the wg has not decided yet
aOutput[i] *= 1.5;
@ -337,7 +338,7 @@ public:
dcoffset = mFinalFrequency / mSource->SampleRate();
// Integrate and offset so we get mAmplitudeAtZero sawtooth. We have a
// very low frequency component somewhere here, but I'm not sure where.
mSaw += UnipolarBLIT() - dcoffset;
mSaw = mSaw * sLeak + (UnipolarBLIT() - dcoffset);
// reverse the saw so we are spec compliant
aOutput[i] = -mSaw * 1.5;
@ -356,7 +357,7 @@ public:
// C6 = k0 / period
// (period is samplingrate / frequency, k0 = (PI/2)/(2*PI)) = 0.25
float C6 = 0.25 / (mSource->SampleRate() / mFinalFrequency);
mTriangle = mTriangle * sLeak + mSquare + C6;
mTriangle = mTriangle * sLeakTriangle + mSquare + C6;
// DC Block, and scale back to [-1.0; 1.0]
aOutput[i] = mDCBlocker.Process(mTriangle) / (mSignalPeriod/2) * 1.5;