arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceLittleFS.h
Go to the documentation of this file.
1#pragma once
2
3#include <LittleFS.h>
4#include "AudioLogger.h"
7
8namespace audio_tools {
9
17public:
19 AudioSourceLittleFS(const char *startFilePath = "/", const char *ext = ".mp3") {
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 }
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;
56 if (file_name==nullptr) return nullptr;
57 LOGI("Using file %s", file_name);
59 return file ? &file : nullptr;
60 }
61
62 virtual Stream *selectStream(const char *path) override {
63 file.close();
64 file = LittleFS.open(path,"r");
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
92#else
94#endif
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
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
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
const char * exension
Definition AudioSourceLittleFS.h:98
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
bool is_sd_setup
Definition AudioSourceLittleFS.h:101
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
File file
Definition AudioSourceLittleFS.h:95
size_t idx_pos
Definition AudioSourceLittleFS.h:96
AudioSourceLittleFS(const char *startFilePath="/", const char *ext=".mp3")
Default constructor.
Definition AudioSourceLittleFS.h:19
SDDirect< fs::LittleFSFS, fs::File > idx
Definition AudioSourceLittleFS.h:93
const char * start_path
Definition AudioSourceLittleFS.h:99
const char * toStr()
provides the actual file name
Definition AudioSourceLittleFS.h:78
const char * file_name
Definition AudioSourceLittleFS.h:97
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 end()
Definition AudioSourceLittleFS.h:42
void setFileFilter(const char *filter)
Definition AudioSourceLittleFS.h:72
const char * file_name_pattern
Definition AudioSourceLittleFS.h:100
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
void begin(const char *startDir, const char *extension, const char *file_name_pattern)
Definition SDDirect.h:26
Definition NoArduino.h:142
Arduino File support using std::fstream.
Definition VFSFile.h:33
void open(const char *name, FileMode mode=VFS_FILE_READ)
Definition VFSFile.h:45
void close()
Definition VFSFile.h:180
const char * name() const
Definition VFSFile.h:182
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(unsigned long ms)
Definition Time.h:23
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512