mirror of
https://github.com/vsariola/sointu.git
synced 2026-02-14 20:23:13 -05:00
refactor: use [][2] as audio buffers, instead of []float32
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.
This commit is contained in:
parent
bb0d4d6800
commit
38e9007bf8
@ -12,9 +12,9 @@ import (
|
||||
//
|
||||
// If pcm16 is set to true, the samples in the WAV-file will be 16-bit signed
|
||||
// integers; otherwise the samples will be 32-bit floats
|
||||
func Wav(buffer []float32, pcm16 bool) ([]byte, error) {
|
||||
func Wav(buffer AudioBuffer, pcm16 bool) ([]byte, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
wavHeader(len(buffer), pcm16, buf)
|
||||
wavHeader(len(buffer)*2, pcm16, buf)
|
||||
err := rawToBuffer(buffer, pcm16, buf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Wav failed: %v", err)
|
||||
@ -27,7 +27,7 @@ func Wav(buffer []float32, pcm16 bool) ([]byte, error) {
|
||||
//
|
||||
// If pcm16 is set to true, the samples will be 16-bit signed integers;
|
||||
// otherwise the samples will be 32-bit floats
|
||||
func Raw(buffer []float32, pcm16 bool) ([]byte, error) {
|
||||
func Raw(buffer AudioBuffer, pcm16 bool) ([]byte, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
err := rawToBuffer(buffer, pcm16, buf)
|
||||
if err != nil {
|
||||
@ -36,12 +36,13 @@ func Raw(buffer []float32, pcm16 bool) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func rawToBuffer(data []float32, pcm16 bool, buf *bytes.Buffer) error {
|
||||
func rawToBuffer(data AudioBuffer, pcm16 bool, buf *bytes.Buffer) error {
|
||||
var err error
|
||||
if pcm16 {
|
||||
int16data := make([]int16, len(data))
|
||||
int16data := make([][2]int16, len(data))
|
||||
for i, v := range data {
|
||||
int16data[i] = int16(clamp(int(v*math.MaxInt16), math.MinInt16, math.MaxInt16))
|
||||
int16data[i][0] = int16(clamp(int(v[0]*math.MaxInt16), math.MinInt16, math.MaxInt16))
|
||||
int16data[i][1] = int16(clamp(int(v[1]*math.MaxInt16), math.MinInt16, math.MaxInt16))
|
||||
}
|
||||
err = binary.Write(buf, binary.LittleEndian, int16data)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user