2026-12-01 18:19:11 +00:00
|
|
|
package sfz
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cmp"
|
|
|
|
|
"fmt"
|
|
|
|
|
"slices"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulatedBy are the settings that controllers move while a note plays, each
|
|
|
|
|
// with the opcodes that set it.
|
2026-12-01 18:19:11 +00:00
|
|
|
var modulatedBy = []struct {
|
|
|
|
|
target string
|
|
|
|
|
Opcodes []string
|
|
|
|
|
}{
|
|
|
|
|
{TargetAmplitude, []string{"amplitude"}},
|
|
|
|
|
{TargetVolume, []string{"volume", "gain"}},
|
|
|
|
|
{TargetPan, []string{"pan"}},
|
|
|
|
|
{TargetWidth, []string{"width"}},
|
|
|
|
|
{TargetTune, []string{"tune", "pitch"}},
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// hasModulation reports whether a numbered controller moves the target.
|
2026-12-01 18:19:11 +00:00
|
|
|
func (e evaluator) hasModulation(ops Opcodes, target string) bool {
|
|
|
|
|
return len(e.modulations(ops, target)) > 0
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulations returns the modulations of a region's settings by the numbered
|
|
|
|
|
// controllers, 0 to 127, or only those of the targets named. They are in the
|
|
|
|
|
// order of the settings, then of the controllers.
|
2026-12-01 18:19:11 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulation returns how far the controllers from the number from up move a
|
|
|
|
|
// target from its own value. It is the sum of the depths of its target_onccN
|
|
|
|
|
// opcodes, or targetccN in SFZ v1, each shaped by target_curveccN at the
|
|
|
|
|
// controller's value.
|
2026-12-01 18:19:11 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulatorOpcode is an opcode of a region that sets the depth of a controller's
|
|
|
|
|
// modulation of a target.
|
2026-12-01 18:19:11 +00:00
|
|
|
type modulatorOpcode struct {
|
|
|
|
|
name string
|
|
|
|
|
controller int
|
|
|
|
|
depth float64
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulators returns the opcodes that modulate the target by the controllers
|
|
|
|
|
// from the number from up, and whose depths are numbers, ordered by controller
|
|
|
|
|
// and then by name. Floating-point sums depend on the order of their terms, so
|
|
|
|
|
// summing in this order gives the same result each time.
|
2026-12-01 18:19:11 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// amplitude returns the region's amplitude as a factor. The amplitude, and the
|
|
|
|
|
// global, master and group amplitudes, are percentages that multiply, and so
|
2026-12-01 18:19:11 +00:00
|
|
|
// 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
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulation by these at its root note and the middle of its velocities, as it
|
|
|
|
|
// can't follow them. It leaves the modulation by the numbered controllers to
|
|
|
|
|
// be applied when a note plays.
|
2026-12-01 18:19:11 +00:00
|
|
|
const extendedOnly = 128
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// shape returns the value x of controller n through the curve that the region
|
|
|
|
|
// gives it for the target, or straight through if it gives none.
|
2026-12-01 18:19:11 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// curveIndex returns the curve that 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.
|
2026-12-01 18:19:11 +00:00
|
|
|
func (e evaluator) curveIndex(ops Opcodes, target string, n int) int {
|
|
|
|
|
if v, ok := parseNumber(ops[fmt.Sprintf("%s_curvecc%d", target, n)]); ok {
|
2026-12-01 20:38:57 +00:00
|
|
|
if _, exists := e.curves[toInt(v)]; exists {
|
|
|
|
|
return toInt(v)
|
2026-12-01 18:19:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// modulator reports whether an opcode sets a controller's modulation of the
|
|
|
|
|
// target, and the controller's number. The opcode is target_onccN, or targetccN
|
|
|
|
|
// or target_ccN in SFZ v1.
|
2026-12-01 18:19:11 +00:00
|
|
|
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
|
|
|
|
|
}
|