arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceURL.h
1
2
3#pragma once
4#include "AudioSource.h"
5#include "AudioTools/Communication/HTTP/AbstractURLStream.h"
6#include "AudioToolsConfig.h"
7
8namespace audio_tools {
9
17 public:
18 template <typename T, size_t N>
19 AudioSourceURL(AbstractURLStream& urlStream, T (&urlArray)[N],
20 const char* mime, int startPos = 0) {
21 TRACED();
22 this->actual_stream = &urlStream;
23 this->mime = mime;
24 this->urlArray = urlArray;
25 this->max = N;
26 this->pos = startPos - 1;
27 this->timeout_auto_next_value = 20000;
28 }
29
30 virtual ~AudioSourceURL() { end(); }
31
33 virtual bool begin() override {
34 TRACED();
35 this->pos = 0;
36 return true;
37 }
38
39 void end() {
40 if (started) actual_stream->end();
41 started = false;
42 }
43
45 Stream* selectStream(int idx) override {
46 if (size() == 0) return nullptr;
47 pos = idx;
48 if (pos < 0) {
49 pos = 0;
50 LOGI("url array out of limits: %d -> %d", idx, pos);
51 }
52 if (pos >= size()) {
53 pos = size() - 1;
54 LOGI("url array out of limits: %d -> %d", idx, pos);
55 }
56 LOGI("selectStream: %d/%d -> %s", pos, size() - 1, value(pos));
57 if (started) actual_stream->end();
58 const char* url = value(pos);
59 if (url == nullptr) return nullptr;
60 actual_stream->begin(url, mime);
61 started = true;
62 return actual_stream;
63 }
64
66 Stream* nextStream(int offset) override {
67 pos += offset;
68 if (pos < 0 || pos >= size()) {
69 pos = 0;
70 }
71 LOGI("nextStream: %d/%d -> %s", pos, max - 1, value(pos));
72 return selectStream(pos);
73 }
74
76 Stream* previousStream(int offset) override {
77 pos -= offset;
78 if (pos < 0 || pos >= size()) {
79 pos = size() - 1;
80 }
81 LOGI("previousStream: %d/%d -> %s", pos, size() - 1, value(pos));
82 return selectStream(pos);
83 }
84
86 Stream* selectStream(const char* path) override {
87 LOGI("selectStream: %s", path);
88 if (started) actual_stream->end();
89 actual_stream->begin(path, mime);
90 started = true;
91 return actual_stream;
92 }
93
94 int index() { return pos; }
95
96 const char* toStr() { return value(pos); }
97
99 void setTimeout(int millisec) { actual_stream->setTimeout(millisec); }
100
101 // provides go not to the next on error
102 virtual bool isAutoNext() { return true; };
103
104 // only the ICYStream supports this
105 bool setMetadataCallback(void (*fn)(MetaDataType info, const char* str,
106 int len),
107 ID3TypeSelection sel = SELECT_ICY) {
108 TRACEI();
109 return actual_stream->setMetadataCallback(fn);
110 }
111
112 virtual int size() { return max; }
113
114 protected:
115 AbstractURLStream* actual_stream = nullptr;
116 const char** urlArray = nullptr;
117 int pos = 0;
118 int max = 0;
119 const char* mime = nullptr;
120 bool started = false;
121
123 AudioSourceURL() = default;
124
125 virtual const char* value(int pos) {
126 if (urlArray == nullptr || size() == 0) return nullptr;
127 return urlArray[pos];
128 }
129};
130
141 public:
142 template <typename T, size_t N>
143 AudioSourceDynamicURL(AbstractURLStream& urlStream, T (&urlArray)[N],
144 const char* mime, int startPos = 0) {
145 this->actual_stream = &urlStream;
146 this->mime = mime;
147 this->pos = startPos - 1;
148 this->timeout_auto_next_value = 20000;
149 for (int j = 0; j < N; j++) {
150 addURL(urlArray[j]);
151 }
152 }
153
155 const char* mime = nullptr, int startPos = 0) {
156 this->actual_stream = &urlStream;
157 this->mime = mime;
158 this->pos = startPos - 1;
159 this->timeout_auto_next_value = 20000;
160 }
161
163 void addURL(const char* url) { url_vector.push_back(url); }
164
165 void clear() { url_vector.clear(); }
166
167 int size() override { return url_vector.size(); }
168
170 Stream* selectStream(const char* path) override {
171 LOGI("selectStream: %s", path);
172 addURL(path);
173 if (started) actual_stream->end();
174 actual_stream->begin(path, mime);
175 started = true;
176 return actual_stream;
177 }
178
179 protected:
180 Vector<Str> url_vector;
181
182 const char* value(int pos) override { return url_vector[pos].c_str(); }
183};
184
185} // namespace audio_tools
Abstract Base class for all URLStream implementations.
Definition AbstractURLStream.h:17
Audio Source which provides the data via the network from an URL. The URLs are stored in an Vector of...
Definition AudioSourceURL.h:140
Stream * selectStream(const char *path) override
Opens the selected url and adds it to the list.
Definition AudioSourceURL.h:170
void addURL(const char *url)
add a new url: a copy of the string will be stored on the heap
Definition AudioSourceURL.h:163
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:16
Audio Source which provides the data via the network from an URL.
Definition AudioSourceURL.h:16
int index()
Returns the actual index of the stream.
Definition AudioSourceURL.h:94
Stream * selectStream(const char *path) override
Opens the selected url.
Definition AudioSourceURL.h:86
virtual bool begin() override
Setup Wifi URL.
Definition AudioSourceURL.h:33
const char * toStr()
provides the actual stream (e.g. file) name or url
Definition AudioSourceURL.h:96
Stream * selectStream(int idx) override
Opens the selected url from the array.
Definition AudioSourceURL.h:45
Stream * nextStream(int offset) override
Opens the next url from the array.
Definition AudioSourceURL.h:66
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceURL.h:102
void setTimeout(int millisec)
Sets the timeout of the URL Stream in milliseconds.
Definition AudioSourceURL.h:99
Stream * previousStream(int offset) override
Opens the Previous url from the array.
Definition AudioSourceURL.h:76
AudioSourceURL()=default
allow use with empty constructor in subclasses
Definition NoArduino.h:142
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
ID3TypeSelection
Enum to filter by type of metadata.
Definition AbstractMetaData.h:8
MetaDataType
Type of meta info.
Definition AbstractMetaData.h:11
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10