Plays audio samples from an array stored in flash. Values are from an audio *.wav file that was automatically converted to a C array data source file. 8-bit samples used in this version and a PWM output pin (instead of AnalogOut)

Dependencies:   mbed

Fork of flash_audio_player_8bit by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 PwmOut speaker(p26);
00003 Ticker sampletick;
00004 DigitalOut myled(LED1);
00005 //Plays Audio Clip using Array in Flash
00006 //8-bit audio samples used on PWM pin
00007 //setup const array in flash with audio values 
00008 //from free wav file conversion tool at
00009 //http://ccgi.cjseymour.plus.com/wavtocode/wavtocode.htm
00010 //see https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/
00011 #include "cylon8bit.h"
00012 
00013 #define sample_freq 11025.0
00014 //get and set the frequency from wav conversion tool GUI
00015 int i=0;
00016 
00017 //interrupt routine to play next audio sample from array in flash
00018 void audio_sample ()
00019 {
00020 
00021     speaker = sound_data[i]/255.0;//scale to 0.0 to 1.0 for PWM
00022     i++;
00023     if (i>= NUM_ELEMENTS) {
00024         i = 0;
00025         sampletick.detach();
00026         myled = 0;
00027     }
00028 }
00029 int main()
00030 {
00031     speaker.period(1.0/250000.0); //PWM freq >10X audio sample rate
00032     while(1) {
00033         myled = 1;
00034 //use a ticker to play send next audio sample value to D/A
00035         sampletick.attach(&audio_sample, 1.0 / sample_freq);
00036 //can do other things while audio plays with timer interrupts
00037         wait(10.0);
00038     }
00039 }