Split instrument.go into model, controls and modulation files
No behaviour change. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+261
@@ -0,0 +1,261 @@
|
|||||||
|
package sfz
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Controllers above 127 that ARIA defines, which depend on the note: its
|
||||||
|
// velocity, its key, and whether it is held.
|
||||||
|
const (
|
||||||
|
extendedVelocityCC = 131
|
||||||
|
extendedKeyCC = 133
|
||||||
|
extendedGateCC = 134
|
||||||
|
)
|
||||||
|
|
||||||
|
// maxControls is the most controllers an instrument has, one for each.
|
||||||
|
const maxControls = 128
|
||||||
|
|
||||||
|
// controls returns the controllers the instrument names with label_ccN, sets
|
||||||
|
// with set_ccN or set_hdccN, or has regions that use, by number, each with its
|
||||||
|
// starting value.
|
||||||
|
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
|
||||||
|
// at the start, or nil if no region uses a keyswitch. Without sw_lokey and
|
||||||
|
// sw_hikey, they are the keys that the regions name.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// ccRE reads a controller number from the end of an opcode, such as the 64 of
|
||||||
|
// locc64.
|
||||||
|
var ccRE = regexp.MustCompile(`^(.*?)(\d+)$`)
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
var defaultControllers = map[int]float64{7: 100.0 / 127, 10: 0.5, 11: 1}
|
||||||
|
|
||||||
|
// controllerValues returns the starting values of the controllers, from 0 to
|
||||||
|
// 1: those that set_cc and set_hdcc give, then defaultControllers, then 0.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// keyShift is how far the <control> header's note_offset and octave_offset
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// hicc's high end includes the whole of value N, as a controller at 0.5 is
|
||||||
|
// 63.5 out of 127 and so within hicc63, as sfizz compares them. So the range
|
||||||
|
// is made to end just below value N + 1, unless N is the top.
|
||||||
|
const topEpsilon = 1e-9
|
||||||
|
|
||||||
|
// controllerBounds are the Opcodes that limit a source that isn't a numbered
|
||||||
|
// controller to a range: the lowest and highest, the range they run over, and
|
||||||
|
// what they default to.
|
||||||
|
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},
|
||||||
|
}
|
||||||
|
|
||||||
|
// conditions returns the ranges a region's locc, hicc, lohdcc and hihdcc, and
|
||||||
|
// its channel, pitch bend and aftertouch Opcodes limit it to. Controllers
|
||||||
|
// above 127, which belong to the note, are left out.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// keyswitch returns the keyswitches a region needs, or nil if it has none.
|
||||||
|
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
|
||||||
|
}
|
||||||
-651
@@ -2,7 +2,6 @@ package sfz
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
"fmt"
|
|
||||||
"math"
|
"math"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -11,253 +10,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Envelope is a region's amplitude envelope. Times are in seconds.
|
|
||||||
type Envelope struct {
|
|
||||||
Delay float64
|
|
||||||
Attack float64
|
|
||||||
Hold float64
|
|
||||||
Decay float64
|
|
||||||
// Sustain is the level held after the decay, as a percentage.
|
|
||||||
Sustain float64
|
|
||||||
Release float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// CurvePoint is how much of its level a velocity plays, from 0 to 1.
|
|
||||||
type CurvePoint struct {
|
|
||||||
Velocity int
|
|
||||||
Gain float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// The curves of a crossfade.
|
|
||||||
const (
|
|
||||||
CrossfadePower = "power"
|
|
||||||
CrossfadeGain = "gain"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Crossfade fades a region in over one range of keys or velocities and out
|
|
||||||
// over another. It plays at no level below the start of its fade in and above
|
|
||||||
// the end of its fade out. A range whose ends are equal doesn't fade.
|
|
||||||
type Crossfade struct {
|
|
||||||
FadeInLow, FadeInHigh int
|
|
||||||
FadeOutLow, FadeOutHigh int
|
|
||||||
// Curve is power, which keeps the loudness steady through a crossfade
|
|
||||||
// between regions, or gain, which fades the level in a straight line.
|
|
||||||
Curve string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Where a condition's value comes from.
|
|
||||||
const (
|
|
||||||
SourceController = "cc"
|
|
||||||
SourceBend = "bend"
|
|
||||||
SourceAftertouch = "aftertouch"
|
|
||||||
SourcePolyAftertouch = "polyAftertouch"
|
|
||||||
SourceChannel = "channel"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Condition limits when a region plays to a source being within a range, which
|
|
||||||
// includes both ends. A controller, aftertouch and polyphonic aftertouch are
|
|
||||||
// from 0 to 1, pitch bend from -1 to 1 and a MIDI channel from 1 to 16.
|
|
||||||
type Condition struct {
|
|
||||||
Source string
|
|
||||||
Controller int
|
|
||||||
Low, High float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keyswitch limits when a region plays to the keyswitches that have been
|
|
||||||
// pressed: the last one, the one before it, and keys that are held or aren't.
|
|
||||||
// A field that isn't set is nil.
|
|
||||||
type Keyswitch struct {
|
|
||||||
Last, Previous, Down, Up *int
|
|
||||||
}
|
|
||||||
|
|
||||||
// The settings a controller moves, as a modulation names them.
|
|
||||||
const (
|
|
||||||
TargetAmplitude = "amplitude"
|
|
||||||
TargetVolume = "volume"
|
|
||||||
TargetPan = "pan"
|
|
||||||
TargetWidth = "width"
|
|
||||||
TargetTune = "tune"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CurveStop is a point of a custom curve: the controller at one of the values
|
|
||||||
// 0 to 127, and the result there.
|
|
||||||
type CurveStop struct {
|
|
||||||
At int
|
|
||||||
Value float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Modulation moves a region's setting as a controller does. The controller
|
|
||||||
// goes through the curve, and its result, from 0 to 1 or beyond, times Depth
|
|
||||||
// is added to the setting, or multiplies it for amplitude. Depth is in the
|
|
||||||
// setting's own units: percent for amplitude, pan and width, dB for volume,
|
|
||||||
// and cents for tune.
|
|
||||||
type Modulation struct {
|
|
||||||
Target string
|
|
||||||
Controller int
|
|
||||||
Depth float64
|
|
||||||
// Curve is the index of one of SFZ's built-in curves, 0 to 6, when Points
|
|
||||||
// is empty.
|
|
||||||
Curve int
|
|
||||||
Points []CurveStop
|
|
||||||
}
|
|
||||||
|
|
||||||
// Control is a controller an instrument uses, with the name its SFZ gives it,
|
|
||||||
// and its starting value from 0 to 1.
|
|
||||||
type Control struct {
|
|
||||||
Controller int
|
|
||||||
Label string
|
|
||||||
Value float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// KeyswitchRange is the keys that are keyswitches, and the one that is down at
|
|
||||||
// the start, or nil if the instrument has no keyswitch.
|
|
||||||
type KeyswitchRange struct {
|
|
||||||
Low, High int
|
|
||||||
Default *int
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultEnvelope is the envelope of a region that sets none: it plays at full
|
|
||||||
// level at once, and stops almost at once when its note ends.
|
|
||||||
var DefaultEnvelope = Envelope{Sustain: 100, Release: defaultRelease}
|
|
||||||
|
|
||||||
// Region is a sample and the notes and velocities that play it, with its
|
|
||||||
// settings at the instrument's starting controller values.
|
|
||||||
type Region struct {
|
|
||||||
// Sample is the sample's path, relative to the folder of the instrument's
|
|
||||||
// main file, with forward slashes.
|
|
||||||
Sample string
|
|
||||||
LowKey int
|
|
||||||
HighKey int
|
|
||||||
RootKey int
|
|
||||||
// RootFromSample says the root note is the one the sample file holds
|
|
||||||
// (pitch_keycenter=sample), which RootKey doesn't know.
|
|
||||||
RootFromSample bool
|
|
||||||
LowVelocity int
|
|
||||||
HighVelocity int
|
|
||||||
// KeyTrack is how far each key repitches the sample, in cents: 100 plays
|
|
||||||
// the keys' pitches, and 0 plays every key at the sample's own.
|
|
||||||
KeyTrack float64
|
|
||||||
// Tune is in cents.
|
|
||||||
Tune float64
|
|
||||||
// Volume is in dB.
|
|
||||||
Volume float64
|
|
||||||
// Pan is from -100, left, to 100, right.
|
|
||||||
Pan float64
|
|
||||||
// Offset is where the sample starts playing, in frames.
|
|
||||||
Offset int64
|
|
||||||
// VelocityTracking is how much velocity sets the level, as a percentage:
|
|
||||||
// 100 follows it fully, and 0 plays every velocity at full level.
|
|
||||||
VelocityTracking float64
|
|
||||||
// VelocityCurve is how much of its level each velocity plays, through
|
|
||||||
// straight lines between its points, or nil if the region sets none. Its
|
|
||||||
// points ascend by velocity, from 0 to 127.
|
|
||||||
VelocityCurve []CurvePoint
|
|
||||||
// KeyCrossfade and VelocityCrossfade are nil if the region sets none.
|
|
||||||
KeyCrossfade *Crossfade
|
|
||||||
VelocityCrossfade *Crossfade
|
|
||||||
Envelope Envelope
|
|
||||||
// Trigger is what starts the region: attack, when a note starts; release,
|
|
||||||
// when it ends; first; or legato.
|
|
||||||
Trigger string
|
|
||||||
// LoopMode is no_loop, one_shot, loop_continuous or loop_sustain, or
|
|
||||||
// empty to follow the sample file.
|
|
||||||
LoopMode string
|
|
||||||
// LoopStart and LoopEnd are frames, or nil to follow the sample file.
|
|
||||||
LoopStart *int64
|
|
||||||
LoopEnd *int64
|
|
||||||
// ReleaseDecay is how much quieter a release region plays for each second
|
|
||||||
// its note was held, in dB.
|
|
||||||
ReleaseDecay float64
|
|
||||||
// SequenceLength and SequencePosition make the region one of a round
|
|
||||||
// robin: of each SequenceLength notes it matches, it plays the one at
|
|
||||||
// SequencePosition, counting from 1.
|
|
||||||
SequenceLength int
|
|
||||||
SequencePosition int
|
|
||||||
// LowRandom and HighRandom make the region a random variant: it plays when
|
|
||||||
// a number drawn for the note, from 0 to 1, is at least LowRandom and less
|
|
||||||
// than HighRandom.
|
|
||||||
LowRandom float64
|
|
||||||
HighRandom float64
|
|
||||||
// Group is the region's choke group, which other regions' OffBy names.
|
|
||||||
Group int
|
|
||||||
// OffBy is the group whose notes stop this region while it plays, or nil
|
|
||||||
// if none does.
|
|
||||||
OffBy *int
|
|
||||||
// Polyphony is how many voices the region's group may have playing at
|
|
||||||
// once, or nil if it sets none. A note past it stops the oldest voice.
|
|
||||||
Polyphony *int
|
|
||||||
// Conditions and Keyswitch decide, from the controllers and keyswitches
|
|
||||||
// when a note starts, whether the region plays. The region plays if every
|
|
||||||
// condition holds and its keyswitch does.
|
|
||||||
Conditions []Condition
|
|
||||||
Keyswitch *Keyswitch
|
|
||||||
// Modulations are the controllers that move the region's settings, which
|
|
||||||
// hold their values with no controller.
|
|
||||||
Modulations []Modulation
|
|
||||||
// Width is the stereo width in percent, or nil for the sample's own. BendUp
|
|
||||||
// and BendDown are how far pitch bend moves the pitch at its ends in cents,
|
|
||||||
// and BendStep the size of its steps, or nil for the SFZ defaults.
|
|
||||||
Width *float64
|
|
||||||
BendUp *float64
|
|
||||||
BendDown *float64
|
|
||||||
BendStep *float64
|
|
||||||
// NotePolyphony is how many voices of the region's group may play the
|
|
||||||
// same key at once, or nil if it sets none.
|
|
||||||
NotePolyphony *int
|
|
||||||
// OffMode is how the region stops when it is stopped by a group or past a
|
|
||||||
// voice limit: fast, time or normal.
|
|
||||||
OffMode string
|
|
||||||
// OffTime is how long a region whose OffMode is time takes to stop, in
|
|
||||||
// seconds.
|
|
||||||
OffTime float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instrument is an SFZ instrument as typed regions, with its settings taken at
|
|
||||||
// the starting controller values.
|
|
||||||
type Instrument struct {
|
|
||||||
Regions []Region
|
|
||||||
// Unread counts, for each opcode or header the typed model has no field
|
|
||||||
// for, the regions that have it, or how often a header appears.
|
|
||||||
Unread map[string]int
|
|
||||||
// Skipped counts the regions that have no typed region, by why.
|
|
||||||
Skipped map[string]int
|
|
||||||
// Controls are the controllers that the instrument names, sets or uses.
|
|
||||||
Controls []Control
|
|
||||||
// Keyswitches are its keyswitch keys, or nil if it has none.
|
|
||||||
Keyswitches *KeyswitchRange
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reasons a region is skipped: the typed model has no region for it.
|
|
||||||
const (
|
|
||||||
SkippedNoSample = "no sample file"
|
|
||||||
SkippedGenerated = "a generated sound"
|
|
||||||
SkippedByCC = "started by a controller"
|
|
||||||
SkippedNotKeys = "not played by keys"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Region defaults that SFZ gives.
|
|
||||||
const (
|
|
||||||
defaultRootKey = 60
|
|
||||||
// defaultLowVelocity is 1, as velocity 0 is a note-off.
|
|
||||||
defaultLowVelocity = 1
|
|
||||||
// defaultRelease is in seconds.
|
|
||||||
defaultRelease = 0.001
|
|
||||||
defaultTrigger = "attack"
|
|
||||||
// defaultOffMode is how a region stops when its group's voice limit or
|
|
||||||
// another group stops it, and defaultOffTime how long that takes when the
|
|
||||||
// mode is time, in seconds.
|
|
||||||
defaultOffMode = "fast"
|
|
||||||
defaultOffTime = 0.006
|
|
||||||
)
|
|
||||||
|
|
||||||
// Controllers above 127 that ARIA defines, which depend on the note: its
|
|
||||||
// velocity, its key, and whether it is held.
|
|
||||||
const (
|
|
||||||
extendedVelocityCC = 131
|
|
||||||
extendedKeyCC = 133
|
|
||||||
extendedGateCC = 134
|
|
||||||
)
|
|
||||||
|
|
||||||
// Instrument reads the flattened instrument into the typed model.
|
// Instrument reads the flattened instrument into the typed model.
|
||||||
func (f *Flat) Instrument() *Instrument {
|
func (f *Flat) Instrument() *Instrument {
|
||||||
inst := &Instrument{Unread: map[string]int{}, Skipped: map[string]int{}}
|
inst := &Instrument{Unread: map[string]int{}, Skipped: map[string]int{}}
|
||||||
@@ -287,156 +39,6 @@ func (f *Flat) Instrument() *Instrument {
|
|||||||
return inst
|
return inst
|
||||||
}
|
}
|
||||||
|
|
||||||
// maxControls is the most controllers an instrument has, one for each.
|
|
||||||
const maxControls = 128
|
|
||||||
|
|
||||||
// controls returns the controllers the instrument names with label_ccN, sets
|
|
||||||
// with set_ccN or set_hdccN, or has regions that use, by number, each with its
|
|
||||||
// starting value.
|
|
||||||
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
|
|
||||||
// at the start, or nil if no region uses a keyswitch. Without sw_lokey and
|
|
||||||
// sw_hikey, they are the keys that the regions name.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// ccRE reads a controller number from the end of an opcode, such as the 64 of
|
|
||||||
// locc64.
|
|
||||||
var ccRE = regexp.MustCompile(`^(.*?)(\d+)$`)
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
var defaultControllers = map[int]float64{7: 100.0 / 127, 10: 0.5, 11: 1}
|
|
||||||
|
|
||||||
// controllerValues returns the starting values of the controllers, from 0 to
|
|
||||||
// 1: those that set_cc and set_hdcc give, then defaultControllers, then 0.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// keyShift is how far the <control> header's note_offset and octave_offset
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// evaluator turns regions' Opcodes into regions.
|
// evaluator turns regions' Opcodes into regions.
|
||||||
type evaluator struct {
|
type evaluator struct {
|
||||||
cc map[int]float64
|
cc map[int]float64
|
||||||
@@ -708,102 +310,6 @@ func voiceLimit(v string) *int {
|
|||||||
// minVolume is the quietest a region plays, in dB, which stands for silence.
|
// minVolume is the quietest a region plays, in dB, which stands for silence.
|
||||||
const minVolume = -144
|
const minVolume = -144
|
||||||
|
|
||||||
// hicc's high end includes the whole of value N, as a controller at 0.5 is
|
|
||||||
// 63.5 out of 127 and so within hicc63, as sfizz compares them. So the range
|
|
||||||
// is made to end just below value N + 1, unless N is the top.
|
|
||||||
const topEpsilon = 1e-9
|
|
||||||
|
|
||||||
// controllerBounds are the Opcodes that limit a source that isn't a numbered
|
|
||||||
// controller to a range: the lowest and highest, the range they run over, and
|
|
||||||
// what they default to.
|
|
||||||
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},
|
|
||||||
}
|
|
||||||
|
|
||||||
// conditions returns the ranges a region's locc, hicc, lohdcc and hihdcc, and
|
|
||||||
// its channel, pitch bend and aftertouch Opcodes limit it to. Controllers
|
|
||||||
// above 127, which belong to the note, are left out.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// keyswitch returns the keyswitches a region needs, or nil if it has none.
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// number returns the number an opcode sets, or nil if it doesn't set one.
|
// number returns the number an opcode sets, or nil if it doesn't set one.
|
||||||
func number(ops Opcodes, name string) *float64 {
|
func number(ops Opcodes, name string) *float64 {
|
||||||
if v, ok := parseNumber(ops[name]); ok {
|
if v, ok := parseNumber(ops[name]); ok {
|
||||||
@@ -814,163 +320,6 @@ func number(ops Opcodes, name string) *float64 {
|
|||||||
|
|
||||||
func ptr[T any](v T) *T { return &v }
|
func ptr[T any](v T) *T { return &v }
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// samplePath joins a region's default_path and sample into a clean relative
|
// samplePath joins a region's default_path and sample into a clean relative
|
||||||
// path with forward slashes.
|
// path with forward slashes.
|
||||||
func samplePath(defaultPath, sample string) string {
|
func samplePath(defaultPath, sample string) string {
|
||||||
|
|||||||
@@ -0,0 +1,240 @@
|
|||||||
|
package sfz
|
||||||
|
|
||||||
|
// Envelope is a region's amplitude envelope. Times are in seconds.
|
||||||
|
type Envelope struct {
|
||||||
|
Delay float64
|
||||||
|
Attack float64
|
||||||
|
Hold float64
|
||||||
|
Decay float64
|
||||||
|
// Sustain is the level held after the decay, as a percentage.
|
||||||
|
Sustain float64
|
||||||
|
Release float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurvePoint is how much of its level a velocity plays, from 0 to 1.
|
||||||
|
type CurvePoint struct {
|
||||||
|
Velocity int
|
||||||
|
Gain float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// The curves of a crossfade.
|
||||||
|
const (
|
||||||
|
CrossfadePower = "power"
|
||||||
|
CrossfadeGain = "gain"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Crossfade fades a region in over one range of keys or velocities and out
|
||||||
|
// over another. It plays at no level below the start of its fade in and above
|
||||||
|
// the end of its fade out. A range whose ends are equal doesn't fade.
|
||||||
|
type Crossfade struct {
|
||||||
|
FadeInLow, FadeInHigh int
|
||||||
|
FadeOutLow, FadeOutHigh int
|
||||||
|
// Curve is power, which keeps the loudness steady through a crossfade
|
||||||
|
// between regions, or gain, which fades the level in a straight line.
|
||||||
|
Curve string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where a condition's value comes from.
|
||||||
|
const (
|
||||||
|
SourceController = "cc"
|
||||||
|
SourceBend = "bend"
|
||||||
|
SourceAftertouch = "aftertouch"
|
||||||
|
SourcePolyAftertouch = "polyAftertouch"
|
||||||
|
SourceChannel = "channel"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Condition limits when a region plays to a source being within a range, which
|
||||||
|
// includes both ends. A controller, aftertouch and polyphonic aftertouch are
|
||||||
|
// from 0 to 1, pitch bend from -1 to 1 and a MIDI channel from 1 to 16.
|
||||||
|
type Condition struct {
|
||||||
|
Source string
|
||||||
|
Controller int
|
||||||
|
Low, High float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keyswitch limits when a region plays to the keyswitches that have been
|
||||||
|
// pressed: the last one, the one before it, and keys that are held or aren't.
|
||||||
|
// A field that isn't set is nil.
|
||||||
|
type Keyswitch struct {
|
||||||
|
Last, Previous, Down, Up *int
|
||||||
|
}
|
||||||
|
|
||||||
|
// The settings a controller moves, as a modulation names them.
|
||||||
|
const (
|
||||||
|
TargetAmplitude = "amplitude"
|
||||||
|
TargetVolume = "volume"
|
||||||
|
TargetPan = "pan"
|
||||||
|
TargetWidth = "width"
|
||||||
|
TargetTune = "tune"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CurveStop is a point of a custom curve: the controller at one of the values
|
||||||
|
// 0 to 127, and the result there.
|
||||||
|
type CurveStop struct {
|
||||||
|
At int
|
||||||
|
Value float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modulation moves a region's setting as a controller does. The controller
|
||||||
|
// goes through the curve, and its result, from 0 to 1 or beyond, times Depth
|
||||||
|
// is added to the setting, or multiplies it for amplitude. Depth is in the
|
||||||
|
// setting's own units: percent for amplitude, pan and width, dB for volume,
|
||||||
|
// and cents for tune.
|
||||||
|
type Modulation struct {
|
||||||
|
Target string
|
||||||
|
Controller int
|
||||||
|
Depth float64
|
||||||
|
// Curve is the index of one of SFZ's built-in curves, 0 to 6, when Points
|
||||||
|
// is empty.
|
||||||
|
Curve int
|
||||||
|
Points []CurveStop
|
||||||
|
}
|
||||||
|
|
||||||
|
// Control is a controller an instrument uses, with the name its SFZ gives it,
|
||||||
|
// and its starting value from 0 to 1.
|
||||||
|
type Control struct {
|
||||||
|
Controller int
|
||||||
|
Label string
|
||||||
|
Value float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyswitchRange is the keys that are keyswitches, and the one that is down at
|
||||||
|
// the start, or nil if the instrument has no keyswitch.
|
||||||
|
type KeyswitchRange struct {
|
||||||
|
Low, High int
|
||||||
|
Default *int
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultEnvelope is the envelope of a region that sets none: it plays at full
|
||||||
|
// level at once, and stops almost at once when its note ends.
|
||||||
|
var DefaultEnvelope = Envelope{Sustain: 100, Release: defaultRelease}
|
||||||
|
|
||||||
|
// Region is a sample and the notes and velocities that play it, with its
|
||||||
|
// settings at the instrument's starting controller values.
|
||||||
|
type Region struct {
|
||||||
|
// Sample is the sample's path, relative to the folder of the instrument's
|
||||||
|
// main file, with forward slashes.
|
||||||
|
Sample string
|
||||||
|
LowKey int
|
||||||
|
HighKey int
|
||||||
|
RootKey int
|
||||||
|
// RootFromSample says the root note is the one the sample file holds
|
||||||
|
// (pitch_keycenter=sample), which RootKey doesn't know.
|
||||||
|
RootFromSample bool
|
||||||
|
LowVelocity int
|
||||||
|
HighVelocity int
|
||||||
|
// KeyTrack is how far each key repitches the sample, in cents: 100 plays
|
||||||
|
// the keys' pitches, and 0 plays every key at the sample's own.
|
||||||
|
KeyTrack float64
|
||||||
|
// Tune is in cents.
|
||||||
|
Tune float64
|
||||||
|
// Volume is in dB.
|
||||||
|
Volume float64
|
||||||
|
// Pan is from -100, left, to 100, right.
|
||||||
|
Pan float64
|
||||||
|
// Offset is where the sample starts playing, in frames.
|
||||||
|
Offset int64
|
||||||
|
// VelocityTracking is how much velocity sets the level, as a percentage:
|
||||||
|
// 100 follows it fully, and 0 plays every velocity at full level.
|
||||||
|
VelocityTracking float64
|
||||||
|
// VelocityCurve is how much of its level each velocity plays, through
|
||||||
|
// straight lines between its points, or nil if the region sets none. Its
|
||||||
|
// points ascend by velocity, from 0 to 127.
|
||||||
|
VelocityCurve []CurvePoint
|
||||||
|
// KeyCrossfade and VelocityCrossfade are nil if the region sets none.
|
||||||
|
KeyCrossfade *Crossfade
|
||||||
|
VelocityCrossfade *Crossfade
|
||||||
|
Envelope Envelope
|
||||||
|
// Trigger is what starts the region: attack, when a note starts; release,
|
||||||
|
// when it ends; first; or legato.
|
||||||
|
Trigger string
|
||||||
|
// LoopMode is no_loop, one_shot, loop_continuous or loop_sustain, or
|
||||||
|
// empty to follow the sample file.
|
||||||
|
LoopMode string
|
||||||
|
// LoopStart and LoopEnd are frames, or nil to follow the sample file.
|
||||||
|
LoopStart *int64
|
||||||
|
LoopEnd *int64
|
||||||
|
// ReleaseDecay is how much quieter a release region plays for each second
|
||||||
|
// its note was held, in dB.
|
||||||
|
ReleaseDecay float64
|
||||||
|
// SequenceLength and SequencePosition make the region one of a round
|
||||||
|
// robin: of each SequenceLength notes it matches, it plays the one at
|
||||||
|
// SequencePosition, counting from 1.
|
||||||
|
SequenceLength int
|
||||||
|
SequencePosition int
|
||||||
|
// LowRandom and HighRandom make the region a random variant: it plays when
|
||||||
|
// a number drawn for the note, from 0 to 1, is at least LowRandom and less
|
||||||
|
// than HighRandom.
|
||||||
|
LowRandom float64
|
||||||
|
HighRandom float64
|
||||||
|
// Group is the region's choke group, which other regions' OffBy names.
|
||||||
|
Group int
|
||||||
|
// OffBy is the group whose notes stop this region while it plays, or nil
|
||||||
|
// if none does.
|
||||||
|
OffBy *int
|
||||||
|
// Polyphony is how many voices the region's group may have playing at
|
||||||
|
// once, or nil if it sets none. A note past it stops the oldest voice.
|
||||||
|
Polyphony *int
|
||||||
|
// Conditions and Keyswitch decide, from the controllers and keyswitches
|
||||||
|
// when a note starts, whether the region plays. The region plays if every
|
||||||
|
// condition holds and its keyswitch does.
|
||||||
|
Conditions []Condition
|
||||||
|
Keyswitch *Keyswitch
|
||||||
|
// Modulations are the controllers that move the region's settings, which
|
||||||
|
// hold their values with no controller.
|
||||||
|
Modulations []Modulation
|
||||||
|
// Width is the stereo width in percent, or nil for the sample's own. BendUp
|
||||||
|
// and BendDown are how far pitch bend moves the pitch at its ends in cents,
|
||||||
|
// and BendStep the size of its steps, or nil for the SFZ defaults.
|
||||||
|
Width *float64
|
||||||
|
BendUp *float64
|
||||||
|
BendDown *float64
|
||||||
|
BendStep *float64
|
||||||
|
// NotePolyphony is how many voices of the region's group may play the
|
||||||
|
// same key at once, or nil if it sets none.
|
||||||
|
NotePolyphony *int
|
||||||
|
// OffMode is how the region stops when it is stopped by a group or past a
|
||||||
|
// voice limit: fast, time or normal.
|
||||||
|
OffMode string
|
||||||
|
// OffTime is how long a region whose OffMode is time takes to stop, in
|
||||||
|
// seconds.
|
||||||
|
OffTime float64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instrument is an SFZ instrument as typed regions, with its settings taken at
|
||||||
|
// the starting controller values.
|
||||||
|
type Instrument struct {
|
||||||
|
Regions []Region
|
||||||
|
// Unread counts, for each opcode or header the typed model has no field
|
||||||
|
// for, the regions that have it, or how often a header appears.
|
||||||
|
Unread map[string]int
|
||||||
|
// Skipped counts the regions that have no typed region, by why.
|
||||||
|
Skipped map[string]int
|
||||||
|
// Controls are the controllers that the instrument names, sets or uses.
|
||||||
|
Controls []Control
|
||||||
|
// Keyswitches are its keyswitch keys, or nil if it has none.
|
||||||
|
Keyswitches *KeyswitchRange
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reasons a region is skipped: the typed model has no region for it.
|
||||||
|
const (
|
||||||
|
SkippedNoSample = "no sample file"
|
||||||
|
SkippedGenerated = "a generated sound"
|
||||||
|
SkippedByCC = "started by a controller"
|
||||||
|
SkippedNotKeys = "not played by keys"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Region defaults that SFZ gives.
|
||||||
|
const (
|
||||||
|
defaultRootKey = 60
|
||||||
|
// defaultLowVelocity is 1, as velocity 0 is a note-off.
|
||||||
|
defaultLowVelocity = 1
|
||||||
|
// defaultRelease is in seconds.
|
||||||
|
defaultRelease = 0.001
|
||||||
|
defaultTrigger = "attack"
|
||||||
|
// defaultOffMode is how a region stops when its group's voice limit or
|
||||||
|
// another group stops it, and defaultOffTime how long that takes when the
|
||||||
|
// mode is time, in seconds.
|
||||||
|
defaultOffMode = "fast"
|
||||||
|
defaultOffTime = 0.006
|
||||||
|
)
|
||||||
+166
@@ -0,0 +1,166 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user