Files
sfz/model.go
T
2026-12-01 18:19:11 +00:00

241 lines
8.1 KiB
Go

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
)