From bb4078cc26f6fb9633b758ecaa5477bf09b04a37 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 14 Nov 2021 09:27:45 +1000 Subject: [PATCH] Fix a bug when reading from a looped data source with a range. --- miniaudio.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index 7ba80064..672535d2 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -52269,7 +52269,16 @@ static ma_result ma_data_source_read_pcm_frames_within_range(ma_data_source* pDa frameCount = (rangeEnd - cursor); } - result = pDataSourceBase->vtable->onRead(pDataSourceBase, pFramesOut, frameCount, &framesRead); + /* + If the cursor is sitting on the end of the range the frame count will be set to 0 which can + result in MA_INVALID_ARGS. In this case, we don't want to try reading, but instead return + MA_AT_END so the higher level function can know about it. + */ + if (frameCount > 0) { + result = pDataSourceBase->vtable->onRead(pDataSourceBase, pFramesOut, frameCount, &framesRead); + } else { + result = MA_AT_END; /* The cursor is sitting on the end of the range which means we're at the end. */ + } } } @@ -52356,8 +52365,8 @@ MA_API ma_result ma_data_source_read_pcm_frames(ma_data_source* pDataSource, voi } /* - We can determine if we've reached the end by checking the return value of the onRead() - callback. To loop back to the start, all we need to do is seek back to the first frame. + We can determine if we've reached the end by checking if ma_data_source_read_pcm_frames_within_range() returned + MA_AT_END. To loop back to the start, all we need to do is seek back to the first frame. */ if (result == MA_AT_END) { /*