diff --git a/src/hardware/dbopl.cpp b/src/hardware/dbopl.cpp index eda3ca6e..dee31958 100644 --- a/src/hardware/dbopl.cpp +++ b/src/hardware/dbopl.cpp @@ -1237,6 +1237,7 @@ void Chip::Setup( Bit32u rate ) { EnvelopeSelect( i, index, shift ); linearRates[i] = (Bit32u)( scale * (EnvelopeIncreaseTable[ index ] << ( RATE_SH + ENV_EXTRA - shift - 3 ))); } + Bit32s attackDiffs[62]; //Generate the best matching attack rate for ( Bit8u i = 0; i < 62; i++ ) { Bit8u index, shift; @@ -1267,22 +1268,23 @@ void Chip::Setup( Bit32u rate ) { if ( lDiff < bestDiff ) { bestDiff = lDiff; bestAdd = guessAdd; + //We hit an exactly matching sample count if ( !bestDiff ) break; } + //Linear correction factor, not exactly perfect but seems to work + double correct = (original - diff) / (double)original; + guessAdd = (Bit32u)(guessAdd * correct); //Below our target if ( diff < 0 ) { - //Better than the last time - Bit32s mul = ((original - diff) << 12) / original; - guessAdd = ((guessAdd * mul) >> 12); + //Always add one here for rounding, an overshoot will get corrected by another pass decreasing guessAdd++; } else if ( diff > 0 ) { - Bit32s mul = ((original - diff) << 12) / original; - guessAdd = (guessAdd * mul) >> 12; - guessAdd--; } } attackRates[i] = bestAdd; + //Keep track of the diffs for some debugging + attackDiffs[i] = bestDiff; } for ( Bit8u i = 62; i < 76; i++ ) { //This should provide instant volume maximizing