arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSourceFTP.h
1#include "FTPClient.h"
2#include "AudioSource.h"
3#include "vector"
4
5namespace audio_tools {
6
20template <class ClientType>
22 public:
24 AudioSourceFTP(FTPClient<ClientType>& client, const char* path, const char* ext,
25 int files = 0) {
26 p_client = &client;
27 timeout_auto_next_value = 5000;
28 if (path) p_path = path;
29 setMaxFiles(files);
30 }
31
33 void begin()override {
34 TRACED();
35 idx = 0;
36 files.clear();
37 addDirectory(p_path);
38 }
39
41 void end() {
42 idx = 0;
43 files.clear();
44 }
45
47 Stream* nextStream(int offset) override {
48 int tmp_idx = idx + offset;
49 if (!isValidIdx(tmp_idx)) return nullptr;
50 return selectStream(tmp_idx);
51 };
52
54 Stream* previousStream(int offset) override { return nextStream(-offset); };
55
58 Stream* selectStream(int index) override {
59 if (!isValidIdx(index)) return nullptr;
60 idx = index;
61 if (file) {
62 file.close(); // close the previous file
63 }
64 file = p_client->open(files[idx].name());
65 return &file;
66 }
67
69 int index() override { return idx; }
70
72 Stream* selectStream(const char* path) override {
73 TRACED();
74 files.clear();
75 idx = 0;
76 addDirectory(path);
77 return selectStream(0);
78 }
79
81 const char* toStr() override {
82 return file.name();
83 }
84
86 void setMaxFiles(int maxCount) { max_files = maxCount; }
87
89 bool addDirectory(const char* path) {
90 TRACED();
91 if (p_client == nullptr) return false;
92 FTPFile dir = p_client->open(path);
93 addFiles(dir, 1);
94 return true;
95 }
96
98 size_t size() { return files.size(); }
99
100 protected:
101 std::vector<FTPFile> files;
102 FTPClient<ClientType>* p_client = nullptr;
103 FTPFile file;
104 int idx = 0;
105 size_t max_files = 0;
106 const char* p_ext = nullptr;
107 const char* p_path = "/";
108 bool is_first = true;
109
111 void addFiles(FTPFile& dir, int level) {
112 if (!endsWith(dir.name(), p_ext)) {
113 LOGI("adding file %s", dir.name());
114 files.push_back(std::move(dir));
115 } else {
116 for (const auto& file : p_client->ls(dir.name())) {
117 if (endsWith(file.name(), p_ext)) {
118 LOGI("adding file %s", file.name());
119 files.push_back(std::move(file));
120 }
121 if (max_files > 0 && files.size() >= max_files) {
122 LOGI("max files reached: %d", max_files);
123 return; // stop if we reached the max number of files
124 }
125 }
126 }
127 }
128
129 bool isValidIdx(int index) {
130 if (index < 0) return false;
131 if (index >= files.size()) {
132 LOGE("index %d is out of range (size: %d)", index, files.size());
133 return false;
134 }
135 return true;
136 }
137
138 bool endsWith(const char* file, const char* ext) {
139 if (file == nullptr) return false;
140 if (p_ext == nullptr) return true;
141 return (StrView(file).endsWith(ext));
142 }
143};
144
145} // namespace audio_tools
An AudioSource that uses the https://github.com/pschatzmann/TinyFTPClient library to retrieve files f...
Definition AudioSourceFTP.h:21
size_t size()
Returns the number of available files.
Definition AudioSourceFTP.h:98
Stream * selectStream(const char *path) override
Returns the FTPFile for the indicated path.
Definition AudioSourceFTP.h:72
Stream * selectStream(int index) override
Definition AudioSourceFTP.h:58
const char * toStr() override
provides the actual stream (e.g. file) name or url
Definition AudioSourceFTP.h:81
void begin() override
Resets the actual data.
Definition AudioSourceFTP.h:33
AudioSourceFTP(FTPClient< ClientType > &client, const char *path, const char *ext, int files=0)
Default constructor: Provide the client class as template argument e.g. AudioSourceFTP<WiFiClient> so...
Definition AudioSourceFTP.h:24
int index() override
Retrieves all files and returns the actual index of the stream.
Definition AudioSourceFTP.h:69
Stream * nextStream(int offset) override
Returns next audio stream.
Definition AudioSourceFTP.h:47
void end()
Resets the actual data.
Definition AudioSourceFTP.h:41
void setMaxFiles(int maxCount)
Defines the max number of files (if value is >0)
Definition AudioSourceFTP.h:86
bool addDirectory(const char *path)
Adds all the files of a directory.
Definition AudioSourceFTP.h:89
Stream * previousStream(int offset) override
Returns previous audio stream.
Definition AudioSourceFTP.h:54
void addFiles(FTPFile &dir, int level)
Adds all files recursively.
Definition AudioSourceFTP.h:111
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:15
Definition NoArduino.h:142
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10