2026-12-01 18:19:11 +00:00
|
|
|
package sfz
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// The controllers above 127 that ARIA defines, which depend on the note: its
|
2026-12-01 18:19:11 +00:00
|
|
|
// velocity, its key, and whether it is held.
|
|
|
|
|
const (
|
|
|
|
|
extendedVelocityCC = 131
|
|
|
|
|
extendedKeyCC = 133
|
|
|
|
|
extendedGateCC = 134
|
|
|
|
|
)
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// maxControls is how many numbered controllers an instrument has, 0 to 127.
|
2026-12-01 18:19:11 +00:00
|
|
|
const maxControls = 128
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// controls returns the controllers that the instrument names with label_ccN,
|
|
|
|
|
// sets with set_ccN or set_hdccN, or has regions that use, in order of number,
|
|
|
|
|
// each with its starting value.
|
2026-12-01 18:19:11 +00:00
|
|
|
func (e evaluator) controls(control Opcodes, regions []Region) []Control {
|
|
|
|
|
labels := map[int]string{}
|
|
|
|
|
used := map[int]bool{}
|
|
|
|
|
for name, v := range control {
|
|
|
|
|
m := ccRE.FindStringSubmatch(name)
|
|
|
|
|
if m == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
n, _ := strconv.Atoi(m[2])
|
|
|
|
|
switch m[1] {
|
|
|
|
|
case "label_cc":
|
|
|
|
|
labels[n] = strings.TrimSpace(v)
|
|
|
|
|
used[n] = true
|
|
|
|
|
case "set_cc", "set_hdcc":
|
|
|
|
|
used[n] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, r := range regions {
|
|
|
|
|
for _, c := range r.Conditions {
|
|
|
|
|
if c.Source == SourceController {
|
|
|
|
|
used[c.Controller] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, m := range r.Modulations {
|
|
|
|
|
used[m.Controller] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var out []Control
|
|
|
|
|
for n := 0; n < maxControls; n++ {
|
|
|
|
|
if used[n] {
|
|
|
|
|
out = append(out, Control{Controller: n, Label: labels[n], Value: e.cc[n]})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// keyswitchRange returns the keys that are keyswitches and the one that is down
|
2026-12-01 19:08:00 +00:00
|
|
|
// at the start, or nil if no region uses a keyswitch. The keys are those that
|
|
|
|
|
// sw_lokey and sw_hikey give, and the keys that the regions name.
|
2026-12-01 18:19:11 +00:00
|
|
|
func keyswitchRange(regions []Opcodes) *KeyswitchRange {
|
|
|
|
|
var r *KeyswitchRange
|
|
|
|
|
for _, ops := range regions {
|
|
|
|
|
shift := keyShift(ops)
|
|
|
|
|
low, lowOK := parseNote(ops["sw_lokey"])
|
|
|
|
|
high, highOK := parseNote(ops["sw_hikey"])
|
|
|
|
|
var keys []int
|
|
|
|
|
for _, name := range []string{"sw_last", "sw_previous", "sw_down", "sw_up"} {
|
|
|
|
|
if n, ok := parseNote(ops[name]); ok {
|
|
|
|
|
keys = append(keys, n+shift)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !lowOK && !highOK && len(keys) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if r == nil {
|
|
|
|
|
r = &KeyswitchRange{Low: 127, High: 0}
|
|
|
|
|
}
|
|
|
|
|
if lowOK {
|
|
|
|
|
keys = append(keys, low+shift)
|
|
|
|
|
}
|
|
|
|
|
if highOK {
|
|
|
|
|
keys = append(keys, high+shift)
|
|
|
|
|
}
|
|
|
|
|
for _, k := range keys {
|
|
|
|
|
r.Low, r.High = min(r.Low, k), max(r.High, k)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if r != nil {
|
|
|
|
|
r.Low, r.High = max(0, r.Low), min(127, r.High)
|
|
|
|
|
if d := defaultKeyswitch(regions); d >= 0 {
|
|
|
|
|
r.Default = &d
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// ccRE reads the name of an opcode that ends in a controller number, such as
|
|
|
|
|
// locc64, as the name without the number and then the number.
|
2026-12-01 18:19:11 +00:00
|
|
|
var ccRE = regexp.MustCompile(`^(.*?)(\d+)$`)
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// defaultControllers are the controllers that start somewhere other than 0,
|
|
|
|
|
// unless set_cc or set_hdcc say otherwise, as sfizz starts them: volume at 100,
|
|
|
|
|
// pan in the middle, and expression at full.
|
2026-12-01 18:19:11 +00:00
|
|
|
var defaultControllers = map[int]float64{7: 100.0 / 127, 10: 0.5, 11: 1}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// controllerValues returns the starting values of the controllers, from 0 to 1.
|
|
|
|
|
// A controller takes the value that set_cc or set_hdcc gives it, or else its
|
|
|
|
|
// value in defaultControllers, or else 0.
|
2026-12-01 18:19:11 +00:00
|
|
|
func controllerValues(control Opcodes) map[int]float64 {
|
|
|
|
|
values := map[int]float64{}
|
|
|
|
|
for n, v := range defaultControllers {
|
|
|
|
|
values[n] = v
|
|
|
|
|
}
|
|
|
|
|
for name, v := range control {
|
|
|
|
|
m := ccRE.FindStringSubmatch(name)
|
|
|
|
|
if m == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
n, _ := strconv.Atoi(m[2])
|
|
|
|
|
f, ok := parseNumber(v)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
switch m[1] {
|
|
|
|
|
case "set_cc":
|
|
|
|
|
values[n] = clamp(f/127, 0, 1)
|
|
|
|
|
case "set_hdcc":
|
|
|
|
|
values[n] = clamp(f, 0, 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return values
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// defaultKeyswitch returns the keyswitch that is active before any is pressed:
|
|
|
|
|
// the first sw_default, or failing that the first sw_last, or -1 if the
|
|
|
|
|
// instrument has no keyswitches. Without an sw_default, SFZ players play no
|
|
|
|
|
// keyswitched region until a keyswitch is pressed. A host that can't press one
|
|
|
|
|
// can use the first articulation instead.
|
2026-12-01 18:19:11 +00:00
|
|
|
func defaultKeyswitch(regions []Opcodes) int {
|
|
|
|
|
for _, ops := range regions {
|
|
|
|
|
if n, ok := parseNote(ops["sw_default"]); ok {
|
|
|
|
|
return n + keyShift(ops)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, ops := range regions {
|
|
|
|
|
if n, ok := parseNote(ops["sw_last"]); ok {
|
|
|
|
|
return n + keyShift(ops)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// keyShift returns how far the <control> header's note_offset and octave_offset
|
2026-12-01 18:19:11 +00:00
|
|
|
// move a region's key numbers.
|
|
|
|
|
func keyShift(ops Opcodes) int {
|
|
|
|
|
shift := 0
|
|
|
|
|
if n, ok := parseNumber(ops["note_offset"]); ok {
|
|
|
|
|
shift += int(n)
|
|
|
|
|
}
|
|
|
|
|
if n, ok := parseNumber(ops["octave_offset"]); ok {
|
|
|
|
|
shift += 12 * int(n)
|
|
|
|
|
}
|
|
|
|
|
return shift
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// topEpsilon is how far below the next whole value the end of a hiccN range is
|
|
|
|
|
// put. The range includes the whole of value N, as a controller at 0.5 is 63.5
|
|
|
|
|
// out of 127 and so is within hicc63, as sfizz compares them. The range ends
|
|
|
|
|
// just below value N + 1, unless N is the top.
|
2026-12-01 18:19:11 +00:00
|
|
|
const topEpsilon = 1e-9
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// controllerBounds are the opcodes that limit a source that isn't a numbered
|
|
|
|
|
// controller to a range. Each has the name of the source, the opcodes for its
|
|
|
|
|
// lowest and highest limits, the scale that their values are the range's limits
|
|
|
|
|
// times, and the range of the source, which the limits default to.
|
2026-12-01 18:19:11 +00:00
|
|
|
var controllerBounds = []struct {
|
|
|
|
|
source, low, high string
|
|
|
|
|
scale float64
|
|
|
|
|
min, max float64
|
|
|
|
|
}{
|
|
|
|
|
{SourceChannel, "lochan", "hichan", 1, 1, 16},
|
|
|
|
|
{SourceBend, "lobend", "hibend", 8192, -1, 1},
|
|
|
|
|
{SourceAftertouch, "lochanaft", "hichanaft", 127, 0, 1},
|
|
|
|
|
{SourcePolyAftertouch, "lopolyaft", "hipolyaft", 127, 0, 1},
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// conditions returns the ranges that a region's locc, hicc, lohdcc and hihdcc
|
|
|
|
|
// opcodes, and its channel, pitch bend and aftertouch opcodes, limit it to.
|
|
|
|
|
// Controllers above 127, which belong to the note, are left out.
|
2026-12-01 18:19:11 +00:00
|
|
|
func conditions(ops Opcodes) []Condition {
|
|
|
|
|
ranges := map[int]*Condition{}
|
|
|
|
|
for name, v := range ops {
|
|
|
|
|
m := ccRE.FindStringSubmatch(name)
|
|
|
|
|
if m == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
n, _ := strconv.Atoi(m[2])
|
|
|
|
|
limit, ok := parseNumber(v)
|
|
|
|
|
if !ok || n >= extendedVelocityCC {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
c := ranges[n]
|
|
|
|
|
if c == nil {
|
|
|
|
|
c = &Condition{Source: SourceController, Controller: n, Low: 0, High: 1}
|
|
|
|
|
}
|
|
|
|
|
switch m[1] {
|
|
|
|
|
case "locc":
|
|
|
|
|
c.Low = clamp(limit/127, 0, 1)
|
|
|
|
|
case "hicc":
|
|
|
|
|
c.High = 1
|
|
|
|
|
if limit < 127 {
|
|
|
|
|
c.High = clamp((limit+1)/127-topEpsilon, 0, 1)
|
|
|
|
|
}
|
|
|
|
|
case "lohdcc":
|
|
|
|
|
c.Low = clamp(limit, 0, 1)
|
|
|
|
|
case "hihdcc":
|
|
|
|
|
c.High = clamp(limit, 0, 1)
|
|
|
|
|
default:
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
ranges[n] = c
|
|
|
|
|
}
|
|
|
|
|
var out []Condition
|
|
|
|
|
for _, b := range controllerBounds {
|
|
|
|
|
lo, loOK := parseNumber(ops[b.low])
|
|
|
|
|
hi, hiOK := parseNumber(ops[b.high])
|
|
|
|
|
if !loOK && !hiOK {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
c := Condition{Source: b.source, Low: b.min, High: b.max}
|
|
|
|
|
if loOK {
|
|
|
|
|
c.Low = clamp(lo/b.scale, b.min, b.max)
|
|
|
|
|
}
|
|
|
|
|
if hiOK {
|
|
|
|
|
c.High = clamp(hi/b.scale, b.min, b.max)
|
|
|
|
|
}
|
|
|
|
|
out = append(out, c)
|
|
|
|
|
}
|
|
|
|
|
for n := 0; n < extendedVelocityCC; n++ {
|
|
|
|
|
if c := ranges[n]; c != nil {
|
|
|
|
|
out = append(out, *c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// keyswitch returns the keyswitches that a region needs, with shift added to
|
|
|
|
|
// their keys, or nil if it has none.
|
2026-12-01 18:19:11 +00:00
|
|
|
func keyswitch(ops Opcodes, shift int) *Keyswitch {
|
|
|
|
|
var k Keyswitch
|
|
|
|
|
found := false
|
|
|
|
|
for name, into := range map[string]**int{"sw_last": &k.Last, "sw_previous": &k.Previous, "sw_down": &k.Down, "sw_up": &k.Up} {
|
|
|
|
|
if n, ok := parseNote(ops[name]); ok {
|
|
|
|
|
key := n + shift
|
|
|
|
|
*into = &key
|
|
|
|
|
found = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &k
|
|
|
|
|
}
|