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 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 00:46:19 +00:00
co-authored by Claude Sonnet 5.5
parent 37d2866fe9
commit 535856ba77
5 changed files with 45 additions and 4 deletions
+29
View File
@@ -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, "<region> 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, "<region> // note\rsample=a.wav").Regions[0]
if r.Sample != "a.wav" {
t.Errorf("sample %q", r.Sample)
}
}
+3 -1
View File
@@ -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 {
+5 -1
View File
@@ -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
+2 -1
View File
@@ -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})
+6 -1
View File
@@ -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