One of the more important things to consider while writing audio code for the Critter Board is how long things take. In most of the examples the sound synthesis happens in an interrupt service routine. This routine is just a function that gets called automatically at the sampling rate. Timing how long this function takes to execute is the best way to measure audio performance.
Download the FM MIDI synth example and take a look at audio.c file which contains the audio interrupt routine:
static void timer0ISR(void)
{
// IOCLR0 |= (1 << 26); // start speed test
/*
here we calculate the 4 FM voices. First the modulator oscillator,
and add the result to the carrier frequency. Each voice is added into
'sample' which holds the actual sample.
*/
// calculate fm voices .........
// IOSET0 |= (1 << 26); // end speed test
/* maintain the interrupt */
T0IR = 0xFF; // clear interrupts
VICVectAddr = 0; // update vic priorities
}
There are two lines commented out that we use to measure how long the audio interrupt routine takes to execute: IOCLR0 |= (1 << 26); and IOSET0 |= (1 << 26);
If you uncomment these lines, Pin 0.26 will go low at the beginning of the audio interrupt, and high at the end of the interrupt. Then an oscilloscope can be used to measure how long the interrupt takes:
The above shows the audio interrupt routine executing at 16 khz, or every 62.5 micro seconds. Pin 0.26 goes low for about 15 micro seconds, so the processor is spending about 1/4 of its time in the audio interrupt routine. This is pretty good, as 4 voices of FM requires 8 frequency and amplitude controlled oscillators. 8-bit oscillators are used in this example.
In this pic we can see the effects of compiler optimization. In the first pic there is no optimization. The second pic is with optimization level 1. (the optimization level is the -O flag specified in the makefile; -O0 for no optimization -O1 for level 1). Increasing optimization does increase performance, but should be used cautiously as it can sometimes lead to funny behavior. (for debugging, optimization should always be turned off)