Files
aneurinandClaude Sonnet 5.5 8134660721 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>
2026-12-01 20:38:57 +00:00

60 lines
1.4 KiB
Go

package sfz
import "strings"
// Value is the Go types that the value of an opcode can be read as.
type Value interface {
int | int64 | float64 | string
}
// GetAs returns the value of an opcode, read as a T, and whether the opcode is
// set and can be read as one. An opcode that the format gives aliases is found
// by any of its names, so GetAs(o, "loop_mode") finds loopmode. An integer
// opcode that takes notes, such as lokey, reads a note name as its MIDI number,
// with middle C as c4. A string is returned without surrounding whitespace.
func GetAs[T Value](o Opcodes, name string) (T, bool) {
var zero T
raw, ok := o[name]
if !ok {
info, known := Lookup(name)
if !known {
return zero, false
}
for _, alias := range append([]string{info.Name}, info.Aliases...) {
if raw, ok = o[alias]; ok {
break
}
}
if !ok {
return zero, false
}
}
raw = strings.TrimSpace(raw)
switch any(zero).(type) {
case string:
return any(raw).(T), true
case float64:
f, ok := parseNumber(raw)
if !ok {
return zero, false
}
return any(f).(T), true
}
var n int
if info, known := Lookup(name); known && info.Note {
n, ok = parseNote(raw)
} else {
var f float64
if f, ok = parseNumber(raw); ok {
n = toInt(f)
}
}
if !ok {
return zero, false
}
if _, is64 := any(zero).(int64); is64 {
return any(int64(n)).(T), true
}
return any(n).(T), true
}