Files
sfz/modulation.go
T

169 lines
5.4 KiB
Go
Raw Normal View History

package sfz
import (
"cmp"
"fmt"
"slices"
"strconv"
"strings"
)
// modulatedBy are the settings that controllers move while a note plays, each
// with the opcodes that set it.
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 numbered controller moves the target.
func (e evaluator) hasModulation(ops Opcodes, target string) bool {
return len(e.modulations(ops, target)) > 0
}
// 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.
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 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.
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 an opcode of a region that sets the depth of a controller's
// modulation of a target.
type modulatorOpcode struct {
name string
controller int
depth float64
}
// 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.
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. The 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 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.
const extendedOnly = 128
// 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.
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 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.
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[toInt(v)]; exists {
return toInt(v)
}
}
return 0
}
// 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.
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
}