|
| 1 | +#include "AudioTools.h" |
| 2 | +#include "AudioTools/AudioCodecs/CodecMP3Helix.h" |
| 3 | +#include "AudioTools/AudioLibs/AudioBoardStream.h" |
| 4 | +#include "SPI.h" |
| 5 | +#include "SdFat.h" |
| 6 | + |
| 7 | +#define PIN_AUDIO_KIT_SD_CARD_CS 13 |
| 8 | +#define PIN_AUDIO_KIT_SD_CARD_MISO 2 |
| 9 | +#define PIN_AUDIO_KIT_SD_CARD_MOSI 15 |
| 10 | +#define PIN_AUDIO_KIT_SD_CARD_CLK 14 |
| 11 | + |
| 12 | +// Callback to convert file path to stream for AudioSourceVector |
| 13 | +File32* fileToStream(const char* path, File32& oldFile); |
| 14 | + |
| 15 | +// Global data |
| 16 | +AudioSourceVector<File32> source(fileToStream); |
| 17 | +AudioBoardStream i2s(AudioKitEs8388V1); // or replace with e.g. I2SStream |
| 18 | +MP3DecoderHelix decoder; // or change to MP3DecoderMAD |
| 19 | +AudioPlayer player(source, i2s, decoder); |
| 20 | +SdFat sd; |
| 21 | +File32 audioFile; |
| 22 | + |
| 23 | + |
| 24 | +// Callback to convert file path to stream for AudioSourceVector |
| 25 | +File32* fileToStream(const char* path, File32& oldFile) { |
| 26 | + oldFile.close(); |
| 27 | + audioFile.open(path); |
| 28 | + |
| 29 | + if (!audioFile) { |
| 30 | + LOGE("Failed to open: %s", path); |
| 31 | + return nullptr; |
| 32 | + } |
| 33 | + return &audioFile; |
| 34 | +} |
| 35 | + |
| 36 | +bool setupDrive() { |
| 37 | + // Initialize SD card |
| 38 | + SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, |
| 39 | + PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS); |
| 40 | + |
| 41 | + if (!sd.begin(PIN_AUDIO_KIT_SD_CARD_CS, SPI_HALF_SPEED)) { |
| 42 | + Serial.println("SD card initialization failed!"); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // Use SDFAT's ls method to list files and automatically add file names |
| 47 | + const char* path = "/Bob Dylan/Bringing It All Back Home"; |
| 48 | + NamePrinter namePrinter(source, path); |
| 49 | + auto dir = sd.open(path, FILE_READ); |
| 50 | + dir.ls(&namePrinter, 0); |
| 51 | + dir.close(); |
| 52 | + |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +void setup() { |
| 57 | + Serial.begin(115200); |
| 58 | + AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info); |
| 59 | + |
| 60 | + // setup vector data source |
| 61 | + if (!setupDrive()) { |
| 62 | + Serial.println("Failed to setup drive"); |
| 63 | + stop(); |
| 64 | + } |
| 65 | + |
| 66 | + // setup output |
| 67 | + auto cfg = i2s.defaultConfig(TX_MODE); |
| 68 | + // we setup up the SPI (pins) outselfs... |
| 69 | + cfg.sd_active = false; |
| 70 | + if (!i2s.begin(cfg)) { |
| 71 | + Serial.println("Failed to start I2S"); |
| 72 | + stop(); |
| 73 | + } |
| 74 | + |
| 75 | + // setup player |
| 76 | + player.setVolume(0.7); |
| 77 | + if (!player.begin()) { |
| 78 | + Serial.println("Failed to start player"); |
| 79 | + stop(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void loop() { |
| 84 | + player.copy(); |
| 85 | +} |
0 commit comments