Skip to content

Commit 71773bf

Browse files
authored
Fix return value AnalogDriverArduino::read (#772)
AnalogDriverArduino::readBytes is supposed to return the number of bytes written to the array passed in as first argument. It does this by calling `buffer->readArray` and returning the same value returned by that function. However, that function actually returns the number of int16_t samples, each of which is 2 bytes, so in fact the readBytes function should return that value multiplied by 2. This is particularly important because if AnalogDriverArduino::readBytes returns a value of 1, then many of the end consumers will simply drop that value. This is because many of the end consumers divide the number of bytes by 2 again to get the number of samples, and in integer arithmetic, dividing 1 by 2 gives 0. In some circumstances this can lead to a lot of samples being dropped. This pr just adds the "times 2" to AnalogDriverArduino::readBytes
1 parent 519a5f7 commit 71773bf

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/AudioAnalog/AnalogAudioArduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class AnalogDriverArduino : public AnalogDriverBase {
5151
size_t readBytes(uint8_t *values, size_t len) {
5252
if (buffer==nullptr) return 0;
5353
int samples = len / 2;
54-
return buffer->readArray((int16_t *)values, samples);
54+
return buffer->readArray((int16_t *)values, samples) * 2;
5555
}
5656

5757
protected:
@@ -100,4 +100,4 @@ using AnalogDriver = AnalogDriverArduino;
100100

101101
} // namespace audio_tools
102102

103-
#endif
103+
#endif

0 commit comments

Comments
 (0)