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

See https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/ for instructions

Committer:
4180_1
Date:
Sun Oct 22 16:50:32 2017 +0000
Revision:
2:9d7fa7666ee4
Parent:
1:bd7bef7df00b
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:e4f991474a45 1 #include "mbed.h"
4180_1 2:9d7fa7666ee4 2 PwmOut speaker(p26);
4180_1 0:e4f991474a45 3 Ticker sampletick;
4180_1 0:e4f991474a45 4 DigitalOut myled(LED1);
4180_1 0:e4f991474a45 5 //Plays Audio Clip using Array in Flash
4180_1 2:9d7fa7666ee4 6 //8-bit audio samples used on PWM pin
4180_1 0:e4f991474a45 7 //setup const array in flash with audio values
4180_1 0:e4f991474a45 8 //from free wav file conversion tool at
4180_1 0:e4f991474a45 9 //http://ccgi.cjseymour.plus.com/wavtocode/wavtocode.htm
4180_1 0:e4f991474a45 10 //see https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/
4180_1 1:bd7bef7df00b 11 #include "cylon8bit.h"
4180_1 0:e4f991474a45 12
4180_1 0:e4f991474a45 13 #define sample_freq 11025.0
4180_1 0:e4f991474a45 14 //get and set the frequency from wav conversion tool GUI
4180_1 0:e4f991474a45 15 int i=0;
4180_1 0:e4f991474a45 16
4180_1 0:e4f991474a45 17 //interrupt routine to play next audio sample from array in flash
4180_1 0:e4f991474a45 18 void audio_sample ()
4180_1 0:e4f991474a45 19 {
4180_1 2:9d7fa7666ee4 20
4180_1 2:9d7fa7666ee4 21 speaker = sound_data[i]/255.0;//scale to 0.0 to 1.0 for PWM
4180_1 0:e4f991474a45 22 i++;
4180_1 0:e4f991474a45 23 if (i>= NUM_ELEMENTS) {
4180_1 0:e4f991474a45 24 i = 0;
4180_1 0:e4f991474a45 25 sampletick.detach();
4180_1 0:e4f991474a45 26 myled = 0;
4180_1 0:e4f991474a45 27 }
4180_1 0:e4f991474a45 28 }
4180_1 0:e4f991474a45 29 int main()
4180_1 0:e4f991474a45 30 {
4180_1 2:9d7fa7666ee4 31 speaker.period(1.0/250000.0); //PWM freq >10X audio sample rate
4180_1 0:e4f991474a45 32 while(1) {
4180_1 0:e4f991474a45 33 myled = 1;
4180_1 0:e4f991474a45 34 //use a ticker to play send next audio sample value to D/A
4180_1 0:e4f991474a45 35 sampletick.attach(&audio_sample, 1.0 / sample_freq);
4180_1 0:e4f991474a45 36 //can do other things while audio plays with timer interrupts
4180_1 0:e4f991474a45 37 wait(10.0);
4180_1 0:e4f991474a45 38 }
4180_1 0:e4f991474a45 39 }