1
1
#include " AudioTools.h"
2
2
#include " AudioTools/Disk/AudioSource.h"
3
3
#include " SdFat.h"
4
+ #include " SPI.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
4
10
5
11
// SDFAT objects
6
12
SdFat sd;
7
13
File32 file;
14
+ const char * path = " /Bob Dylan/Bringing It All Back Home" ;
8
15
9
16
// Audio objects
10
17
AudioSourceVector<File32> audioSource;
11
- NamePrinter namePrinter (audioSource);
18
+ NamePrinter namePrinter (audioSource, path);
19
+ File32 audioFile;
12
20
13
21
// Callback to convert file path to stream for AudioSourceVector
14
22
File32* fileToStream (const char * path, File32& oldFile) {
15
- oldFile.close ();
16
- file .open (path);
17
-
18
- if (!file ) {
23
+ oldFile.close ();
24
+ audioFile .open (path);
25
+
26
+ if (!audioFile ) {
19
27
Serial.print (" Failed to open: " );
20
28
Serial.println (path);
21
29
return nullptr ;
22
30
}
23
- return &file ;
31
+ return &audioFile ;
24
32
}
25
33
26
34
void setup () {
27
35
Serial.begin (115200 );
28
36
AudioLogger::instance ().begin (Serial, AudioLogger::Info);
29
-
37
+
30
38
Serial.println (" AudioSourceVector with SDFAT Test" );
31
-
39
+
32
40
// Initialize SD card
33
- if (!sd.begin (SS)) {
41
+ SPI.begin (PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
42
+ if (!sd.begin (PIN_AUDIO_KIT_SD_CARD_CS, SPI_HALF_SPEED)) {
34
43
Serial.println (" SD card initialization failed!" );
35
44
return ;
36
45
}
37
46
Serial.println (" SD card initialized successfully" );
38
-
47
+
48
+ Serial.println (" \n --- Collecting audio files from SD card ---" );
49
+
39
50
// Set up the callback for AudioSourceVector
40
51
audioSource.setNameToStreamCallback (fileToStream);
41
-
42
- Serial.println (" \n --- Collecting audio files from SD card ---" );
43
-
52
+
44
53
// Use SDFAT's ls method to list files and automatically add them via NamePrinter
45
54
// This will print each file name, and NamePrinter will capture each line
46
55
// and call audioSource.addName() for each file
47
-
48
- Serial.println (" Files found on SD card:" );
49
- sd.ls (&namePrinter, LS_R); // Recursive listing, output goes to NamePrinter
50
-
51
- // Flush any remaining content in the NamePrinter buffer
52
- namePrinter.flush ();
53
-
56
+ auto dir = sd.open (path, FILE_READ);
57
+ dir.ls (&namePrinter, 0 );
58
+ dir.close ();
59
+
54
60
Serial.print (" \n Total files collected: " );
55
61
Serial.println (audioSource.size ());
56
-
62
+
57
63
// Display collected files
58
64
Serial.println (" \n --- Collected Files ---" );
59
65
for (int i = 0 ; i < audioSource.size (); i++) {
60
66
Serial.print (i);
61
67
Serial.print (" : " );
62
68
Serial.println (audioSource.name (i));
63
69
}
64
-
70
+
65
71
// Test navigation
66
72
if (audioSource.size () > 0 ) {
67
73
Serial.println (" \n --- Testing AudioSource Navigation ---" );
68
-
74
+
69
75
// Start playback simulation
70
76
audioSource.begin ();
71
-
77
+
72
78
// Select first file
73
79
File32* stream = audioSource.selectStream (0 );
74
80
if (stream) {
75
81
Serial.print (" Selected file 0: " );
76
82
Serial.println (audioSource.toStr ());
77
83
stream->close ();
78
84
}
79
-
85
+
80
86
// Try next file
81
87
if (audioSource.size () > 1 ) {
82
88
stream = audioSource.nextStream (1 );
@@ -86,13 +92,13 @@ void setup() {
86
92
stream->close ();
87
93
}
88
94
}
89
-
95
+
90
96
// Test selectStream by path
91
97
if (audioSource.size () > 0 ) {
92
98
const char * firstFile = audioSource.name (0 );
93
99
Serial.print (" Selecting by path: " );
94
100
Serial.println (firstFile);
95
-
101
+
96
102
stream = audioSource.selectStream (firstFile);
97
103
if (stream) {
98
104
Serial.println (" Successfully selected by path!" );
@@ -102,7 +108,7 @@ void setup() {
102
108
}
103
109
}
104
110
}
105
-
111
+
106
112
Serial.println (" \n Test completed!" );
107
113
}
108
114
0 commit comments