| wikiC/2.06 Main page Alphabetic index Recent Edits |
Sine |
login 38.107.179.241 |
// Geinterpoleerde Wavetable osc 4 AudioSDK // Jorn Lemon 31-3-2004 www.twistedlemon.nl
class osc
{
protected:
public:
osc(int samplerate)
sin_osc(float &output, double freq);
sin_LFO(float &output, double freq);
sin_FM(float &output, double freq, float mod_index);
double *wave;
/*static */
double phase, phase1, phase2;
int i;
double rest;
int ndx;
double TWOPIE, STEP;
};
osc::osc(int samplerate)
{
TWOPIE = 8.0*atan(1.0);
STEP = TWOPIE/samplerate;
wave = new float[2049];
phase1, phase = 0.0;
wstep = 2048.0/samplerate;
for(i=0;i<2049;i++) wave[i] = sin(TWOPIE*i/2048.0);
}
osc::sin_LFO(float &output, double freq)
{ // a very simple sinwave oscillator, better for LFO
output = sin(phase);
// increment the phase
phase += freq*STEP;
}
osc::sin_FM(float &output, double freq, float modulator, float mod_index)
{ // FM voice modulator, carrier.
output = sin(phase1 + mod_index*freq*STEP*modulator); // the output FM
// increment the phase
phase1 += freq*STEP;
}
osc::sin_osc(float &output, double freq)
{ // wave table oscilator
phase += freq * wstep;
while(phase >= 2048.0) phase -= 2048.0;
while(phase < 0.0) phase += 2048.0;
ndx = int(phase);
rest = phase - ndx;
output = wave[ndx] + rest*(wave[ndx+1] - wave[ndx]); // interpolate
}