Skip to content

Commit 88c546e

Browse files
committed
Copy indicate number of bytes
1 parent 151803a commit 88c546e

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/AudioTools/AudioPlayer.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,12 @@ class AudioPlayer : public AudioInfoSupport, public VolumeSupport {
357357
/// Defines the wait time in ms if the target output is full
358358
virtual void setDelayIfOutputFull(int delayMs) { delay_if_full = delayMs; }
359359

360-
/// Call this method in the loop.
361360
virtual size_t copy() {
361+
return copy(copier.bufferSize());
362+
}
363+
364+
/// Call this method in the loop.
365+
virtual size_t copy(size_t bytes) {
362366
size_t result = 0;
363367
if (active) {
364368
TRACED();
@@ -371,7 +375,7 @@ class AudioPlayer : public AudioInfoSupport, public VolumeSupport {
371375
return 0;
372376
}
373377
// handle sound
374-
result = copier.copy();
378+
result = copier.copyBytes(bytes);
375379
if (result > 0 || timeout == 0) {
376380
// reset timeout if we had any data
377381
timeout = millis() + p_source->timeoutAutoNext();
@@ -380,9 +384,10 @@ class AudioPlayer : public AudioInfoSupport, public VolumeSupport {
380384
moveToNextFileOnTimeout();
381385

382386
// return silence when there was no data
383-
if (result == 0 && silence_on_inactive){
384-
writeSilence(1024);
387+
if (result < bytes && silence_on_inactive){
388+
writeSilence(bytes - result);
385389
}
390+
386391
} else {
387392
// e.g. A2DP should still receive data to keep the connection open
388393
if (silence_on_inactive) {

src/AudioTools/StreamCopy.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ class StreamCopyT {
8989
}
9090

9191
/// copies the data from the source to the destination - the result is in bytes
92-
inline size_t copy(){
93-
LOGD("copy %s", log_name);
92+
inline size_t copy() {
93+
return copyBytes(buffer_size);
94+
}
95+
96+
/// copies the inicated number of bytes from the source to the destination - the result is in bytes
97+
inline size_t copyBytes(size_t bytes){
98+
LOGD("copy %d bytes %s", bytes, log_name);
9499
if (!active) return 0;
95100
// if not initialized we do nothing
96101
if (from==nullptr && to==nullptr) return 0;
@@ -105,13 +110,19 @@ class StreamCopyT {
105110
return 0;
106111
}
107112

113+
// resize copy buffer if necessary
114+
if (buffer.size() < bytes){
115+
LOGI("Resize to %d", bytes);
116+
buffer.resize(bytes);
117+
}
118+
108119
size_t result = 0;
109120
size_t delayCount = 0;
110-
size_t len = buffer_size;
121+
size_t len = bytes;
111122
if (check_available) {
112123
len = available();
113124
}
114-
size_t bytes_to_read = buffer_size;
125+
size_t bytes_to_read = bytes;
115126
size_t bytes_read = 0;
116127

117128
if (len > 0){

0 commit comments

Comments
 (0)