From 535856ba77815255fc8d3488e360dba702cbd172 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Wed, 2 Dec 2026 00:46:19 +0000 Subject: [PATCH] Read repeated point spellings in a fixed order and end comments at a carriage return Two spellings of one curve point, as v064 and v64, now give the same result each time. A line comment ends at a lone carriage return, as in sfizz. Header.Set's documentation says an include in the header's range can override what it sets. Co-Authored-By: Claude Sonnet 5.5 --- conformance_test.go | 29 +++++++++++++++++++++++++++++ edit.go | 4 +++- instrument.go | 6 +++++- lex.go | 3 ++- values.go | 7 ++++++- 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/conformance_test.go b/conformance_test.go index 6dcc9da..ce2224e 100644 --- a/conformance_test.go +++ b/conformance_test.go @@ -172,3 +172,32 @@ func TestDefineValueIsOneWord(t *testing.T) { t.Errorf("SetDefine: %v", err) } } + +// TestTwoSpellingsOfOnePoint checks that two spellings of one velocity or +// curve point read the same way every time (choice: the one that sorts last +// wins). +func TestTwoSpellingsOfOnePoint(t *testing.T) { + for i := 0; i < 50; i++ { + inst := parseText(t, " sample=a.wav amp_velcurve_064=0.2 amp_velcurve_64=0.9") + for _, p := range inst.Regions[0].VelocityCurve { + if p.Velocity == 64 && p.Gain != 0.9 { + t.Fatalf("velocity point %v", p) + } + } + _, stops := readCurves([]Opcodes{{"curve_index": "20", "v064": "0.2", "v64": "0.9"}}) + for _, s := range stops[20] { + if s.At == 64 && s.Value != 0.9 { + t.Fatalf("curve point %v", s) + } + } + } +} + +// TestCommentEndsAtCarriageReturn checks that a line comment ends at a lone +// carriage return (sfizz). +func TestCommentEndsAtCarriageReturn(t *testing.T) { + r := parseText(t, " // note\rsample=a.wav").Regions[0] + if r.Sample != "a.wav" { + t.Errorf("sample %q", r.Sample) + } +} diff --git a/edit.go b/edit.go index f1db625..1964706 100644 --- a/edit.go +++ b/edit.go @@ -115,7 +115,9 @@ func onlySpace(nodes []*Node) bool { // opcode that the header has keeps its place and the text around it, and takes // the value as written, even if the value was a variable. A new opcode goes // after the header's last, and after any comment on that opcode's line, laid -// out as that opcode is. +// out as that opcode is. Set doesn't read the files that #include reads in the +// header's range, so an opcode that one of them sets after this one takes +// effect instead. func (h *Header) Set(name, value string) error { value = strings.TrimSpace(value) if err := checkOpcode(name, value); err != nil { diff --git a/instrument.go b/instrument.go index 95d7061..cbc36fd 100644 --- a/instrument.go +++ b/instrument.go @@ -242,7 +242,11 @@ var velocityCurveRE = regexp.MustCompile(`^amp_velcurve_(\d{1,3})$`) // and ends at the full level for 127. It returns nil if the region sets none. func velocityCurve(ops Opcodes) []CurvePoint { points := map[int]float64{} - for name, v := range ops { + // The opcodes are read in the order of their names, so that two spellings of + // one velocity, as amp_velcurve_064 and amp_velcurve_64, give the same result + // each time. + for _, name := range slices.Sorted(maps.Keys(ops)) { + v := ops[name] m := velocityCurveRE.FindStringSubmatch(name) if m == nil { continue diff --git a/lex.go b/lex.go index c3ecfa7..1011eea 100644 --- a/lex.go +++ b/lex.go @@ -103,7 +103,8 @@ func lex(src string) []*Node { l.add(src, j, &Node{Kind: KindSpace}) case strings.HasPrefix(rest, "//"): j := len(src) - if e := strings.IndexByte(rest, '\n'); e >= 0 { + // A comment ends at either character of a line break, as in sfizz. + if e := strings.IndexAny(rest, "\r\n"); e >= 0 { j = i + e } l.add(src, j, &Node{Kind: KindComment}) diff --git a/values.go b/values.go index 1e456f7..0b03c5e 100644 --- a/values.go +++ b/values.go @@ -1,8 +1,10 @@ package sfz import ( + "maps" "math" "regexp" + "slices" "sort" "strconv" "strings" @@ -85,7 +87,10 @@ func readCurves(defs []Opcodes) (map[int]curve, map[int][]CurveStop) { continue } points := map[int]float64{0: 0, 127: 1} - for name, v := range def { + // The points are read in the order of their names, so that two spellings of + // one value, as v064 and v64, give the same result each time. + for _, name := range slices.Sorted(maps.Keys(def)) { + v := def[name] m := pointRE.FindStringSubmatch(name) if m == nil { continue