play_from_header.ino
1 #include "Adafruit_MP3.h" 2 #include "sine.h" 3 4 Adafruit_MP3 player; 5 6 void writeDacs(int16_t l, int16_t r){ 7 uint16_t val = map(l, -32768, 32767, 0, 4095); 8 #if defined(__SAMD51__) // feather/metro m4 9 analogWrite(A0, val); 10 #elif defined(__MK66FX1M0__) || defined(__MK20DX256__) // teensy 3.6 11 analogWrite(A21, val); 12 #endif 13 } 14 15 uint8_t *currentPtr; 16 uint32_t thisManyBytesLeft; 17 18 int getMoreData(uint8_t *writeHere, int thisManyBytes){ 19 int toWrite = min(thisManyBytesLeft, thisManyBytes); 20 memcpy(writeHere, currentPtr, toWrite); 21 currentPtr += toWrite; 22 thisManyBytesLeft -= toWrite; 23 return toWrite; 24 } 25 26 // the setup routine runs once when you press reset: 27 void setup() { 28 29 #if defined(__SAMD51__) 30 //set the DAC to the center of the range 31 analogWrite(A0, 2048); 32 #endif 33 34 currentPtr = (uint8_t*)sine_mp3; 35 thisManyBytesLeft = sizeof(sine_mp3); 36 37 player.begin(); 38 39 //do this when there are samples ready 40 player.setSampleReadyCallback(writeDacs); 41 42 //do this when more data is required 43 player.setBufferCallback(getMoreData); 44 45 player.play(); 46 } 47 48 // the loop routine runs over and over again forever: 49 void loop() { 50 player.tick(); 51 }