Skip to content

Commit

Permalink
Copy indicate number of bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Jun 18, 2024
1 parent 151803a commit 88c546e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/AudioTools/AudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
19 changes: 15 additions & 4 deletions src/AudioTools/StreamCopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down

0 comments on commit 88c546e

Please sign in to comment.