Plays audio samples from an array stored in flash using I2S. Values are from an audio *.wav file that was automatically converted to a C array data source file.

Dependencies:   I2S 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 #include "I2S.h"
00003 Ticker sampletick;
00004 DigitalOut myled(LED1);
00005 //Plays Audio Clip using Array in Flash
00006 //using I2S input Class D audio amp
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 //tested on MAX98357A
00012 #include "cylonbyc.h"
00013 #define sample_freq 11025.0
00014 
00015 I2S i2s(I2S_TRANSMIT, p5, p6, p7);
00016 //p5(PWM value sent as serial data bit) <-> Din
00017 //p6(WS) <-> LRC left/right channel clock
00018 //p7(bit clock) <-> BCLK
00019 
00020 int i = 0;
00021 int bufflen = 1;
00022 int buffer[1];
00023 void isr()
00024 {
00025     buffer[0] = sound_data[i]>>1;//scale down volume a bit on amp
00026     i2s.write(buffer, bufflen);//Send next PWM value to amp
00027     i++;
00028     if (i>= NUM_ELEMENTS) {
00029         i = 0;
00030         sampletick.detach();
00031         i2s.stop();
00032         myled = 0;
00033     }
00034 }
00035 
00036 int main()
00037 {
00038     i2s.stereomono(I2S_MONO);
00039     i2s.masterslave(I2S_MASTER);
00040     i2s.frequency(sample_freq);
00041     while(1) {
00042         i2s.start();
00043         myled = 1;
00044         sampletick.attach(&isr, 1.0/sample_freq);//turn on timer interrupt
00045         wait(10);
00046     }
00047 }