diff --git a/src/AudioTools/AudioPlayer.h b/src/AudioTools/AudioPlayer.h index 8a15a8fcf9..d463ce980a 100644 --- a/src/AudioTools/AudioPlayer.h +++ b/src/AudioTools/AudioPlayer.h @@ -357,8 +357,12 @@ class AudioPlayer : public AudioInfoSupport, public VolumeSupport { /// Defines the wait time in ms if the target output is full virtual void setDelayIfOutputFull(int delayMs) { delay_if_full = delayMs; } - /// Call this method in the loop. virtual size_t copy() { + return copy(copier.bufferSize()); + } + + /// Call this method in the loop. + virtual size_t copy(size_t bytes) { size_t result = 0; if (active) { TRACED(); @@ -371,7 +375,7 @@ class AudioPlayer : public AudioInfoSupport, public VolumeSupport { return 0; } // handle sound - result = copier.copy(); + result = copier.copyBytes(bytes); if (result > 0 || timeout == 0) { // reset timeout if we had any data timeout = millis() + p_source->timeoutAutoNext(); @@ -380,9 +384,10 @@ class AudioPlayer : public AudioInfoSupport, public VolumeSupport { moveToNextFileOnTimeout(); // return silence when there was no data - if (result == 0 && silence_on_inactive){ - writeSilence(1024); + if (result < bytes && silence_on_inactive){ + writeSilence(bytes - result); } + } else { // e.g. A2DP should still receive data to keep the connection open if (silence_on_inactive) { diff --git a/src/AudioTools/StreamCopy.h b/src/AudioTools/StreamCopy.h index 27b0f123f8..c682e0c5b6 100644 --- a/src/AudioTools/StreamCopy.h +++ b/src/AudioTools/StreamCopy.h @@ -89,8 +89,13 @@ class StreamCopyT { } /// copies the data from the source to the destination - the result is in bytes - inline size_t copy(){ - LOGD("copy %s", log_name); + inline size_t copy() { + return copyBytes(buffer_size); + } + + /// copies the inicated number of bytes from the source to the destination - the result is in bytes + inline size_t copyBytes(size_t bytes){ + LOGD("copy %d bytes %s", bytes, log_name); if (!active) return 0; // if not initialized we do nothing if (from==nullptr && to==nullptr) return 0; @@ -105,13 +110,19 @@ class StreamCopyT { return 0; } + // resize copy buffer if necessary + if (buffer.size() < bytes){ + LOGI("Resize to %d", bytes); + buffer.resize(bytes); + } + size_t result = 0; size_t delayCount = 0; - size_t len = buffer_size; + size_t len = bytes; if (check_available) { len = available(); } - size_t bytes_to_read = buffer_size; + size_t bytes_to_read = bytes; size_t bytes_read = 0; if (len > 0){