175 lines
7.3 KiB
Go
175 lines
7.3 KiB
Go
package sfz
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// The expected values of these tests are marked as in sfz_test.go: "spec" is
|
||
|
|
// sfzformat.com, "sfizz" is the sfizz player's source, and "choice" is
|
||
|
|
// Zampler's own.
|
||
|
|
|
||
|
|
// TestOtherNames checks that an opcode set under another name that the format
|
||
|
|
// gives it is read as the opcode (spec: each opcode's page lists its aliases),
|
||
|
|
// and that the opcode's own name wins if both are set.
|
||
|
|
func TestOtherNames(t *testing.T) {
|
||
|
|
r := parseText(t, "<region> sample=a.wav amp_attack=0.5 amp_release=2 offby=3 bendup=100 benddown=-50 tune_keytrack=0 loopmode=one_shot loopstart=10 loopend=20 polyphony_group=4").Regions[0]
|
||
|
|
if r.Envelope.Attack != 0.5 || r.Envelope.Release != 2 || r.OffBy == nil || *r.OffBy != 3 || r.KeyTrack != 0 ||
|
||
|
|
r.BendUp == nil || *r.BendUp != 100 || r.BendDown == nil || *r.BendDown != -50 ||
|
||
|
|
r.LoopMode != "one_shot" || r.LoopStart == nil || *r.LoopStart != 10 || r.LoopEnd == nil || *r.LoopEnd != 20 || r.Group != 4 {
|
||
|
|
t.Errorf("other names %+v", r)
|
||
|
|
}
|
||
|
|
inst := parseText(t, "<region> sample=a.wav amp_release=2 ampeg_release=7")
|
||
|
|
if got := inst.Regions[0].Envelope.Release; got != 7 {
|
||
|
|
t.Errorf("release %v with both names", got)
|
||
|
|
}
|
||
|
|
if len(inst.Unread) != 0 {
|
||
|
|
t.Errorf("unread %v", inst.Unread)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestKeyMinusOne checks that SFZ v2's -1 for lokey, hikey and key, which keeps
|
||
|
|
// a region from playing, isn't a problem (spec).
|
||
|
|
func TestKeyMinusOne(t *testing.T) {
|
||
|
|
doc := mustParse(t, "<region> sample=a.wav lokey=-1 hikey=-1\n<region> sample=b.wav lokey=60 hikey=-1\n<region> sample=c.wav key=-1\n", nil)
|
||
|
|
if p := doc.Validate(); len(p) != 0 {
|
||
|
|
t.Errorf("problems %v", p)
|
||
|
|
}
|
||
|
|
if inst := readInstrument(t, doc); len(inst.Regions) != 0 || inst.Skipped[SkippedNotKeys] != 3 {
|
||
|
|
t.Errorf("regions %v, skipped %v", inst.Regions, inst.Skipped)
|
||
|
|
}
|
||
|
|
if p := mustParse(t, "<region> sample=a.wav lokey=-2\n", nil).Validate(); len(p) != 1 || p[0].Code != "value.range" {
|
||
|
|
t.Errorf("-2: %v", p)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestHugeNumbers checks that a number beyond what a setting can hold is cut to
|
||
|
|
// the nearest it can, and doesn't change sign.
|
||
|
|
func TestHugeNumbers(t *testing.T) {
|
||
|
|
r := parseText(t, "<region> sample=a.wav offset=1e30 loop_start=1e30 loop_end=1e30 group=1e30 off_by=-1e30 seq_length=1e30 polyphony=1e30 note_polyphony=1e30").Regions[0]
|
||
|
|
if r.Offset <= 0 || *r.LoopStart <= 0 || *r.LoopEnd <= 0 || r.Group <= 0 || *r.OffBy >= 0 ||
|
||
|
|
r.SequenceLength <= 0 || *r.Polyphony <= 0 || *r.NotePolyphony <= 0 {
|
||
|
|
t.Errorf("huge numbers %+v", r)
|
||
|
|
}
|
||
|
|
if n, ok := GetAs[int](Opcodes{"group": "1e30"}, "group"); !ok || n <= 0 {
|
||
|
|
t.Errorf("GetAs %d %v", n, ok)
|
||
|
|
}
|
||
|
|
// A controller number or an octave that doesn't fit isn't read as 0.
|
||
|
|
inst := parseText(t, "<region> sample=a.wav locc99999999999999999999=5 lokey=c99999999999999999999")
|
||
|
|
if len(inst.Regions[0].Conditions) != 0 || inst.Regions[0].LowKey != 0 {
|
||
|
|
t.Errorf("huge names %+v", inst.Regions[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSetDefineKeepsByteOrderMark checks that a new #define goes after a byte
|
||
|
|
// order mark, which has to stay at the start of the file.
|
||
|
|
func TestSetDefineKeepsByteOrderMark(t *testing.T) {
|
||
|
|
for _, text := range []string{"\xef\xbb\xbf<region> sample=a.wav\n", "\xef\xbb\xbf\n<region> sample=a.wav\n"} {
|
||
|
|
doc := mustParse(t, text, nil)
|
||
|
|
if err := doc.SetDefine("$X", "1"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got := doc.String()
|
||
|
|
if !strings.HasPrefix(got, "\xef\xbb\xbf") || strings.Count(got, "\xef\xbb\xbf") != 1 {
|
||
|
|
t.Errorf("byte order mark moved: %q", got)
|
||
|
|
}
|
||
|
|
again := mustParse(t, got, nil)
|
||
|
|
if p := again.Validate(); len(p) != 0 {
|
||
|
|
t.Errorf("%q: problems %v", got, p)
|
||
|
|
}
|
||
|
|
if ds := again.Defines(); len(ds) != 1 || ds[0].Name != "$X" {
|
||
|
|
t.Errorf("%q: defines %v", got, ds)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestPolyphonyLegato checks that polyphony's text values, which limit the
|
||
|
|
// region to one voice, read as a limit of 1 (spec).
|
||
|
|
func TestPolyphonyLegato(t *testing.T) {
|
||
|
|
for _, v := range []string{"legato_high", "legato_last", "legato_low"} {
|
||
|
|
if r := parseText(t, "<region> sample=a.wav polyphony="+v).Regions[0]; r.Polyphony == nil || *r.Polyphony != 1 {
|
||
|
|
t.Errorf("%s: %v", v, r.Polyphony)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if r := parseText(t, "<region> sample=a.wav polyphony=legato_sideways").Regions[0]; r.Polyphony != nil {
|
||
|
|
t.Errorf("an unknown word: %v", *r.Polyphony)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestBuiltinCurves checks the curves that SFZ players define (spec, sfizz):
|
||
|
|
// 0 straight, 1 bipolar, 2 inverted, 3 bipolar inverted, 4 x², 5 √x and 6 √(1-x).
|
||
|
|
func TestBuiltinCurves(t *testing.T) {
|
||
|
|
curves, _ := readCurves(nil)
|
||
|
|
for index, want := range map[int][3]float64{
|
||
|
|
0: {0, 0.25, 1}, 1: {-1, -0.5, 1}, 2: {1, 0.75, 0}, 3: {1, 0.5, -1},
|
||
|
|
4: {0, 0.0625, 1}, 5: {0, 0.5, 1}, 6: {1, 0.8660254037844386, 0},
|
||
|
|
} {
|
||
|
|
c := curves[index]
|
||
|
|
for i, x := range []float64{0, 0.25, 1} {
|
||
|
|
if !near(c(x), want[i]) {
|
||
|
|
t.Errorf("curve %d at %v is %v, want %v", index, x, c(x), want[i])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestDefaultPathNeedsItsSlash checks that default_path and the sample are
|
||
|
|
// joined as they are, so a default_path with no slash at its end runs into the
|
||
|
|
// sample's name (sfizz; the spec says either it ends in a slash or the sample
|
||
|
|
// starts with one).
|
||
|
|
func TestDefaultPathNeedsItsSlash(t *testing.T) {
|
||
|
|
if got := parseText(t, "<control> default_path=Samples\n<region> sample=a.wav").Regions[0].Sample; got != "Samplesa.wav" {
|
||
|
|
t.Errorf("sample %q", got)
|
||
|
|
}
|
||
|
|
if got := parseText(t, "<control> default_path=Samples\n<region> sample=/a.wav").Regions[0].Sample; got != "Samples/a.wav" {
|
||
|
|
t.Errorf("sample %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestConditionLimitsAreRead checks that a region that sets a limit as lohdcc,
|
||
|
|
// where it inherits one as locc, reads the lohdcc each time, and that Apply's
|
||
|
|
// change to such a region reads back (choice: the opcodes have no order, so the
|
||
|
|
// hdcc one wins).
|
||
|
|
func TestConditionLimitsAreRead(t *testing.T) {
|
||
|
|
doc := mustParse(t, "<group> locc7=14 hicc7=25\n<region> sample=a.wav\n", nil)
|
||
|
|
inst := readInstrument(t, doc)
|
||
|
|
r := inst.Regions[0]
|
||
|
|
r.Conditions = []Condition{{Source: SourceController, Controller: 7, Low: 0.5, High: 1}}
|
||
|
|
if err := inst.Apply(0, r); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for i := 0; i < 50; i++ {
|
||
|
|
got := readInstrument(t, doc).Regions[0].Conditions
|
||
|
|
if len(got) != 1 || !near(got[0].Low, 0.5) || !near(got[0].High, 1) {
|
||
|
|
t.Fatalf("read %d: %+v", i, got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestDefineValueIsOneWord checks that a #define's value ends at the first
|
||
|
|
// space, so that two can share a line, and that what follows is read as it
|
||
|
|
// would be anywhere else (sfizz, and ARIA, which sfizz follows here).
|
||
|
|
func TestDefineValueIsOneWord(t *testing.T) {
|
||
|
|
doc := mustParse(t, "#define $lo 1 #define $hi 25\n<region> sample=a.wav lovel=$lo hivel=$hi\n", nil)
|
||
|
|
r := readInstrument(t, doc).Regions[0]
|
||
|
|
if r.LowVelocity != 1 || r.HighVelocity != 25 {
|
||
|
|
t.Errorf("velocities %d to %d", r.LowVelocity, r.HighVelocity)
|
||
|
|
}
|
||
|
|
if p := doc.Validate(); len(p) != 0 {
|
||
|
|
t.Errorf("problems %v", p)
|
||
|
|
}
|
||
|
|
if ds := doc.Defines(); len(ds) != 2 || ds[0].Value != "1" || ds[1].Value != "25" {
|
||
|
|
t.Errorf("defines %v", ds)
|
||
|
|
}
|
||
|
|
// A value with a space in it is its first word, and the rest is text.
|
||
|
|
doc = mustParse(t, "#define $path some folder/name\n<region> sample=$path\n", nil)
|
||
|
|
if got := doc.Flatten().Regions[0].Opcodes["sample"]; got != "some" {
|
||
|
|
t.Errorf("sample %q", got)
|
||
|
|
}
|
||
|
|
// So SetDefine can't write one.
|
||
|
|
if err := doc.SetDefine("$path", "some folder"); !errors.Is(err, ErrInvalid) {
|
||
|
|
t.Errorf("SetDefine: %v", err)
|
||
|
|
}
|
||
|
|
}
|