mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-28 03:10:24 -04:00
Throughout sointu, we assume stereo audiobuffers, but were passing around []float32. This had several issues, including len(buf)/2 and numSamples*2 type of length conversion in many places. Also, it caused one bug in a test case, causing it to succeed when it should have not (the test had +-1 when it should have had +-2). This refactoring makes it impossible to have odd length buffer issues.
33 lines
771 B
Go
33 lines
771 B
Go
package oto
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/vsariola/sointu"
|
|
)
|
|
|
|
// FloatBufferTo16BitLE is a naive helper method to convert []float32 buffers to
|
|
// 16-bit little-endian, but encoded in byte buffer
|
|
//
|
|
// Appends the encoded bytes into "to" slice, allowing you to preallocate the
|
|
// capacity or just use nil
|
|
func FloatBufferTo16BitLE(from sointu.AudioBuffer, to []byte) []byte {
|
|
for _, v := range from {
|
|
left := to16BitSample(v[0])
|
|
right := to16BitSample(v[1])
|
|
to = append(to, byte(left&255), byte(left>>8), byte(right&255), byte(right>>8))
|
|
}
|
|
return to
|
|
}
|
|
|
|
// convert float32 to int16, clamping to min and max
|
|
func to16BitSample(v float32) int16 {
|
|
if v < -1.0 {
|
|
return -math.MaxInt16
|
|
}
|
|
if v > 1.0 {
|
|
return math.MaxInt16
|
|
}
|
|
return int16(v * math.MaxInt16)
|
|
}
|