Skip to content

Commit e010d74

Browse files
committed
AudioPlayer corrections
1 parent d767a1a commit e010d74

File tree

3 files changed

+70
-37
lines changed

3 files changed

+70
-37
lines changed

examples/examples-player/player-sd-a2dp/player-sd-a2dp.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* @file player-sd-a2dp.ino
3+
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-player/player-sd-a2dp/README.md
4+
*
5+
* @author Phil Schatzmann
6+
* @copyright GPLv3
7+
*/
8+
19
// set this in AudioConfig.h or here after installing https://github.com/pschatzmann/arduino-libhelix.git
210
#define USE_HELIX
311
#define USE_A2DP

examples/tests/test-player/test-player.ino

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,27 @@ void testSD() {
4343
}
4444
}
4545
Serial.println("--------------------------");
46+
}
4647

48+
void testSDNext() {
49+
sdsource.begin();
50+
for (int j=0;j<20;j++){
51+
Stream *out = sdsource.nextStream(1);
52+
Serial.printf("%d -> %d / %s \n", j, sdsource.index(), sdsource.toStr());
53+
if (out!=nullptr){
54+
assert(out->available()>0);
55+
}
56+
}
57+
Serial.println("--------------------------");
4758
}
4859

60+
4961
void setup() {
5062
Serial.begin(115200);
5163
AudioLogger::instance().begin(Serial, AudioLogger::Error);
5264
testUrl();
5365
testSD();
66+
testSDNext();
5467
}
5568

5669
void loop(){

src/AudioTools/AudioPlayer.h

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ namespace audio_tools {
5050
return nullptr;
5151
}
5252

53+
virtual void setTimeoutMs(int millisec) {
54+
timeout_value = millisec;
55+
}
56+
5357
/// Provides the timeout which is triggering to move to the next stream
5458
virtual int timeoutMs() {
55-
return 500;
59+
return timeout_value;
5660
}
5761

62+
protected:
63+
int timeout_value = 500;
64+
5865
};
5966

