Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored #75 #94

Merged
merged 9 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Adafruit_VS1053.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SIGNAL(TIMER0_COMPA_vect) { myself->feedBuffer(); }
#endif

volatile boolean feedBufferLock = false; //!< Locks feeding the buffer
boolean _loopPlayback; //!< internal variable, used to control playback looping

#if defined(ESP8266)
ICACHE_RAM_ATTR
Expand Down Expand Up @@ -102,6 +103,7 @@ Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t rst, int8_t cs,

playingMusic = false;
_cardCS = cardcs;
_loopPlayback = false;
}

Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t cs, int8_t dcs,
Expand All @@ -111,6 +113,7 @@ Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t cs, int8_t dcs,

playingMusic = false;
_cardCS = cardcs;
_loopPlayback = false;
}

Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t mosi, int8_t miso,
Expand All @@ -122,6 +125,7 @@ Adafruit_VS1053_FilePlayer::Adafruit_VS1053_FilePlayer(int8_t mosi, int8_t miso,

playingMusic = false;
_cardCS = cardcs;
_loopPlayback = false;
}

boolean Adafruit_VS1053_FilePlayer::begin(void) {
Expand Down Expand Up @@ -174,6 +178,12 @@ boolean Adafruit_VS1053_FilePlayer::stopped(void) {
return (!playingMusic && !currentTrack);
}

void Adafruit_VS1053_FilePlayer::playbackLoop(boolean loopState) {
_loopPlayback = loopState;
}

boolean Adafruit_VS1053_FilePlayer::playbackLooped() { return _loopPlayback; }

// Just checks to see if the name ends in ".mp3"
boolean Adafruit_VS1053_FilePlayer::isMP3File(const char *fileName) {
return (strlen(fileName) > 4) &&
Expand Down Expand Up @@ -297,10 +307,20 @@ void Adafruit_VS1053_FilePlayer::feedBuffer_noLock(void) {
int bytesread = currentTrack.read(mp3buffer, VS1053_DATABUFFERLEN);

if (bytesread == 0) {
// must be at the end of the file, wrap it up!
playingMusic = false;
currentTrack.close();
break;
// must be at the end of the file
if (_loopPlayback) {
// play in loop
if (isMP3File(currentTrack.name())) {
currentTrack.seek(mp3_ID3Jumper(currentTrack));
} else {
currentTrack.seek(0);
}
} else {
// wrap it up!
playingMusic = false;
currentTrack.close();
break;
}
}

playData(mp3buffer, bytesread);
Expand Down
12 changes: 12 additions & 0 deletions Adafruit_VS1053.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,18 @@ class Adafruit_VS1053_FilePlayer : public Adafruit_VS1053 {
* @param pause whether or not to pause playback
*/
void pausePlaying(boolean pause);
/*!
* @brief Set state for playback looping
* @param loopState Sets playback loop state (Note: Only use with
* startPlayingFile, if used with playFullFile no subsequent code in the
* sketch executes)
*/
void playbackLoop(boolean loopState);
/*!
* @brief Retrieve playback loop state
* @return Returns true when looped playback is enabled
*/
boolean playbackLooped();
/*!
* @brief Determine current playback speed
* @return Returns playback speed, i.e. 1 for 1x, 2 for 2x, 3 for 3x
Expand Down