mirror of https://bitbucket.org/ausocean/av.git
ADPCM: added helper function for adding int16s without overflowing
This commit is contained in:
parent
8762b3c74f
commit
6edd86f5da
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue