1 #ifndef _WDL_SINEWAVEGEN_H_
2 #define _WDL_SINEWAVEGEN_H_
5 // note: calling new WDL_SineWaveGenerator isnt strictly necessary, you can also do WDL_SineWaveGenerator *gens = (WDL_SineWaveGenerator *)malloc(512*sizeof(WDL_SineWaveGenerator));
6 // as long as you call Reset() and SetFreq() it should be fine.
9 // note: won't really work for high frequencies...
11 class WDL_SineWaveGenerator
18 WDL_SineWaveGenerator() { Reset(); m_mul1
=m_mul2
=m_pos
=m_vel
=0.0; }
19 ~WDL_SineWaveGenerator() { }
21 void Reset() { m_lastfreq
=0.0; } // must call this before anything
23 void SetFreq(double freq
) // be sure to call this before calling Gen(), or on freq change, or after a Reset()
24 // freq is frequency/(samplerate*0.5) (so 0..1 is valid, though over 0.3 is probably not a good idea)
26 freq
*=3.1415926535897932384626433832795; // scale to freq*PI
35 if (freq
==m_lastfreq
) return;
36 m_vel
*= m_lastfreq
/freq
;
40 double tmp2
= 1.0/(1.0+(freq
*=freq
));
41 m_mul1
= (1.0-freq
)*tmp2
;
42 m_mul2
= freq
*2.0*tmp2
;
45 double Gen() // returns sine
48 m_vel
-= rv
+ (m_pos
= rv
*m_mul1
+ m_vel
*m_mul2
);
52 double GetNextCos() // call BEFORE Gen() if you want the cosine of the next value
54 return m_vel
* m_lastfreq
;