Add lossless SFZ document, flattening, typed model and opcode registry
Parse keeps a file exactly as written, with #define and #include unexpanded and each include as its own file in a tree. Flatten resolves directives and header inheritance, and Flat.Instrument reads regions into the typed model ported from Zampler's internal/sfz. The opcode registry is generated from the SFZ format's syntax data and drives Lookup, GetAs and Validate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package sfz
|
||||
|
||||
import (
|
||||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// noteRE reads a note name such as c4, C#0 or eb-1. Middle C is c4, MIDI
|
||||
// note 60.
|
||||
var noteRE = regexp.MustCompile(`^([A-Ga-g])([#b]?)(-?[0-9]+)$`)
|
||||
|
||||
// semitones are the notes' places in the octave from C.
|
||||
var semitones = map[byte]int{'c': 0, 'd': 2, 'e': 4, 'f': 5, 'g': 7, 'a': 9, 'b': 11}
|
||||
|
||||
// parseNote reads a MIDI note number or a note name. It reports false if the
|
||||
// value is neither.
|
||||
func parseNote(v string) (int, bool) {
|
||||
v = strings.TrimSpace(v)
|
||||
if n, err := strconv.Atoi(v); err == nil {
|
||||
return n, true
|
||||
}
|
||||
m := noteRE.FindStringSubmatch(v)
|
||||
if m == nil {
|
||||
return 0, false
|
||||
}
|
||||
octave, _ := strconv.Atoi(m[3])
|
||||
n := (octave+1)*12 + semitones[strings.ToLower(m[1])[0]]
|
||||
switch m[2] {
|
||||
case "#":
|
||||
n++
|
||||
case "b":
|
||||
n--
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
|
||||
// parseNumber reads a number, and reports false if the value isn't one.
|
||||
func parseNumber(v string) (float64, bool) {
|
||||
f, err := strconv.ParseFloat(strings.TrimSpace(v), 64)
|
||||
if err != nil || math.IsNaN(f) || math.IsInf(f, 0) {
|
||||
return 0, false
|
||||
}
|
||||
return f, true
|
||||
}
|
||||
|
||||
// curve maps a controller's value, from 0 to 1, to how much of a
|
||||
// modulation's depth applies.
|
||||
type curve func(x float64) float64
|
||||
|
||||
// builtinCurves are the curves SFZ players define, by index.
|
||||
var builtinCurves = map[int]curve{
|
||||
0: func(x float64) float64 { return x },
|
||||
1: func(x float64) float64 { return 2*x - 1 },
|
||||
2: func(x float64) float64 { return 1 - x },
|
||||
3: func(x float64) float64 { return 1 - 2*x },
|
||||
4: func(x float64) float64 { return x * x },
|
||||
5: func(x float64) float64 { return math.Sqrt(x) },
|
||||
6: func(x float64) float64 { return math.Sqrt(1 - x) },
|
||||
}
|
||||
|
||||
// pointRE reads a <curve> point, such as v064, whose number is the
|
||||
// controller value from 0 to 127.
|
||||
var pointRE = regexp.MustCompile(`^v([0-9]{1,3})$`)
|
||||
|
||||
// readCurves returns the instrument's curves: the built-in ones, and those of
|
||||
// its <curve> headers, which pass through their points in straight lines, and
|
||||
// the points of the custom ones.
|
||||
// A curve that doesn't set v000 or v127 starts at 0 or ends at 1.
|
||||
func readCurves(defs []Opcodes) (map[int]curve, map[int][]CurveStop) {
|
||||
curves := make(map[int]curve, len(builtinCurves)+len(defs))
|
||||
stops := map[int][]CurveStop{}
|
||||
for i, c := range builtinCurves {
|
||||
curves[i] = c
|
||||
}
|
||||
for _, def := range defs {
|
||||
index, err := strconv.Atoi(def["curve_index"])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
points := map[int]float64{0: 0, 127: 1}
|
||||
for name, v := range def {
|
||||
m := pointRE.FindStringSubmatch(name)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
at, _ := strconv.Atoi(m[1])
|
||||
if y, ok := parseNumber(v); ok && at <= 127 {
|
||||
points[at] = y
|
||||
}
|
||||
}
|
||||
xs := make([]int, 0, len(points))
|
||||
for x := range points {
|
||||
xs = append(xs, x)
|
||||
}
|
||||
sort.Ints(xs)
|
||||
for _, x := range xs {
|
||||
stops[index] = append(stops[index], CurveStop{At: x, Value: points[x]})
|
||||
}
|
||||
curves[index] = func(x float64) float64 {
|
||||
at := x * 127
|
||||
for i := 1; i < len(xs); i++ {
|
||||
if at <= float64(xs[i]) {
|
||||
x0, x1 := float64(xs[i-1]), float64(xs[i])
|
||||
y0, y1 := points[xs[i-1]], points[xs[i]]
|
||||
return y0 + (y1-y0)*(at-x0)/(x1-x0)
|
||||
}
|
||||
}
|
||||
return points[xs[len(xs)-1]]
|
||||
}
|
||||
}
|
||||
return curves, stops
|
||||
}
|
||||
Reference in New Issue
Block a user