Skip to content

Commit 44ef791

Browse files
committed
AudioSource: add indexOf
1 parent 90ea78f commit 44ef791

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/AudioTools/Disk/AudioSource.h

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,9 @@ class AudioSourceVector : public AudioSource {
230230
TRACED();
231231
if (path == nullptr) return nullptr;
232232

233-
// Find the file by path
234-
for (int i = 0; i < (int)files.size(); i++) {
235-
Str fullPath = getFullPath(i);
236-
if (fullPath.equals(path)) {
237-
return selectStream(i);
238-
}
233+
int idx = indexOf(path);
234+
if (idx >= 0) {
235+
return selectStream(idx);
239236
}
240237

241238
LOGE("File not found: %s", path);
@@ -247,6 +244,20 @@ class AudioSourceVector : public AudioSource {
247244
return current_index;
248245
}
249246

247+
/// Find index of file by path
248+
int indexOf(const char* path) {
249+
if (path == nullptr) return -1;
250+
251+
// Find the file by path
252+
for (int i = 0; i < (int)files.size(); i++) {
253+
Str fullPath = getFullPath(i);
254+
if (fullPath.equals(path)) {
255+
return i;
256+
}
257+
}
258+
return -1;
259+
}
260+
250261
/// Add a file with full path (path and name will be separated automatically)
251262
void addName(const char* nameWithPath) {
252263
TRACED();
@@ -326,6 +337,11 @@ class AudioSourceVector : public AudioSource {
326337
}
327338
return nullptr;
328339
}
340+
341+
/// provides the name at the given index
342+
const char* name(int index){
343+
return getFullPath(index).c_str();
344+
}
329345

330346
protected:
331347
Vector<FileEntry> files;
@@ -464,12 +480,9 @@ class AudioSourceArray : public AudioSource {
464480
TRACED();
465481
if (path == nullptr) return nullptr;
466482

467-
// Find the file by path
468-
for (int i = 0; i < (int)array_size; i++) {
469-
const char* filePath = file_array[i];
470-
if (StrView(path).equals(filePath)) {
471-
return selectStream(i);
472-
}
483+
int idx = indexOf(path);
484+
if (idx >= 0) {
485+
return selectStream(idx);
473486
}
474487

475488
LOGE("File not found: %s", path);
@@ -481,6 +494,20 @@ class AudioSourceArray : public AudioSource {
481494
return current_index;
482495
}
483496

497+
/// Find index of file by path
498+
int indexOf(const char* path) {
499+
if (path == nullptr) return -1;
500+
501+
// Find the file by path
502+
for (int i = 0; i < (int)array_size; i++) {
503+
const char* filePath = file_array[i];
504+
if (filePath && StrView(path).equals(filePath)) {
505+
return i;
506+
}
507+
}
508+
return -1;
509+
}
510+
484511
/// Set the array of file names
485512
template<size_t N>
486513
void setArray(const char* (&nameArray)[N]) {
@@ -533,7 +560,13 @@ class AudioSourceArray : public AudioSource {
533560
}
534561
return nullptr;
535562
}
563+
564+
/// provides the name at the given index
565+
const char* name(int index){
566+
return getFilePath(index);
567+
}
536568

569+
537570
protected:
538571
const char* const* file_array = nullptr; // Pointer to array of const char*
539572
size_t array_size = 0;

0 commit comments

Comments
 (0)