AddRegion, RemoveRegion and MoveRegion now refuse an edit whose written text would read differently from the nodes, as when a comment that never ends takes in the text after it, and an instrument whose regions were changed other than by Apply. Build refuses an unknown trigger and an empty random range. Add tests for edits across files, defines, skipped regions and edge cases, a random-sequence test and FuzzRegionEdits. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
627 lines
23 KiB
Go
627 lines
23 KiB
Go
package sfz
|
|
|
|
import (
|
|
"errors"
|
|
"math/rand"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// regionSamples returns the samples of an instrument's regions, in order.
|
|
func regionSamples(inst *Instrument) string {
|
|
var out []string
|
|
for _, r := range inst.Regions {
|
|
out = append(out, r.Sample)
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|
|
|
|
// TestSource checks that a typed region finds its own header when other regions
|
|
// are skipped, which makes the typed and flat indexes differ.
|
|
func TestSource(t *testing.T) {
|
|
doc := mustParse(t, "<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> sample=c.wav hikey=3 hikey=2 lokey=9\n", nil)
|
|
inst := readInstrument(t, doc)
|
|
if regionSamples(inst) != "a.wav b.wav" || inst.Skipped[SkippedNoSample] != 1 || inst.Skipped[SkippedNotKeys] != 1 {
|
|
t.Fatalf("regions %q, skipped %v", regionSamples(inst), inst.Skipped)
|
|
}
|
|
src, ok := inst.Source(1)
|
|
if !ok || src.Own["sample"] != "b.wav" || src.Line != 3 {
|
|
t.Fatalf("source %+v %v", src, ok)
|
|
}
|
|
if err := src.Header.Set("zampler_zone", "abc123"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := doc.Flatten().Regions[2].Own["zampler_zone"]; got != "abc123" {
|
|
t.Errorf("zampler_zone is %q on the flat region", got)
|
|
}
|
|
for _, i := range []int{-1, 2} {
|
|
if _, ok := inst.Source(i); ok {
|
|
t.Errorf("Source(%d) found a region", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRegionEdits checks the text and the instrument after each structural
|
|
// edit, and that comments and opcodes the model doesn't read stay.
|
|
func TestRegionEdits(t *testing.T) {
|
|
text := "<group> volume=-3\n<region> sample=a.wav // first\n zampler_zone=1\n<region> sample=b.wav\n<region> sample=c.wav zampler_zone=3\n"
|
|
tests := map[string]struct {
|
|
edit func(*Instrument) error
|
|
want string
|
|
next string
|
|
}{
|
|
"remove the first": {
|
|
func(i *Instrument) error { return i.RemoveRegion(0) },
|
|
"<group> volume=-3\n<region> sample=b.wav\n<region> sample=c.wav zampler_zone=3\n", "b.wav c.wav",
|
|
},
|
|
"remove the last": {
|
|
func(i *Instrument) error { return i.RemoveRegion(2) },
|
|
"<group> volume=-3\n<region> sample=a.wav // first\n zampler_zone=1\n<region> sample=b.wav\n", "a.wav b.wav",
|
|
},
|
|
"move up": {
|
|
func(i *Instrument) error { return i.MoveRegion(2, 0) },
|
|
"<group> volume=-3\n<region> sample=c.wav zampler_zone=3\n<region> sample=a.wav // first\n zampler_zone=1\n<region> sample=b.wav\n", "c.wav a.wav b.wav",
|
|
},
|
|
"move down": {
|
|
func(i *Instrument) error { return i.MoveRegion(0, 2) },
|
|
"<group> volume=-3\n<region> sample=b.wav\n<region> sample=c.wav zampler_zone=3\n<region> sample=a.wav // first\n zampler_zone=1\n", "b.wav c.wav a.wav",
|
|
},
|
|
"add": {
|
|
func(i *Instrument) error {
|
|
r := i.Regions[0]
|
|
r.Sample, r.Volume = "d.wav", -3
|
|
return i.AddRegion(r)
|
|
},
|
|
text + "<region>\nsample=d.wav\nvolume=-3\n", "a.wav b.wav c.wav d.wav",
|
|
},
|
|
}
|
|
for name, tc := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
doc := mustParse(t, text, nil)
|
|
inst := readInstrument(t, doc)
|
|
if err := tc.edit(inst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := doc.String(); got != tc.want {
|
|
t.Errorf("document\n%q\nwant\n%q", got, tc.want)
|
|
}
|
|
if regionSamples(inst) != tc.next {
|
|
t.Errorf("instrument has %q", regionSamples(inst))
|
|
}
|
|
fresh := readInstrument(t, mustParse(t, doc.String(), nil))
|
|
if len(fresh.Regions) != len(inst.Regions) {
|
|
t.Fatalf("the document reads as %d regions, not %d", len(fresh.Regions), len(inst.Regions))
|
|
}
|
|
for i := range fresh.Regions {
|
|
if wrong := differing(fresh.Regions[i], inst.Regions[i]); len(wrong) > 0 {
|
|
t.Errorf("region %d differs from a fresh read in %v", i, wrong)
|
|
}
|
|
}
|
|
// The instrument's sources still lead to the headers.
|
|
for i := range inst.Regions {
|
|
src, ok := inst.Source(i)
|
|
if !ok || src.Header == nil || src.Own["sample"] != inst.Regions[i].Sample {
|
|
t.Errorf("source %d is %+v", i, src)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAddRegionOverridesInherited checks that a new region overrides what the
|
|
// header above it sets, and that a default_path is taken off its sample.
|
|
func TestAddRegionOverridesInherited(t *testing.T) {
|
|
doc := mustParse(t, "<control> default_path=S/\n<group> hikey=60 volume=-6\n<region> sample=a.wav\n", nil)
|
|
inst := readInstrument(t, doc)
|
|
r := NewRegion("S/b.wav")
|
|
if err := inst.AddRegion(r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := inst.Regions[1]; got.HighKey != 127 || got.Volume != 0 || got.Sample != "S/b.wav" {
|
|
t.Errorf("region %+v", got)
|
|
}
|
|
if !strings.Contains(doc.String(), "sample=b.wav") || strings.Contains(doc.String(), "S/b.wav") {
|
|
t.Errorf("document %q", doc.String())
|
|
}
|
|
}
|
|
|
|
// TestRegionEditRefusals checks that an edit that can't be made says why and
|
|
// leaves the document as it was.
|
|
func TestRegionEditRefusals(t *testing.T) {
|
|
text := "<group> hikey=50\n<region> sample=a.wav\n<group> hikey=70\n<region> sample=b.wav\n<region> sample=c.wav\n"
|
|
files := map[string]string{"r.sfz": "<region> sample=x.wav\n"}
|
|
tests := map[string]struct {
|
|
text string
|
|
edit func(*Instrument) error
|
|
want error
|
|
}{
|
|
"remove out of range": {text, func(i *Instrument) error { return i.RemoveRegion(3) }, ErrNoSource},
|
|
"move out of range": {text, func(i *Instrument) error { return i.MoveRegion(0, -1) }, ErrNoSource},
|
|
"move across a group": {text, func(i *Instrument) error { return i.MoveRegion(0, 2) }, ErrUnpatchable},
|
|
"add under a group": {text, func(i *Instrument) error {
|
|
r := NewRegion("d.wav")
|
|
r.HighKey = 127
|
|
r.Pan = 3
|
|
r.Width = nil
|
|
return i.AddRegion(r)
|
|
}, nil},
|
|
"add a zero region": {text, func(i *Instrument) error { return i.AddRegion(Region{Sample: "d.wav"}) }, ErrInvalid},
|
|
"add a bad sample": {text, func(i *Instrument) error { return i.AddRegion(NewRegion("a b=c.wav")) }, ErrInvalid},
|
|
"remove an include": {"#include \"r.sfz\"\n<region> sample=y.wav\n", func(i *Instrument) error { return i.RemoveRegion(1) }, nil},
|
|
"remove before include": {"<region> sample=y.wav\n#include \"r.sfz\"\n", func(i *Instrument) error { return i.RemoveRegion(0) }, ErrNoSource},
|
|
}
|
|
for name, tc := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
doc := mustParse(t, tc.text, files)
|
|
inst := readInstrument(t, doc)
|
|
before := doc.String()
|
|
err := tc.edit(inst)
|
|
if tc.want == nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return
|
|
}
|
|
if !errors.Is(err, tc.want) {
|
|
t.Fatalf("error %v, want %v", err, tc.want)
|
|
}
|
|
if doc.String() != before {
|
|
t.Errorf("document changed to %q", doc.String())
|
|
}
|
|
if len(inst.Regions) != len(readInstrument(t, doc).Regions) {
|
|
t.Errorf("instrument changed")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestRegionEditsKeepUnknownOpcodes checks that opcodes the format doesn't
|
|
// define, set through a region's header, survive every kind of edit and a
|
|
// write.
|
|
func TestRegionEditsKeepUnknownOpcodes(t *testing.T) {
|
|
doc := mustParse(t, "<region> sample=a.wav\n<region> sample=b.wav\n", nil)
|
|
inst := readInstrument(t, doc)
|
|
for i, id := range []string{"za", "zb"} {
|
|
src, _ := inst.Source(i)
|
|
if err := src.Header.Set("zampler_zone", id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := inst.AddRegion(NewRegion("c.wav")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := inst.MoveRegion(2, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := inst.RemoveRegion(1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []string
|
|
for i := range inst.Regions {
|
|
src, _ := inst.Source(i)
|
|
got = append(got, inst.Regions[i].Sample+":"+src.Own["zampler_zone"])
|
|
}
|
|
if strings.Join(got, " ") != "c.wav: b.wav:zb" {
|
|
t.Errorf("regions %v\n%s", got, doc.String())
|
|
}
|
|
out := readInstrument(t, mustParse(t, doc.String(), nil))
|
|
if src, _ := out.Source(1); src.Own["zampler_zone"] != "zb" {
|
|
t.Errorf("zampler_zone didn't survive writing: %q", doc.String())
|
|
}
|
|
var reported bool
|
|
for _, p := range doc.Validate() {
|
|
if p.Severity != SeverityInfo {
|
|
t.Errorf("problem %+v", p)
|
|
}
|
|
reported = reported || p.Code == "opcode.unknown"
|
|
}
|
|
if !reported {
|
|
t.Error("zampler_zone isn't reported as an unknown opcode")
|
|
}
|
|
}
|
|
|
|
// TestBuildRefusesZeroRegion checks that Build says what is wrong with a zero
|
|
// region, and that a region from NewRegion builds and reads back.
|
|
func TestBuildRefusesZeroRegion(t *testing.T) {
|
|
_, err := Build(&Instrument{Regions: []Region{{Sample: "a.wav"}}})
|
|
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), "region 0: the trigger is empty") {
|
|
t.Errorf("error %v", err)
|
|
}
|
|
inst := &Instrument{Regions: []Region{NewRegion("a.wav")}}
|
|
doc, err := Build(inst)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := readInstrument(t, doc); len(got.Regions) != 1 || len(differing(got.Regions[0], inst.Regions[0])) > 0 {
|
|
t.Errorf("read back %+v", got.Regions)
|
|
}
|
|
}
|
|
|
|
// TestRegionEditsOnFixtures checks, on a real instrument, that removing and
|
|
// moving regions leaves the instrument as a fresh read of the
|
|
// document, and that a move to another group is refused.
|
|
func TestRegionEditsOnFixtures(t *testing.T) {
|
|
doc, err := ParseFile(os.DirFS(filepath.Join("testdata", "splendid")), "Splendid Grand Piano.sfz")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inst := doc.Flatten().Instrument()
|
|
n := len(inst.Regions)
|
|
before := doc.String()
|
|
if err := inst.MoveRegion(n-1, 0); !errors.Is(err, ErrUnpatchable) || doc.String() != before {
|
|
t.Fatalf("a move to another group returned %v", err)
|
|
}
|
|
// Two neighbours that inherit the same opcodes can swap.
|
|
pair := -1
|
|
for i := 0; i+1 < n && pair < 0; i++ {
|
|
if reflect.DeepEqual(inst.sources[i].Inherited, inst.sources[i+1].Inherited) {
|
|
pair = i
|
|
}
|
|
}
|
|
if pair < 0 {
|
|
t.Fatal("no neighbours share a group")
|
|
}
|
|
first, second := inst.Regions[pair].Sample, inst.Regions[pair+1].Sample
|
|
steps := []struct {
|
|
name string
|
|
do func() error
|
|
n int
|
|
at int
|
|
want string
|
|
}{
|
|
{"swap neighbours", func() error { return inst.MoveRegion(pair+1, pair) }, n, pair, second},
|
|
{"remove one", func() error { return inst.RemoveRegion(pair) }, n - 1, pair, first},
|
|
}
|
|
for _, s := range steps {
|
|
if err := s.do(); err != nil {
|
|
t.Fatalf("%s: %v", s.name, err)
|
|
}
|
|
if len(inst.Regions) != s.n || inst.Regions[s.at].Sample != s.want {
|
|
t.Fatalf("%s: %d regions, region %d is %q", s.name, len(inst.Regions), s.at, inst.Regions[s.at].Sample)
|
|
}
|
|
fresh := doc.Flatten().Instrument()
|
|
for i := range fresh.Regions {
|
|
if wrong := differing(fresh.Regions[i], inst.Regions[i]); len(wrong) > 0 {
|
|
t.Fatalf("%s: region %d differs from a fresh read in %v", s.name, i, wrong)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestCheckRegion checks each way that a region is refused.
|
|
func TestCheckRegion(t *testing.T) {
|
|
tests := map[string]struct {
|
|
change func(*Region)
|
|
want string
|
|
}{
|
|
"empty sample": {func(r *Region) { r.Sample = "" }, "sample is empty"},
|
|
"empty trigger": {func(r *Region) { r.Trigger = "" }, "trigger is empty"},
|
|
"unknown trigger": {func(r *Region) { r.Trigger = "bogus" }, `trigger "bogus"`},
|
|
"unknown off mode": {func(r *Region) { r.OffMode = "" }, "off mode"},
|
|
"sequence length": {func(r *Region) { r.SequenceLength = 0 }, "sequence"},
|
|
"sequence position": {func(r *Region) { r.SequencePosition = 0 }, "sequence"},
|
|
"never plays": {func(r *Region) { r.HighRandom = 0 }, "random range"},
|
|
"empty random range": {func(r *Region) { r.LowRandom, r.HighRandom = 0.5, 0.5 }, "random range"},
|
|
"keys": {func(r *Region) { r.LowKey, r.HighKey = 10, 9 }, "highest key"},
|
|
"velocities": {func(r *Region) { r.LowVelocity, r.HighVelocity = 10, 9 }, "highest velocity"},
|
|
}
|
|
for name, tc := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := NewRegion("a.wav")
|
|
tc.change(&r)
|
|
_, err := Build(&Instrument{Regions: []Region{r}})
|
|
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), tc.want) {
|
|
t.Errorf("Build returned %v", err)
|
|
}
|
|
})
|
|
}
|
|
for _, trigger := range []string{"attack", "release", "first", "legato", "release_key"} {
|
|
r := NewRegion("a.wav")
|
|
r.Trigger = trigger
|
|
if _, err := Build(&Instrument{Regions: []Region{r}}); err != nil {
|
|
t.Errorf("trigger %s: %v", trigger, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRegionEditEdgeCases checks edits at the edges of the text: layout that
|
|
// the text of a region's span can't be moved through, and documents that don't
|
|
// end in a line break.
|
|
func TestRegionEditEdgeCases(t *testing.T) {
|
|
tests := map[string]struct {
|
|
text string
|
|
edit func(*Instrument) error
|
|
want string // the document afterwards, or "" if the edit is refused
|
|
}{
|
|
"move a region that ends in a comment that never ends": {
|
|
"<region> sample=a.wav\n<region> sample=b.wav\n/* open",
|
|
func(i *Instrument) error { return i.MoveRegion(1, 0) }, "",
|
|
},
|
|
"add after a comment that never ends": {
|
|
"<region> sample=a.wav /* open",
|
|
func(i *Instrument) error { return i.AddRegion(NewRegion("b.wav")) }, "",
|
|
},
|
|
"move the last of a file with no final line break": {
|
|
"<region> sample=a.wav\n<region> sample=b.wav",
|
|
func(i *Instrument) error { return i.MoveRegion(1, 0) }, "<region> sample=b.wav\n<region> sample=a.wav\n",
|
|
},
|
|
"remove the only region": {
|
|
"<region> sample=a.wav\n",
|
|
func(i *Instrument) error { return i.RemoveRegion(0) }, "",
|
|
},
|
|
"comments above a header stay with the header before": {
|
|
"// first\n<region> sample=a.wav\n// second\n<region> sample=b.wav\n",
|
|
func(i *Instrument) error { return i.RemoveRegion(0) }, "// first\n<region> sample=b.wav\n",
|
|
},
|
|
"add to an empty document": {
|
|
"",
|
|
func(i *Instrument) error { return i.AddRegion(NewRegion("a.wav")) }, "<region>\nsample=a.wav\n",
|
|
},
|
|
}
|
|
for name, tc := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
doc := mustParse(t, tc.text, nil)
|
|
inst := readInstrument(t, doc)
|
|
n := len(inst.Regions)
|
|
err := tc.edit(inst)
|
|
switch {
|
|
case tc.want == "" && name == "remove the only region":
|
|
if err != nil || doc.String() != "" || len(inst.Regions) != 0 {
|
|
t.Errorf("err %v, document %q, %d regions", err, doc.String(), len(inst.Regions))
|
|
}
|
|
case tc.want == "":
|
|
if !(errors.Is(err, ErrUnpatchable) || errors.Is(err, ErrInvalid)) || doc.String() != tc.text || len(inst.Regions) != n {
|
|
t.Errorf("err %v, document %q, %d regions", err, doc.String(), len(inst.Regions))
|
|
}
|
|
default:
|
|
if err != nil || doc.String() != tc.want {
|
|
t.Errorf("err %v, document %q, want %q", err, doc.String(), tc.want)
|
|
}
|
|
}
|
|
if got := readInstrument(t, mustParse(t, doc.String(), nil)); len(got.Regions) != len(inst.Regions) {
|
|
t.Errorf("the written text reads as %d regions, the instrument has %d", len(got.Regions), len(inst.Regions))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestRegionEditsNeedAnUnchangedInstrument checks that an edit refuses an
|
|
// instrument that the document or the caller has changed since it was read.
|
|
func TestRegionEditsNeedAnUnchangedInstrument(t *testing.T) {
|
|
doc := mustParse(t, "<region> sample=a.wav\n", nil)
|
|
inst := readInstrument(t, doc)
|
|
inst.Regions[0].Volume = -9
|
|
if err := inst.AddRegion(NewRegion("b.wav")); !errors.Is(err, ErrNoSource) || doc.String() != "<region> sample=a.wav\n" {
|
|
t.Errorf("a changed region: %v, %q", err, doc.String())
|
|
}
|
|
// Apply is how a region is changed.
|
|
inst = readInstrument(t, doc)
|
|
r := inst.Regions[0]
|
|
r.Volume = -9
|
|
if err := inst.Apply(0, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := inst.AddRegion(NewRegion("b.wav")); err != nil {
|
|
t.Errorf("after Apply: %v", err)
|
|
}
|
|
h, _ := doc.AddHeader("region", nil)
|
|
h.Set("sample", "q.wav")
|
|
if err := inst.RemoveRegion(0); !errors.Is(err, ErrNoSource) {
|
|
t.Errorf("a changed document: %v", err)
|
|
}
|
|
if err := (&Instrument{Regions: []Region{NewRegion("a.wav")}}).AddRegion(NewRegion("b.wav")); !errors.Is(err, ErrNoSource) {
|
|
t.Errorf("an instrument not read from a document: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestRegionEditsWithSkippedRegions checks that typed indexes count only the
|
|
// regions the model has, that skipped regions stay where they are, and that
|
|
// Apply and Source follow the edits.
|
|
func TestRegionEditsWithSkippedRegions(t *testing.T) {
|
|
text := "<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> hikey=2\n<region> sample=c.wav\n"
|
|
doc := mustParse(t, text, nil)
|
|
inst := readInstrument(t, doc)
|
|
if err := inst.MoveRegion(2, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := "<region> sample=c.wav\n<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> hikey=2\n"; doc.String() != want {
|
|
t.Errorf("document %q", doc.String())
|
|
}
|
|
if regionSamples(inst) != "c.wav a.wav b.wav" || inst.Skipped[SkippedNoSample] != 2 {
|
|
t.Errorf("regions %q, skipped %v", regionSamples(inst), inst.Skipped)
|
|
}
|
|
r := inst.Regions[2]
|
|
r.Pan = 10
|
|
if err := inst.Apply(2, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(doc.String(), "sample=b.wav pan=10") {
|
|
t.Errorf("Apply changed the wrong header: %q", doc.String())
|
|
}
|
|
if err := inst.RemoveRegion(1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if regionSamples(inst) != "c.wav b.wav" || strings.Count(doc.String(), "<region>") != 4 {
|
|
t.Errorf("regions %q, document %q", regionSamples(inst), doc.String())
|
|
}
|
|
}
|
|
|
|
// TestRegionEditsAcrossFiles checks edits of documents that have includes.
|
|
func TestRegionEditsAcrossFiles(t *testing.T) {
|
|
files := map[string]string{"one.sfz": "<region> sample=a.wav\n<region> sample=b.wav\n", "two.sfz": "<region> sample=c.wav\n"}
|
|
doc := mustParse(t, "#include \"one.sfz\"\n#include \"two.sfz\"\n", files)
|
|
inst := readInstrument(t, doc)
|
|
if err := inst.MoveRegion(2, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if regionSamples(inst) != "c.wav a.wav b.wav" {
|
|
t.Errorf("regions %q", regionSamples(inst))
|
|
}
|
|
var one, two string
|
|
for _, f := range doc.Files() {
|
|
if f.Path == "one.sfz" {
|
|
one = f.String()
|
|
} else if f.Path == "two.sfz" {
|
|
two = f.String()
|
|
}
|
|
}
|
|
if one != "<region> sample=c.wav\n<region> sample=a.wav\n<region> sample=b.wav\n" || strings.Contains(two, "region") {
|
|
t.Errorf("one.sfz %q, two.sfz %q", one, two)
|
|
}
|
|
if err := inst.AddRegion(NewRegion("d.wav")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := doc.Root.String(); !strings.HasSuffix(got, "<region>\nsample=d.wav\n") || strings.Count(got, "#include") != 2 {
|
|
t.Errorf("main file %q", got)
|
|
}
|
|
if regionSamples(inst) != "c.wav a.wav b.wav d.wav" {
|
|
t.Errorf("regions %q", regionSamples(inst))
|
|
}
|
|
}
|
|
|
|
// TestRegionEditsWithDefines checks that moving a region over a #define that
|
|
// changes it is refused, and that one that doesn't is allowed.
|
|
func TestRegionEditsWithDefines(t *testing.T) {
|
|
text := "#define $v 1\n<region> sample=a.wav volume=$v\n#define $v 2\n<region> sample=b.wav volume=$v\n"
|
|
doc := mustParse(t, text, nil)
|
|
inst := readInstrument(t, doc)
|
|
if err := inst.MoveRegion(1, 0); !errors.Is(err, ErrUnpatchable) || doc.String() != text {
|
|
t.Errorf("a move past a #define: %v, %q", err, doc.String())
|
|
}
|
|
}
|
|
|
|
// TestAddRegionFields checks that a region with settings that are read from
|
|
// several opcodes is added as it is.
|
|
func TestAddRegionFields(t *testing.T) {
|
|
doc := mustParse(t, "<region> sample=a.wav\n", nil)
|
|
inst := readInstrument(t, doc)
|
|
r := NewRegion("b.wav")
|
|
width := 50.0
|
|
r.RootFromSample, r.Width, r.Tune, r.Pan = true, &width, 12, -30
|
|
r.LowKey, r.HighKey, r.LowVelocity, r.HighVelocity = 36, 72, 20, 100
|
|
r.VelocityCurve = []CurvePoint{{Velocity: 0, Gain: 0}, {Velocity: 30, Gain: 0.5}, {Velocity: 100, Gain: 0.9}, {Velocity: 127, Gain: 1}}
|
|
r.Envelope.Attack, r.Envelope.Release = 0.1, 1.5
|
|
r.Trigger, r.SequenceLength, r.SequencePosition = "release", 4, 2
|
|
r.LowRandom, r.HighRandom = 0.25, 0.5
|
|
if err := inst.AddRegion(r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if wrong := differing(inst.Regions[1], r); len(wrong) > 0 {
|
|
t.Errorf("the region differs in %v", wrong)
|
|
}
|
|
}
|
|
|
|
// editOps applies the edits that ops asks for to the document of text, and
|
|
// fails the test if an edit that is accepted writes text that reads as
|
|
// something else than the instrument, or one that is refused changes anything.
|
|
// Each byte of ops is an edit: its low bits choose which, and the rest which
|
|
// regions.
|
|
func editOps(t *testing.T, text string, inc map[string]string, ops []byte) {
|
|
t.Helper()
|
|
doc, err := Parse(strings.NewReader(text), WithResolver(files(inc)))
|
|
if err != nil {
|
|
t.Skip()
|
|
}
|
|
inst := doc.Flatten().Instrument()
|
|
for _, op := range ops {
|
|
n := len(inst.Regions)
|
|
before := doc.String()
|
|
var err error
|
|
var name string
|
|
switch op % 3 {
|
|
case 0:
|
|
name = "AddRegion"
|
|
err = inst.AddRegion(NewRegion("n" + strconv.Itoa(int(op)) + ".wav"))
|
|
case 1:
|
|
if n == 0 {
|
|
continue
|
|
}
|
|
name = "RemoveRegion"
|
|
err = inst.RemoveRegion(int(op/3) % n)
|
|
case 2:
|
|
if n == 0 {
|
|
continue
|
|
}
|
|
name = "MoveRegion"
|
|
err = inst.MoveRegion(int(op/3)%n, int(op/3/7)%n)
|
|
}
|
|
if err != nil {
|
|
if doc.String() != before || len(inst.Regions) != n {
|
|
t.Fatalf("%s was refused with %v and changed the document to %q", name, err, doc.String())
|
|
}
|
|
continue
|
|
}
|
|
fresh := doc.Flatten().Instrument()
|
|
reparsed, perr := Parse(strings.NewReader(doc.String()), WithResolver(files(inc)))
|
|
if perr != nil {
|
|
t.Fatalf("%s wrote text that doesn't parse: %v", name, perr)
|
|
}
|
|
// The text of each file has to read back as its nodes. A file with
|
|
// includes isn't written by String, so each file is checked on its own.
|
|
for _, f := range doc.Files() {
|
|
if got, want := significant(lex(f.String())), significant(f.Nodes); !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("%s wrote %q, which reads as %q, not %q", name, f.String(), got, want)
|
|
}
|
|
}
|
|
if len(fresh.Regions) != len(inst.Regions) {
|
|
t.Fatalf("%s: the instrument has %d regions, the document %d", name, len(inst.Regions), len(fresh.Regions))
|
|
}
|
|
for i := range fresh.Regions {
|
|
if wrong := differing(fresh.Regions[i], inst.Regions[i]); len(wrong) > 0 {
|
|
t.Fatalf("%s: region %d differs from a fresh read in %v", name, i, wrong)
|
|
}
|
|
}
|
|
if len(reparsed.Flatten().Instrument().Regions) != len(inst.Regions) && len(inc) == 0 {
|
|
t.Fatalf("%s: the written text %q reads as other than the instrument", name, doc.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
// regionEditTexts are documents for the tests of edits in sequence.
|
|
var regionEditTexts = []string{
|
|
"<region> sample=a.wav\n<region> sample=b.wav\n<region> sample=c.wav\n",
|
|
"<group> volume=-3\n<region> sample=a.wav // one\n zampler_zone=1\n<region> lokey=3\n<group> hikey=60\n<region> sample=b.wav\n<region> sample=c.wav\n",
|
|
"// top\n<control> default_path=S/\n<region> sample=S/a.wav /* note */ <region> sample=S/b.wav\n",
|
|
"<region> sample=a.wav\n<region> sample=b.wav\n/* open",
|
|
"#define $x 1\n<region> sample=a.wav volume=$x\n#define $x 2\n<region> sample=b.wav volume=$x",
|
|
"<region> sample=a.wav\r\n<region> sample=b.wav\r\n",
|
|
"",
|
|
}
|
|
|
|
// TestRegionEditSequences applies random sequences of edits to documents.
|
|
func TestRegionEditSequences(t *testing.T) {
|
|
rng := rand.New(rand.NewSource(1))
|
|
for _, text := range regionEditTexts {
|
|
for run := 0; run < 60; run++ {
|
|
ops := make([]byte, 1+rng.Intn(12))
|
|
for i := range ops {
|
|
ops[i] = byte(rng.Intn(256))
|
|
}
|
|
editOps(t, text, nil, ops)
|
|
}
|
|
}
|
|
files := map[string]string{"one.sfz": "<region> sample=a.wav\n<group> volume=1\n<region> sample=b.wav\n", "two.sfz": "<region> sample=c.wav\n"}
|
|
for run := 0; run < 60; run++ {
|
|
ops := make([]byte, 1+rng.Intn(12))
|
|
for i := range ops {
|
|
ops[i] = byte(rng.Intn(256))
|
|
}
|
|
editOps(t, "#include \"one.sfz\"\n#include \"two.sfz\"\n", files, ops)
|
|
}
|
|
}
|
|
|
|
// FuzzRegionEdits checks that any sequence of region edits, of any document,
|
|
// either fails and changes nothing, or writes text that reads as the instrument.
|
|
func FuzzRegionEdits(f *testing.F) {
|
|
for _, text := range regionEditTexts {
|
|
f.Add(text, []byte{0, 4, 5, 8, 14, 3, 20})
|
|
}
|
|
f.Fuzz(func(t *testing.T, text string, ops []byte) {
|
|
editOps(t, text, nil, ops)
|
|
})
|
|
}
|