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

merge from master #617

Merged
merged 5 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
111 changes: 57 additions & 54 deletions src/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ size_t AudioBuffer::bufferFilled() {
return m_dataLength;
}

size_t AudioBuffer::getMaxAvailableBytes() {
if(m_writePtr >= m_readPtr) { m_dataLength = (m_writePtr - m_readPtr); }
else { m_dataLength = (m_endPtr - m_readPtr);}
return m_dataLength;
}

void AudioBuffer::bytesWritten(size_t bw) {
m_writePtr += bw;
if(m_writePtr == m_endPtr) { m_writePtr = m_buffer; }
Expand Down Expand Up @@ -1077,13 +1083,17 @@ bool Audio::latinToUTF8(char* buff, size_t bufflen) {

pos = 0;

while(buff[pos] != 0) {
len = strlen(buff);
if(buff[pos] >= 0x80 && buff[pos + 1] < 0x80) { // is not UTF8, is latin?
for(int i = len + 1; i > pos; i--) { buff[i + 1] = buff[i]; }
while(buff[pos] != 0){
if ((buff[pos] & 0x80) == 0) {pos++; continue;}
else{
len = strlen(buff);
for(int i = len + 1; i > pos; i--){
buff[i+1] = buff[i];
}
uint8_t c = buff[pos];
buff[pos++] = 0xc0 | ((c >> 6) & 0x1f); // 2+1+5 bits
buff[pos++] = 0x80 | ((char)c & 0x3f); // 1+1+6 bits
buff[pos] = 0xc0 | ((c >> 6)& 0x1f); // 2+1+5 bits
pos++;
buff[pos] = 0x80 | ((char)c & 0x3f); // 1+1+6 bits
}
pos++;
if(pos > bufflen - 3) {
Expand Down Expand Up @@ -1535,14 +1545,13 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(m_controlCounter == 2) { // skip extended header if exists
if(ehsz > 256) {
ehsz -= 256;
remainingHeaderBytes -= 256;
return 256;
} // Throw it away
else {
m_controlCounter++;
if(m_controlCounter == 2){ // skip extended header if exists
if(ehsz > len) {
ehsz -=len;
remainingHeaderBytes -= len;
return len;} // Throw it away
else {
m_controlCounter ++;
remainingHeaderBytes -= ehsz;
return ehsz;
} // Throw it away
Expand Down Expand Up @@ -1597,11 +1606,11 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
return 6;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(m_controlCounter == 5) { // If the frame is larger than 512 bytes, skip the rest
if(framesize > 512) {
framesize -= 512;
remainingHeaderBytes -= 512;
return 512;
if(m_controlCounter == 5){ // If the frame is larger than 512 bytes, skip the rest
if(framesize > len){
framesize -= len;
remainingHeaderBytes -= len;
return len;
}
else {
m_controlCounter = 3; // check next frame
Expand Down Expand Up @@ -1640,8 +1649,10 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
}

size_t fs = framesize;
if(fs > 512) fs = 512;
for(int i = 0; i < fs; i++) { m_ibuff[i] = *(data + i); }
if(fs >1024) fs = 1024;
for(int i=0; i<fs; i++){
m_ibuff[i] = *(data + i);
}
framesize -= fs;
remainingHeaderBytes -= fs;
m_ibuff[fs] = 0;
Expand All @@ -1652,19 +1663,15 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {

if(!isUnicode) {
uint16_t j = 0, k = 0;
j = 0;
k = 0;
while(j < fs) {
if(m_ibuff[j] == 0x0A) m_ibuff[j] = 0x20; // replace LF by space
if(m_ibuff[j] > 0x1F) {
m_ibuff[k] = m_ibuff[j];
m_ibuff[k] = m_ibuff[j]; //remove non printables
k++;
}
j++;
} // remove non printables
if(k > 0) m_ibuff[k] = 0;
else
m_ibuff[0] = 0; // new termination
}
m_ibuff[k] = '\0'; // new termination
latinToUTF8(m_ibuff, k - 1);
}
showID3Tag(tag, m_ibuff);
return fs;
Expand Down Expand Up @@ -1693,13 +1700,13 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
frameid[3] = 0;
for(uint8_t i = 0; i < 4; i++) tag[i] = frameid[i]; // tag = frameid
remainingHeaderBytes -= 3;
size_t len = bigEndian(data + 3, 3);
universal_tmp = len;
size_t dataLen = bigEndian(data + 3, 3);
universal_tmp = dataLen;
remainingHeaderBytes -= 3;
char value[256];
if(len > 249) { len = 249; }
memcpy(value, (data + 7), len);
value[len + 1] = 0;
if(dataLen > 249) {dataLen = 249; }
memcpy(value, (data + 7), dataLen);
value[dataLen + 1] = 0;
m_chbuf[0] = 0;
if(startsWith(tag, "PIC")) { // image embedded in header
if(getDatamode() == AUDIO_LOCALFILE) {
Expand All @@ -1719,21 +1726,21 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
}
else { showID3Tag(tag, value); }
remainingHeaderBytes -= universal_tmp;
universal_tmp -= len;
universal_tmp -= dataLen;

if(len == 0) m_controlCounter = 98;
if(dataLen == 0) m_controlCounter = 98;
if(remainingHeaderBytes == 0) m_controlCounter = 98;

return 3 + 3 + len;
return 3 + 3 + dataLen;
}
// -- end section V2.2 -----------

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(m_controlCounter == 98) { // skip all ID3 metadata (mostly spaces)
if(remainingHeaderBytes > 256) {
remainingHeaderBytes -= 256;
return 256;
} // Throw it away
if(m_controlCounter == 98){ // skip all ID3 metadata (mostly spaces)
if(remainingHeaderBytes > len) {
remainingHeaderBytes -=len;
return len;
} // Throw it away
else {
m_controlCounter = 99;
return remainingHeaderBytes;
Expand Down Expand Up @@ -2887,8 +2894,8 @@ void Audio::processLocalFile() {
m_f_running = false;
return;
}
if(InBuff.bufferFilled() > maxFrameSize) { // read the file header first
InBuff.bytesWasRead(readAudioHeader(InBuff.bufferFilled()));
if(InBuff.bufferFilled() > maxFrameSize){ // read the file header first
InBuff.bytesWasRead(readAudioHeader(InBuff.getMaxAvailableBytes()));
}
return;
}
Expand Down Expand Up @@ -3150,9 +3157,9 @@ void Audio::processWebFile() {
}

// we have a webfile, read the file header first - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(m_controlCounter != 100) {
if(InBuff.bufferFilled() > maxFrameSize) { // read the file header first
int32_t bytesRead = readAudioHeader(maxFrameSize);
if(m_controlCounter != 100){
if(InBuff.bufferFilled() > maxFrameSize){ // read the file header first
int32_t bytesRead = readAudioHeader(InBuff.getMaxAvailableBytes());
if(bytesRead > 0) InBuff.bytesWasRead(bytesRead);
}
return;
Expand Down Expand Up @@ -5866,13 +5873,9 @@ uint32_t Audio::flac_correctResumeFilePos(uint32_t resumeFilePos) {

p1 = audiofile.read();
p2 = audiofile.read();
pos += 2;
while(!found || pos == m_file_size) {
if(p1 == 0xFF && p2 == 0xF8) {
found = true;
log_i("found");
break;
}
pos+=2;
while(!found || pos == m_file_size){
if(p1 == 0xFF && p2 == 0xF8){found = true; break;}
p1 = p2;
p2 = audiofile.read();
pos++;
Expand Down
1 change: 1 addition & 0 deletions src/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class AudioBuffer {
size_t freeSpace(); // number of free bytes to overwrite
size_t writeSpace(); // space fom writepointer to bufferend
size_t bufferFilled(); // returns the number of filled bytes
size_t getMaxAvailableBytes(); // max readable bytes in one block
void bytesWritten(size_t bw); // update writepointer
void bytesWasRead(size_t br); // update readpointer
uint8_t* getWritePtr(); // returns the current writepointer
Expand Down