arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceFTP.h
Go to the documentation of this file.
1#include "FTPClient.h"
2#include "AudioSource.h"
3#include "vector"
4
5namespace audio_tools {
6
22template <class ClientType>
24 public:
26 AudioSourceFTP(FTPClient<ClientType>& client, const char* path, const char* ext,
27 int files = 0) {
28 p_client = &client;
30 if (path) p_path = path;
32 }
33
35 bool begin()override {
36 TRACED();
37 idx = 0;
38 files.clear();
39 return addDirectory(p_path);
40 }
41
43 void end() {
44 idx = 0;
45 files.clear();
46 }
47
49 Stream* nextStream(int offset) override {
50 int tmp_idx = idx + offset;
51 if (!isValidIdx(tmp_idx)) return nullptr;
52 return selectStream(tmp_idx);
53 };
54
56 Stream* previousStream(int offset) override { return nextStream(-offset); };
57
60 Stream* selectStream(int index) override {
61 if (!isValidIdx(index)) return nullptr;
62 idx = index;
63 if (file) {
64 file.close(); // close the previous file
65 }
66 file = p_client->open(files[idx].name());
67 return &file;
68 }
69
71 int index() override { return idx; }
72
74 Stream* selectStream(const char* path) override {
75 TRACED();
76 files.clear();
77 idx = 0;
78 addDirectory(path);
79 return selectStream(0);
80 }
81
83 const char* toStr() override {
84 return file.name();
85 }
86
89
91 bool addDirectory(const char* path) {
92 TRACED();
93 if (p_client == nullptr) return false;
94 FTPFile dir = p_client->open(path);
95 addFiles(dir, 1);
96 return true;
97 }
98
100 size_t size() { return files.size(); }
101
102 protected:
103 std::vector<FTPFile> files;
106 int idx = 0;
107 size_t max_files = 0;
108 const char* p_ext = nullptr;
109 const char* p_path = "/";
110 bool is_first = true;
111
113 void addFiles(FTPFile& dir, int level) {
114 if (!endsWith(dir.name(), p_ext)) {
115 LOGI("adding file %s", dir.name());
116 files.push_back(std::move(dir));
117 } else {
118 for (const auto& file : p_client->ls(dir.name())) {
119 if (endsWith(file.name(), p_ext)) {
120 LOGI("adding file %s", file.name());
121 files.push_back(std::move(file));
122 }
123 if (max_files > 0 && files.size() >= max_files) {
124 LOGI("max files reached: %d", max_files);
125 return; // stop if we reached the max number of files
126 }
127 }
128 }
129 }
130
131 bool isValidIdx(int index) {
132 if (index < 0) return false;
133 if (index >= files.size()) {
134 LOGE("index %d is out of range (size: %d)", index, files.size());
135 return false;
136 }
137 return true;
138 }
139
140 bool endsWith(const char* file, const char* ext) {
141 if (file == nullptr) return false;
142 if (p_ext == nullptr) return true;
143 return (StrView(file).endsWith(ext));
144 }
145};
146
147} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:136
An AudioSource that uses the https://github.com/pschatzmann/TinyFTPClient library to retrieve files f...
Definition AudioSourceFTP.h:23
bool isValidIdx(int index)
Definition AudioSourceFTP.h:131
size_t size()
Returns the number of available files.
Definition AudioSourceFTP.h:100
bool endsWith(const char *file, const char *ext)
Definition AudioSourceFTP.h:140
FTPClient< ClientType > * p_client
Definition AudioSourceFTP.h:104
const char * p_path
Definition AudioSourceFTP.h:109
Stream * selectStream(const char *path) override
Returns the FTPFile for the indicated path.
Definition AudioSourceFTP.h:74
Stream * selectStream(int index) override
Definition AudioSourceFTP.h:60
bool is_first
Definition AudioSourceFTP.h:110
const char * toStr() override
provides the actual stream (e.g. file) name or url
Definition AudioSourceFTP.h:83
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:26
size_t max_files
Definition AudioSourceFTP.h:107
int index() override
Retrieves all files and returns the actual index of the stream.
Definition AudioSourceFTP.h:71
std::vector< FTPFile > files
Definition AudioSourceFTP.h:103
Stream * nextStream(int offset) override
Returns next audio stream.
Definition AudioSourceFTP.h:49
void end()
Resets the actual data.
Definition AudioSourceFTP.h:43
void setMaxFiles(int maxCount)
Defines the max number of files (if value is >0)
Definition AudioSourceFTP.h:88
FTPFile file
Definition AudioSourceFTP.h:105
bool begin() override
Resets the actual data.
Definition AudioSourceFTP.h:35
const char * p_ext
Definition AudioSourceFTP.h:108
bool addDirectory(const char *path)
Adds all the files of a directory.
Definition AudioSourceFTP.h:91
int idx
Definition AudioSourceFTP.h:106
Stream * previousStream(int offset) override
Returns previous audio stream.
Definition AudioSourceFTP.h:56
void addFiles(FTPFile &dir, int level)
Adds all files recursively.
Definition AudioSourceFTP.h:113
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:17
int timeout_auto_next_value
Definition AudioSource.h:76
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508