Skip to content

Commit 7f5ba6d

Browse files
committed
AudioSourceVector corrections for sdfat
1 parent 4ad1369 commit 7f5ba6d

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed

examples/tests/player/test-vector-source/README.md

Whitespace-only changes.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "AudioTools.h"
2+
#include "AudioTools/Disk/AudioSource.h"
3+
#include "SdFat.h"
4+
5+
// SDFAT objects
6+
SdFat sd;
7+
File32 file;
8+
9+
// Audio objects
10+
AudioSourceVector<File32> audioSource;
11+
NamePrinter namePrinter(audioSource);
12+
13+
// Callback to convert file path to stream for AudioSourceVector
14+
File32* fileToStream(const char* path, File32& oldFile) {
15+
oldFile.close();
16+
file.open(path);
17+
18+
if (!file) {
19+
Serial.print("Failed to open: ");
20+
Serial.println(path);
21+
return nullptr;
22+
}
23+
return &file;
24+
}
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
29+
30+
Serial.println("AudioSourceVector with SDFAT Test");
31+
32+
// Initialize SD card
33+
if (!sd.begin(SS)) {
34+
Serial.println("SD card initialization failed!");
35+
return;
36+
}
37+
Serial.println("SD card initialized successfully");
38+
39+
// Set up the callback for AudioSourceVector
40+
audioSource.setNameToStreamCallback(fileToStream);
41+
42+
Serial.println("\n--- Collecting audio files from SD card ---");
43+
44+
// Use SDFAT's ls method to list files and automatically add them via NamePrinter
45+
// This will print each file name, and NamePrinter will capture each line
46+
// 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+
54+
Serial.print("\nTotal files collected: ");
55+
Serial.println(audioSource.size());
56+
57+
// Display collected files
58+
Serial.println("\n--- Collected Files ---");
59+
for (int i = 0; i < audioSource.size(); i++) {
60+
Serial.print(i);
61+
Serial.print(": ");
62+
Serial.println(audioSource.name(i));
63+
}
64+
65+
// Test navigation
66+
if (audioSource.size() > 0) {
67+
Serial.println("\n--- Testing AudioSource Navigation ---");
68+
69+
// Start playback simulation
70+
audioSource.begin();
71+
72+
// Select first file
73+
File32* stream = audioSource.selectStream(0);
74+
if (stream) {
75+
Serial.print("Selected file 0: ");
76+
Serial.println(audioSource.toStr());
77+
stream->close();
78+
}
79+
80+
// Try next file
81+
if (audioSource.size() > 1) {
82+
stream = audioSource.nextStream(1);
83+
if (stream) {
84+
Serial.print("Next file: ");
85+
Serial.println(audioSource.toStr());
86+
stream->close();
87+
}
88+
}
89+
90+
// Test selectStream by path
91+
if (audioSource.size() > 0) {
92+
const char* firstFile = audioSource.name(0);
93+
Serial.print("Selecting by path: ");
94+
Serial.println(firstFile);
95+
96+
stream = audioSource.selectStream(firstFile);
97+
if (stream) {
98+
Serial.println("Successfully selected by path!");
99+
stream->close();
100+
} else {
101+
Serial.println("Failed to select by path");
102+
}
103+
}
104+
}
105+
106+
Serial.println("\nTest completed!");
107+
}
108+
109+
void loop() {
110+
// Nothing to do in loop for this test
111+
delay(1000);
112+
}

src/AudioTools/Disk/AudioSource.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class NamePrinter : public Print {
227227
template <typename FileType>
228228
class AudioSourceVector : public AudioSource, public PathNamesRegistry {
229229
public:
230-
typedef FileType* (*FileToStreamCallback)(const char* path, FileType oldFile);
230+
typedef FileType* (*FileToStreamCallback)(const char* path, FileType& oldFile);
231231

232232
AudioSourceVector() = default;
233233

@@ -336,7 +336,7 @@ class AudioSourceVector : public AudioSource, public PathNamesRegistry {
336336
int pathIndex = findOrAddPath(pathStr.c_str());
337337

338338
// Create file entry with path index
339-
FileEntry entry(pathIndex, nameStr.c_str());
339+
FileEntry entry{pathIndex, nameStr.c_str()};
340340
files.push_back(entry);
341341

342342
LOGI("Added file: %s (total: %d, path_index: %d)", nameWithPath,
@@ -371,11 +371,12 @@ class AudioSourceVector : public AudioSource, public PathNamesRegistry {
371371
}
372372

373373
/// Get the current file reference for use in callback
374-
FileType getCurrentFile() {
374+
FileType& getCurrentFile() {
375375
if (current_stream) {
376376
return *current_stream;
377377
}
378-
return FileType();
378+
static FileType empty;
379+
return empty;
379380
}
380381

381382
/// provides the actual stream (e.g. file) name or url
@@ -391,7 +392,7 @@ class AudioSourceVector : public AudioSource, public PathNamesRegistry {
391392
const char* name(int index) { return getFullPath(index).c_str(); }
392393

393394
protected:
394-
Vector<FileEntry> files;
395+
Vector<FileEntry> files; // List of all files
395396
Vector<Str> path_registry; // Shared registry of unique paths
396397
int current_index = 0;
397398
FileType* current_stream = nullptr;
@@ -417,7 +418,7 @@ class AudioSourceVector : public AudioSource, public PathNamesRegistry {
417418
}
418419

419420
/// Get file entry at index
420-
const FileEntry& getFileEntry(int index) const { return files[index]; }
421+
FileEntry& getFileEntry(int index) const { return files[index]; }
421422

422423
/// Get full path of file at index
423424
Str getFullPath(int index) {
@@ -462,7 +463,7 @@ class AudioSourceVector : public AudioSource, public PathNamesRegistry {
462463
template <typename FileType>
463464
class AudioSourceArray : public AudioSource {
464465
public:
465-
typedef FileType* (*FileToStreamCallback)(const char* path, FileType file);
466+
typedef FileType* (*FileToStreamCallback)(const char* path, FileType& file);
466467

467468
AudioSourceArray() = default;
468469

@@ -579,11 +580,12 @@ class AudioSourceArray : public AudioSource {
579580
nameToStreamCallback = callback;
580581
}
581582

582-
FileType getCurrentFile() {
583+
FileType& getCurrentFile() {
583584
if (current_stream) {
584585
return *current_stream;
585586
}
586-
return FileType();
587+
static FileType empty;
588+
return empty;
587589
}
588590

589591
/// Get file path at index

0 commit comments

Comments
 (0)