arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceIdxSDMMC.h
1#pragma once
2
3#include <FS.h>
4#include <SD_MMC.h>
5#include "AudioLogger.h"
6#include "AudioTools/Disk/AudioSource.h"
7#include "AudioTools/Disk/SDIndex.h"
8
9namespace audio_tools {
10
34public:
36 AudioSourceIdxSDMMC(const char *startFilePath = "/", const char *ext = ".mp3", bool setupIndex=true) {
37 start_path = startFilePath;
38 exension = ext;
39 setup_index = setupIndex;
40 }
41
42 virtual bool begin() override {
43 TRACED();
44 if (!is_sd_setup) {
45 if (!SD_MMC.begin("/sdcard", true)) {
46 LOGE("SD_MMC.begin failed");
47 return false;
48 }
49 is_sd_setup = true;
50 }
51 idx.begin(start_path, exension, file_name_pattern, setup_index);
52 idx_pos = 0;
53 return is_sd_setup;
54 }
55
56 void end() {
57 SD_MMC.end();
58 is_sd_setup = false;
59 }
60
61 virtual Stream *nextStream(int offset = 1) override {
62 LOGI("nextStream: %d", offset);
63 return selectStream(idx_pos + offset);
64 }
65
66 virtual Stream *selectStream(int index) override {
67 LOGI("selectStream: %d", index);
68 idx_pos = index;
69 file_name = idx[index];
70 if (file_name==nullptr) return nullptr;
71 LOGI("Using file %s", file_name);
72 file = SD_MMC.open(file_name);
73 return file ? &file : nullptr;
74 }
75
76 virtual Stream *selectStream(const char *path) override {
77 file.close();
78 file = SD_MMC.open(path);
79 file_name = file.name();
80 LOGI("-> selectStream: %s", path);
81 return file ? &file : nullptr;
82 }
83
86 void setFileFilter(const char *filter) { file_name_pattern = filter; }
87
89 int index() { return idx_pos; }
90
92 const char *toStr() { return file_name; }
93
94 // provides default setting go to the next
95 virtual bool isAutoNext() { return true; }
96
98 virtual void setPath(const char *p) { start_path = p; }
99
101 long size() { return idx.size();}
102
104 int indexOf(const char* filename) {
105 return idx.indexOf(filename);
106 }
107
109 const char* name(int pos) {
110 return idx[pos];
111 }
112
113protected:
115 File file;
116 size_t idx_pos = 0;
117 const char *file_name;
118 const char *exension = nullptr;
119 const char *start_path = nullptr;
120 const char *file_name_pattern = "*";
121 bool setup_index = true;
122 bool is_sd_setup = false;
123};
124
125} // namespace audio_tools
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:16
ESP32 AudioSource for AudioPlayer using an SD card as data source. An index file is used to speed up ...
Definition AudioSourceIdxSDMMC.h:33
int index()
Provides the current index position.
Definition AudioSourceIdxSDMMC.h:89
long size()
Provides the number of files (The max index is size()-1)
Definition AudioSourceIdxSDMMC.h:101
virtual Stream * selectStream(int index) override
Definition AudioSourceIdxSDMMC.h:66
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceIdxSDMMC.h:42
const char * toStr()
provides the actual file name
Definition AudioSourceIdxSDMMC.h:92
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceIdxSDMMC.h:98
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceIdxSDMMC.h:95
int indexOf(const char *filename)
Provides the index of the file with the given name.
Definition AudioSourceIdxSDMMC.h:104
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceIdxSDMMC.h:61
void setFileFilter(const char *filter)
Definition AudioSourceIdxSDMMC.h:86
AudioSourceIdxSDMMC(const char *startFilePath="/", const char *ext=".mp3", bool setupIndex=true)
Default constructor.
Definition AudioSourceIdxSDMMC.h:36
const char * name(int pos)
Provides the file name for the indicated index.
Definition AudioSourceIdxSDMMC.h:109
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceIdxSDMMC.h:76
We store all the relevant file names in an sequential index file. Form there we can access them via a...
Definition SDIndex.h:20
int indexOf(const char *filename)
Find the index of a filename.
Definition SDIndex.h:115
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10