Skip to content

Commit

Permalink
DM: should skip id3 tags
Browse files Browse the repository at this point in the history
  • Loading branch information
deanm1278 committed Nov 6, 2017
1 parent 65c576c commit 215724c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/Adafruit_mp3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool Adafruit_mp3::begin()
WAIT_TC16_REGS_SYNC(MP3_TC)

//TODO: calculate based on timer clock
MP3_TC->COUNT16.CC[0].reg = (uint16_t) 650;
MP3_TC->COUNT16.CC[0].reg = (uint16_t)( (SystemCoreClock >> 2) / MP3_SAMPLE_RATE_DEFAULT);
WAIT_TC16_REGS_SYNC(MP3_TC)

// Enable the TONE_TC interrupt request
Expand Down Expand Up @@ -167,6 +167,30 @@ void Adafruit_mp3::resume()
enableTimer();
}

/**
*****************************************************************************************
* @brief Get the number of bytes until the end of the ID3 tag.
*
* @param readPtr current read pointer
*
* @return none
****************************************************************************************/
int Adafruit_mp3::findID3Offset(uint8_t *readPtr)
{
char header[10];
memcpy(header, readPtr, 10);
//http://id3.org/id3v2.3.0#ID3v2_header
if(header[0] == 0x49 && header[1] == 0x44 && header[2] == 0x33 && header[3] < 0xFF){
//this is a tag
uint32_t sz = ((uint32_t)header[6] << 23) | ((uint32_t)header[7] << 15) | ((uint32_t)header[8] << 7) | header[9];
return sz;
}
else{
//this is not a tag
return 0;
}
}

/**
*****************************************************************************************
* @brief The main loop of the mp3 player. This function should be called as fast as
Expand Down Expand Up @@ -217,18 +241,17 @@ int Adafruit_mp3::tick(){

err = MP3GetNextFrameInfo(hMP3Decoder, &frameInfo, readPtr);
if(err != ERR_MP3_INVALID_FRAMEHEADER){
if(frameInfo.samprate != 44100)
if(frameInfo.samprate != MP3_SAMPLE_RATE_DEFAULT)
{
//TODO: set the output timer for the sample rate of the file
// For this example, we want only data which
// was sampled at 44100 Hz. Ignore this frame.
return 1;
}
else{
playing = true;
channels = frameInfo.nChans;
disableTimer();
MP3_TC->COUNT16.CC[0].reg = (uint16_t)( (SystemCoreClock >> 2) / frameInfo.samprate);
WAIT_TC16_REGS_SYNC(MP3_TC);
enableTimer();
}
playing = true;
channels = frameInfo.nChans;
}
else return 1; //we couldn't find a frame header. It might be ok once we get more data though.
}

offset = MP3FindSyncWord(readPtr, bytesLeft);
Expand Down
3 changes: 3 additions & 0 deletions src/Adafruit_mp3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#define BUFFER_LOWER_THRESH (8 * 1024)

#define MP3_SAMPLE_RATE_DEFAULT 44100

#define MP3_TC TC2
#define MP3_IRQn TC2_IRQn
#define MP3_Handler TC2_Handler
Expand Down Expand Up @@ -47,6 +49,7 @@ class Adafruit_mp3 {
bool playing = false;

int (*bufferCallback)(uint8_t *, int);
int findID3Offset(uint8_t *readPtr);

};

Expand Down

0 comments on commit 215724c

Please sign in to comment.