No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
167 lines
5.2 KiB
Go
167 lines
5.2 KiB
Go
package sfz
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// modulatedBy are the settings controllers move while a note plays, with the
|
|
// Opcodes that set each.
|
|
var modulatedBy = []struct {
|
|
target string
|
|
Opcodes []string
|
|
}{
|
|
{TargetAmplitude, []string{"amplitude"}},
|
|
{TargetVolume, []string{"volume", "gain"}},
|
|
{TargetPan, []string{"pan"}},
|
|
{TargetWidth, []string{"width"}},
|
|
{TargetTune, []string{"tune", "pitch"}},
|
|
}
|
|
|
|
// hasModulation reports whether a controller moves a target.
|
|
func (e evaluator) hasModulation(ops Opcodes, target string) bool {
|
|
return len(e.modulations(ops, target)) > 0
|
|
}
|
|
|
|
// modulations returns the controller modulations of a region's settings, which
|
|
// are the controllers 0 to 127, or only those of the targets named. They are in
|
|
// the order of the settings, then of the controllers.
|
|
func (e evaluator) modulations(ops Opcodes, only ...string) []Modulation {
|
|
var out []Modulation
|
|
for _, set := range modulatedBy {
|
|
if len(only) > 0 && !slices.Contains(only, set.target) {
|
|
continue
|
|
}
|
|
for _, opcode := range set.Opcodes {
|
|
for name, v := range ops {
|
|
n, ok := modulator(name, opcode)
|
|
depth, isNumber := parseNumber(v)
|
|
if !ok || !isNumber || n >= extendedOnly {
|
|
continue
|
|
}
|
|
m := Modulation{Target: set.target, Controller: n, Depth: depth, Curve: e.curveIndex(ops, opcode, n)}
|
|
if stops, ok := e.stops[m.Curve]; ok {
|
|
m.Points = stops
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
}
|
|
order := func(t string) int {
|
|
for i, set := range modulatedBy {
|
|
if set.target == t {
|
|
return i
|
|
}
|
|
}
|
|
return len(modulatedBy)
|
|
}
|
|
slices.SortFunc(out, func(a, b Modulation) int {
|
|
return cmp.Or(order(a.Target)-order(b.Target), a.Controller-b.Controller, cmp.Compare(a.Depth, b.Depth))
|
|
})
|
|
return out
|
|
}
|
|
|
|
// modulation returns how much the controllers from the number given up move an
|
|
// opcode from its own value: the sum of each target_onccN depth, or its SFZ v1
|
|
// form targetccN, shaped by target_curveccN at the controller's value.
|
|
func (e evaluator) modulation(ops Opcodes, target string, cc func(int) float64, from int) float64 {
|
|
total := 0.0
|
|
for _, m := range modulators(ops, target, from) {
|
|
total += m.depth * e.shape(ops, target, m.controller, cc(m.controller))
|
|
}
|
|
return total
|
|
}
|
|
|
|
// modulatorOpcode is a controller modulation of a target in a region's
|
|
// opcodes.
|
|
type modulatorOpcode struct {
|
|
name string
|
|
controller int
|
|
depth float64
|
|
}
|
|
|
|
// modulators returns the controller modulations of a target from the
|
|
// controller given up, whose depths are numbers, ordered by controller and then
|
|
// by name. The order makes sums of them, which floating-point addition makes
|
|
// depend on it, the same each time.
|
|
func modulators(ops Opcodes, target string, from int) []modulatorOpcode {
|
|
var out []modulatorOpcode
|
|
for name, v := range ops {
|
|
n, ok := modulator(name, target)
|
|
if !ok || n < from {
|
|
continue
|
|
}
|
|
if depth, ok := parseNumber(v); ok {
|
|
out = append(out, modulatorOpcode{name, n, depth})
|
|
}
|
|
}
|
|
slices.SortFunc(out, func(a, b modulatorOpcode) int {
|
|
return cmp.Or(cmp.Compare(a.controller, b.controller), cmp.Compare(a.name, b.name))
|
|
})
|
|
return out
|
|
}
|
|
|
|
// amplitude returns the region's amplitude as a factor: amplitude and the
|
|
// global, master and group amplitudes are percentages that multiply, and so
|
|
// does each amplitude_oncc of the controllers above 127.
|
|
func (e evaluator) amplitude(ops Opcodes, cc func(int) float64) float64 {
|
|
a := 1.0
|
|
for _, name := range []string{"amplitude", "global_amplitude", "master_amplitude", "group_amplitude"} {
|
|
if v, ok := parseNumber(ops[name]); ok {
|
|
a *= v / 100
|
|
}
|
|
}
|
|
for _, m := range modulators(ops, "amplitude", extendedOnly) {
|
|
a *= m.depth / 100 * e.shape(ops, "amplitude", m.controller, cc(m.controller))
|
|
}
|
|
return a
|
|
}
|
|
|
|
// extendedOnly is the lowest of the controllers above 127, which belong to the
|
|
// note: its velocity, its key and whether it is held. A region takes the
|
|
// modulation of these at its root note and the middle of its velocities, as it
|
|
// can't follow them, and leaves the numbered controllers to be applied when a
|
|
// note plays.
|
|
const extendedOnly = 128
|
|
|
|
// shape returns a controller's value through the curve the region gives it
|
|
// for the target, or straight through if it gives none.
|
|
func (e evaluator) shape(ops Opcodes, target string, n int, x float64) float64 {
|
|
c, ok := e.curves[e.curveIndex(ops, target, n)]
|
|
if !ok {
|
|
c = builtinCurves[0]
|
|
}
|
|
return c(x)
|
|
}
|
|
|
|
// curveIndex returns the curve a region gives a controller's modulation of a
|
|
// target, or 0, the straight one, if it gives none or the curve doesn't exist.
|
|
func (e evaluator) curveIndex(ops Opcodes, target string, n int) int {
|
|
if v, ok := parseNumber(ops[fmt.Sprintf("%s_curvecc%d", target, n)]); ok {
|
|
if _, exists := e.curves[int(v)]; exists {
|
|
return int(v)
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// modulator reports whether an opcode is a controller modulation of the
|
|
// target, and of which controller: target_onccN, or targetccN or target_ccN
|
|
// in SFZ v1.
|
|
func modulator(name, target string) (int, bool) {
|
|
rest, ok := strings.CutPrefix(name, target)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
for _, prefix := range []string{"_oncc", "cc", "_cc"} {
|
|
if digits, ok := strings.CutPrefix(rest, prefix); ok {
|
|
n, err := strconv.Atoi(digits)
|
|
return n, err == nil
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|