ADPCM: added helper function for adding int16s without overflowing

This commit is contained in:
Trek H 2019-02-13 12:43:13 +10:30
parent c30b3de7c3
commit 43c6027888
1 changed files with 17 additions and 16 deletions

View File

@ -97,9 +97,9 @@ func encodeSample(sample int16) byte {
// adjust predicted sample based on calculated difference // adjust predicted sample based on calculated difference
if nibble&8 != 0 { if nibble&8 != 0 {
encPred -= diff encPred = capAdd16(encPred, -diff)
} else { } else {
encPred += diff encPred = capAdd16(encPred, diff)
} }
// check for underflow and overflow // check for underflow and overflow
@ -142,20 +142,8 @@ func decodeSample(nibble byte) int16 {
diff = -diff diff = -diff
} }
// adjust predicted sample based on calculated difference, check for overflow // adjust predicted sample based on calculated difference
if diff > 0 { decPred = capAdd16(decPred, diff)
if decPred > math.MaxInt16-diff {
decPred = math.MaxInt16
} else {
decPred += diff
}
} else {
if decPred < math.MaxInt16-diff {
decPred = math.MaxInt16
} else {
decPred += diff
}
}
// adjust index into step size lookup table using nibble // adjust index into step size lookup table using nibble
decIndex += indexTable[nibble] decIndex += indexTable[nibble]
@ -173,6 +161,19 @@ func decodeSample(nibble byte) int16 {
return decPred return decPred
} }
// capAdd16 adds two int16s together and caps at max/min int16 instead of overflowing
func capAdd16(a, b int16) int16 {
c := int32(a) + int32(b)
switch {
case c < math.MinInt16:
return math.MinInt16
case c > math.MaxInt16:
return math.MaxInt16
default:
return int16(c)
}
}
func calcHead(sample []byte) ([]byte, error) { func calcHead(sample []byte) ([]byte, error) {
// check that we are given 1 16-bit sample (2 bytes) // check that we are given 1 16-bit sample (2 bytes)
sampSize := 2 sampSize := 2