sointu/bridge/bridge_test.go
Veikko Sariola 470ba28592 Change the Render function in bridge.go to return a tuple of: number of samples rendered; bool indicating if rowend was reached; and a possible error.
The callbacks are gone; the row looping is the job of the user which is probably better for everyone.
2020-10-26 08:30:43 +02:00

70 lines
2.2 KiB
Go

package bridge_test
import (
"bytes"
"encoding/binary"
"io/ioutil"
"path"
"runtime"
"testing"
"github.com/vsariola/sointu/bridge"
)
const BPM = 100
const SAMPLE_RATE = 44100
const TOTAL_ROWS = 16
const SAMPLES_PER_ROW = SAMPLE_RATE * 4 * 60 / (BPM * 16)
const su_max_samples = SAMPLES_PER_ROW * TOTAL_ROWS
// const bufsize = su_max_samples * 2
func TestBridge(t *testing.T) {
s := bridge.NewSynthState()
s.SetPatch([]bridge.Instrument{
bridge.Instrument{1, []bridge.Unit{
bridge.Unit{bridge.Envelope, []byte{64, 64, 64, 80, 128}},
bridge.Unit{bridge.Envelope, []byte{95, 64, 64, 80, 128}},
bridge.Unit{bridge.Out.Stereo(), []byte{128}},
}},
})
s.Trigger(0, 64)
s.SamplesPerRow = SAMPLES_PER_ROW * 8 // this song is two blocks of 8 rows, release before second block start
buffer := make([]float32, 2*su_max_samples)
n, rowend, err := s.Render(buffer)
if n < su_max_samples/2 {
t.Fatalf("render should have filled half of the buffer on first call, %v samples rendered, %v expected", n, su_max_samples/2)
}
if rowend != true {
t.Fatalf("Row end should have been hit (rowend should have been true) on the first call to Render")
}
s.Release(0)
n, rowend, err = s.Render(buffer[(n * 2):])
if n < su_max_samples/2 {
t.Fatalf("render should have filled second half of the buffer on the second call, %v samples rendered, %v expected", n, su_max_samples/2)
}
if rowend != true {
t.Fatalf("Row end should have been hit (rowend should have been true) on the second call to Render")
}
_, filename, _, _ := runtime.Caller(0)
expectedb, err := ioutil.ReadFile(path.Join(path.Dir(filename), "..", "tests", "expected_output", "test_render_samples.raw"))
if err != nil {
t.Fatalf("cannot read expected: %v", err)
}
var createdbuf bytes.Buffer
err = binary.Write(&createdbuf, binary.LittleEndian, buffer)
if err != nil {
t.Fatalf("error converting buffer: %v", err)
}
createdb := createdbuf.Bytes()
if len(createdb) != len(expectedb) {
t.Fatalf("buffer length mismatch, got %v, expected %v", len(createdb), len(expectedb))
}
for i, v := range createdb {
if expectedb[i] != v {
t.Errorf("byte mismatch @ %v, got %v, expected %v", i, v, expectedb[i])
}
}
}