[haiku-commits] Change in haiku[master]: Fix wrap for AUDIO_INT

  • From: Gerrit <review@xxxxxxxxxxxxxxxxxxx>
  • To: waddlesplash <waddlesplash@xxxxxxxxx>, haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 15 Dec 2020 16:53:05 +0000

From Máximo Castañeda <antiswen@xxxxxxxx>:

Máximo Castañeda has uploaded this change for review. ( 
https://review.haiku-os.org/c/haiku/+/3511 ;)


Change subject: Fix wrap for AUDIO_INT
......................................................................

Fix wrap for AUDIO_INT

Floats don't have enough precision for all 32 bit integers. In
particular, near INT32_MAX their value is INT32_MAX + 1, which, when
converted back to int becomes INT32_MIN.

Change-Id: Ief3c1177b4f69baac13df5bac977882fea95ae01
---
M src/add-ons/media/media-add-ons/mixer/Interpolate.cpp
M src/add-ons/media/media-add-ons/mixer/Resampler.cpp
M src/add-ons/media/media-add-ons/multi_audio/Resampler.cpp
M src/kits/game/FileGameSound.cpp
M src/kits/game/GameSoundBuffer.cpp
5 files changed, 12 insertions(+), 10 deletions(-)



  git pull ssh://git.haiku-os.org:22/haiku refs/changes/11/3511/1

diff --git a/src/add-ons/media/media-add-ons/mixer/Interpolate.cpp 
b/src/add-ons/media/media-add-ons/mixer/Interpolate.cpp
index 48ffd26..d75e711 100644
--- a/src/add-ons/media/media-add-ons/mixer/Interpolate.cpp
+++ b/src/add-ons/media/media-add-ons/mixer/Interpolate.cpp
@@ -33,7 +33,7 @@
        if (srcSampleCount == destSampleCount) {
                // optimized case for no resampling
                while (count--) {
-                       float tmp = ((*(const inType*)src) - inMiddle) * gain + 
outMiddle;
+                       double tmp = ((*(const inType*)src) - inMiddle) * gain 
+ outMiddle;
                        if (tmp < min) tmp = min;
                        if (tmp > max) tmp = max;
                        *(outType *)dest = (outType)tmp;
@@ -50,8 +50,8 @@
        #define SRC *(const inType*)(src)

        while (count--) {
-               float tmp = (gain * (oldSample + (SRC - oldSample) * current - 
inMiddle)
-                        + outMiddle);
+               double tmp = ((oldSample + (SRC - oldSample) * current - 
inMiddle)
+                        * gain + outMiddle);
                if (tmp < min) tmp = min;
                if (tmp > max) tmp = max;
                *(outType *)dest = (outType)tmp;
diff --git a/src/add-ons/media/media-add-ons/mixer/Resampler.cpp 
b/src/add-ons/media/media-add-ons/mixer/Resampler.cpp
index 58f43fc..567e961 100644
--- a/src/add-ons/media/media-add-ons/mixer/Resampler.cpp
+++ b/src/add-ons/media/media-add-ons/mixer/Resampler.cpp
@@ -33,7 +33,7 @@
        if (srcSampleCount == destSampleCount) {
                // optimized case for no resampling
                while (count--) {
-                       float tmp = ((*(const inType*)src) - inMiddle) * gain + 
outMiddle;
+                       double tmp = ((*(const inType*)src) - inMiddle) * gain 
+ outMiddle;
                        if (tmp < min) tmp = min;
                        if (tmp > max) tmp = max;
                        *(outType *)dest = (outType)tmp;
@@ -48,7 +48,7 @@

        // downsample
        while (count--) {
-               float tmp = ((*(const inType*)src) - inMiddle) * gain + 
outMiddle;
+               double tmp = ((*(const inType*)src) - inMiddle) * gain + 
outMiddle;
                if (tmp < min) tmp = min;
                if (tmp > max) tmp = max;
                *(outType *)dest = (outType)tmp;
diff --git a/src/add-ons/media/media-add-ons/multi_audio/Resampler.cpp 
b/src/add-ons/media/media-add-ons/multi_audio/Resampler.cpp
index 5ff5e77..a0d02ce 100644
--- a/src/add-ons/media/media-add-ons/multi_audio/Resampler.cpp
+++ b/src/add-ons/media/media-add-ons/multi_audio/Resampler.cpp
@@ -215,7 +215,8 @@
        void *outputData, uint32 outputStride, uint32 sampleCount)
 {
        while (sampleCount > 0) {
-               *(int32*)outputData = (int32)(*(const float*)inputData * 
2147483647.0f);
+               *(int32*)outputData = (int32)(*(const float*)inputData
+                       * (double)INT32_MAX);

                outputData = (void*)((uint8*)outputData + outputStride);
                inputData = (void*)((uint8*)inputData + inputStride);
@@ -304,7 +305,8 @@
        void *outputData, uint32 outputStride, uint32 sampleCount)
 {
        while (sampleCount > 0) {
-               *(int32*)outputData = (int32)(*(const double*)inputData * 
2147483647.0f);
+               *(int32*)outputData = (int32)(*(const double*)inputData
+                       * (double)INT32_MAX);

                outputData = (void*)((uint8*)outputData + outputStride);
                inputData = (void*)((uint8*)inputData + inputStride);
diff --git a/src/kits/game/FileGameSound.cpp b/src/kits/game/FileGameSound.cpp
index 7463864..645701b 100644
--- a/src/kits/game/FileGameSound.cpp
+++ b/src/kits/game/FileGameSound.cpp
@@ -40,7 +40,7 @@

        for (size_t sample = 0; sample < samples; sample++) {
                float gain = *ramp->value;
-               dest[sample] = T(float(src[sample] - middle) * gain + middle);
+               dest[sample] = T(double(src[sample] - middle) * gain + middle);

                if (ChangeRamp(ramp)) {
                        *bytes = sample * sizeof(T);
diff --git a/src/kits/game/GameSoundBuffer.cpp 
b/src/kits/game/GameSoundBuffer.cpp
index 294803b..f67c5ec 100644
--- a/src/kits/game/GameSoundBuffer.cpp
+++ b/src/kits/game/GameSoundBuffer.cpp
@@ -47,8 +47,8 @@
 static inline void
 ApplyMod(T* data, int64 index, float* pan)
 {
-       data[index * 2] = T(float(data[index * 2] - middle) * pan[0] + middle);
-       data[index * 2 + 1] = T(float(data[index * 2 + 1] - middle) * pan[1]
+       data[index * 2] = T(double(data[index * 2] - middle) * pan[0] + middle);
+       data[index * 2 + 1] = T(double(data[index * 2 + 1] - middle) * pan[1]
                                                        + middle);
 }


--
To view, visit https://review.haiku-os.org/c/haiku/+/3511
To unsubscribe, or for help writing mail filters, visit 
https://review.haiku-os.org/settings

Gerrit-Project: haiku
Gerrit-Branch: master
Gerrit-Change-Id: Ief3c1177b4f69baac13df5bac977882fea95ae01
Gerrit-Change-Number: 3511
Gerrit-PatchSet: 1
Gerrit-Owner: Máximo Castañeda <antiswen@xxxxxxxx>
Gerrit-MessageType: newchange

Other related posts:

  • » [haiku-commits] Change in haiku[master]: Fix wrap for AUDIO_INT - Gerrit