arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceLittleFS.h
1#pragma once
2
3#include <LittleFS.h>
4#include "AudioLogger.h"
5#include "AudioTools/Disk/AudioSource.h"
6#include "AudioTools/Disk/SDDirect.h"
7
8namespace audio_tools {
9
17public:
19 AudioSourceLittleFS(const char *startFilePath = "/", const char *ext = ".mp3") {
20 start_path = startFilePath;
21 exension = ext;
22 }
23
24 virtual bool begin() override {
25 TRACED();
26 if (!is_sd_setup) {
27 int retry = 10;
28 while (!LittleFS.begin()) {
29 LOGE("LittleFS.begin failed");
30 delay(500);
31 if (--retry >= 0) {
32 return false;
33 }
34 }
35 is_sd_setup = true;
36 }
37 idx.begin(start_path, exension, file_name_pattern);
38 idx_pos = 0;
39 return is_sd_setup;
40 }
41
42 void end() {
43 LittleFS.end();
44 is_sd_setup = false;
45 }
46
47 virtual Stream *nextStream(int offset = 1) override {
48 LOGI("nextStream: %d", offset);
49 return selectStream(idx_pos + offset);
50 }
51
52 virtual Stream *selectStream(int index) override {
53 LOGI("selectStream: %d", index);
54 idx_pos = index;
55 file_name = idx[index];
56 if (file_name==nullptr) return nullptr;
57 LOGI("Using file %s", file_name);
58 file = LittleFS.open(file_name,"r");
59 return file ? &file : nullptr;
60 }
61
62 virtual Stream *selectStream(const char *path) override {
63 file.close();
64 file = LittleFS.open(path,"r");
65 file_name = file.name();
66 LOGI("-> selectStream: %s", path);
67 return file ? &file : nullptr;
68 }
69
72 void setFileFilter(const char *filter) { file_name_pattern = filter; }
73
75 int index() { return idx_pos; }
76
78 const char *toStr() { return file_name; }
79
80 // provides default setting go to the next
81 virtual bool isAutoNext() { return true; }
82
84 virtual void setPath(const char *p) { start_path = p; }
85
87 long size() { return idx.size();}
88
89protected:
90#ifdef RP2040_HOWER
91 SDDirect<FS,File> idx{LittleFS};
92#else
93 SDDirect<fs::LittleFSFS,fs::File> idx{LittleFS};
94#endif
95 File file;
96 size_t idx_pos = 0;
97 const char *file_name;
98 const char *exension = nullptr;
99 const char *start_path = nullptr;
100 const char *file_name_pattern = "*";
101 bool is_sd_setup = false;
102};
103
104} // 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 the LittleFS file system.
Definition AudioSourceLittleFS.h:16
int index()
Provides the current index position.
Definition AudioSourceLittleFS.h:75
long size()
Provides the number of files (The max index is size()-1): WARNING this is very slow if you have a lot...
Definition AudioSourceLittleFS.h:87
virtual Stream * selectStream(int index) override
Definition AudioSourceLittleFS.h:52
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceLittleFS.h:24
AudioSourceLittleFS(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition AudioSourceLittleFS.h:19
const char * toStr()
provides the actual file name
Definition AudioSourceLittleFS.h:78
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceLittleFS.h:84
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceLittleFS.h:81
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceLittleFS.h:47
void setFileFilter(const char *filter)
Definition AudioSourceLittleFS.h:72
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceLittleFS.h:62
We access the files directy with an index. The index is determined by a recurseve tree walk thru the ...
Definition SDDirect.h:22
long size()
Provides the number of files.
Definition SDDirect.h:50
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10