6067
/**
@@ -74,19 +81,19 @@ namespace audio_tools {
7481
}
7582

7683
/// Reset actual stream and move to root
77-
virtual void begin() {
84+
virtual void begin() override {
7885
LOGD(LOG_METHOD);
7986
if (onStartCallback != nullptr) onStartCallback();
8087
};
8188

8289
/// Returns next (with positive index) or previous stream (with negative index)
83-
virtual Stream* nextStream(int offset) {
90+
virtual Stream* nextStream(int offset) override {
8491
LOGD(LOG_METHOD);
8592
return nextStreamCallback == nullptr ? nullptr : nextStreamCallback();
8693
}
8794

8895
/// Returns selected audio stream
89-
virtual Stream* selectStream(int index) {
96+
virtual Stream* selectStream(int index) override {
9097
return indexStreamCallback == nullptr ? nullptr : indexStreamCallback(index);
9198
}
9299

@@ -102,7 +109,6 @@ namespace audio_tools {
102109
indexStreamCallback = callback;
103110
}
104111

105-
106112
protected:
107113
void (*onStartCallback)() = nullptr;
108114
Stream* (*nextStreamCallback)() = nullptr;
@@ -139,25 +145,22 @@ namespace audio_tools {
139145
exension = ext;
140146
}
141147

142-
virtual void begin() {
148+
virtual void begin() override {
143149
LOGD(LOG_METHOD);
144-
pos = 0;
150+
idx_pos = 0;
145151
}
146152

147-
virtual Stream* nextStream(int offset) {
148-
LOGD(LOG_METHOD);
149-
return selectStream(pos+offset);
153+
virtual Stream* nextStream(int offset) override {
154+
LOGW("-> nextStream: %d", offset);
155+
return selectStream(idx_pos+offset);
150156
}
151157

152-
virtual Stream* selectStream(int index) {
153-
pos = index;
154-
if (pos<0){
155-
pos = 0;
156-
}
158+
virtual Stream* selectStream(int index) override {
159+
idx_pos = index<0 ? 0 : index;
157160
file.close();
158-
file = getFile(start_path, pos);
161+
file = getFile(start_path, idx_pos);
159162
file.getName(file_name, MAX_FILE_LEN);
160-
LOGI("-> selectStream: '%s'", file_name);
163+
LOGW("-> selectStream: %d '%s'", idx_pos, file_name);
161164
return file ? &file : nullptr;
162165
}
163166

@@ -166,10 +169,20 @@ namespace audio_tools {
166169
file_name_pattern = filter;
167170
}
168171

172+
/// Provides the current index position
173+
int index() {
174+
return idx_pos;
175+
}
176+
177+
/// provides the actual file name
178+
const char *toStr() {
179+
return file_name;
180+
}
181+
169182
protected:
170183
AudioFile file;
171184
AudioFs sd;
172-
size_t pos = 0;
185+
size_t idx_pos = 0;
173186
char file_name[MAX_FILE_LEN];
174187
const char* exension = nullptr;
175188
const char* start_path = nullptr;
@@ -258,15 +271,16 @@ namespace audio_tools {
258271
this->urlArray = urlArray;
259272
this->max = N;
260273
this->pos = startPos - 1;
274+
this->timeout_value = 60000;
261275
}
262276

263277
/// Setup Wifi URL
264-
virtual void begin() {
278+
virtual void begin() override {
265279
LOGD(LOG_METHOD);
266280
}
267281

268282
/// Opens the next url from the array
269-
Stream* nextStream(int offset) {
283+
Stream* nextStream(int offset) override {
270284
pos += offset;
271285
if (pos < 1 || pos > max) {
272286
pos = 1;
@@ -281,19 +295,19 @@ namespace audio_tools {
281295
}
282296

283297
/// Opens the selected url from the array
284-
Stream* selectStream(int Station) {
298+
Stream* selectStream(int idx) override {
285299
//pos += offset;
286-
pos = Station;
300+
pos = idx;
287301
if (pos < 1) {
288302
pos = 1;
289-
LOGI("url array out of limits: %d -> %d", Station, pos);
303+
LOGI("url array out of limits: %d -> %d", idx, pos);
290304
}
291305
if (pos > max) {
292306
pos = max;
293-
LOGI("url array out of limits: %d -> %d", Station, pos);
307+
LOGI("url array out of limits: %d -> %d", idx, pos);
294308
}
295309
LOGI("selectStream: %d/%d -> %s", pos, max, urlArray[pos-1]);
296-
if (Station != 0 || actual_stream == nullptr) {
310+
if (idx != 0 || actual_stream == nullptr) {
297311
if (started) actual_stream->end();
298312
actual_stream->begin(urlArray[pos-1], mime);
299313
started = true;
@@ -302,7 +316,7 @@ namespace audio_tools {
302316
}
303317

304318
/// Opens the Previous url from the array
305-
Stream* previousStream(int offset) {
319+
Stream* previousStream(int offset) override {
306320
pos -= offset;
307321
if (pos < 1 || pos > max) {
308322
pos = max;
@@ -316,12 +330,12 @@ namespace audio_tools {
316330
return actual_stream;
317331
}
318332

319-
void setTimeoutMs(int millisec) {
320-
timeout = millisec;
333+
int index() {
334+
return pos;
321335
}
322336

323-
int timeoutMs() {
324-
return timeout;
337+
const char *toStr() {
338+
return urlArray[pos];
325339
}
326340

327341
protected:
@@ -330,7 +344,6 @@ namespace audio_tools {
330344
int pos = 0;
331345
int max = 0;
332346
const char* mime = nullptr;
333-
int timeout = 60000;
334347
bool started = false;
335348

336349
};
@@ -513,9 +526,9 @@ namespace audio_tools {
513526
}
514527

515528
/// moves to next file
516-
virtual bool next() {
529+
virtual bool next(int offset=1) {
517530
LOGD(LOG_METHOD);
518-
active = setStream(*(p_source->nextStream(+1)));
531+
active = setStream(*(p_source->nextStream(offset)));
519532
return active;
520533
}
521534

@@ -527,17 +540,16 @@ namespace audio_tools {
527540
}
528541

529542
/// moves to previous file
530-
virtual bool previous() {
543+
virtual bool previous(int offset=1) {
531544
LOGD(LOG_METHOD);
532-
active = setStream(*(p_source->previousStream(+1)));
545+
active = setStream(*(p_source->previousStream(offset)));
533546
return active;
534547
}
535548

536549
/// start selected input stream
537550
virtual bool setStream(Stream &input) {
538551
end();
539552
p_out_decoding->begin();
540-
p_source->begin();
541553
p_input_stream = &input;
542554
if (p_input_stream != nullptr) {
543555
LOGD("open selected stream");
@@ -585,7 +597,7 @@ namespace audio_tools {
585597
if (millis() > timeout) {
586598
LOGW("-> timeout - moving to next stream");
587599
// open next stream
588-
if (!next()) {
600+
if (!next(1)) {
589601
LOGD("stream is null");
590602
}
591603
}

0 commit comments

Comments
 (0)