Downloads: Hello Sine Wave example code.
Creating a sine wave is always the first step when exploring a new computer music system. This example illustrates one technique we use for audio synthesis on the Critter Board.
After downloading the example code, unpack it, and in a terminal window run 'make' to compile the code. Then upload the resulting main.hex file to the Critter Board, and you should hear some tones.
There are a few different files that make up this program. The audio synthesis happens in 'audio_sinewave.c', some system level stuff in 'system.c', and the main program is in 'main.c'. This keeps the main program small, and creating sound is as simple as a few function calls:
int main (void) {
Initialize(); // Initialize the MCU
synth_init(); // initialize synthesis
synth_amp(255); // full volume
synth_freq(440); // play 440 Hz for 2 seconds
delay_ms(2000);
for (;;){
synth_freq(200); // play four notes over and over
delay_ms(500);
synth_freq(400);
delay_ms(500);
synth_freq(600);
delay_ms(500);
synth_freq(800);
delay_ms(500);
}
}
The basic idea is to use one of the hardware timers on the LPC2138 to trigger an interrupt at the sampling rate (this example uses a 16 KHz sampling rate, but a higher rate can be used). Each time this audio interrupt occurs, the LPC2138 jumps to a special function known as the interrupt service routine (ISR). All the sound synthesis takes place in this function. The function must complete execution before the next audio interrupt occurs, or else the processor will not have time to do anything else. (generally it is a bad idea to have a lot of code in an ISR, but if there is only one interrupt source, it is stable)
The guts of the sine wave oscillator are in audio_sinewave.c file, along with a bunch of comments. It is a basic phase accumulator/lookup table oscillator. With a 24 bit phase accumulator, the frequency resolution is good. The overall amplitude is scaled by an 8 bit value.