Hi all,
I'm pretty (read: very) new to Maple, and thought I would start off with a little audio synthesis...
Anyway, first project is simple wavetable oscillator, I've written code, I have it all programmed so it works, and I can change the frequency...
But, there is a problem... Though I can get it to do this, the board doesn't seem to want to go audio rate, which from the documentation it should. The code is programmed at the moment so that at high frequencies I'm skipping out entries on the wave table.
The code I'm running is as such, with the PWM output going to a Rugged Circuits Audiovox shield. The board is a rev.5.
float sinTable[256] = {0,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,1.22465E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674,-2.44929E-16,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,3.67394E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674};
float cosTable[256] = {0,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,1.22465E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674,-2.44929E-16,0.049067674,0.09801714,0.146730474,0.195090322,0.24298018,0.290284677,0.336889853,0.382683432,0.427555093,0.471396737,0.514102744,0.555570233,0.595699304,0.634393284,0.671558955,0.707106781,0.740951125,0.773010453,0.803207531,0.831469612,0.85772861,0.881921264,0.903989293,0.923879533,0.941544065,0.956940336,0.970031253,0.98078528,0.98917651,0.995184727,0.998795456,1,0.998795456,0.995184727,0.98917651,0.98078528,0.970031253,0.956940336,0.941544065,0.923879533,0.903989293,0.881921264,0.85772861,0.831469612,0.803207531,0.773010453,0.740951125,0.707106781,0.671558955,0.634393284,0.595699304,0.555570233,0.514102744,0.471396737,0.427555093,0.382683432,0.336889853,0.290284677,0.24298018,0.195090322,0.146730474,0.09801714,0.049067674,3.67394E-16,-0.049067674,-0.09801714,-0.146730474,-0.195090322,-0.24298018,-0.290284677,-0.336889853,-0.382683432,-0.427555093,-0.471396737,-0.514102744,-0.555570233,-0.595699304,-0.634393284,-0.671558955,-0.707106781,-0.740951125,-0.773010453,-0.803207531,-0.831469612,-0.85772861,-0.881921264,-0.903989293,-0.923879533,-0.941544065,-0.956940336,-0.970031253,-0.98078528,-0.98917651,-0.995184727,-0.998795456,-1,-0.998795456,-0.995184727,-0.98917651,-0.98078528,-0.970031253,-0.956940336,-0.941544065,-0.923879533,-0.903989293,-0.881921264,-0.85772861,-0.831469612,-0.803207531,-0.773010453,-0.740951125,-0.707106781,-0.671558955,-0.634393284,-0.595699304,-0.555570233,-0.514102744,-0.471396737,-0.427555093,-0.382683432,-0.336889853,-0.290284677,-0.24298018,-0.195090322,-0.146730474,-0.09801714,-0.049067674};
float oscOutput;
float i = 0.0;
float noteFreq = 200;
int bits = 8;
float steps = float(pow(2,bits));
int halfpwmRes = 65536/2;
int waveTable = 1;
void setup(){
}
void loop(){
noteFreq = (analogRead(15)+25)*2;
if(noteFreq==0){
noteFreq=1;
}
//Cycles scanning oscillator from 0 to 'steps'
if(i>=steps){
while(i>=steps){
i=i-steps;
}
}
else{
pwmWrite(9,(i*steps)-1);
// i=i+((steps/noteFreq)*100);
i=i+((0.00943016759776536*noteFreq)+0.528491620111732); //Allows stepping through wavetables at various speeds defined by potentiometer
}
//Selects wavetable, also uses scanning oscillator to look up value in wavetable
switch (waveTable){
case 1:
oscOutput=sinTable[int(i)];
break;
case 2:
oscOutput=cosTable[int(i)];
break;
default:
oscOutput=sinTable[int(i)];
break;
}
//Outputs using pwm, scaled to fit between 0 and PWM resolution
pwmWrite(9,(int((oscOutput+1)*halfpwmRes)));
//Debug via serial
SerialUSB.print("Frequency = ");
SerialUSB.print(noteFreq);
SerialUSB.print(" ScanOscVal = ");
SerialUSB.print(i);
SerialUSB.print(" Output + ");
SerialUSB.print((oscOutput+1)*halfpwmRes);
SerialUSB.print("\r\n");
Any ideas greatly appreciated!
Andrew