Skip to content

Commit 414470f

Browse files
committed
AudioPlayer: correct broken getFileByPos()
1 parent f139e04 commit 414470f

File tree

2 files changed

+70
-80
lines changed

2 files changed

+70
-80
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ void testUrl(){
3030
Serial.println("--------------------------");
3131
}
3232

33-
const char *startFilePath="/test";
33+
const char *startFilePath="/";
3434
const char* ext="mp3";
35-
AudioSourceSdFat sdsource(startFilePath, ext);
35+
AudioSourceSdFat sdSource(startFilePath, ext);
3636

3737
void testSD() {
38-
sdSource.setPath("test")
38+
sdSource.setPath("/");
39+
sdSource.begin();
40+
3941
for (int j=-5;j<20;j++){
40-
Stream *out = sdsource.selectStream(j);
41-
Serial.printf("%d -> %d / %s \n", j, sdsource.index(), sdsource.toStr());
42+
Stream *out = sdSource.selectStream(j);
43+
Serial.printf("%d -> %d / %s \n", j, sdSource.index(), sdSource.toStr());
4244
if (out!=nullptr){
4345
assert(out->available()>0);
4446
}
@@ -47,11 +49,11 @@ void testSD() {
4749
}
4850

4951
void testSDNext() {
50-
sdSource.setPath("test")
51-
sdsource.begin();
52+
sdSource.setPath("/");
53+
sdSource.begin();
5254
for (int j=0;j<20;j++){
53-
Stream *out = sdsource.nextStream(1);
54-
Serial.printf("%d -> %d / %s \n", j, sdsource.index(), sdsource.toStr());
55+
Stream *out = sdSource.nextStream(1);
56+
Serial.printf("%d -> %d / %s \n", j, sdSource.index(), sdSource.toStr());
5557
if (out!=nullptr){
5658
assert(out->available()>0);
5759
}

src/AudioTools/AudioPlayer.h

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,19 @@ namespace audio_tools {
145145
#if defined(ARDUINO_ARCH_RP2040) && !defined(PICO)
146146
// only RP2040 from Earle Phil Hower is using the library with a sdfat namespace
147147
typedef sdfat::SdSpiConfig SdSpiConfig;
148-
typedef sdfat::FsFile AudioFile;
149148
typedef sdfat::SdFs AudioFs;
150149
#else
151150
#if SD_FAT_TYPE == 0
152151
typedef SdFat AudioFs;
153-
typedef File AudioDir;
154152
typedef File AudioFile;
155153
#elif SD_FAT_TYPE == 1
156154
typedef SdFat32 AudioFs;
157-
//typedef File32 AudioDir;
158155
typedef File32 AudioFile;
159156
#elif SD_FAT_TYPE == 2
160157
typedef SdExFat AudioFs;
161-
typedef ExFile AudioDir;
162158
typedef ExFile AudioFile;
163159
#elif SD_FAT_TYPE == 3
164160
typedef SdFs AudioFs;
165-
typedef FsFile AudioDir;
166161
typedef FsFile AudioFile;
167162
#else // SD_FAT_TYPE
168163
#endif
@@ -181,6 +176,7 @@ namespace audio_tools {
181176
LOGD(LOG_METHOD);
182177
LOGI("SD chipSelect: %d", chipSelect);
183178
LOGI("SD speedMHz: %d", speedMHz);
179+
LOGI("ext: %s", ext);
184180
p_cfg = new SdSpiConfig(chipSelect, DEDICATED_SPI, SD_SCK_MHZ(speedMHz));
185181
owns_cfg = true;
186182
start_path = startFilePath;
@@ -196,6 +192,7 @@ namespace audio_tools {
196192
exension = ext;
197193
}
198194

195+
/// Destructor
199196
virtual ~AudioSourceSdFat(){
200197
LOGD(LOG_METHOD);
201198
if (p_cfg!=nullptr && owns_cfg){
@@ -252,6 +249,11 @@ namespace audio_tools {
252249
return true;
253250
};
254251

252+
/// Allows to "correct" the start path if not defined in the constructor
253+
virtual void setPath(const char* p) {
254+
start_path = p;
255+
}
256+
255257
protected:
256258
AudioFile file;
257259
SdSpiConfig *p_cfg = nullptr;
@@ -273,26 +275,6 @@ namespace audio_tools {
273275
return result;
274276
}
275277

276-
/// Determines the file at the indicated index (starting with 0)
277-
AudioFile getFileByPos(const char* dirStr, int pos) {
278-
AudioFile dir;
279-
AudioFile result;
280-
if (sd.exists(dirStr)){
281-
LOGI("directory: %s", dirStr);
282-
} else {
283-
LOGE("directory: %s does not exist", dirStr);
284-
stop();
285-
}
286-
287-
dir.open(dirStr);
288-
size_t count = 0;
289-
getFileAtIndex(dir, pos, count, result);
290-
result.getName(file_name, MAX_FILE_LEN);
291-
LOGD("-> getFile: '%s': %d", file_name, pos);
292-
dir.close();
293-
return result;
294-
}
295-
296278
AudioFile getFileByPath(char* path) {
297279
AudioFile dir;
298280
Str inPath(path);
@@ -303,19 +285,13 @@ namespace audio_tools {
303285
strfileName.substring(path, pos, inPath.length());
304286
if (!dir.open(strPath.c_str())) {
305287
LOGE("directory: %s not open", path);
306-
}
307-
else
308-
{
288+
} else {
309289
if (!dir.isDir()) {
310290
LOGE("directory: %s is not dictory", path);
311-
}
312-
else
313-
{
291+
} else {
314292
if (!file.open(&dir, strfileName.c_str(), O_RDWR)) {
315293
LOGE("file: %s not open", path);
316-
}
317-
else
318-
{
294+
} else{
319295
LOGD("-> getFileByPath: %s , %s", strPath.c_str(), strfileName.c_str());
320296
}
321297
}
@@ -324,47 +300,63 @@ namespace audio_tools {
324300
return file;
325301
}
326302

327-
/// Recursively walk the directory tree to find the file at the indicated pos.
303+
/// Determines the file at the indicated index (starting with 0)
304+
AudioFile getFileByPos(const char* dirStr, int pos) {
305+
AudioFile dir;
306+
AudioFile result;
307+
if (sd.exists(dirStr)){
308+
LOGI("directory: '%s'", dirStr);
309+
if (dir.open(dirStr)){
310+
size_t count = 0;
311+
getFileAtIndex(dir, pos, count, result);
312+
result.getName(file_name, MAX_FILE_LEN);
313+
LOGD("-> getFile: '%s': %d", file_name, pos);
314+
} else {
315+
LOGE("Could not open direcotry: '%s'", dirStr);
316+
}
317+
} else {
318+
LOGE("directory: '%s' does not exist", dirStr);
319+
}
320+
321+
dir.close();
322+
return result;
323+
}
324+
328325
void getFileAtIndex(AudioFile dir, size_t pos, size_t& idx, AudioFile& result) {
329326
LOGD("%s: %d", LOG_METHOD, idx);
327+
char file_name_act[MAX_FILE_LEN];
328+
dir.getName(file_name_act, MAX_FILE_LEN);
329+
LOGD("-> processing directory: %s ", file_name_act);
330+
if (!dir.isDir()) {
331+
LOGE("'%s' is not a directory!", file_name_act);
332+
return;
333+
}
330334
AudioFile file;
331-
if (idx > pos) return;
332-
333-
bool found = false;
334-
while (!found && file.openNext(&dir, O_RDONLY )) {
335-
//if (idx > pos) return;
336-
337-
// close all files except result
338-
char file_name_act[MAX_FILE_LEN];
339-
file.getName(file_name_act, MAX_FILE_LEN);
340-
LOGD("-> processing: %s with index %d", file_name_act, idx);
335+
dir.rewind();
336+
while (!result && file.openNext(&dir, O_RDONLY)) {
337+
// indent for dir level
341338
if (!file.isHidden()) {
342-
if (!file.isDir()) {
343-
if (isValidAudioFile(file)) {
344-
if (idx == pos) {
345-
found = true;
346-
result = file;
347-
result.getName(file_name, MAX_FILE_LEN);
348-
LOGI("==> found: '%s' at index %d", file_name, idx);
349-
}
350-
idx++;
339+
file.getName(file_name_act, MAX_FILE_LEN);
340+
LOGD("-> processing: %s with index %d", file_name_act, idx);
341+
342+
if (isValidAudioFile(file)){
343+
if (idx == pos) {
344+
result = file;
345+
result.getName(file_name, MAX_FILE_LEN);
346+
LOGI("==> found: '%s' at index %d", file_name, idx);
351347
}
352-
} else {
353-
// process subdirectory
354-
getFileAtIndex(file, pos, idx, result);
348+
idx++;
355349
}
356350

357-
if (!Str(file_name_act).equals(file_name)) {
358-
file.getName(file_name, MAX_FILE_LEN);
359-
file.close();
360-
LOGD("-> close: '%s'", file_name);
361-
}
351+
if (file.isDir()) {
352+
getFileAtIndex(file, pos, idx, result);
353+
}
362354
}
355+
file.close();
363356
}
357+
return;
364358
}
365-
366359
};
367-
368360
#endif
369361

370362

@@ -722,8 +714,7 @@ namespace audio_tools {
722714
volume_out.setVolume(volume);
723715
current_volume = volume;
724716
}
725-
}
726-
else {
717+
} else {
727718
LOGE("setVolume value '%f' out of range (0.0 -1.0)", volume);
728719
}
729720
}
@@ -756,17 +747,14 @@ namespace audio_tools {
756747
if (!next(1)) {
757748
LOGD("stream is null");
758749
}
759-
}
760-
else {
750+
} else {
761751
LOGW("-> timeout - moving to previous stream");
762752
// open previous stream
763753
if (!previous(1)) {
764754
LOGD("stream is null");
765755
}
766756
}
767-
}
768-
else
769-
{
757+
} else {
770758
active = false;
771759
}
772760
timeout = millis() + p_source->timeoutAutoNext();

0 commit comments

Comments
 (0)