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.

Dependencies:   mbed

Fork of flash_audio_player 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 AnalogOut speaker(p18);
00003 Ticker sampletick;
00004 DigitalOut myled(LED1);
00005 //Plays Audio Clip using Array in Flash
00006 //8-bit audio samples used
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 union short_or_char {
00018     short s;
00019     char  c[2];
00020 };
00021 
00022 union short_or_char sample;
00023 
00024 
00025 //interrupt routine to play next audio sample from array in flash
00026 void audio_sample ()
00027 {
00028     sample.c[1] = sound_data[i];
00029     speaker.write_u16(sample.s);
00030     i++;
00031     if (i>= NUM_ELEMENTS) {
00032         i = 0;
00033         sampletick.detach();
00034         myled = 0;
00035     }
00036 }
00037 int main()
00038 {
00039     sample.c[0] = 0; //setup fast byte to short conversion using union
00040     while(1) {
00041         myled = 1;
00042 //use a ticker to play send next audio sample value to D/A
00043         sampletick.attach(&audio_sample, 1.0 / sample_freq);
00044 //can do other things while audio plays with timer interrupts
00045         wait(10.0);
00046     }
00047 }