arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceFTP.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioSource.h"
5#include "FTPClient.h"
6
7namespace audio_tools {
8
25
26template <class ClientType>
28 public:
31 AudioSourceFTP(FTPClient<ClientType>& client, const char* path,
32 const char* ext, int files = 0) {
33 p_client = &client;
35 directory = path != nullptr ? path : "/";
36 p_ext = ext;
37 setMaxFiles(files);
38 }
39
41 bool begin() override {
42 TRACED();
43 idx = 0;
44 file.close();
45 file_count = count();
46 LOGI("Number of files: %d", file_count);
47 return file_count > 0;
48 }
49
51 void end() {
52 idx = 0;
53 file.close();
54 }
55
57 Stream* nextStream(int offset) override {
58 return selectStream(idx + offset);
59 };
60
62 Stream* previousStream(int offset) override { return nextStream(-offset); };
63
66 Stream* selectStream(int index) override {
67 if (!isValidIdx(index)) return nullptr;
68 Str path;
69 if (!findFile(index, path)) {
70 LOGW("No file at index %d", index);
71 return nullptr;
72 }
73 idx = index;
74 return openFile(path.c_str());
75 }
76
78 int index() override { return idx; }
79
82 Stream* selectStream(const char* path) override {
83 TRACED();
84 if (path == nullptr) return nullptr;
85 if (p_ext != nullptr && endsWith(path, p_ext)) return openFile(path);
86 directory = path;
87 if (!begin()) return nullptr;
88 return selectStream(0);
89 }
90
92 const char* toStr() override { return file.name(); }
93
95 void setMaxFiles(int maxCount) { max_files = maxCount; }
96
98 void setFileExtension(const char* ext) { p_ext = ext; }
99
101 size_t size() { return file_count < 0 ? 0 : file_count; }
102
103 protected:
104 FTPClient<ClientType>* p_client = nullptr;
105 FTPFile file;
107 int idx = 0;
108 int file_count = -1; // -1: unknown
109 size_t max_files = 0;
110 const char* p_ext = nullptr;
111
112 Stream* openFile(const char* path) {
113 file.close(); // close the previous file
114 LOGI("open %s", path);
115 file = p_client->open(path);
116 if (!file) {
117 LOGE("Could not open %s", path);
118 return nullptr;
119 }
120 return &file;
121 }
122
124 bool findFile(int index, Str& path) {
125 if (p_client == nullptr) return false;
126 int n = 0;
127 for (auto it = p_client->ls(directory.c_str()); it != it.end(); ++it) {
128 const char* name = it.fileName();
129 if (!endsWith(name, p_ext)) continue;
130 if (n++ == index) {
131 toPath(name, path);
132 return true;
133 }
134 }
135 return false;
136 }
137
139 int count() {
140 if (p_client == nullptr) return 0;
141 int n = 0;
142 for (auto it = p_client->ls(directory.c_str()); it != it.end(); ++it) {
143 if (!endsWith(it.fileName(), p_ext)) continue;
144 if (++n >= max_files && max_files > 0) break;
145 }
146 return n;
147 }
148
150 void toPath(const char* name, Str& result) {
151 if (strchr(name, '/') != nullptr) {
152 result = name;
153 return;
154 }
155 result = directory.c_str();
156 if (!result.isEmpty() && !result.endsWith("/")) result += "/";
157 result += name;
158 }
159
160 bool isValidIdx(int index) {
161 if (index < 0) return false;
162 if (max_files > 0 && index >= (int)max_files) return false;
163 if (file_count >= 0 && index >= file_count) {
164 LOGW("index %d is out of range (size: %d)", index, file_count);
165 return false;
166 }
167 return true;
168 }
169
170 bool endsWith(const char* file, const char* ext) {
171 if (file == nullptr) return false;
172 if (ext == nullptr) return true;
173 return StrView(file).endsWithIgnoreCase(ext);
174 }
175};
176
177} // namespace audio_tools
#define LOGW(...)
#define TRACED()
#define LOGI(...)
#define LOGE(...)
bool isValidIdx(int index)
Definition AudioSourceFTP.h:160
size_t size()
Returns the number of files that were found by begin().
Definition AudioSourceFTP.h:101
Stream * openFile(const char *path)
Definition AudioSourceFTP.h:112
bool endsWith(const char *file, const char *ext)
Definition AudioSourceFTP.h:170
FTPClient< ClientType > * p_client
Definition AudioSourceFTP.h:104
Stream * selectStream(const char *path) override
Definition AudioSourceFTP.h:82
Stream * selectStream(int index) override
Definition AudioSourceFTP.h:66
const char * toStr() override
provides the actual file name
Definition AudioSourceFTP.h:92
void toPath(const char *name, Str &result)
Some servers list the names with the directory, others without it.
Definition AudioSourceFTP.h:150
AudioSourceFTP(FTPClient< ClientType > &client, const char *path, const char *ext, int files=0)
Definition AudioSourceFTP.h:31
size_t max_files
Definition AudioSourceFTP.h:109
int index() override
Returns the actual index of the stream.
Definition AudioSourceFTP.h:78
Stream * nextStream(int offset) override
Returns next audio stream.
Definition AudioSourceFTP.h:57
int count()
Lists the directory and counts the matching files.
Definition AudioSourceFTP.h:139
void setFileExtension(const char *ext)
Defines the file extension filter (e.g. "mp3"): nullptr for all files.
Definition AudioSourceFTP.h:98
void end()
Closes the actual file.
Definition AudioSourceFTP.h:51
void setMaxFiles(int maxCount)
Defines the max number of files (if value is >0).
Definition AudioSourceFTP.h:95
bool findFile(int index, Str &path)
Lists the directory and provides the path of the n-th matching file.
Definition AudioSourceFTP.h:124
int file_count
Definition AudioSourceFTP.h:108
FTPFile file
Definition AudioSourceFTP.h:105
bool begin() override
Checks that the directory contains some files.
Definition AudioSourceFTP.h:41
const char * p_ext
Definition AudioSourceFTP.h:110
Str directory
Definition AudioSourceFTP.h:106
int idx
Definition AudioSourceFTP.h:107
Stream * previousStream(int offset) override
Returns previous audio stream.
Definition AudioSourceFTP.h:62
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
Str which keeps the data on the heap. We grow the allocated memory only if the copy source is not fit...
Definition Str.h:24
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:29
virtual bool endsWithIgnoreCase(const char *str)
checks if the string ends with the indicated substring
Definition StrView.h:218
Definition CodecOpusOgg.h:137
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition CodecOpusOgg.h:210
virtual bool isEmpty()
checks if the string is empty
Definition CodecOpusOgg.h:463
virtual const char * c_str()
provides the string value as const char*
Definition CodecOpusOgg.h:456
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6