Skip to content

Commit ee8b03a

Browse files
committed
cleanup AudioPlayer
1 parent 10a21c1 commit ee8b03a

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

examples/.DS_Store

2 KB
Binary file not shown.

examples/streams-sd_mp3-i2s/streams-sd_mp3-i2s.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using namespace audio_tools;
2020

2121
const int chipSelect=10;
2222
I2SStream i2s; // final output of decoded stream
23-
EncodedAudioStream decoder(i2s, new MP3DecoderHelix()); // Decoding stream
23+
EncodedAudioStream decoder(&i2s, new MP3DecoderHelix()); // Decoding stream
2424
StreamCopy copier;
2525

2626

src/AudioTools/AudioPlayer.h

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ namespace audio_tools {
4040
virtual Stream* nextStream(int offset) = 0;
4141

4242
/// Returns next audio stream
43-
virtual Stream* selectStream(int station) = 0;
43+
virtual Stream* selectStream(int station) {
44+
LOGE("Not Supported!");
45+
return nullptr;
46+
}
4447

4548
/// Provides the timeout which is triggering to move to the next stream
4649
virtual int timeoutMs() {
@@ -280,6 +283,7 @@ namespace audio_tools {
280283
return actual_stream;
281284
}
282285

286+
283287
void setTimeoutMs(int millisec) {
284288
timeout = millisec;
285289
}
@@ -419,7 +423,7 @@ namespace audio_tools {
419423
}
420424

421425
/// (Re)Starts the playing of the music (from the beginning)
422-
virtual bool begin(int station = 1, bool isActive = true) {
426+
virtual bool begin(bool isActive = true) {
423427
LOGD(LOG_METHOD);
424428
bool result = false;
425429

@@ -428,7 +432,33 @@ namespace audio_tools {
428432
p_source->begin();
429433
meta_out.begin();
430434

431-
p_input_stream = p_source->selectStream(station);
435+
p_input_stream = p_source->nextStream(1);
436+
if (p_input_stream != nullptr) {
437+
if (meta_active) {
438+
copier.setCallbackOnWrite(decodeMetaData, this);
439+
}
440+
copier.begin(*p_out_decoding, *p_input_stream);
441+
timeout = millis() + p_source->timeoutMs();
442+
active = isActive;
443+
result = true;
444+
}
445+
else {
446+
LOGW("-> begin: no data found");
447+
}
448+
return result;
449+
}
450+
451+
/// (Re)Starts the playing of the music (from the beginning)
452+
virtual bool begin(int index, bool isActive = true) {
453+
LOGD(LOG_METHOD);
454+
bool result = false;
455+
456+
// start dependent objects
457+
p_out_decoding->begin();
458+
p_source->begin();
459+
meta_out.begin();
460+
461+
p_input_stream = p_source->selectStream(index);
432462
if (p_input_stream != nullptr) {
433463
if (meta_active) {
434464
copier.setCallbackOnWrite(decodeMetaData, this);
@@ -477,17 +507,34 @@ namespace audio_tools {
477507
}
478508

479509
/// moves to next file
480-
virtual void next() {
510+
virtual bool next() {
481511
LOGD(LOG_METHOD);
482-
active = startNextStream();
512+
active = setStream(*(p_source->nextStream(+1)));
513+
return active;
483514
}
515+
484516
/// moves to next file
485-
virtual void Select(int station) {
517+
virtual bool setIndex(int idx) {
486518
LOGD(LOG_METHOD);
487-
active = startSelectedStream(station);
488-
//startSelectedStream(station);
519+
active = setStream(*(p_source->selectStream(idx)));
520+
return active;
521+
}
522+
523+
/// start selected input stream
524+
virtual bool setStream(Stream &input) {
525+
end();
526+
p_out_decoding->begin();
527+
p_source->begin();
528+
p_input_stream = &input;
529+
if (p_input_stream != nullptr) {
530+
LOGD("open selected stream");
531+
meta_out.begin();
532+
copier.begin(*p_out_decoding, *p_input_stream);
533+
}
534+
return p_input_stream != nullptr;
489535
}
490536

537+
491538
/// determines if the player is active
492539
virtual bool isActive() {
493540
return active;
@@ -526,7 +573,7 @@ namespace audio_tools {
526573
if (millis() > timeout) {
527574
LOGW("-> timeout - moving to next stream");
528575
// open next stream
529-
if (!startNextStream()) {
576+
if (!next()) {
530577
LOGD("stream is null");
531578
}
532579
}
@@ -563,33 +610,6 @@ namespace audio_tools {
563610
float current_volume = -1; // illegal value which will trigger an update
564611

565612

566-
/// start next stream
567-
virtual bool startNextStream() {
568-
end();
569-
p_out_decoding->begin();
570-
p_source->begin();
571-
p_input_stream = p_source->nextStream(+1);
572-
if (p_input_stream != nullptr) {
573-
LOGD("open next stream");
574-
meta_out.begin();
575-
copier.begin(*p_out_decoding, *p_input_stream);
576-
}
577-
return p_input_stream != nullptr;
578-
}
579-
/// start selected stream
580-
virtual bool startSelectedStream(int station) {
581-
end();
582-
p_out_decoding->begin();
583-
p_source->begin();
584-
p_input_stream = p_source->selectStream(station);
585-
if (p_input_stream != nullptr) {
586-
LOGD("open selected stream");
587-
meta_out.begin();
588-
copier.begin(*p_out_decoding, *p_input_stream);
589-
}
590-
return p_input_stream != nullptr;
591-
}
592-
593613
/// Callback implementation which writes to metadata
594614
static void decodeMetaData(void* obj, void* data, size_t len) {
595615
LOGD(LOG_METHOD);

0 commit comments

Comments
 (0)