Skip to content

Commit b11c4b6

Browse files
committed
AudioSourceVector corrections
1 parent 7f5ba6d commit b11c4b6

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

examples/tests/player/test-vector-source/test-vector-source.ino

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,88 @@
11
#include "AudioTools.h"
22
#include "AudioTools/Disk/AudioSource.h"
33
#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
410

511
// SDFAT objects
612
SdFat sd;
713
File32 file;
14+
const char* path = "/Bob Dylan/Bringing It All Back Home";
815

916
// Audio objects
1017
AudioSourceVector<File32> audioSource;
11-
NamePrinter namePrinter(audioSource);
18+
NamePrinter namePrinter(audioSource, path);
19+
File32 audioFile;
1220

1321
// Callback to convert file path to stream for AudioSourceVector
1422
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) {
1927
Serial.print("Failed to open: ");
2028
Serial.println(path);
2129
return nullptr;
2230
}
23-
return &file;
31+
return &audioFile;
2432
}
2533

2634
void setup() {
2735
Serial.begin(115200);
2836
AudioLogger::instance().begin(Serial, AudioLogger::Info);
29-
37+
3038
Serial.println("AudioSourceVector with SDFAT Test");
31-
39+
3240
// 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)) {
3443
Serial.println("SD card initialization failed!");
3544
return;
3645
}
3746
Serial.println("SD card initialized successfully");
38-
47+
48+
Serial.println("\n--- Collecting audio files from SD card ---");
49+
3950
// Set up the callback for AudioSourceVector
4051
audioSource.setNameToStreamCallback(fileToStream);
41-
42-
Serial.println("\n--- Collecting audio files from SD card ---");
43-
52+
4453
// Use SDFAT's ls method to list files and automatically add them via NamePrinter
4554
// This will print each file name, and NamePrinter will capture each line
4655
// 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+
5460
Serial.print("\nTotal files collected: ");
5561
Serial.println(audioSource.size());
56-
62+
5763
// Display collected files
5864
Serial.println("\n--- Collected Files ---");
5965
for (int i = 0; i < audioSource.size(); i++) {
6066
Serial.print(i);
6167
Serial.print(": ");
6268
Serial.println(audioSource.name(i));
6369
}
64-
70+
6571
// Test navigation
6672
if (audioSource.size() > 0) {
6773
Serial.println("\n--- Testing AudioSource Navigation ---");
68-
74+
6975
// Start playback simulation
7076
audioSource.begin();
71-
77+
7278
// Select first file
7379
File32* stream = audioSource.selectStream(0);
7480
if (stream) {
7581
Serial.print("Selected file 0: ");
7682
Serial.println(audioSource.toStr());
7783
stream->close();
7884
}
79-
85+
8086
// Try next file
8187
if (audioSource.size() > 1) {
8288
stream = audioSource.nextStream(1);
@@ -86,13 +92,13 @@ void setup() {
8692
stream->close();
8793
}
8894
}
89-
95+
9096
// Test selectStream by path
9197
if (audioSource.size() > 0) {
9298
const char* firstFile = audioSource.name(0);
9399
Serial.print("Selecting by path: ");
94100
Serial.println(firstFile);
95-
101+
96102
stream = audioSource.selectStream(firstFile);
97103
if (stream) {
98104
Serial.println("Successfully selected by path!");
@@ -102,7 +108,7 @@ void setup() {
102108
}
103109
}
104110
}
105-
111+
106112
Serial.println("\nTest completed!");
107113
}
108114

src/AudioTools/CoreAudio/AudioBasic/Str.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,17 @@ class Str : public StrView {
163163
this->len = result_idx;
164164
}
165165

166+
/// clears and resizes to 0
166167
void clear() override {
168+
clear(true);
169+
}
170+
171+
void clear(bool resize) {
167172
len = 0;
168-
maxlen = 0;
169-
vector.resize(0);
173+
if (resize){
174+
maxlen = 0;
175+
vector.resize(0);
176+
}
170177
chars = nullptr;
171178
}
172179

src/AudioTools/Disk/AudioSource.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,26 @@ class PathNamesRegistry {
177177

178178
class NamePrinter : public Print {
179179
public:
180-
NamePrinter(PathNamesRegistry& dataSource) : dataSource(dataSource) {}
181-
180+
NamePrinter(PathNamesRegistry& dataSource, const char* prefix = nullptr)
181+
: dataSource(dataSource), prefix(prefix) {}
182+
void setPrefix(const char* prefix) { this->prefix = prefix; }
182183
size_t write(uint8_t ch) override {
183184
if (ch == '\n' || ch == '\r') {
184185
// End of line - process the accumulated line
185186
if (line_buffer.length() > 0) {
186-
LOGI("adding %s", line_buffer.c_str());
187-
dataSource.addName(line_buffer.c_str());
188-
line_buffer.clear();
187+
line_buffer.trim();
188+
LOGI("adding '%s'", line_buffer.c_str());
189+
if (prefix != nullptr) {
190+
// Prepend prefix if set
191+
Str name{prefix};
192+
name.add("/");
193+
name.add(line_buffer.c_str());
194+
dataSource.addName(name.c_str());
195+
} else {
196+
// Add line as is
197+
dataSource.addName(line_buffer.c_str());
198+
}
199+
line_buffer.clear(false);
189200
}
190201
return 1;
191202
} else {
@@ -213,6 +224,7 @@ class NamePrinter : public Print {
213224
private:
214225
PathNamesRegistry& dataSource;
215226
Str line_buffer{200}; // Buffer to accumulate characters for each line
227+
const char* prefix = nullptr;
216228
};
217229

218230
/**
@@ -227,7 +239,8 @@ class NamePrinter : public Print {
227239
template <typename FileType>
228240
class AudioSourceVector : public AudioSource, public PathNamesRegistry {
229241
public:
230-
typedef FileType* (*FileToStreamCallback)(const char* path, FileType& oldFile);
242+
typedef FileType* (*FileToStreamCallback)(const char* path,
243+
FileType& oldFile);
231244

232245
AudioSourceVector() = default;
233246

0 commit comments

Comments
 (0)