Hi,
i created a basic wavetable synthesizer that gets it's input over midi and outputs through a single PWM pin.
I have a problem now:
Everytime a note starts to play, i hear a "click"-noise. However this only happens when there is a pause between the new an the old note. If two notes are exactly aligned behind each other in the midi, there is no click. So i guess the "click" thing has something to do with the PWM going "LOW" in the pause between two notes.
But then again, if there are 2 or more notes playing at the same time (i.e. bassline and strings) and there is a pause in for example the bassline channel, the click also appears ...so the PWM-pin-going-low-thing might not be it after all.
Here's the relevant code
static int32_t sum;
for(int i = 0; i < NUM_CHANNELS; i++)
{
channel[i].phase += channel[i].speed; //increase phase by note frequency
//if(channel[i].phase >= 65536) channel[i].phase -= 65536;
uint16_t waveTableIndex = ( channel[i].phase >> 8 ); //get wavetable index
sum += ( wavetable[3][waveTableIndex] * channel[i].volume * channel[i].volumeMax ); //get sample, apply ADSR and master volume
}
//get rid of that "click" on note start and end...WHY DOES THIS EVEN HAPPEN!? GRRRR
//sum = syn_iSmooth32(audioBuffer, sum, 0.925f); //results in a pseudo low pass....meh
//audioBuffer = sum;
sum >>= 8;
OC1RS = sum;
wavetable
uint8_t wavetable[4][256] = {
{ //square
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //32
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //32
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //64
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //64
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //32
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //32
},
{}, //sawtooth
{},//triangle
{ //sine
127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,
176,179,182,184,187,190,193,195,198,200,203,205,208,210,213,215,
217,219,221,224,226,228,229,231,233,235,236,238,239,241,242,244,
245,246,247,248,249,250,251,251,252,253,253,254,254,254,254,254,
255,254,254,254,254,254,253,253,252,251,251,250,249,248,247,246,
245,244,242,241,239,238,236,235,233,231,229,228,226,224,221,219,
217,215,213,210,208,205,203,200,198,195,193,190,187,184,182,179,
176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,
127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,
78,75,72,70,67,64,61,59,56,54,51,49,46,44,41,39,
37,35,33,30,28,26,25,23,21,19,18,16,15,13,12,10,
9,8,7,6,5,4,3,3,2,1,1,0,0,0,0,0,
0,0,0,0,0,0,1,1,2,3,3,4,5,6,7,8,
9,10,12,13,15,16,18,19,21,23,25,26,28,30,33,35,
37,39,41,44,46,49,51,54,56,59,61,64,67,70,72,75,
78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124
}
};
void wavetable_generateSawtooth(int inTable)
{
for (int i = 0; i < 256; ++i) {
wavetable[inTable][i] = i; // sawtooth
}
}
void wavetable_generateInverseSawtooth(int inTable)
{
for (int i = 0; i < 256; ++i) {
wavetable[inTable][i] = 255-i; // sawtooth
}
}
void wavetable_generateTriangle(int inTable)
{
for (int i = 0; i < 128; ++i) {
wavetable[inTable][i] = i * 2;
}
int value = 255;
for (int i = 128; i < 256; ++i) {
wavetable[inTable][i] = value;
value -= 2;
}
}
void SetupWavetable()
{
wavetable_generateSawtooth(1);
wavetable_generateTriangle(2);
}
Anyone here who had similar problems when playing a tone via pwm?
This i really driving me nuts.