arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioSourceURL.h
1
2
3#pragma once
4#include "AudioConfig.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 void begin() override {
32 TRACED();
33 this->pos = 0;
34 }
35
37 Stream* selectStream(int idx) override {
38 pos = idx;
39 if (pos < 0) {
40 pos = 0;
41 LOGI("url array out of limits: %d -> %d", idx, pos);
42 }
43 if (pos >= size()) {
44 pos = size() - 1;
45 LOGI("url array out of limits: %d -> %d", idx, pos);
46 }
47 LOGI("selectStream: %d/%d -> %s", pos, size() - 1, value(pos));
48 if (started) actual_stream->end();
49 actual_stream->begin(value(pos), mime);
50 started = true;
51 return actual_stream;
52 }
53
55 Stream* nextStream(int offset) override {
56 pos += offset;
57 if (pos < 0 || pos >= size()) {
58 pos = 0;
59 }
60 LOGI("nextStream: %d/%d -> %s", pos, max - 1, value(pos));
61 return selectStream(pos);
62 }
63
65 Stream* previousStream(int offset) override {
66 pos -= offset;
67 if (pos < 0 || pos >= size()) {
68 pos = size() - 1;
69 }
70 LOGI("previousStream: %d/%d -> %s", pos, size() - 1, value(pos));
71 return selectStream(pos);
72 }
73
75 Stream* selectStream(const char* path) override {
76 LOGI("selectStream: %s", path);
77 if (started) actual_stream->end();
78 actual_stream->begin(path, mime);
79 started = true;
80 return actual_stream;
81 }
82
83 int index() { return pos; }
84
85 const char* toStr() { return value(pos); }
86
88 void setTimeout(int millisec) { actual_stream->setTimeout(millisec); }
89
90 // provides go not to the next on error
91 virtual bool isAutoNext() { return true; };
92
93 // only the ICYStream supports this
94 bool setMetadataCallback(void (*fn)(MetaDataType info, const char* str,
95 int len),
96 ID3TypeSelection sel = SELECT_ICY) {
97 TRACEI();
98 return actual_stream->setMetadataCallback(fn);
99 }
100
101 protected:
102 AbstractURLStream* actual_stream = nullptr;
103 const char** urlArray = nullptr;
104 int pos = 0;
105 int max = 0;
106 const char* mime = nullptr;
107 bool started = false;
108
110 AudioSourceURL() = default;
111
112 virtual const char* value(int pos) {
113 if (urlArray == nullptr) return nullptr;
114 return urlArray[pos];
115 }
116
117 virtual int size() { return max; }
118};
119
129 public:
130 template <typename T, size_t N>
131 AudioSourceDynamicURL(AbstractURLStream& urlStream, T (&urlArray)[N],
132 const char* mime, int startPos = 0) {
133 this->actual_stream = &urlStream;
134 this->mime = mime;
135 this->pos = startPos - 1;
136 this->timeout_auto_next_value = 20000;
137 for (int j = 0; j < N; j++) {
138 addURL(urlArray[j]);
139 }
140 }
141
142 AudioSourceDynamicURL(AbstractURLStream& urlStream, const char* mime,
143 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 }
149
151 void addURL(const char* url) { url_vector.push_back(url); }
152
153 void clear() { url_vector.clear(); }
154
155 protected:
156 Vector<Str> url_vector;
157
158 const char* value(int pos) override { return url_vector[pos].c_str(); }
159
160 int size() override { return url_vector.size(); }
161};
162
163} // 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:128
void addURL(const char *url)
add a new url: a copy of the string will be stored on the heap
Definition AudioSourceURL.h:151
Abstract Audio Data Source for the AudioPlayer which is used by the Audio Players.
Definition AudioSource.h:14
Audio Source which provides the data via the network from an URL.
Definition AudioSourceURL.h:16
Stream * selectStream(const char *path) override
Opens the selected url.
Definition AudioSourceURL.h:75
virtual void begin() override
Setup Wifi URL.
Definition AudioSourceURL.h:31
Stream * selectStream(int idx) override
Opens the selected url from the array.
Definition AudioSourceURL.h:37
Stream * nextStream(int offset) override
Opens the next url from the array.
Definition AudioSourceURL.h:55
virtual bool isAutoNext()
Returns default setting go to the next.
Definition AudioSourceURL.h:91
void setTimeout(int millisec)
Sets the timeout of the URL Stream in milliseconds.
Definition AudioSourceURL.h:88
Stream * previousStream(int offset) override
Opens the Previous url from the array.
Definition AudioSourceURL.h:65
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 AudioConfig.h:885