arduino-audio-tools
Loading...
Searching...
No Matches
AudioSourceURL.h
1
2
3#pragma once
4#include "AudioToolsConfig.h"
5#include "AudioSource.h"
6#include "AudioTools/Communication/HTTP/AbstractURLStream.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() {
31 end();
32 }
33
34
36 virtual bool begin() override {
37 TRACED();
38 this->pos = 0;
39 return true;
40 }
41
42 void end() {
43 if (started) actual_stream->end();
44 started = false;
45 }
46
48 Stream* selectStream(int idx) override {
49 pos = idx;
50 if (pos < 0) {
51 pos = 0;
52 LOGI("url array out of limits: %d -> %d", idx, pos);
53 }
54 if (pos >= size()) {
55 pos = size() - 1;
56 LOGI("url array out of limits: %d -> %d", idx, pos);
57 }
58 LOGI("selectStream: %d/%d -> %s", pos, size() - 1, value(pos));
59 if (started) actual_stream->end();
60 actual_stream->begin(value(pos), 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 protected:
113 AbstractURLStream* actual_stream = nullptr;
114 const char** urlArray = nullptr;
115 int pos = 0;
116 int max = 0;
117 const char* mime = nullptr;
118 bool started = false;
119
121 AudioSourceURL() = default;
122
123 virtual const char* value(int pos) {
124 if (urlArray == nullptr) return nullptr;
125 return urlArray[pos];
126 }
127
128 virtual int size() { return max; }
129};
130
140 public:
141 template <typename T, size_t N>
142 AudioSourceDynamicURL(AbstractURLStream& urlStream, T (&urlArray)[N],
143 const char* mime, int startPos = 0) {
144 this->actual_stream = &urlStream;
145 this->mime = mime;
146 this->pos = startPos - 1;
147 this->timeout_auto_next_value = 20000;
148 for (int j = 0; j < N; j++) {
149 addURL(urlArray[j]);
150 }
151 }
152
153 AudioSourceDynamicURL(AbstractURLStream& urlStream, const char* mime,
154 int startPos = 0) {
155 this->actual_stream = &urlStream;
156 this->mime = mime;
157 this->pos = startPos - 1;
158 this->timeout_auto_next_value = 20000;
159 }
160
162 void addURL(const char* url) { url_vector.push_back(url); }
163
164 void clear() { url_vector.clear(); }
165
166 protected:
167 Vector<Str> url_vector;
168
169 const char* value(int pos) override { return url_vector[pos].c_str(); }
170
171 int size() override { return url_vector.size(); }
172};
173
174} // 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:139
void addURL(const char *url)
add a new url: a copy of the string will be stored on the heap
Definition AudioSourceURL.h:162
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:36
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:48
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
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