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