Skip to content

Commit 61a9ea1

Browse files
committed
range request is working now
1 parent 3d27bfd commit 61a9ea1

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/Audio.cpp

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
audio.cpp
44
55
Created on: Oct 28.2018 */char audioI2SVers[] ="\
6-
Version 3.4.0k ";
7-
/* Updated on: Aug 02.2025
6+
Version 3.4.1 ";
7+
/* Updated on: Aug 05.2025
88
99
Author: Wolle (schreibfaul1)
1010
Audio library for ESP32, ESP32-S3 or ESP32-P4
@@ -6506,20 +6506,30 @@ bool Audio::readID3V1Tag() {
65066506
}
65076507
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
65086508
int32_t Audio::newInBuffStart(int32_t m_resumeFilePos){
6509-
if(m_controlCounter != 100){AUDIO_LOG_WARN("timeOffset not possible"); m_resumeFilePos = -1; return -1;}
6509+
uint16_t remaining = 0;
6510+
int32_t offset = 0, buffFillSize = 0, res = 0;
6511+
uint32_t timeOut = 0;
6512+
6513+
if(m_controlCounter != 100){AUDIO_LOG_WARN("timeOffset not possible"); m_resumeFilePos = -1; offset = -1; goto exit;}
6514+
if(m_resumeFilePos >= (int32_t)m_audioDataStart + m_audioDataSize) { m_resumeFilePos = -1; offset = -1; goto exit;}
6515+
if(m_codec == CODEC_M4A && ! m_stsz_position){ m_resumeFilePos = -1; offset = -1; goto exit;}
65106516

65116517
if(m_resumeFilePos < (int32_t)m_audioDataStart) m_resumeFilePos = m_audioDataStart;
6512-
if(m_resumeFilePos >= (int32_t)m_audioDataStart + m_audioDataSize) {return - 1;}
6513-
int32_t buffFillSize = min(m_audioDataSize - m_resumeFilePos, UINT16_MAX);
6518+
buffFillSize = min(m_audioDataSize - m_resumeFilePos, UINT16_MAX);
65146519

65156520
m_f_lockInBuffer = true; // lock the buffer, the InBuffer must not be re-entered in playAudioData()
65166521
while(m_f_audioTaskIsDecoding) vTaskDelay(1); // We can't reset the InBuffer while the decoding is in progress
6517-
int res = audioFileSeek(m_resumeFilePos);
65186522
m_f_allDataReceived = false;
6523+
6524+
/* process before */
6525+
if(m_codec == CODEC_M4A) m_resumeFilePos += m4a_correctResumeFilePos(); {if(m_resumeFilePos == -1) goto exit;}
6526+
6527+
/* skip to position */
6528+
res = audioFileSeek(m_resumeFilePos);
65196529
InBuff.resetBuffer();
6520-
uint16_t remaining = buffFillSize;
6521-
int32_t offset = 0;
6522-
uint32_t timeOut = millis();
6530+
remaining = buffFillSize;
6531+
offset = 0;
6532+
timeOut = millis();
65236533
while (remaining > 0) {
65246534
int bytesRead = audioFileRead(InBuff.getReadPtr() + offset, remaining);
65256535
if (bytesRead <= 0) {
@@ -6530,22 +6540,24 @@ int32_t Audio::newInBuffStart(int32_t m_resumeFilePos){
65306540
offset += bytesRead;
65316541
}
65326542
InBuff.bytesWritten(buffFillSize);
6533-
6534-
offset = -1;
6543+
/* process after */
6544+
offset = 0;
65356545
if(m_codec == CODEC_OPUS || m_codec == CODEC_VORBIS) {if(InBuff.bufferFilled() < 0xFFFF) return - 1;} // ogg frame <= 64kB
65366546
if(m_codec == CODEC_WAV) {while((m_resumeFilePos % 4) != 0){m_resumeFilePos++; offset++; if(m_resumeFilePos >= m_audioFileSize) goto exit;}} // must divisible by four
65376547
if(m_codec == CODEC_MP3) {offset = mp3_correctResumeFilePos(); if(offset == -1) goto exit; MP3Decoder_ClearBuffer();}
65386548
if(m_codec == CODEC_FLAC) {offset = flac_correctResumeFilePos(); if(offset == -1) goto exit; FLACDecoderReset();}
6539-
if(m_codec == CODEC_M4A) {offset = m4a_correctResumeFilePos(); if(offset == -1) goto exit;}
65406549
if(m_codec == CODEC_VORBIS){offset = ogg_correctResumeFilePos(); if(offset == -1) goto exit; VORBISDecoder_ClearBuffers();}
65416550
if(m_codec == CODEC_OPUS) {offset = ogg_correctResumeFilePos(); if(offset == -1) goto exit; OPUSDecoder_ClearBuffers();}
65426551

65436552

65446553
InBuff.bytesWasRead(offset);
6554+
6555+
m_f_lockInBuffer = false;
6556+
return res + offset;
65456557
exit:
6546-
log_w("offset %i", offset);
65476558
m_f_lockInBuffer = false;
6548-
return res;
6559+
stopSong();
6560+
return offset;
65496561
}
65506562
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
65516563
boolean Audio::streamDetection(uint32_t bytesAvail) {
@@ -6597,19 +6609,32 @@ uint32_t Audio::m4a_correctResumeFilePos() {
65976609
bool found = false;
65986610
audioFileSeek(m_stsz_position);
65996611

6612+
auto read_next = [&]() -> int {
6613+
int r;
6614+
int cnt = 0;
6615+
while(true){
6616+
r = audioFileRead();
6617+
if(r >= 0) break;
6618+
vTaskDelay(10);
6619+
cnt++;
6620+
if(cnt > 300) {AUDIO_LOG_ERROR("timeout"); break;}
6621+
}
6622+
return r;
6623+
};
6624+
66006625
while(i < m_stsz_numEntries) {
66016626
i++;
6602-
uu.u8[3] = audioFileRead();
6603-
uu.u8[2] = audioFileRead();
6604-
uu.u8[1] = audioFileRead();
6605-
uu.u8[0] = audioFileRead();
6627+
uu.u8[3] = read_next();
6628+
uu.u8[2] = read_next();
6629+
uu.u8[1] = read_next();
6630+
uu.u8[0] = read_next();
66066631

66076632
pos += uu.u32;
66086633
if(pos >= m_resumeFilePos) {found = true; break;}
66096634
}
66106635
if(!found) return -1; // not found
66116636

6612-
audioFileSeek(filePtr); // restore file pointer
6637+
// audioFileSeek(filePtr); // restore file pointer
66136638
return pos - m_resumeFilePos; // return the number of bytes to jump
66146639
}
66156640
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)