package sfz import ( "errors" "os" "path/filepath" "reflect" "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, " sample=a.wav\n lokey=1\n sample=b.wav\n 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 := " volume=-3\n sample=a.wav // first\n zampler_zone=1\n sample=b.wav\n 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) }, " volume=-3\n sample=b.wav\n sample=c.wav zampler_zone=3\n", "b.wav c.wav", }, "remove the last": { func(i *Instrument) error { return i.RemoveRegion(2) }, " volume=-3\n sample=a.wav // first\n zampler_zone=1\n sample=b.wav\n", "a.wav b.wav", }, "move up": { func(i *Instrument) error { return i.MoveRegion(2, 0) }, " volume=-3\n sample=c.wav zampler_zone=3\n sample=a.wav // first\n zampler_zone=1\n sample=b.wav\n", "c.wav a.wav b.wav", }, "move down": { func(i *Instrument) error { return i.MoveRegion(0, 2) }, " volume=-3\n sample=b.wav\n sample=c.wav zampler_zone=3\n 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 + "\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, " default_path=S/\n hikey=60 volume=-6\n 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 := " hikey=50\n sample=a.wav\n hikey=70\n sample=b.wav\n sample=c.wav\n" files := map[string]string{"r.sfz": " 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 sample=y.wav\n", func(i *Instrument) error { return i.RemoveRegion(1) }, nil}, "remove before include": {" 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, " sample=a.wav\n 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()) } if problems := doc.Validate(); len(problems) == 0 { t.Log("zampler_zone isn't reported") } else { for _, p := range problems { if p.Severity != SeverityInfo { t.Errorf("problem %+v", p) } } } } // 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(), "trigger") { 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) } } } }