Hi, I am trying to make a DDS synthesizer with Arduino using direct digital synthesis with wavetable compression.
Compression works by storing just the first 90° of the sine wave (see here for details).
Anyway, I have some issues with the performance of the timer interrupt code. If I play the uncompressed wave the produced sound is smooth, but the compressed wave sounds like if the sampling rate were much slower.
I guess this is due to the more expensive operations I use to "unpack" the wave. This is the code to obtain the wave sample:
compressionSwitch = phaccu >> 30; //upper 2 bits are the compression switch (on which part of the sinewave are we)
compIcnt = phaccu >> 22;
cbi(compIcnt, 9);
cbi(compIcnt, 10);
icnt = compIcnt;
switch(compressionSwitch) {
case 0: //0° to 90°
waveSample = pgm_read_byte_near(sine + icnt);
break;
case 1: //90° to 180°
waveSample = pgm_read_byte_near(sine + LENGTH - icnt);
break;
case 2: //180° to 270°
waveSample = pgm_read_byte_near(sine + icnt) - OFFSET;
break;
case 3: //270° to 360°
waveSample = pgm_read_byte_near(sine + LENGTH - icnt) - OFFSET;
}
I am relative new to C/C++, but I don't think that Sum, bit shifts and bit set are expensive operations. Or am I wrong?
Do you see something wrong with this code?
I attached the whole sketch too.
Thanks
dds.cpp (5.98 KB)