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 bool begin() override {
25 TRACED();
26 if (!is_sd_setup) {
27 int retry = 10;
28 while (!SPIFFS.begin()) {
29 LOGE("SPIFFS.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 SPIFFS.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 = SPIFFS.open(file_name);
59 return file ? &file : nullptr;
60 }
61
62 virtual Stream *selectStream(const char *path) override {
63 file.close();
64 file = SPIFFS.open(path);
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:
91 File file;
92 size_t idx_pos = 0;
93 const char *file_name;
94 const char *exension = nullptr;
95 const char *start_path = nullptr;
96 const char *file_name_pattern = "*";
97 bool is_sd_setup = false;
98};
99
100} // 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:75
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:87
virtual Stream * selectStream(int index) override
Definition AudioSourceSPIFFS.h:52
virtual bool begin() override
Reset actual stream and move to root.
Definition AudioSourceSPIFFS.h:24
const char * toStr()
provides the actual file name
Definition AudioSourceSPIFFS.h:78
virtual void setPath(const char *p)
Allows to "correct" the start path if not defined in the constructor.
Definition AudioSourceSPIFFS.h:84
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceSPIFFS.h:81
virtual Stream * nextStream(int offset=1) override
Returns next audio stream.
Definition AudioSourceSPIFFS.h:47
void setFileFilter(const char *filter)
Definition AudioSourceSPIFFS.h:72
virtual Stream * selectStream(const char *path) override
Returns audio stream by path: The index is not changed!
Definition AudioSourceSPIFFS.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
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