fixed a lot of warnings shown up by compiling with clang in xcode5
[wdl/wdl-ol.git] / WDL / sinewavegen.h
blob7e8739d514677450ac3aff564b46264cb6a2c1a9
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
13 double m_lastfreq;
14 double m_mul1,m_mul2;
15 double m_pos,m_vel;
17 public:
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
28 if (m_lastfreq<=0.0)
30 m_pos=0.0;
31 m_vel = 1.0/freq;
33 else
35 if (freq==m_lastfreq) return;
36 m_vel *= m_lastfreq/freq;
38 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
47 double rv=m_pos;
48 m_vel -= rv + (m_pos = rv*m_mul1 + m_vel*m_mul2);
49 return rv;
52 double GetNextCos() // call BEFORE Gen() if you want the cosine of the next value
54 return m_vel * m_lastfreq;
59 #endif