Skip to content

Commit 7cdec51

Browse files
committed
R2ROutput blocking correction
1 parent 53414e7 commit 7cdec51

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

examples/examples-stream/streams-generator-r2r/streams-generator-r2r.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SineWaveGenerator<int16_t> sineWave; // subclass of SoundG
1313
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
1414
R2ROutput out;
1515
StreamCopy copier(out, sound); // copies sound into i2s
16-
const int pins1[] = {13,12,14,27,26,25, 33, 32}; // r2r pins 32 is least significant
16+
const int pins1[] = {12,14,27,26,25,33,32, 35}; // ESP32 pins
1717

1818
// Arduino Setup
1919
void setup(void) {

src/AudioLibs/R2ROutput.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class R2RConfig : public AudioInfo {
8585
uint16_t buffer_count = 2; // double buffer
8686
R2RDriverBase *driver = &r2r_driver; // by default use Arduino driver
8787
bool is_blocking = true;
88+
int blocking_retry_delay_ms = 5;
8889
int timer_id = 0;
8990
};
9091

@@ -145,26 +146,36 @@ class R2ROutput : public AudioOutput {
145146
}
146147

147148
size_t write(const uint8_t *data, size_t len) override {
149+
LOGD("write: %d", len);
150+
size_t result = 0;
148151
// if buffer has not been allocated (buffer_size==0)
149152
if (len > rcfg.buffer_size) {
150153
LOGE("buffer_size %d too small for write size: %d", rcfg.buffer_size,
151154
len);
152155
return len;
153156
}
154157

155-
// wait for buffer to have enough space
156158
if (rcfg.is_blocking){
157-
while(buffer.availableForWrite()<len){
158-
delay(5);
159+
// write of all bytes
160+
int open = len;
161+
while(open > 0){
162+
int written = buffer.writeArray(data + result, open);
163+
open -= written;
164+
result += written;
165+
if (open > 0){
166+
delay(rcfg.blocking_retry_delay_ms);
167+
}
159168
}
169+
} else {
170+
// write as much as possible
171+
result = buffer.writeArray(data, len);
160172
}
161-
162-
size_t result = buffer.writeArray(data, len);
163173
// activate output when buffer is half full
164174
if (!is_active && buffer.bufferCountFilled() >= rcfg.buffer_count / 2) {
165175
LOGI("is_active = true");
166176
is_active = true;
167177
}
178+
168179
return result;
169180
}
170181

0 commit comments

Comments
 (0)