2026-12-01 17:41:34 +00:00
|
|
|
package sfz
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"testing/fstest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// instruments are typed instruments to build documents from, from the
|
|
|
|
|
// fixtures and from text that sets what the typed model holds.
|
|
|
|
|
func instruments(t *testing.T) map[string]*Instrument {
|
|
|
|
|
t.Helper()
|
|
|
|
|
doc, err := ParseFile(os.DirFS(filepath.Join("testdata", "splendid")), "Splendid Grand Piano.sfz")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return map[string]*Instrument{
|
|
|
|
|
"splendid": doc.Flatten().Instrument(),
|
|
|
|
|
"everything": parseText(t, `
|
|
|
|
|
<control> default_path=S/ label_cc20=Mic set_hdcc20=0.25
|
|
|
|
|
<curve> curve_index=8 v000=0 v064=0.9 v127=1
|
|
|
|
|
<global> sw_lokey=36 sw_hikey=40 sw_default=37
|
|
|
|
|
<region> sample=a.wav lokey=c3 hikey=e3 pitch_keycenter=sample lovel=20 hivel=100 tune=12 volume=-3 pan=-30
|
|
|
|
|
width=50 offset=100 amp_veltrack=60 amp_velcurve_30=0.5 amp_velcurve_100=0.9
|
|
|
|
|
xfin_lokey=40 xfin_hikey=50 xfout_lokey=60 xfout_hikey=70 xf_keycurve=gain
|
|
|
|
|
xfin_lovel=1 xfin_hivel=10
|
|
|
|
|
ampeg_attack=0.1 ampeg_decay=0.2 ampeg_sustain=70 ampeg_release=1.5 ampeg_hold=0.05 ampeg_delay=0.01
|
|
|
|
|
trigger=release rt_decay=3 loop_mode=loop_sustain loop_start=10 loop_end=2000
|
|
|
|
|
seq_length=4 seq_position=2 lorand=0.25 hirand=0.5 group=3 off_by=4 polyphony=2 note_polyphony=1
|
|
|
|
|
off_mode=time off_time=0.5 bend_up=200 bend_down=-200 bend_step=10
|
|
|
|
|
locc20=10 hicc20=100 lochan=2 hichan=5 lobend=-4096 hibend=4096 lochanaft=10 hichanaft=100
|
|
|
|
|
sw_last=37 sw_previous=36 sw_down=38 sw_up=39
|
|
|
|
|
volume_oncc7=-6 volume_curvecc7=8 pan_oncc10=40 tune_oncc1=100 amplitude_oncc11=50 width_oncc2=5
|
|
|
|
|
<region> sample=b.wav key=60
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// TestBuildRoundTrip checks that an instrument built into a document, written
|
|
|
|
|
// to files and read again, is the same instrument, for each way of laying the
|
|
|
|
|
// document out.
|
2026-12-01 17:41:34 +00:00
|
|
|
func TestBuildRoundTrip(t *testing.T) {
|
|
|
|
|
for name, inst := range instruments(t) {
|
|
|
|
|
for label, opts := range map[string]BuildOptions{
|
|
|
|
|
"plain": {},
|
|
|
|
|
"variables": {Variables: true, MinRepeat: 2},
|
|
|
|
|
"split": {SplitFiles: true, RegionsPerFile: 7},
|
|
|
|
|
"both": {SplitFiles: true, Variables: true, RegionsPerFile: 50, Indent: "\t"},
|
|
|
|
|
} {
|
|
|
|
|
doc, err := Build(inst, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
got := rebuilt(t, doc)
|
|
|
|
|
if !reflect.DeepEqual(got.Regions, inst.Regions) {
|
|
|
|
|
for i := range inst.Regions {
|
|
|
|
|
if i < len(got.Regions) && !reflect.DeepEqual(got.Regions[i], inst.Regions[i]) {
|
|
|
|
|
t.Fatalf("%s/%s: region %d\n got %+v\nwant %+v", name, label, i, got.Regions[i], inst.Regions[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
t.Fatalf("%s/%s: %d regions, want %d", name, label, len(got.Regions), len(inst.Regions))
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(got.Controls, inst.Controls) || !reflect.DeepEqual(got.Keyswitches, inst.Keyswitches) {
|
|
|
|
|
t.Errorf("%s/%s: controls %+v keyswitches %+v, want %+v %+v", name, label, got.Controls, got.Keyswitches, inst.Controls, inst.Keyswitches)
|
|
|
|
|
}
|
|
|
|
|
if problems := doc.Validate(); name == "everything" && len(problems) != 0 {
|
|
|
|
|
t.Errorf("%s/%s: problems %+v", name, label, problems)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rebuilt writes a document to files and reads them back.
|
|
|
|
|
func rebuilt(t *testing.T, doc *Document) *Instrument {
|
|
|
|
|
t.Helper()
|
|
|
|
|
files := fstest.MapFS{}
|
|
|
|
|
err := doc.WriteFiles("out.sfz", func(p string, data []byte) error {
|
|
|
|
|
files[p] = &fstest.MapFile{Data: data}
|
|
|
|
|
return nil
|
|
|
|
|
}, WriteOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
back, err := ParseFile(files, "out.sfz")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(back.Files()) != len(doc.Files()) {
|
|
|
|
|
t.Errorf("%d files, want %d", len(back.Files()), len(doc.Files()))
|
|
|
|
|
}
|
|
|
|
|
return back.Flatten().Instrument()
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// TestBuildLayout checks the text that Build writes for the plain, variable
|
|
|
|
|
// and split layouts.
|
2026-12-01 17:41:34 +00:00
|
|
|
func TestBuildLayout(t *testing.T) {
|
|
|
|
|
inst := parseText(t, "<region> sample=Dir/a.wav lokey=40\n<region> sample=Dir/b.wav lokey=40\n<region> sample=Dir/c.wav lokey=41\n")
|
|
|
|
|
doc, err := Build(inst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
want := "<region>\n sample=Dir/a.wav\n lokey=40\n<region>\n sample=Dir/b.wav\n lokey=40\n<region>\n sample=Dir/c.wav\n lokey=41\n"
|
|
|
|
|
if got := doc.String(); got != want {
|
|
|
|
|
t.Errorf("got\n%s\nwant\n%s", got, want)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doc, err = Build(inst, BuildOptions{Variables: true, MinRepeat: 2})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if got := doc.String(); !strings.HasPrefix(got, "#define $LOKEY_AAA 40\n<control>\n default_path=Dir/\n") ||
|
|
|
|
|
strings.Count(got, "lokey=$LOKEY_AAA") != 2 || !strings.Contains(got, "sample=a.wav") {
|
|
|
|
|
t.Errorf("variables:\n%s", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doc, err = Build(inst, BuildOptions{SplitFiles: true, RegionsPerFile: 2, FileNamer: func(i int) string { return "part" + string(rune('a'+i)) + ".sfz" }})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if got := doc.String(); got != "#include \"parta.sfz\"\n#include \"partb.sfz\"\n" || len(doc.Files()) != 3 {
|
|
|
|
|
t.Errorf("split:\n%s", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// TestWriteFilesPaths checks that WriteFiles writes the included files in the
|
|
|
|
|
// folder of the main file.
|
2026-12-01 17:41:34 +00:00
|
|
|
func TestWriteFilesPaths(t *testing.T) {
|
|
|
|
|
doc, err := Build(parseText(t, "<region> sample=a.wav\n"), BuildOptions{SplitFiles: true})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var paths []string
|
|
|
|
|
if err := doc.WriteFiles("Piano/main.sfz", func(p string, _ []byte) error { paths = append(paths, p); return nil }, WriteOptions{}); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(paths, []string{"Piano/main.sfz", "Piano/regions/001.sfz"}) {
|
|
|
|
|
t.Errorf("paths %q", paths)
|
|
|
|
|
}
|
|
|
|
|
}
|