From 9bfbbf0a0c55a14d82ca90857c0076cd82301e9d Mon Sep 17 00:00:00 2001 From: avihal Date: Mon, 15 Aug 2011 16:55:23 +0000 Subject: [PATCH] SPU2-X: Stretcher: fixed a bug which may be triggered by some compiler optimizations (floating point comparisons, doesn't happen with current VS2010 project). Thanks to pseudonym for tracking it down, reading tons of asm code, and learning x87 while at it... git-svn-id: http://pcsx2.googlecode.com/svn/trunk@4872 96395faa-99c1-11dd-bbfe-3dabce05a288 --- plugins/spu2-x/src/Timestretcher.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/plugins/spu2-x/src/Timestretcher.cpp b/plugins/spu2-x/src/Timestretcher.cpp index 58744fb959..b7742aa37b 100644 --- a/plugins/spu2-x/src/Timestretcher.cpp +++ b/plugins/spu2-x/src/Timestretcher.cpp @@ -118,10 +118,9 @@ float addToAvg(float val){ } template -T clamp(T val, T min, T max){ - if( val > max ) return max; - if( val < min ) return min; - return val; +bool IsInRange(const T& val, const T& min, const T& max) +{ + return ( min <= val && val <= max ); } //actual stretch algorithm implementation @@ -162,26 +161,23 @@ void SndBuffer::UpdateTempoChangeSoundTouch2() //Algorithm params: (threshold params (hysteresis), etc) const float hys_ok_factor=1.04; - int hys_min_ok_count=clamp((int)(50.0 *(float)targetIPS/750.0), 2, 100); //consecutive iterations within hys_ok before going to 1:1 mode + int hys_min_ok_count = GetClamped((int)(50.0 *(float)targetIPS/750.0), 2, 100); //consecutive iterations within hys_ok before going to 1:1 mode const float hys_bad_factor=1.2; - int compensationDivider=clamp((int)(100.0 *(float)targetIPS/750), 15, 150); + int compensationDivider = GetClamped((int)(100.0 *(float)targetIPS/750), 15, 150); float tempoAdjust=bufferFullness/dynamicTargetFullness; float avgerage = addToAvg(tempoAdjust); tempoAdjust = avgerage; - tempoAdjust = clamp( tempoAdjust, 0.05f, 10.0f); + tempoAdjust = GetClamped( tempoAdjust, 0.05f, 10.0f); dynamicTargetFullness += (baseTargetFullness/tempoAdjust - dynamicTargetFullness)/(double)compensationDivider; - if( - tempoAdjust == clamp(tempoAdjust, 0.9f, 1.1f) - && dynamicTargetFullness == clamp( dynamicTargetFullness, baseTargetFullness*0.9f, baseTargetFullness*1.1f) - ) + if( IsInRange(tempoAdjust, 0.9f, 1.1f) && IsInRange( dynamicTargetFullness, baseTargetFullness*0.9f, baseTargetFullness*1.1f) ) dynamicTargetFullness=baseTargetFullness; if( !inside_hysteresis ) { - if( tempoAdjust == clamp( tempoAdjust, 1.0f/hys_ok_factor, hys_ok_factor ) ) + if( IsInRange( tempoAdjust, 1.0f/hys_ok_factor, hys_ok_factor ) ) hys_ok_count++; else hys_ok_count=0; @@ -192,7 +188,7 @@ void SndBuffer::UpdateTempoChangeSoundTouch2() } } - else if( tempoAdjust != clamp( tempoAdjust, 1.0f/hys_bad_factor, hys_bad_factor ) ){ + else if( !IsInRange( tempoAdjust, 1.0f/hys_bad_factor, hys_bad_factor ) ){ if(MsgOverruns()) ConLog("~~~~~~> stretch: Dynamic\n"); inside_hysteresis=false; hys_ok_count=0;