audio: Filter overflow values

This commit is contained in:
Hajime Hoshi 2016-03-03 12:22:10 +09:00
parent 3859bc7421
commit 05f52b5e57

View File

@ -64,10 +64,15 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) {
resultLen = min(len(p.buf)/2*2, resultLen) resultLen = min(len(p.buf)/2*2, resultLen)
} }
for i := 0; i < resultLen/2; i++ { for i := 0; i < resultLen/2; i++ {
x := int16(0) x := 0
for p := range s.context.players { for p := range s.context.players {
// TODO: Overflow check to avoid distortion x += int(int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8))
x += int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8) }
if x > (1<<15)-1 {
x = (1 << 15) - 1
}
if x < -(1 << 15) {
x = -(1 << 15)
} }
b[2*i] = byte(x) b[2*i] = byte(x)
b[2*i+1] = byte(x >> 8) b[2*i+1] = byte(x >> 8)