Merge pull request #36 from lioncash/generic-clamp-function

Introduce a generic clamp function to clean up some of the various 'if-else' clamps throughout parts of the codebase.
This commit is contained in:
Matthew Parlane 2014-02-05 22:07:53 +13:00
commit cfa2d012c8
10 changed files with 43 additions and 61 deletions

View File

@ -13,6 +13,7 @@
#include <stdlib.h>
#include <string.h>
#include "DPL2Decoder.h"
#include "MathUtil.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
@ -72,17 +73,6 @@ template<class T> static T firfilter(const T *buf, int pos, int len, int count,
return T(r1+r2);
}
template<class T> inline const T& limit(const T& val, const T& min, const T& max)
{
if (val < min) {
return min;
} else if (val > max) {
return max;
} else {
return val;
}
}
/*
// Hamming
// 2*pi*k
@ -133,7 +123,7 @@ float* design_fir(unsigned int *n, float* fc, float opt)
// Sanity check
if(*n==0) return NULL;
fc[0]=limit(fc[0],float(0.001),float(1));
MathUtil::Clamp(&fc[0],float(0.001),float(1));
float *w=(float*)calloc(sizeof(float),*n);

View File

@ -13,6 +13,15 @@
namespace MathUtil
{
template<class T>
inline void Clamp(T* val, const T& min, const T& max)
{
if (*val < min)
*val = min;
else if (*val > max)
*val = max;
}
static const u64 DOUBLE_SIGN = 0x8000000000000000ULL,
DOUBLE_EXP = 0x7FF0000000000000ULL,

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include "Common.h"
#include "MathUtil.h"
#include "DSPCore.h"
#include "DSPHost.h"
@ -36,11 +37,7 @@ static s16 ADPCM_Step(u32& _rSamplePos)
// 0x400 = 0.5 in 11-bit fixed point
int val = (scale * temp) + ((0x400 + coef1 * (s16)g_dsp.ifx_regs[DSP_YN1] + coef2 * (s16)g_dsp.ifx_regs[DSP_YN2]) >> 11);
// Clamp values.
if (val > 0x7FFF)
val = 0x7FFF;
else if (val < -0x7FFF)
val = -0x7FFF;
MathUtil::Clamp(&val, -0x7FFF, 0x7FFF);
g_dsp.ifx_regs[DSP_YN2] = g_dsp.ifx_regs[DSP_YN1];
g_dsp.ifx_regs[DSP_YN1] = val;

View File

@ -51,6 +51,7 @@ This file mainly deals with the [Drive I/F], however [AIDFR] controls
*/
#include "Common.h"
#include "MathUtil.h"
#include "StreamADPCM.h"
#include "AudioInterface.h"
@ -153,7 +154,7 @@ void Init()
m_Control.hex = 0;
m_Control.AISFR = AIS_48KHz;
m_Volume.hex = 0;
m_SampleCounter = 0;
m_SampleCounter = 0;
m_InterruptTiming = 0;
g_LastCPUTime = 0;
@ -207,8 +208,8 @@ void Write32(const u32 _Value, const u32 _Address)
{
AICR tmpAICtrl(_Value);
m_Control.AIINTMSK = tmpAICtrl.AIINTMSK;
m_Control.AIINTVLD = tmpAICtrl.AIINTVLD;
m_Control.AIINTMSK = tmpAICtrl.AIINTMSK;
m_Control.AIINTVLD = tmpAICtrl.AIINTVLD;
// Set frequency of streaming audio
if (tmpAICtrl.AISFR != m_Control.AISFR)
@ -232,7 +233,7 @@ void Write32(const u32 _Value, const u32 _Address)
if (tmpAICtrl.PSTAT != m_Control.PSTAT)
{
DEBUG_LOG(AUDIO_INTERFACE, "%s streaming audio", tmpAICtrl.PSTAT ? "start":"stop");
m_Control.PSTAT = tmpAICtrl.PSTAT;
m_Control.PSTAT = tmpAICtrl.PSTAT;
g_LastCPUTime = CoreTiming::GetTicks();
// Tell Drive Interface to start/stop streaming
@ -338,13 +339,11 @@ unsigned int Callback_GetStreaming(short* _pDestBuffer, unsigned int _numSamples
if (i % 3)
{
pcm_l = (((pcm_l + (int)pcm[pos*2]) / 2 * lvolume) >> 8) + (int)(*_pDestBuffer);
if (pcm_l > 32767) pcm_l = 32767;
else if (pcm_l < -32767) pcm_l = -32767;
MathUtil::Clamp(&pcm_l, -32767, 32767);
*_pDestBuffer++ = pcm_l;
pcm_r = (((pcm_r + (int)pcm[pos*2+1]) / 2 * rvolume) >> 8) + (int)(*_pDestBuffer);
if (pcm_r > 32767) pcm_r = 32767;
else if (pcm_r < -32767) pcm_r = -32767;
MathUtil::Clamp(&pcm_r, -32767, 32767);
*_pDestBuffer++ = pcm_r;
}
pcm_l = pcm[pos*2];
@ -361,7 +360,7 @@ unsigned int Callback_GetStreaming(short* _pDestBuffer, unsigned int _numSamples
static s16 l1 = 0;
static s16 l2 = 0;
if ( frac >= 0x10000 || frac == 0)
if (frac >= 0x10000 || frac == 0)
{
frac &= 0xffff;
@ -374,13 +373,11 @@ unsigned int Callback_GetStreaming(short* _pDestBuffer, unsigned int _numSamples
pcm_l = (pcm_l * lvolume >> 8) + (int)(*_pDestBuffer);
if (pcm_l > 32767) pcm_l = 32767;
else if (pcm_l < -32767) pcm_l = -32767;
MathUtil::Clamp(&pcm_l, -32767, 32767);
*_pDestBuffer++ = pcm_l;
pcm_r = (pcm_r * lvolume >> 8) + (int)(*_pDestBuffer);
if (pcm_r > 32767) pcm_r = 32767;
else if (pcm_r < -32767) pcm_r = -32767;
MathUtil::Clamp(&pcm_r, -32767, 32767);
*_pDestBuffer++ = pcm_r;
frac += ratio;
@ -390,13 +387,11 @@ unsigned int Callback_GetStreaming(short* _pDestBuffer, unsigned int _numSamples
else //1:1 no resampling
{
pcm_l = (((int)pcm[pos*2] * lvolume) >> 8) + (int)(*_pDestBuffer);
if (pcm_l > 32767) pcm_l = 32767;
else if (pcm_l < -32767) pcm_l = -32767;
MathUtil::Clamp(&pcm_l, -32767, 32767);
*_pDestBuffer++ = pcm_l;
pcm_r = (((int)pcm[pos*2+1] * rvolume) >> 8) + (int)(*_pDestBuffer);
if (pcm_r > 32767) pcm_r = 32767;
else if (pcm_r < -32767) pcm_r = -32767;
MathUtil::Clamp(&pcm_r, -32767, 32767);
*_pDestBuffer++ = pcm_r;
pos++;

View File

@ -6,6 +6,7 @@
#include "../../DSP.h"
#include "FileUtil.h"
#include "ConfigManager.h"
#include "MathUtil.h"
#define AX_GC
#include "UCode_AX_Voice.h"
@ -559,12 +560,10 @@ void CUCode_AX::OutputSamples(u32 lr_addr, u32 surround_addr)
int left = m_samples_left[i];
int right = m_samples_right[i];
if (left < -32767) left = -32767;
if (left > 32767) left = 32767;
if (right < -32767) right = -32767;
if (right > 32767) right = 32767;
MathUtil::Clamp(&left, -32767, 32767);
MathUtil::Clamp(&right, -32767, 32767);
buffer[2 * i] = Common::swap16(right);
buffer[2 * i + 0] = Common::swap16(right);
buffer[2 * i + 1] = Common::swap16(left);
}

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2
// Refer to the license.txt file included.
#include "MathUtil.h"
#include "StringUtil.h"
#include "../MailHandler.h"
@ -617,10 +618,8 @@ void CUCode_AXWii::OutputSamples(u32 lr_addr, u32 surround_addr, u16 volume,
left = ((s64)left * volume_ramp[i]) >> 15;
right = ((s64)right * volume_ramp[i]) >> 15;
if (left < -32767) left = -32767;
if (left > 32767) left = 32767;
if (right < -32767) right = -32767;
if (right > 32767) right = 32767;
MathUtil::Clamp(&left, -32767, 32767);
MathUtil::Clamp(&right, -32767, 32767);
m_samples_left[i] = left;
m_samples_right[i] = right;
@ -655,8 +654,7 @@ void CUCode_AXWii::OutputWMSamples(u32* addresses)
for (u32 j = 0; j < 3 * 6; ++j)
{
int sample = in[j];
if (sample < -32767) sample = -32767;
if (sample > 32767) sample = 32767;
MathUtil::Clamp(&sample, -32767, 32767);
out[j] = Common::swap16((u16)sample);
}
}

View File

@ -14,6 +14,7 @@
#endif
#include "Common.h"
#include "MathUtil.h"
#include "UCode_AXStructs.h"
#include "../../DSP.h"
@ -216,9 +217,7 @@ u16 AcceleratorGetSample()
temp -= 16;
int val = (scale * temp) + ((0x400 + coef1 * acc_pb->adpcm.yn1 + coef2 * acc_pb->adpcm.yn2) >> 11);
if (val > 0x7FFF) val = 0x7FFF;
else if (val < -0x7FFF) val = -0x7FFF;
MathUtil::Clamp(&val, -0x7FFF, 0x7FFF);
acc_pb->adpcm.yn2 = acc_pb->adpcm.yn1;
acc_pb->adpcm.yn1 = val;

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include "Common.h"
#include "MathUtil.h"
#include "UCode_Zelda.h"
void CUCode_Zelda::AFCdecodebuffer(const s16 *coef, const char *src, signed short *out, short *histp, short *hist2p, int type)
@ -55,10 +56,7 @@ void CUCode_Zelda::AFCdecodebuffer(const s16 *coef, const char *src, signed shor
{
int sample = delta * nibbles[i] + ((int)hist * coef[idx * 2]) + ((int)hist2 * coef[idx * 2 + 1]);
sample >>= 11;
if (sample > 32767)
sample = 32767;
if (sample < -32768)
sample = -32768;
MathUtil::Clamp(&sample, -32768, 32767);
out[i] = sample;
hist2 = hist;
hist = (short)sample;

View File

@ -8,6 +8,7 @@
#include "UCode_Zelda.h"
#include "AudioCommon.h"
#include "MathUtil.h"
#include "Mixer.h"
#include "../../Memmap.h"
#include "../../DSP.h"
@ -780,12 +781,10 @@ void CUCode_Zelda::MixAdd(short *_Buffer, int _Size)
s32 left = (s32)_Buffer[0] + m_LeftBuffer[i];
s32 right = (s32)_Buffer[1] + m_RightBuffer[i];
if (left < -32768) left = -32768;
if (left > 32767) left = 32767;
MathUtil::Clamp(&left, -32768, 32767);
_Buffer[0] = (short)left;
if (right < -32768) right = -32768;
if (right > 32767) right = 32767;
MathUtil::Clamp(&right, -32768, 32767);
_Buffer[1] = (short)right;
_Buffer += 2;

View File

@ -5,6 +5,7 @@
// Adapted from in_cube by hcs & destop
#include "StreamADPCM.h"
#include "MathUtil.h"
// STATE_TO_SAVE (not saved yet!)
static s32 histl1;
@ -31,8 +32,7 @@ s16 ADPDecodeSample(s32 bits, s32 q, s32& hist1, s32& hist2)
break;
}
hist = (hist + 0x20) >> 6;
if (hist > 0x1fffff) hist = 0x1fffff;
if (hist < -0x200000) hist = -0x200000;
MathUtil::Clamp(&hist, -0x200000, 0x1fffff);
s32 cur = (((s16)(bits << 12) >> (q & 0xf)) << 6) + hist;
@ -40,9 +40,7 @@ s16 ADPDecodeSample(s32 bits, s32 q, s32& hist1, s32& hist2)
hist1 = cur;
cur >>= 6;
if (cur < -0x8000) return -0x8000;
if (cur > 0x7fff) return 0x7fff;
MathUtil::Clamp(&cur, -0x8000, 0x7fff);
return (s16)cur;
}