From 15de111dcf542b27344dad9f4eb81f079a329fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Casta=C3=B1eda?= Date: Fri, 4 Dec 2020 21:26:40 +0100 Subject: [PATCH] FileGameSound: fix buffer advance accounting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I15bb2b1e703cad955544a1151adc6a1277b077a8 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3467 Reviewed-by: Jérôme Duval --- src/kits/game/FileGameSound.cpp | 84 ++++----------------------------- 1 file changed, 10 insertions(+), 74 deletions(-) diff --git a/src/kits/game/FileGameSound.cpp b/src/kits/game/FileGameSound.cpp index 7749467025..dae021e98a 100644 --- a/src/kits/game/FileGameSound.cpp +++ b/src/kits/game/FileGameSound.cpp @@ -32,17 +32,18 @@ struct _gs_media_tracker { // Local utility functions ----------------------------------------------- +template bool -FillBuffer(_gs_ramp* ramp, uint8* data, uint8* buffer, size_t* bytes) +FillBuffer(_gs_ramp* ramp, T* dest, const T* src, size_t* bytes) { - int32 samples = *bytes / sizeof(uint8); + size_t samples = *bytes / sizeof(T); - for (int32 byte = 0; byte < samples; byte++) { + for (size_t sample = 0; sample < samples; sample++) { float gain = *ramp->value; - data[byte] = uint8(float(buffer[byte]) * gain); + dest[sample] = T(float(src[sample]) * gain); if (ChangeRamp(ramp)) { - *bytes = byte * sizeof(uint8); + *bytes = sample * sizeof(T); return true; } } @@ -51,71 +52,6 @@ FillBuffer(_gs_ramp* ramp, uint8* data, uint8* buffer, size_t* bytes) } -bool -FillBuffer(_gs_ramp* ramp, int16* data, int16* buffer, size_t* bytes) -{ - int32 samples = *bytes / sizeof(int16); - - for (int32 byte = 0; byte < samples; byte++) { - float gain = *ramp->value; - data[byte] = int16(float(buffer[byte]) * gain); - - if (ChangeRamp(ramp)) { - *bytes = byte * sizeof(int16); - return true; - } - } - - return false; -} - - -bool -FillBuffer(_gs_ramp* ramp, int32* data, int32* buffer, size_t* bytes) -{ - size_t byte = 0; - bool bytesAreReady = (*bytes > 0); - - while (bytesAreReady) { - float gain = *ramp->value; - data[byte] = int32(float(buffer[byte]) * gain); - - if (ChangeRamp(ramp)) { - *bytes = byte; - return true; - } - - byte++; - bytesAreReady = (byte >= *bytes); - } - - return false; -} - - -bool -FillBuffer(_gs_ramp* ramp, float* data, float* buffer, size_t* bytes) -{ - size_t byte = 0; - bool bytesAreReady = (*bytes > 0); - - while (bytesAreReady) { - float gain = *ramp->value; - data[byte] = buffer[byte] * gain; - - if (ChangeRamp(ramp)) { - *bytes = byte; - return true; - } - - byte++; - bytesAreReady = (byte >= *bytes); - } - - return false; -} - - // BFileGameSound ------------------------------------------------------- BFileGameSound::BFileGameSound(const entry_ref* file, bool looping, BGameSoundDevice* device) @@ -273,25 +209,25 @@ BFileGameSound::FillBuffer(void* inBuffer, size_t inByteCount) switch(Format().format) { case gs_audio_format::B_GS_U8: - rampDone = ::FillBuffer(fPausing, + rampDone = ::FillBuffer(fPausing, (uint8*)&buffer[out_offset], (uint8*)&fBuffer[fPlayPosition], &bytes); break; case gs_audio_format::B_GS_S16: - rampDone = ::FillBuffer(fPausing, + rampDone = ::FillBuffer(fPausing, (int16*)&buffer[out_offset], (int16*)&fBuffer[fPlayPosition], &bytes); break; case gs_audio_format::B_GS_S32: - rampDone = ::FillBuffer(fPausing, + rampDone = ::FillBuffer(fPausing, (int32*)&buffer[out_offset], (int32*)&fBuffer[fPlayPosition], &bytes); break; case gs_audio_format::B_GS_F: - rampDone = ::FillBuffer(fPausing, + rampDone = ::FillBuffer(fPausing, (float*)&buffer[out_offset], (float*)&fBuffer[fPlayPosition], &bytes); break;