Wave Player using MODDMA
Introduction
This library is a variation of Steve Ravet's wave player library. It has the same class and functions as the wave player library; however, it uses DMA to write to the DAC. Andy Kirkham's MODDMA library is used to transfer a buffer of samples from the .wav file to the DAC, instead of individually writing each sample to the AnalogOut pin. More information on the MODDMA library is found on the MODDMA cookbook page.
Speaker Setup
The wave player plays the audio on the AnalogOut pin. However, the volume is too quiet to hook a speaker directly to the pin, so a driver circuit is necessary. The speaker amplifier circuit used in the Hello World example is shown below.
Pinout for a 2N3904 NPN General Purpose Amplifier
Example speaker amplifier circuit
Other methods to connect a speaker are described on the Audio Speaker handbook page.
SD Card Setup
A SD card is a convenient way to store the .wav file to play using the wave player MODDMA library. Using USB would likely be too slow. To play a .wav file, the file must first be downsampled to less than 22kHz (16kHz is a good value) for the sample rate to be slow enough to be read off of the SD card. For best results, the .wav file should also be converted from stereo to mono and exported as a 16 bit file. Audacity is a free audio editing tool that can be used to create a .wav file with all the required formatting. SparkFun has a mircoSD breakout board which can be used to connect to the mbed.
SparkFun microSD breakout board
Wiring
Wiring for SparkFun microSD breakout board
MicroSD breakout board | mbed pin | mbed pin type |
---|---|---|
CS | 8 | DigitalOut |
DI | 5 | MOSI |
VCC | VOUT | |
SCK | 7 | SCLK |
GND | GND | |
DO | 6 | MISO |
CD | any DigitalOut | DigitalOut |
Overall Setup
For this example, a .wav file is stored on a microSD card, and a speaker is connected to the mbed using the previously described circuit.
Mbed setup for the Hello World example
Hello World Example
This example uses the MODDMA wave player library to play a sample.wav file located on the microSD.
main.cpp
#include "mbed.h" #include "SDFileSystem.h" #include "wave_player.h" SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card AnalogOut DACout(p18); wave_player waver(&DACout); int main() { FILE *wave_file; printf("\n\n\nHello, wave world!\n"); wave_file=fopen("/sd/sample.wav","r"); waver.play(wave_file); fclose(wave_file); }
Import programWavePlayer_MODDMA
Wave player using MODDMA example Hello World program.
Demonstration
Library
Import library
Public Member Functions |
|
wave_player (AnalogOut *_dac) | |
Create a wave player using a pointer to the given AnalogOut object.
|
|
void | play (FILE *wavefile) |
the player function.
|
|
void | set_verbosity (int v) |
Set the printf verbosity of the wave player.
|
Please log in to post comments.