Skip to content

Commit 7e1c60c

Browse files
committed
AudioPlayer for SD and SDMMC 2.0
1 parent 5f46d82 commit 7e1c60c

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

src/AudioLibs/SDDirect.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ class SDDirect {
5252
size_t requested_idx;
5353
long max_idx=-1;
5454
bool found = false;
55-
#ifdef USE_SDFAT
5655
List<String> file_path_stack;
5756
String file_path_str;
58-
#endif
5957

6058
const char *ext = nullptr;
6159
const char *file_name_pattern = nullptr;
@@ -148,20 +146,16 @@ class SDDirect {
148146

149147
void pushPath(const char* name){
150148
TRACED();
151-
#ifdef USE_SDFAT
152149
LOGD("pushPath: %s", name);
153150
String nameStr(name);
154151
file_path_stack.push_back(nameStr);
155-
#endif
156152
}
157153

158154
void popPath(){
159155
TRACED();
160-
#ifdef USE_SDFAT
161-
String str;
162-
file_path_stack.pop_back(str);
163-
LOGD("popPath: %s", str.c_str());
164-
#endif
156+
String str;
157+
file_path_stack.pop_back(str);
158+
LOGD("popPath: %s", str.c_str());
165159
}
166160

167161
/// checks if the file is a valid audio file
@@ -182,29 +176,34 @@ class SDDirect {
182176

183177
/// Returns the filename w/o path
184178
const char* fileName(FileT&file){
185-
#ifdef USE_SDFAT
179+
#ifdef USE_SDFAT
186180
// add name
187181
static char name[MAX_FILE_LEN];
188182
file.getName(name,MAX_FILE_LEN);
189183
return name;
190184
#else
191-
return file.name();
185+
Str tmp(file.name());
186+
int pos=0;
187+
// remove directories
188+
if (tmp.contains("/")){
189+
pos = tmp.lastIndexOf("/")+1;
190+
}
191+
return file.name()+pos;
192192
#endif
193193
}
194194

195195
/// Returns the filename including the path
196196
const char* fileNamePath(FileT &file){
197-
#ifdef USE_SDFAT
197+
#if defined(USE_SDFAT) || ESP_IDF_VERSION_MAJOR >= 4
198198
LOGD("-> fileNamePath: %s", fileName(file));
199199
file_path_str.clear();
200200
file_path_str += "/";
201201
for (int j=0; j<file_path_stack.size(); j++){
202202
file_path_str += file_path_stack[j]+"/";
203203
}
204-
205204
// add name
206205
static char name[MAX_FILE_LEN];
207-
file.getName(name,MAX_FILE_LEN);
206+
strncpy(name, fileName(file), MAX_FILE_LEN);
208207
file_path_str += name;
209208
const char* result = file_path_str.c_str();
210209
LOGD("<- fileNamePath: %s", result);
@@ -216,7 +215,7 @@ class SDDirect {
216215

217216
bool isHidden(FileT f){
218217
#ifdef USE_SDFAT
219-
return f.isHidden();
218+
return f.isHidden();
220219
#else
221220
return Str(f.name()).contains("/.");
222221
#endif

src/AudioLibs/SDIndex.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ class SDIndex {
4848
this->ext = extension;
4949
this->file_name_pattern = file_name_pattern;
5050
listDir(p, startDir);
51-
#ifdef USE_SDFAT
52-
file_path_stack.clear();
53-
file_path_str.clear();
54-
#endif
51+
file_path_stack.clear();
52+
file_path_str.clear();
5553

5654
}
5755

@@ -101,10 +99,8 @@ class SDIndex {
10199
String idx_path;
102100
String idx_defpath;
103101
SDT *p_sd = nullptr;
104-
#ifdef USE_SDFAT
105102
List<String> file_path_stack;
106103
String file_path_str;
107-
#endif
108104

109105
const char *ext = nullptr;
110106
const char *file_name_pattern = nullptr;
@@ -178,20 +174,16 @@ class SDIndex {
178174

179175
void pushPath(const char* name){
180176
LOGD("pushPath: %s", name);
181-
#ifdef USE_SDFAT
182177
LOGD("pushPath: %s", name);
183178
String nameStr(name);
184179
file_path_stack.push_back(nameStr);
185-
#endif
186180
}
187181

188182
void popPath(){
189183
TRACED();
190-
#ifdef USE_SDFAT
191184
String str;
192185
file_path_stack.pop_back(str);
193186
LOGD("popPath: %s", str.c_str());
194-
#endif
195187
}
196188

197189
/// checks if the file is a valid audio file
@@ -238,31 +230,36 @@ class SDIndex {
238230
#endif
239231
}
240232

241-
/// Returns the filename including the path
233+
/// Returns the filename w/o the path
242234
const char* fileName(FileT&file){
243235
#ifdef USE_SDFAT
244236
// add name
245237
static char name[MAX_FILE_LEN];
246238
file.getName(name,MAX_FILE_LEN);
247239
return name;
248240
#else
249-
return file.name();
241+
Str tmp(file.name());
242+
int pos=0;
243+
// remove directories
244+
if (tmp.contains("/")){
245+
pos = tmp.lastIndexOf("/")+1;
246+
}
247+
return file.name()+pos;
250248
#endif
251249
}
252250

253251
/// Returns the filename including the path
254-
const char* fileNamePath(FileT &file){
255-
#ifdef USE_SDFAT
252+
const char* fileNamePath(FileT &file){
253+
#if defined(USE_SDFAT) || ESP_IDF_VERSION_MAJOR >= 4
256254
LOGD("-> fileNamePath: %s", fileName(file));
257255
file_path_str.clear();
258256
file_path_str += "/";
259257
for (int j=0; j<file_path_stack.size(); j++){
260258
file_path_str += file_path_stack[j]+"/";
261259
}
262-
263260
// add name
264261
static char name[MAX_FILE_LEN];
265-
file.getName(name,MAX_FILE_LEN);
262+
strncpy(name, fileName(file), MAX_FILE_LEN);
266263
file_path_str += name;
267264
const char* result = file_path_str.c_str();
268265
LOGD("<- fileNamePath: %s", result);

0 commit comments

Comments
 (0)