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/CoreAudio/AudioHttp/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
31 virtual bool begin() override {
32 TRACED();
33 this->pos = 0;
34 return true;
35 }
36
38 Stream* selectStream(int idx) override {
39 pos = idx;
40 if (pos < 0) {
41 pos = 0;
42 LOGI("url array out of limits: %d -> %d", idx, pos);
43 }
44 if (pos >= size()) {
45 pos = size() - 1;
46 LOGI("url array out of limits: %d -> %d", idx, pos);
47 }
48 LOGI("selectStream: %d/%d -> %s", pos, size() - 1, value(pos));
49 if (started) actual_stream->end();
50 actual_stream->begin(value(pos), mime);
51 started = true;
52 return actual_stream;
53 }
54
56 Stream* nextStream(int offset) override {
57 pos += offset;
58 if (pos < 0 || pos >= size()) {
59 pos = 0;
60 }
61 LOGI("nextStream: %d/%d -> %s", pos, max - 1, value(pos));
62 return selectStream(pos);
63 }
64
66 Stream* previousStream(int offset) override {
67 pos -= offset;
68 if (pos < 0 || pos >= size()) {
69 pos = size() - 1;
70 }
71 LOGI("previousStream: %d/%d -> %s", pos, size() - 1, value(pos));
72 return selectStream(pos);
73 }
74
76 Stream* selectStream(const char* path) override {
77 LOGI("selectStream: %s", path);
78 if (started) actual_stream->end();
79 actual_stream->begin(path, mime);
80 started = true;
81 return actual_stream;
82 }
83
84 int index() { return pos; }
85
86 const char* toStr() { return value(pos); }
87
89 void setTimeout(int millisec) { actual_stream->setTimeout(millisec); }
90
91 // provides go not to the next on error
92 virtual bool isAutoNext() { return true; };
93
94 // only the ICYStream supports this
95 bool setMetadataCallback(void (*fn)(MetaDataType info, const char* str,
96 int len),
97 ID3TypeSelection sel = SELECT_ICY) {
98 TRACEI();
99 return actual_stream->setMetadataCallback(fn);
100 }
101
102 protected:
103 AbstractURLStream* actual_stream = nullptr;
104 const char** urlArray = nullptr;
105 int pos = 0;
106 int max = 0;
107 const char* mime = nullptr;
108 bool started = false;
109
111 AudioSourceURL() = default;
112
113 virtual const char* value(int pos) {
114 if (urlArray == nullptr) return nullptr;
115 return urlArray[pos];
116 }
117
118 virtual int size() { return max; }
119};
120
130 public:
131 template <typename T, size_t N>
132 AudioSourceDynamicURL(AbstractURLStream& urlStream, T (&urlArray)[N],
133 const char* mime, int startPos = 0) {
134 this->actual_stream = &urlStream;
135 this->mime = mime;
136 this->pos = startPos - 1;
137 this->timeout_auto_next_value = 20000;
138 for (int j = 0; j < N; j++) {
139 addURL(urlArray[j]);
140 }
141 }
142
143 AudioSourceDynamicURL(AbstractURLStream& urlStream, const char* mime,
144 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 }
150
152 void addURL(const char* url) { url_vector.push_back(url); }
153
154 void clear() { url_vector.clear(); }
155
156 protected:
157 Vector<Str> url_vector;
158
159 const char* value(int pos) override { return url_vector[pos].c_str(); }
160
161 int size() override { return url_vector.size(); }
162};
163
164} // 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:129
void addURL(const char *url)
add a new url: a copy of the string will be stored on the heap
Definition AudioSourceURL.h:152
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:84
Stream * selectStream(const char *path) override
Opens the selected url.
Definition AudioSourceURL.h:76
virtual bool begin() override
Setup Wifi URL.
Definition AudioSourceURL.h:31
const char * toStr()
provides the actual stream (e.g. file) name or url
Definition AudioSourceURL.h:86
Stream * selectStream(int idx) override
Opens the selected url from the array.
Definition AudioSourceURL.h:38
Stream * nextStream(int offset) override
Opens the next url from the array.
Definition AudioSourceURL.h:56
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceURL.h:92
void setTimeout(int millisec)
Sets the timeout of the URL Stream in milliseconds.
Definition AudioSourceURL.h:89
Stream * previousStream(int offset) override
Opens the Previous url from the array.
Definition AudioSourceURL.h:66
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