Skip to content

Commit 28951c6

Browse files
authored
Saturate the IMA ADPCM predictor instead of wrapping it (#20)
1 parent bb3fb55 commit 28951c6

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Fmod5Sharp/CodecRebuilders/FmodImaAdPcmRebuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ private static void ExpandNibble(MemoryStream stream, long byteOffset, int nibbl
5252
//Sample changes by the delta
5353
sampleDecoded += delta;
5454

55-
//New sample becomes the previous value, but clamped to a short.
56-
hist = Utils.Clamp((short)sampleDecoded, short.MinValue, short.MaxValue);
55+
//New sample becomes the previous value, saturated - NOT wrapped - to a short.
56+
//Casting to short first would truncate (i.e. wrap around) any out-of-range value,
57+
//which is exactly what the clamp is supposed to prevent. See ClampToShort.
58+
hist = Utils.ClampToShort(sampleDecoded);
5759

5860
//Step index changes based on what was stored in the file, clamped to fit in the array
5961
stepIndex += IMA_IndexTable[sampleNibble];

0 commit comments

Comments
 (0)