|
| 1 | +#include "AudioTools.h" |
| 2 | +#include "AudioTools/Disk/AudioSource.h" |
| 3 | +#include "SPI.h" |
| 4 | +#include "SdFat.h" |
| 5 | + |
| 6 | +#define PIN_AUDIO_KIT_SD_CARD_CS 13 |
| 7 | +#define PIN_AUDIO_KIT_SD_CARD_MISO 2 |
| 8 | +#define PIN_AUDIO_KIT_SD_CARD_MOSI 15 |
| 9 | +#define PIN_AUDIO_KIT_SD_CARD_CLK 14 |
| 10 | + |
| 11 | +// SDFAT objects |
| 12 | +SdFat sd; |
| 13 | +File32 file; |
| 14 | + |
| 15 | +const char* filesNames[] = |
| 16 | + {"/Bob Dylan/Bringing It All Back Home/04 Love minus zero_no limit.mp3", |
| 17 | + "/Bob Dylan/Bringing It All Back Home/11 It's all over now, Baby Blue.mp3", |
| 18 | + "/Bob Dylan/Bringing It All Back Home/06 On the road again.mp3", |
| 19 | + "/Bob Dylan/Bringing It All Back Home/08 Mr. Tambourine Man.mp3", |
| 20 | + "/Bob Dylan/Bringing It All Back Home/10 It's alright, Ma (I'm only bl.mp3", |
| 21 | + "/Bob Dylan/Bringing It All Back Home/02 She belongs to me.mp3", |
| 22 | + "/Bob Dylan/Bringing It All Back Home/03 Maggie's farm.mp3", |
| 23 | + "/Bob Dylan/Bringing It All Back Home/01 Subterranean homesick blues.mp3", |
| 24 | + "/Bob Dylan/Bringing It All Back Home/07 Bob Dylan's 115th dream.mp3", |
| 25 | + "/Bob Dylan/Bringing It All Back Home/05 Outlaw blues.mp3", |
| 26 | + "/Bob Dylan/Bringing It All Back Home/09 Gates of Eden.mp3" |
| 27 | +}; |
| 28 | + |
| 29 | +// Audio objects |
| 30 | +File32* fileToStreamCB(const char* path, File32& oldFile); |
| 31 | +AudioSourceArray<File32> audioSource(filesNames, fileToStreamCB); |
| 32 | +File32 audioFile; |
| 33 | + |
| 34 | +// Callback to convert file path to stream for AudioSourceVector |
| 35 | +File32* fileToStreamCB(const char* path, File32& oldFile) { |
| 36 | + oldFile.close(); |
| 37 | + audioFile.open(path); |
| 38 | + |
| 39 | + if (!audioFile) { |
| 40 | + Serial.print("Failed to open: "); |
| 41 | + Serial.println(path); |
| 42 | + return nullptr; |
| 43 | + } |
| 44 | + return &audioFile; |
| 45 | +} |
| 46 | + |
| 47 | +void setup() { |
| 48 | + Serial.begin(115200); |
| 49 | + AudioLogger::instance().begin(Serial, AudioLogger::Info); |
| 50 | + |
| 51 | + Serial.println("AudioSourceVector with SDFAT Test"); |
| 52 | + |
| 53 | + // Initialize SD card |
| 54 | + SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, |
| 55 | + PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS); |
| 56 | + if (!sd.begin(PIN_AUDIO_KIT_SD_CARD_CS, SPI_HALF_SPEED)) { |
| 57 | + Serial.println("SD card initialization failed!"); |
| 58 | + return; |
| 59 | + } |
| 60 | + Serial.println("SD card initialized successfully"); |
| 61 | + Serial.print("\nTotal files: "); |
| 62 | + Serial.println(audioSource.size()); |
| 63 | + |
| 64 | + // Display collected files |
| 65 | + Serial.println("\n--- Collected Files ---"); |
| 66 | + for (int i = 0; i < audioSource.size(); i++) { |
| 67 | + Serial.print(i); |
| 68 | + Serial.print(": "); |
| 69 | + Serial.println(audioSource.name(i)); |
| 70 | + } |
| 71 | + |
| 72 | + // Test navigation |
| 73 | + if (audioSource.size() > 0) { |
| 74 | + Serial.println("\n--- Testing AudioSource Navigation ---"); |
| 75 | + |
| 76 | + // Start playback simulation |
| 77 | + audioSource.begin(); |
| 78 | + |
| 79 | + // Select first file |
| 80 | + File32* stream = audioSource.selectStream(0); |
| 81 | + if (stream) { |
| 82 | + Serial.print("Selected file 0: "); |
| 83 | + Serial.println(audioSource.toStr()); |
| 84 | + stream->close(); |
| 85 | + } |
| 86 | + |
| 87 | + // Try next file |
| 88 | + if (audioSource.size() > 1) { |
| 89 | + stream = audioSource.nextStream(1); |
| 90 | + if (stream) { |
| 91 | + Serial.print("Next file: "); |
| 92 | + Serial.println(audioSource.toStr()); |
| 93 | + stream->close(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Test selectStream by path |
| 98 | + if (audioSource.size() > 0) { |
| 99 | + const char* firstFile = audioSource.name(0); |
| 100 | + Serial.print("Selecting by path: "); |
| 101 | + Serial.println(firstFile); |
| 102 | + |
| 103 | + stream = audioSource.selectStream(firstFile); |
| 104 | + if (stream) { |
| 105 | + Serial.println("Successfully selected by path!"); |
| 106 | + stream->close(); |
| 107 | + } else { |
| 108 | + Serial.println("Failed to select by path"); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + Serial.println("\nTest completed!"); |
| 114 | +} |
| 115 | + |
| 116 | +void loop() { |
| 117 | + // Nothing to do in loop for this test |
| 118 | + delay(1000); |
| 119 | +} |
0 commit comments