feat(go/audio): implement basic audio output with oto

splitting implementation into a separate package to potentially allow for other sorts of output, too.
This commit is contained in:
Matias Lahti
2020-11-07 19:22:59 +02:00
parent 41ce5d1efe
commit fa772ddd77
5 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package audio
import (
"bytes"
"encoding/binary"
"fmt"
"math"
)
// FloatBufferTo16BitLE is a naive helper method to convert []float32 buffers to
// 16-bit little-endian integer buffers.
// TODO: optimize/refactor this, current is far from the best solution
func FloatBufferTo16BitLE(buff []float32) ([]byte, error) {
var buf bytes.Buffer
for i, v := range buff {
uv := int16(v * math.MaxInt16)
if err := binary.Write(&buf, binary.LittleEndian, uv); err != nil {
return nil, fmt.Errorf("error converting buffer (@ %v, value %v) to bytes: %w", i, v, err)
}
}
return buf.Bytes(), nil
}