Skip to content

Commit 61df006

Browse files
BREAKING: ABM::available(ForWrite) return bytes
The Arduino spec is for available and availableForWrite to return a number of bytes, not uint32_t's. Libraries and apps using AudioBufferManager were getting number of 32b units available, not bytes (I2S, PWMAudio, BT Audio).
1 parent 452ef17 commit 61df006

File tree

11 files changed

+10
-12
lines changed

11 files changed

+10
-12
lines changed

libraries/ADCInput/src/ADCInput.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ int ADCInput::available() {
156156
if (!_running) {
157157
return 0;
158158
} else {
159-
return _arb->available();
159+
return _arb->available() + _isHolding ? sizeof(int16_t) : 0;
160160
}
161161
}
162162

libraries/AudioBufferManager/src/AudioBufferManager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ int AudioBufferManager::available() {
259259
return 0;
260260
}
261261

262-
int avail = _wordsPerBuffer - _userOff; // Currently available in this buffer
262+
int avail = (_wordsPerBuffer - _userOff) * sizeof(uint32_t); // Currently available in this buffer
263263

264264
// Each add'l buffer has wpb spaces...
265265
auto x = p->next;
266266
while (x) {
267-
avail += _wordsPerBuffer;
267+
avail += _wordsPerBuffer * sizeof(uint32_t);
268268
x = x->next;
269269
}
270270
return avail;

libraries/BluetoothAudio/examples/A2DPSource/A2DPSource.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void setup() {
8080
}
8181

8282
void loop() {
83-
while ((size_t)a2dp.availableForWrite() > sizeof(pcm)) {
83+
while ((size_t)a2dp.availableForWrite() >= sizeof(pcm)) {
8484
fillPCM();
8585
a2dp.write((const uint8_t *)pcm, sizeof(pcm));
8686
}

libraries/BluetoothAudio/src/A2DPSource.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ int A2DPSource::availableForWrite() {
262262
} else {
263263
avail = _pcmBufferSize - _pcmWriter + _pcmReader - 1;
264264
}
265-
avail /= sizeof(uint32_t); // availableForWrite always 32b sample pairs in this core...
266265
return avail;
267266
}
268267

libraries/BluetoothAudio/src/BluetoothAudioConsumerI2S.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void BluetoothAudioConsumerI2S::close() {
6969
}
7070

7171
void BluetoothAudioConsumerI2S::fill() {
72-
int num_samples = _i2s->availableForWrite() / 2;
72+
int num_samples = _i2s->availableForWrite() / (2 * sizeof(int16_t));
7373
int16_t buff[32 * 2];
7474
while (num_samples > 63) {
7575
_a2dpSink->playback_handler((int16_t *) buff, 32);

libraries/BluetoothAudio/src/BluetoothAudioConsumerPWM.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void BluetoothAudioConsumerPWM::close() {
7070
}
7171

7272
void BluetoothAudioConsumerPWM::fill() {
73-
int num_samples = _pwm->availableForWrite() / 2;
73+
int num_samples = _pwm->availableForWrite() / (2 * sizeof(int16_t));
7474
int16_t buff[32 * 2];
7575
while (num_samples > 63) {
7676
_a2dpSink->playback_handler((int16_t *) buff, 32);

libraries/BluetoothHIDMaster/examples/KeyboardPiano/KeyboardPiano.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void silenceOscillators() {
4646

4747
// PWM callback, generates sum of online oscillators
4848
void fill() {
49-
int num_samples = pwm.availableForWrite() / 2;
49+
int num_samples = pwm.availableForWrite() / (2 * sizeof(int16_t));
5050
int16_t buff[32 * 2];
5151

5252
while (num_samples > 63) {

libraries/BluetoothHIDMaster/examples/KeyboardPianoBLE/KeyboardPianoBLE.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void silenceOscillators() {
4646

4747
// PWM callback, generates sum of online oscillators
4848
void fill() {
49-
int num_samples = pwm.availableForWrite() / 2;
49+
int num_samples = pwm.availableForWrite() / (2 * sizeof(int16_t);
5050
int16_t buff[32 * 2];
5151

5252
while (num_samples > 63) {

libraries/I2S/src/I2S.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ int I2S::available() {
307307
return 0;
308308
} else {
309309
auto avail = _arb->available();
310-
avail *= 4; // 4 samples per 32-bits
311310
if (_bps < 24 && !_isOutput) {
312311
avail += _isHolding / 8;
313312
}

libraries/PWMAudio/examples/PlayRaw/PlayRaw.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ PWMAudio pwm(1);
1717
unsigned int count = 0;
1818

1919
void cb() {
20-
while (pwm.availableForWrite()) {
20+
while (pwm.availableForWrite() > sizeof(int16_t)) {
2121
pwm.write(*p++);
2222
count += 2;
2323
if (count >= sizeof(out_raw)) {

libraries/PWMAudio/examples/PlayStereo/PlayStereo.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ double sineTable[128]; // Precompute sine wave in 128 steps
2424

2525
unsigned int cnt = 0;
2626
void cb() {
27-
while (pwm.availableForWrite()) {
27+
while (pwm.availableForWrite() >= 2 * sizeof(int16_t)) {
2828
double now = ((double)cnt) / (double)freq;
2929
int fl = freqL << 7; // Prescale by 128 to avoid FP math later on
3030
int fr = freqR << 7; // Prescale by 128 to avoid FP math later on

0 commit comments

Comments
 (0)