Sample Programs

This program demonstrates how to use the "freq" command to change the sample playback rate to affect the playback speed and pitch of the audio. This example features a horn honk sound effect and a drum loop that can be played at three different playback rates depending on which button is pressed.

Download the program files: fun_with_frequency.zip


HOOK_UP DIAGRAM

HOW THE PROGRAM WORKS

In the [device] section, we declare the SNC21030 device. There are only a couple short audio files in this program, so we can use a small device.

In the [key] section, we set the number of direct inputs to 7. This program uses six buttons to connected to pins P1.0 - P1.2 and P2.0 - P2.2. We are skipping over one input (pin P1.3), which is why the number of inputs is 7 and not 6.

In the [files] section, we list the audio files that will be included as part of the program.

In the [input_states] section, we define one input state called "enabled". When this input state is active, all six buttons will be active and can be used to trigger the program. If P1.0 is triggered, the button1: label will be called, if P1.1 is triggered, the button2: label will be called, and so on...

The [paths] section contains the various routines used in the program.

init: The "init" label always gets called first on power up and on reset. In this program, the init path sets the input state to "enabled" so that all buttons will be active. The end command puts the chip into a low power state. Since the "enabled" state is the active input state, when any button is pressed the program will jump to the appropriate label based on which button was pressed.

button1: When P1.0 is triggered, the button1: path is called. The "freq" command is used to set the audio playback rate to 10kHz. Since the audio file itself is at 12kHz, setting the playback rate to a lower value has the effect of slowing down the playback and lowering the pitch of the audio.

button2: When P1.1 is triggered, the button2: path is called. The "freq" command is used to set the audio playback rate to 12kHz. Since the audio file itself is at 12kHz, setting the playback rate to the same value has the effect of playing the audio back at the same speed and pitch as the original audio file.

button3: When P1.2 is triggered, the button3: path is called. The "freq" command is used to set the audio playback rate to 14kHz. Since the audio file itself is at 12kHz, setting the playback rate to a higher value has the effect of speeding up the playback and raising the pitch of the audio.

button4: When P2.0 is triggered, the button4: path is called. The "freq" command is used to set the audio playback rate to 10kHz. Since the audio file itself is at 12kHz, setting the playback rate to a lower value has the effect of slowing down the playback and lowering the pitch of the audio. At the end of this path, the button4: label is called so that the drum loop pattern plays over and over.

button5: When P2.1 is triggered, the button5: path is called. The "freq" command is used to set the audio playback rate to 12kHz. Since the audio file itself is at 12kHz, setting the playback rate to the same value has the effect of playing the audio back at the same speed and pitch as the original audio file. At the end of this path, the button5: label is called so that the drum loop pattern plays over and over.

button6: When P2.2 is triggered, the button6: path is called. The "freq" command is used to set the audio playback rate to 14kHz. Since the audio file itself is at 12kHz, setting the playback rate to a higher value has the effect of speeding up the playback and raising the pitch of the audio. At the end of this path, the button6: label is called so that the drum loop pattern plays over and over.