mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-28 03:10:24 -04:00
Sointu.asm / lib stuff lives at the root folder. There is a folder called "go4k", which is where all go stuff lives. Following the ideas from https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1 the go4k folder is the "domain-model" of the go side, and should have no dependencies. It contains Unit, Instrument, Synth interface etc. Putting go4k under a sub-folder is actually in the spirit of Ben, as go4k adds dependency to the go language. Bridge ties the domain-model to the sointulib through cgo. It returns C.Synth, but makes sure the C.Synth implements the Synth interface, so others are able to use the Synth no matter how it actually is done. MockSynth and WebProxy synth are good prospects for other implementations of Synth. It is a bit fuzzy where methods like "Play" that have no dependencies other than domain model structs should go. They probably should live in the go4k package as well. The file-organization on the Go-side is not at all finalized. But how packages are broken into files is mostly a documentation issue; it does not affect the users of the packages at all. BTW: The name go4k was chosen because Ben advocated naming the subpackages according to the dependency they introduce AND because the prototype of 4klang was called go4k (there are still some defines in the 4klang source revealing this). go4k thus honors our roots but is also not so bad name: it's the main package of a 4k synth tracker, written in go.
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package bridge_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"io/ioutil"
|
|
"path"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/vsariola/sointu/go4k"
|
|
"github.com/vsariola/sointu/go4k/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) {
|
|
patch := []go4k.Instrument{
|
|
go4k.Instrument{1, []go4k.Unit{
|
|
go4k.Unit{"envelope", false, map[string]int{"attack": 64, "decay": 64, "sustain": 64, "release": 80, "gain": 128}},
|
|
go4k.Unit{"envelope", false, map[string]int{"attack": 95, "decay": 64, "sustain": 64, "release": 80, "gain": 128}},
|
|
go4k.Unit{"out", true, map[string]int{"gain": 128}},
|
|
}}}
|
|
synth, err := bridge.Synth(patch)
|
|
if err != nil {
|
|
t.Fatalf("bridge compile error: %v", err)
|
|
}
|
|
synth.Trigger(0, 64)
|
|
buffer := make([]float32, 2*su_max_samples)
|
|
err = go4k.Render(synth, buffer[:len(buffer)/2])
|
|
if err != nil {
|
|
t.Fatalf("first render gave an error")
|
|
}
|
|
synth.Release(0)
|
|
err = go4k.Render(synth, buffer[len(buffer)/2:])
|
|
if err != nil {
|
|
t.Fatalf("first render gave an error")
|
|
}
|
|
_, 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])
|
|
}
|
|
}
|
|
}
|