4 #include "AudioLogger.h"
5 #include "AudioTools/CoreAudio/AudioSource.h"
6 #include "AudioTools/AudioLibs/SDIndex.h"
31 class AudioSourceIdxSD :
public AudioSource {
34 AudioSourceIdxSD(
const char *startFilePath =
"/",
const char *ext =
".mp3",
int chipSelect = PIN_CS,
bool setupIndex=
true) {
35 start_path = startFilePath;
37 setup_index = setupIndex;
42 #ifdef USE_SD_SUPPORTS_SPI
44 AudioSourceSD(
const char *startFilePath,
const char *ext,
int chipSelect, SPIClass &spiInstance,
bool setupIndex=
true) {
45 start_path = startFilePath;
47 setup_index = setupIndex;
53 virtual void begin()
override {
57 LOGW(
"SD.begin cs=%d failed", cs);
62 idx.begin(start_path, exension, file_name_pattern, setup_index);
71 virtual Stream *nextStream(
int offset = 1)
override {
72 LOGI(
"nextStream: %d", offset);
73 return selectStream(idx_pos + offset);
76 virtual Stream *selectStream(
int index)
override {
77 LOGI(
"selectStream: %d", index);
79 file_name = idx[index];
80 if (file_name==
nullptr)
return nullptr;
81 LOGI(
"Using file %s", file_name);
82 file = SD.open(file_name);
83 return file ? &file :
nullptr;
86 virtual Stream *selectStream(
const char *path)
override {
89 file_name = file.name();
90 LOGI(
"-> selectStream: %s", path);
91 return file ? &file :
nullptr;
96 void setFileFilter(
const char *filter) { file_name_pattern = filter; }
99 int index() {
return idx_pos; }
102 const char *
toStr() {
return file_name; }
105 virtual bool isAutoNext() {
return true; }
108 virtual void setPath(
const char *p) { start_path = p; }
111 long size() {
return idx.size();}
114 #if defined(USE_SD_NO_NS)
115 SDIndex<SDClass, File> idx{SD};
117 SDIndex<fs::SDFS,fs::File> idx{SD};
121 const char *file_name;
122 const char *exension =
nullptr;
123 const char *start_path =
nullptr;
124 const char *file_name_pattern =
"*";
125 bool setup_index =
true;
126 bool is_sd_setup =
false;
127 SPIClass *p_spi =
nullptr;
131 #ifdef USE_SD_SUPPORTS_SPI
132 return SD.begin(cs, *p_spi);