Files
sfz/values.go
T

125 lines
3.5 KiB
Go
Raw Normal View History

package sfz
import (
"maps"
"math"
"regexp"
"slices"
"sort"
"strconv"
"strings"
)
// noteRE reads a note name such as c4, C#0 or eb-1, as the letter, the sharp
// or flat if any, and the octave. Middle C is c4, MIDI note 60.
var noteRE = regexp.MustCompile(`^([A-Ga-g])([#b]?)(-?[0-9]+)$`)
// semitones are how many semitones each natural note is above C, by the
// note's lowercase letter.
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, and reports whether v was
// one.
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, err := strconv.Atoi(m[3])
if err != nil {
return 0, false
}
n := (octave+1)*12 + semitones[strings.ToLower(m[1])[0]]
switch m[2] {
case "#":
n++
case "b":
n--
}
return n, true
}
// parseNumber reads a finite number, and reports whether v was 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 that 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 the name of 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 by index, and the points of its
// custom curves. The curves are the built-in ones and those of its <curve>
// headers, which pass through their points in straight lines. A curve that
// doesn't set v000 starts at 0, and one that doesn't set v127 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}
// 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
}
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
}