Fix the differences from the spec and sfizz that a conformance review found
Read opcodes under their other names. The registry lists the other names an opcode has, and the typed model now reads each under its own name: amp_release for ampeg_release, offby, bendup, tune_keytrack and the rest. Apply treats an old name as belonging to the same field, so it doesn't leave one behind. Cut a #define's value at the first space, as ARIA and sfizz do, so that two defines can share a line. Before, the first define took the rest of the line, and a kit that writes "#define $lo 1 #define $hi 25" lost its velocity layers. Ignore opcodes before any header when flattening, as sfizz does, and report them as a warning. Included files that hold only opcodes are not affected. Read the limits of a controller the same way each time. A region that sets lohdccN and inherits loccN took whichever the map gave first, so Apply's change could read back as the old limits. The hdcc opcodes now win. Accept -1 for lokey, hikey and key, which SFZ v2 allows, and don't report a region with a hikey of -1 as an inverted range. Cut numbers that don't fit to what a setting can hold, so offset=1e30 doesn't turn negative, and ignore a controller number or octave that overflows. Read polyphony=legato_high, legato_last and legato_low as a limit of 1. Put a new #define after a byte order mark. Mark in the tests where each expected value comes from, correct the tags that the spec doesn't support, and add tests for each of these. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+65
-26
@@ -2,6 +2,7 @@ package sfz
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"maps"
|
||||
"math"
|
||||
"path"
|
||||
"regexp"
|
||||
@@ -25,7 +26,7 @@ func (f *Flat) Instrument() *Instrument {
|
||||
regions := make([]Opcodes, len(f.Regions))
|
||||
for i, fr := range f.Regions {
|
||||
ops := fr.Opcodes
|
||||
regions[i] = ops
|
||||
regions[i] = withOwnNames(ops)
|
||||
r, reason := e.region(ops)
|
||||
if reason != "" {
|
||||
inst.Skipped[reason]++
|
||||
@@ -57,6 +58,7 @@ type evaluator struct {
|
||||
// region returns the region that the opcodes describe, or the reason, one of
|
||||
// the Skipped constants, that there is none.
|
||||
func (e evaluator) region(ops Opcodes) (Region, string) {
|
||||
ops = withOwnNames(ops)
|
||||
sample := strings.TrimSpace(ops["sample"])
|
||||
switch {
|
||||
case sample == "":
|
||||
@@ -177,7 +179,7 @@ func (e evaluator) region(ops Opcodes) (Region, string) {
|
||||
r.Conditions = conditions(ops)
|
||||
r.Keyswitch = keyswitch(ops, shift)
|
||||
r.Modulations = e.modulations(ops)
|
||||
r.Offset = int64(max(0, value("offset", 0)))
|
||||
r.Offset = toInt64(max(0, value("offset", 0)))
|
||||
r.VelocityTracking = clamp(value("amp_veltrack", 100), -100, 100)
|
||||
r.VelocityCurve = velocityCurve(ops)
|
||||
r.KeyCrossfade = crossfade(ops, "key", shift)
|
||||
@@ -193,17 +195,17 @@ func (e evaluator) region(ops Opcodes) (Region, string) {
|
||||
if t := strings.TrimSpace(ops["trigger"]); t != "" {
|
||||
r.Trigger = t
|
||||
}
|
||||
r.LoopMode = strings.TrimSpace(firstOf(ops, "loop_mode", "loopmode"))
|
||||
r.LoopStart = frames(firstOf(ops, "loop_start", "loopstart"))
|
||||
r.LoopEnd = frames(firstOf(ops, "loop_end", "loopend"))
|
||||
r.LoopMode = strings.TrimSpace(ops["loop_mode"])
|
||||
r.LoopStart = frames(ops["loop_start"])
|
||||
r.LoopEnd = frames(ops["loop_end"])
|
||||
r.ReleaseDecay = max(0, value("rt_decay", 0))
|
||||
|
||||
// A seq_length or seq_position below 1 plays as 1, as sfizz plays it.
|
||||
if n, ok := parseNumber(ops["seq_length"]); ok {
|
||||
r.SequenceLength = max(1, int(n))
|
||||
r.SequenceLength = max(1, toInt(n))
|
||||
}
|
||||
if n, ok := parseNumber(ops["seq_position"]); ok {
|
||||
r.SequencePosition = max(1, int(n))
|
||||
r.SequencePosition = max(1, toInt(n))
|
||||
}
|
||||
if n, ok := parseNumber(ops["lorand"]); ok {
|
||||
r.LowRandom = clamp(n, 0, 1)
|
||||
@@ -211,15 +213,14 @@ func (e evaluator) region(ops Opcodes) (Region, string) {
|
||||
if n, ok := parseNumber(ops["hirand"]); ok {
|
||||
r.HighRandom = clamp(n, 0, 1)
|
||||
}
|
||||
// polyphony_group is SFZ v2's name for group.
|
||||
if n, ok := parseNumber(firstOf(ops, "group", "polyphony_group")); ok {
|
||||
r.Group = int(n)
|
||||
if n, ok := parseNumber(ops["group"]); ok {
|
||||
r.Group = toInt(n)
|
||||
}
|
||||
if n, ok := parseNumber(ops["off_by"]); ok {
|
||||
g := int(n)
|
||||
g := toInt(n)
|
||||
r.OffBy = &g
|
||||
}
|
||||
r.Polyphony = voiceLimit(ops["polyphony"])
|
||||
r.Polyphony = polyphonyLimit(ops["polyphony"])
|
||||
r.NotePolyphony = voiceLimit(ops["note_polyphony"])
|
||||
r.OffMode = defaultOffMode
|
||||
switch mode := strings.ToLower(strings.TrimSpace(ops["off_mode"])); mode {
|
||||
@@ -284,7 +285,7 @@ func crossfade(ops Opcodes, which string, shift int) *Crossfade {
|
||||
n += shift
|
||||
}
|
||||
} else if f, isNumber := parseNumber(v); isNumber {
|
||||
n, ok = int(f), true
|
||||
n, ok = toInt(f), true
|
||||
}
|
||||
if !ok {
|
||||
return fallback, false
|
||||
@@ -306,6 +307,17 @@ func crossfade(ops Opcodes, which string, shift int) *Crossfade {
|
||||
return c
|
||||
}
|
||||
|
||||
// polyphonyLimit reads the value of polyphony, which is a number of voices, or
|
||||
// legato_high, legato_last or legato_low, which each limit the region to one
|
||||
// voice and choose which note sounds. It returns nil if v is neither.
|
||||
func polyphonyLimit(v string) *int {
|
||||
switch strings.TrimSpace(v) {
|
||||
case "legato_high", "legato_last", "legato_low":
|
||||
return ptr(1)
|
||||
}
|
||||
return voiceLimit(v)
|
||||
}
|
||||
|
||||
// voiceLimit reads a number of voices, which is at least 1. It returns nil if
|
||||
// v isn't one.
|
||||
func voiceLimit(v string) *int {
|
||||
@@ -313,7 +325,7 @@ func voiceLimit(v string) *int {
|
||||
if !ok || n < 1 {
|
||||
return nil
|
||||
}
|
||||
limit := int(n)
|
||||
limit := toInt(n)
|
||||
return &limit
|
||||
}
|
||||
|
||||
@@ -338,27 +350,25 @@ func samplePath(defaultPath, sample string) string {
|
||||
return strings.TrimPrefix(path.Clean(p), "./")
|
||||
}
|
||||
|
||||
// firstOf returns the value of the first of the named opcodes that is set, or
|
||||
// an empty string if none is.
|
||||
func firstOf(ops Opcodes, names ...string) string {
|
||||
for _, n := range names {
|
||||
if v, ok := ops[n]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// frames reads a sample position in frames. It returns nil if v isn't one.
|
||||
func frames(v string) *int64 {
|
||||
n, ok := parseNumber(v)
|
||||
if !ok || n < 0 {
|
||||
return nil
|
||||
}
|
||||
f := int64(n)
|
||||
f := toInt64(n)
|
||||
return &f
|
||||
}
|
||||
|
||||
// toInt returns a number as a whole number, cut to the range of 32 bits, which
|
||||
// is as far as the format goes. A number that doesn't fit would otherwise
|
||||
// change sign.
|
||||
func toInt(f float64) int { return int(clamp(math.Trunc(f), math.MinInt32, math.MaxInt32)) }
|
||||
|
||||
// toInt64 returns a number as a whole number, cut to the range that a float64
|
||||
// holds exactly.
|
||||
func toInt64(f float64) int64 { return int64(clamp(math.Trunc(f), -1<<53, 1<<53)) }
|
||||
|
||||
// clamp returns v, or the nearest of lo and hi if v is outside them.
|
||||
func clamp(v, lo, hi float64) float64 {
|
||||
return math.Min(hi, math.Max(lo, v))
|
||||
@@ -396,8 +406,37 @@ var modulatedTargets = []string{
|
||||
// controller number, or only name something.
|
||||
var handledRE = regexp.MustCompile(`^(?:(?:lo|hi)(?:hd)?cc\d+|set_(?:hd)?cc\d+|label_cc\d+|amp_velcurve_\d+|[a-z_]*_label)$`)
|
||||
|
||||
// withOwnNames returns the opcodes with each name that is another name of an
|
||||
// opcode replaced by the opcode's own, such as loopmode by loop_mode. An opcode
|
||||
// that is set under its own name keeps that value, and one that is set under
|
||||
// several other names takes the value of the name that sorts last, so that the
|
||||
// result is the same each time.
|
||||
func withOwnNames(ops Opcodes) Opcodes {
|
||||
var others []string
|
||||
for name := range ops {
|
||||
if ownName(name) != name {
|
||||
others = append(others, name)
|
||||
}
|
||||
}
|
||||
if others == nil {
|
||||
return ops
|
||||
}
|
||||
slices.Sort(others)
|
||||
out := maps.Clone(ops)
|
||||
for _, name := range others {
|
||||
delete(out, name)
|
||||
}
|
||||
for _, name := range others {
|
||||
if _, set := ops[ownName(name)]; !set {
|
||||
out[ownName(name)] = ops[name]
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// handled reports whether the typed model reads an opcode.
|
||||
func handled(name string) bool {
|
||||
name = ownName(name)
|
||||
if handledNames[name] || handledRE.MatchString(name) {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user