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

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

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Tue Dec 05 01:55:44 2017 +0000
Parent:
0:e4f991474a45
Commit message:
ver 1.0

Changed in this revision

I2S.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r e4f991474a45 -r 8fda76e11be8 I2S.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2S.lib	Tue Dec 05 01:55:44 2017 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/p07gbar/code/I2S/#455d5826751b
diff -r e4f991474a45 -r 8fda76e11be8 main.cpp
--- a/main.cpp	Sat Oct 21 20:53:36 2017 +0000
+++ b/main.cpp	Tue Dec 05 01:55:44 2017 +0000
@@ -1,37 +1,47 @@
 #include "mbed.h"
-AnalogOut speaker(p18);
+#include "I2S.h"
 Ticker sampletick;
 DigitalOut myled(LED1);
 //Plays Audio Clip using Array in Flash
-//
-//setup const array in flash with audio values 
+//using I2S input Class D audio amp
+//setup const array in flash with audio values
 //from free wav file conversion tool at
 //http://ccgi.cjseymour.plus.com/wavtocode/wavtocode.htm
 //see https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/
+//tested on MAX98357A
 #include "cylonbyc.h"
-
 #define sample_freq 11025.0
-//get and set the frequency from wav conversion tool GUI
-int i=0;
+
+I2S i2s(I2S_TRANSMIT, p5, p6, p7);
+//p5(PWM value sent as serial data bit) <-> Din
+//p6(WS) <-> LRC left/right channel clock
+//p7(bit clock) <-> BCLK
 
-//interrupt routine to play next audio sample from array in flash
-void audio_sample ()
+int i = 0;
+int bufflen = 1;
+int buffer[1];
+void isr()
 {
-    speaker.write_u16(sound_data[i]);
+    buffer[0] = sound_data[i]>>1;//scale down volume a bit on amp
+    i2s.write(buffer, bufflen);//Send next PWM value to amp
     i++;
     if (i>= NUM_ELEMENTS) {
         i = 0;
         sampletick.detach();
+        i2s.stop();
         myled = 0;
     }
 }
+
 int main()
 {
+    i2s.stereomono(I2S_MONO);
+    i2s.masterslave(I2S_MASTER);
+    i2s.frequency(sample_freq);
     while(1) {
+        i2s.start();
         myled = 1;
-//use a ticker to play send next audio sample value to D/A
-        sampletick.attach(&audio_sample, 1.0 / sample_freq);
-//can do other things while audio plays with timer interrupts
-        wait(10.0);
+        sampletick.attach(&isr, 1.0/sample_freq);//turn on timer interrupt
+        wait(10);
     }
 }