Play WAV audio files from SD card
It is quite interesting to add sounds and recorded voices into your projects or interactive devices. Ansteron Board is capable of playing audio files in WAV format with acceptable quality and easy to program. The combinations of different sounds in multiple files can create many sound effects for your projects.
The library, Wav player, handles low level tasks for loading and playing WAV files from SD card. The output is PWM signal on pin B1 which can be used to drive a small speaker through one NPN transistor. Below is the basic circuit:
Output signal is fixed to pin B1. While Wav file is playing, PWM functions on pin B1, B2, B3 and D3 cannot be used. Besides, TimeStamp library cannot be included.
If an audio amplifier is used, connect pin B1 to the input of amplifier though a RC filter.
3. Wav files
Wav player library supports files with 8-bit mono PCM format. The default sample rate is 16 KHz but files between 11 KHz and 32 KHz can be played. Audio files usually need to be converted into appropriate format before Ansteron Board can play them. There are many software to do this, either free or commercial. You can search and download them on the Internet.
Note that Wav player library support mono (1 channel) audio only.
Below is an example setting for Wav file:
Beside audio files you got online or from CDs, you can even record them using software on your computer such as "Sound Recorder" (included in Windows). They can then be edited (cut/trimmed) with other software like Windows Movie Maker. Wav files can also be created from other audio formats (mp3, wma...).
Once your files are ready, you can load them into an SD card for playing on Ansteron Board. Remember that only 8.3 file name format is supported. Keep the name of your audio files simple, for example: "SOUND.WAV", "CAT.WAV", "DOG.WAV",...
To play Wav files in your program, you will need to have a buffer for temporally storing of audio data. This buffer must be between 256 and 1024 bytes in size. In the example below, a buffer of 512 bytes (
char buffer;) is created in global variable section and passed to
WAV_player() function when it run. The buffer can be used for other purposes when not playing.
User program has to set sample rate for each file before playing if it's not 16 KHz. However, playing Wav files at a different sample rate than it is can create some good sound effects as well.
To change the sample rate of WAV player, use function
rate can be one of the following:
Wav player library relies on SD Card library. It must also be included in your program.
WAV_player() will not return until given file is finished playing. To stop playing in the middle of wav file, an interrupt must be used. Your program can set up an external interrupt or any of the available interrupts before playing wav file. Function
WAV_player_stop() is called in the handler to stop Wav player.