mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-21 06:24:32 -04:00
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:
22
go4k/audio/convertbuffer.go
Normal file
22
go4k/audio/convertbuffer.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user