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.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 AnalogOut speaker(p18);
00003 Ticker sampletick;
00004 DigitalOut myled(LED1);
00005 //Plays Audio Clip using Array in Flash
00006 //
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 "cylonbyc.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     speaker.write_u16(sound_data[i]);
00021     i++;
00022     if (i>= NUM_ELEMENTS) {
00023         i = 0;
00024         sampletick.detach();
00025         myled = 0;
00026     }
00027 }
00028 int main()
00029 {
00030     while(1) {
00031         myled = 1;
00032 //use a ticker to play send next audio sample value to D/A
00033         sampletick.attach(&audio_sample, 1.0 / sample_freq);
00034 //can do other things while audio plays with timer interrupts
00035         wait(10.0);
00036     }
00037 }