#include "FS.h" #include "SD.h" #include "SPI.h" #include bool sd_sign = false; const char * songs[]={"/banana_audio1_16000.wav", "/banana_audio2_16000.wav", "/banana_audio3_16000.wav", "/banana_audio4_16000.wav", "/banana_audio5_16000.wav", "/banana_audio6_16000.wav"}; void setup_audio(); void play_song(const char* filename); void play_random_song(); void setup_audio(){ I2S.setAllPins(D2, D3, D1, -1, -1); //SCK, WS, SDOUT, SDIN, MCLK I2S.begin(I2S_PHILIPS_MODE, 16000, 16); if(!SD.begin(21)){ Serial.println("Card Mount Failed"); return; } uint8_t cardType = SD.cardType(); // Determine if the type of SD card is available if(cardType == CARD_NONE){ Serial.println("No SD card attached"); return; } Serial.print("SD Card Type: "); if(cardType == CARD_MMC){ Serial.println("MMC"); } else if(cardType == CARD_SD){ Serial.println("SDSC"); } else if(cardType == CARD_SDHC){ Serial.println("SDHC"); } else { Serial.println("UNKNOWN"); } sd_sign = true; // sd initialization check passes } void play_song(const char* filename){ if(sd_sign){ if(!SD.exists(filename)){ Serial.println("AHHHH"); delay(1000);}else{ File file = SD.open(filename, FILE_READ); int16_t sample; while (file.read((byte *)&sample, 2)) { // Serial.println(sample); I2S.write(sample); I2S.write(sample); } } } // try to get rid of noise for(int i =0 ; i<10000;i++){ I2S.write(0); } } void play_random_song(){ int song_num = random(6); Serial.print("playing song #"); Serial.println(song_num); const char * filename= songs[song_num]; play_song(filename); }