Skip to content

Commit e2e65fd

Browse files
Fix I2s::available() to skip currently playing (#1043)
Fixes #963 The available space calculation didn't account for the fact that one of the buffers was currently being output, causing ::available() to be too large and ::write() to block in that case.
1 parent f22ed52 commit e2e65fd

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

libraries/I2S/src/AudioRingBuffer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ int AudioRingBuffer::available() {
215215
}
216216
int avail;
217217
avail = _wordsPerBuffer - _userOff;
218-
avail += ((_bufferCount + _curBuffer - _userBuffer) % _bufferCount) * _wordsPerBuffer;
218+
avail += ((_bufferCount + _curBuffer - _userBuffer) % _bufferCount) * _wordsPerBuffer /* total other buffers */ - _wordsPerBuffer /* minus the one currently being output */;
219+
if (avail < 0) {
220+
avail = 0; // Should never hit
221+
}
219222
return avail;
220223
}
221224

0 commit comments

Comments
 (0)