From 7cdec5139a47e479662067ec2dfc8f0301398ba6 Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Tue, 3 Sep 2024 13:03:32 +0200 Subject: [PATCH] R2ROutput blocking correction --- .../streams-generator-r2r.ino | 2 +- src/AudioLibs/R2ROutput.h | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/examples/examples-stream/streams-generator-r2r/streams-generator-r2r.ino b/examples/examples-stream/streams-generator-r2r/streams-generator-r2r.ino index 36da48321..365b5f23b 100644 --- a/examples/examples-stream/streams-generator-r2r/streams-generator-r2r.ino +++ b/examples/examples-stream/streams-generator-r2r/streams-generator-r2r.ino @@ -13,7 +13,7 @@ SineWaveGenerator sineWave; // subclass of SoundG GeneratedSoundStream sound(sineWave); // Stream generated from sine wave R2ROutput out; StreamCopy copier(out, sound); // copies sound into i2s -const int pins1[] = {13,12,14,27,26,25, 33, 32}; // r2r pins 32 is least significant +const int pins1[] = {12,14,27,26,25,33,32, 35}; // ESP32 pins // Arduino Setup void setup(void) { diff --git a/src/AudioLibs/R2ROutput.h b/src/AudioLibs/R2ROutput.h index bfaac0d75..9ed7ac74d 100644 --- a/src/AudioLibs/R2ROutput.h +++ b/src/AudioLibs/R2ROutput.h @@ -85,6 +85,7 @@ class R2RConfig : public AudioInfo { uint16_t buffer_count = 2; // double buffer R2RDriverBase *driver = &r2r_driver; // by default use Arduino driver bool is_blocking = true; + int blocking_retry_delay_ms = 5; int timer_id = 0; }; @@ -145,6 +146,8 @@ class R2ROutput : public AudioOutput { } size_t write(const uint8_t *data, size_t len) override { + LOGD("write: %d", len); + size_t result = 0; // if buffer has not been allocated (buffer_size==0) if (len > rcfg.buffer_size) { LOGE("buffer_size %d too small for write size: %d", rcfg.buffer_size, @@ -152,19 +155,27 @@ class R2ROutput : public AudioOutput { return len; } - // wait for buffer to have enough space if (rcfg.is_blocking){ - while(buffer.availableForWrite() 0){ + int written = buffer.writeArray(data + result, open); + open -= written; + result += written; + if (open > 0){ + delay(rcfg.blocking_retry_delay_ms); + } } + } else { + // write as much as possible + result = buffer.writeArray(data, len); } - - size_t result = buffer.writeArray(data, len); // activate output when buffer is half full if (!is_active && buffer.bufferCountFilled() >= rcfg.buffer_count / 2) { LOGI("is_active = true"); is_active = true; } + return result; }