@@ -145,24 +145,19 @@ namespace audio_tools {
145
145
#if defined(ARDUINO_ARCH_RP2040) && !defined(PICO)
146
146
// only RP2040 from Earle Phil Hower is using the library with a sdfat namespace
147
147
typedef sdfat::SdSpiConfig SdSpiConfig;
148
- typedef sdfat::FsFile AudioFile;
149
148
typedef sdfat::SdFs AudioFs;
150
149
#else
151
150
#if SD_FAT_TYPE == 0
152
151
typedef SdFat AudioFs;
153
- typedef File AudioDir;
154
152
typedef File AudioFile;
155
153
#elif SD_FAT_TYPE == 1
156
154
typedef SdFat32 AudioFs;
157
- // typedef File32 AudioDir;
158
155
typedef File32 AudioFile;
159
156
#elif SD_FAT_TYPE == 2
160
157
typedef SdExFat AudioFs;
161
- typedef ExFile AudioDir;
162
158
typedef ExFile AudioFile;
163
159
#elif SD_FAT_TYPE == 3
164
160
typedef SdFs AudioFs;
165
- typedef FsFile AudioDir;
166
161
typedef FsFile AudioFile;
167
162
#else // SD_FAT_TYPE
168
163
#endif
@@ -181,6 +176,7 @@ namespace audio_tools {
181
176
LOGD (LOG_METHOD);
182
177
LOGI (" SD chipSelect: %d" , chipSelect);
183
178
LOGI (" SD speedMHz: %d" , speedMHz);
179
+ LOGI (" ext: %s" , ext);
184
180
p_cfg = new SdSpiConfig (chipSelect, DEDICATED_SPI, SD_SCK_MHZ (speedMHz));
185
181
owns_cfg = true ;
186
182
start_path = startFilePath;
@@ -196,6 +192,7 @@ namespace audio_tools {
196
192
exension = ext;
197
193
}
198
194
195
+ // / Destructor
199
196
virtual ~AudioSourceSdFat (){
200
197
LOGD (LOG_METHOD);
201
198
if (p_cfg!=nullptr && owns_cfg){
@@ -252,6 +249,11 @@ namespace audio_tools {
252
249
return true ;
253
250
};
254
251
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
+
255
257
protected:
256
258
AudioFile file;
257
259
SdSpiConfig *p_cfg = nullptr ;
@@ -273,26 +275,6 @@ namespace audio_tools {
273
275
return result;
274
276
}
275
277
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
-
296
278
AudioFile getFileByPath (char * path) {
297
279
AudioFile dir;
298
280
Str inPath (path);
@@ -303,19 +285,13 @@ namespace audio_tools {
303
285
strfileName.substring (path, pos, inPath.length ());
304
286
if (!dir.open (strPath.c_str ())) {
305
287
LOGE (" directory: %s not open" , path);
306
- }
307
- else
308
- {
288
+ } else {
309
289
if (!dir.isDir ()) {
310
290
LOGE (" directory: %s is not dictory" , path);
311
- }
312
- else
313
- {
291
+ } else {
314
292
if (!file.open (&dir, strfileName.c_str (), O_RDWR)) {
315
293
LOGE (" file: %s not open" , path);
316
- }
317
- else
318
- {
294
+ } else {
319
295
LOGD (" -> getFileByPath: %s , %s" , strPath.c_str (), strfileName.c_str ());
320
296
}
321
297
}
@@ -324,47 +300,63 @@ namespace audio_tools {
324
300
return file;
325
301
}
326
302
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
+
328
325
void getFileAtIndex (AudioFile dir, size_t pos, size_t & idx, AudioFile& result) {
329
326
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
+ }
330
334
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
341
338
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);
351
347
}
352
- } else {
353
- // process subdirectory
354
- getFileAtIndex (file, pos, idx, result);
348
+ idx++;
355
349
}
356
350
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
+ }
362
354
}
355
+ file.close ();
363
356
}
357
+ return ;
364
358
}
365
-
366
359
};
367
-
368
360
#endif
369
361
370
362
@@ -722,8 +714,7 @@ namespace audio_tools {
722
714
volume_out.setVolume (volume);
723
715
current_volume = volume;
724
716
}
725
- }
726
- else {
717
+ } else {
727
718
LOGE (" setVolume value '%f' out of range (0.0 -1.0)" , volume);
728
719
}
729
720
}
@@ -756,17 +747,14 @@ namespace audio_tools {
756
747
if (!next (1 )) {
757
748
LOGD (" stream is null" );
758
749
}
759
- }
760
- else {
750
+ } else {
761
751
LOGW (" -> timeout - moving to previous stream" );
762
752
// open previous stream
763
753
if (!previous (1 )) {
764
754
LOGD (" stream is null" );
765
755
}
766
756
}
767
- }
768
- else
769
- {
757
+ } else {
770
758
active = false ;
771
759
}
772
760
timeout = millis () + p_source->timeoutAutoNext ();
0 commit comments