2026-12-01 18:19:11 +00:00
package sfz
// Envelope is a region's amplitude envelope. Times are in seconds.
type Envelope struct {
2026-12-01 18:31:40 +00:00
Delay float64 `json:"delay" doc:"How long the region waits before its attack starts, in seconds." minimum:"0"`
Attack float64 `json:"attack" doc:"How long the level takes to rise to full, in seconds." minimum:"0"`
Hold float64 `json:"hold" doc:"How long the level stays at full before it decays, in seconds." minimum:"0"`
Decay float64 `json:"decay" doc:"How long the level takes to fall to the sustain level, in seconds." minimum:"0"`
2026-12-01 18:19:11 +00:00
// Sustain is the level held after the decay, as a percentage.
2026-12-01 18:31:40 +00:00
Sustain float64 `json:"sustain" doc:"The level held after the decay while the note is held, as a percentage." minimum:"0" maximum:"100"`
Release float64 `json:"release" doc:"How long the level takes to fall to silence once the note ends, in seconds." minimum:"0"`
2026-12-01 18:19:11 +00:00
}
// CurvePoint is how much of its level a velocity plays, from 0 to 1.
type CurvePoint struct {
2026-12-01 18:31:40 +00:00
Velocity int `json:"velocity" doc:"The MIDI velocity the point is at." minimum:"0" maximum:"127"`
Gain float64 `json:"gain" doc:"How much of its level the velocity plays." minimum:"0" maximum:"1"`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
FadeInLow int `json:"fadeInLow" doc:"The key or velocity below which the region doesn't play." minimum:"0" maximum:"127"`
FadeInHigh int `json:"fadeInHigh" doc:"The key or velocity at which the region reaches full level." minimum:"0" maximum:"127"`
FadeOutLow int `json:"fadeOutLow" doc:"The key or velocity at which the region starts to fade out." minimum:"0" maximum:"127"`
FadeOutHigh int `json:"fadeOutHigh" doc:"The key or velocity above which the region doesn't play." minimum:"0" maximum:"127"`
2026-12-01 18:19:11 +00:00
// Curve is power, which keeps the loudness steady through a crossfade
// between regions, or gain, which fades the level in a straight line.
2026-12-01 18:31:40 +00:00
Curve string `json:"curve" doc:"How the level changes through the fade: power, which keeps the loudness steady through a crossfade between regions, or gain, which fades the level in a straight line."`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
Source string `json:"source" doc:"Where the value comes from: cc, bend, aftertouch, polyAftertouch or channel."`
Controller int `json:"controller" doc:"The controller number, when the source is cc." minimum:"0" maximum:"127"`
Low float64 `json:"low" doc:"The lowest value the region plays at, in the source's range: 0 to 1 for a controller, aftertouch and polyphonic aftertouch, -1 to 1 for pitch bend and 1 to 16 for a channel."`
High float64 `json:"high" doc:"The highest value the region plays at, in the source's range."`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
Last * int `json:"last,omitempty" doc:"The key of the keyswitch pressed last, or nil if the region doesn't depend on it."`
Previous * int `json:"previous,omitempty" doc:"The key of the keyswitch pressed before the last, or nil if the region doesn't depend on it."`
Down * int `json:"down,omitempty" doc:"A keyswitch key that must be held, or nil if the region doesn't depend on it."`
Up * int `json:"up,omitempty" doc:"A keyswitch key that must not be held, or nil if the region doesn't depend on it."`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
At int `json:"at" doc:"The controller value the point is at." minimum:"0" maximum:"127"`
Value float64 `json:"value" doc:"The result of the curve at that value."`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
Target string `json:"target" doc:"The setting the controller moves: amplitude, volume, pan, width or tune."`
Controller int `json:"controller" doc:"The controller number." minimum:"0" maximum:"127"`
Depth float64 `json:"depth" doc:"How far the controller at 1 moves the setting, in the setting's own units: percent for amplitude, pan and width, dB for volume and cents for tune."`
2026-12-01 18:19:11 +00:00
// Curve is the index of one of SFZ's built-in curves, 0 to 6, when Points
// is empty.
2026-12-01 18:31:40 +00:00
Curve int `json:"curve,omitempty" doc:"The index of the curve the controller goes through: one of SFZ's built-in curves, 0 to 6, or that of a custom curve whose points are given." minimum:"0"`
Points [] CurveStop `json:"points,omitempty" doc:"The points of a custom curve, which pass through straight lines, or none for a built-in curve."`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
Controller int `json:"controller" doc:"The controller number." minimum:"0" maximum:"127"`
Label string `json:"label,omitempty" doc:"The name the instrument gives the controller."`
Value float64 `json:"value" doc:"The controller's starting value." minimum:"0" maximum:"1"`
2026-12-01 18:19:11 +00:00
}
// 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 {
2026-12-01 18:31:40 +00:00
Low int `json:"low" doc:"The lowest key that is a keyswitch." minimum:"0" maximum:"127"`
High int `json:"high" doc:"The highest key that is a keyswitch." minimum:"0" maximum:"127"`
Default * int `json:"default,omitempty" doc:"The keyswitch that is active before any is pressed, or nil if there is none."`
2026-12-01 18:19:11 +00:00
}
// 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.
2026-12-01 18:31:40 +00:00
Sample string `json:"sample" doc:"The sample's path, relative to the folder of the instrument's main file, with forward slashes."`
LowKey int `json:"lowKey" doc:"The lowest key that plays the region." minimum:"0" maximum:"127"`
HighKey int `json:"highKey" doc:"The highest key that plays the region." minimum:"0" maximum:"127"`
RootKey int `json:"rootKey" doc:"The key at which the sample plays at its own pitch." minimum:"0" maximum:"127"`
2026-12-01 18:19:11 +00:00
// RootFromSample says the root note is the one the sample file holds
// (pitch_keycenter=sample), which RootKey doesn't know.
2026-12-01 18:31:40 +00:00
RootFromSample bool `json:"rootFromSample,omitempty" doc:"Whether the root note is the one the sample file holds, which RootKey doesn't know."`
LowVelocity int `json:"lowVelocity" doc:"The lowest velocity that plays the region." minimum:"0" maximum:"127"`
HighVelocity int `json:"highVelocity" doc:"The highest velocity that plays the region." minimum:"0" maximum:"127"`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
KeyTrack float64 `json:"keyTrack" doc:"How far each key repitches the sample, in cents: 100 plays the keys' pitches, and 0 plays every key at the sample's own."`
2026-12-01 18:19:11 +00:00
// Tune is in cents.
2026-12-01 18:31:40 +00:00
Tune float64 `json:"tune" doc:"How far the sample is repitched, in cents."`
2026-12-01 18:19:11 +00:00
// Volume is in dB.
2026-12-01 18:31:40 +00:00
Volume float64 `json:"volume" doc:"The region's level, in dB." minimum:"-144"`
2026-12-01 18:19:11 +00:00
// Pan is from -100, left, to 100, right.
2026-12-01 18:31:40 +00:00
Pan float64 `json:"pan" doc:"Where the region sits in the stereo field, from -100, left, to 100, right." minimum:"-100" maximum:"100"`
2026-12-01 18:19:11 +00:00
// Offset is where the sample starts playing, in frames.
2026-12-01 18:31:40 +00:00
Offset int64 `json:"offset,omitempty" doc:"Where the sample starts playing, in frames." minimum:"0"`
2026-12-01 18:19:11 +00:00
// VelocityTracking is how much velocity sets the level, as a percentage:
// 100 follows it fully, and 0 plays every velocity at full level.
2026-12-01 18:31:40 +00:00
VelocityTracking float64 `json:"velocityTracking" doc:"How much velocity sets the level, as a percentage: 100 follows it fully, and 0 plays every velocity at full level." minimum:"-100" maximum:"100"`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
VelocityCurve [] CurvePoint `json:"velocityCurve,omitempty" doc:"How much of its level each velocity plays, through straight lines between its points, which ascend by velocity from 0 to 127, or none if the region sets none."`
2026-12-01 18:19:11 +00:00
// KeyCrossfade and VelocityCrossfade are nil if the region sets none.
2026-12-01 18:31:40 +00:00
KeyCrossfade * Crossfade `json:"keyCrossfade,omitempty" doc:"The crossfade over keys, or nil if the region sets none."`
VelocityCrossfade * Crossfade `json:"velocityCrossfade,omitempty" doc:"The crossfade over velocities, or nil if the region sets none."`
Envelope Envelope `json:"envelope" doc:"The region's amplitude envelope."`
2026-12-01 18:19:11 +00:00
// Trigger is what starts the region: attack, when a note starts; release,
// when it ends; first; or legato.
2026-12-01 18:31:40 +00:00
Trigger string `json:"trigger" doc:"What starts the region: attack, when a note starts; release, when it ends; first; or legato."`
2026-12-01 18:19:11 +00:00
// LoopMode is no_loop, one_shot, loop_continuous or loop_sustain, or
// empty to follow the sample file.
2026-12-01 18:31:40 +00:00
LoopMode string `json:"loopMode,omitempty" doc:"How the sample loops: no_loop, one_shot, loop_continuous or loop_sustain, or empty to follow the sample file."`
2026-12-01 18:19:11 +00:00
// LoopStart and LoopEnd are frames, or nil to follow the sample file.
2026-12-01 18:31:40 +00:00
LoopStart * int64 `json:"loopStart,omitempty" doc:"The frame the loop starts at, or nil to follow the sample file." minimum:"0"`
LoopEnd * int64 `json:"loopEnd,omitempty" doc:"The frame the loop ends at, or nil to follow the sample file." minimum:"0"`
2026-12-01 18:19:11 +00:00
// ReleaseDecay is how much quieter a release region plays for each second
// its note was held, in dB.
2026-12-01 18:31:40 +00:00
ReleaseDecay float64 `json:"releaseDecay,omitempty" doc:"How much quieter a release region plays for each second its note was held, in dB." minimum:"0"`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
SequenceLength int `json:"sequenceLength" doc:"How many notes make a round robin: of each SequenceLength notes the region matches, it plays the one at SequencePosition." minimum:"1"`
SequencePosition int `json:"sequencePosition" doc:"Which of the notes of a round robin plays the region, counting from 1." minimum:"1"`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
LowRandom float64 `json:"lowRandom,omitempty" doc:"The lowest number, from 0 to 1, drawn for a note that plays the region." minimum:"0" maximum:"1"`
HighRandom float64 `json:"highRandom" doc:"The number, from 0 to 1, that the one drawn for a note must be below for the region to play." minimum:"0" maximum:"1"`
2026-12-01 18:19:11 +00:00
// Group is the region's choke group, which other regions' OffBy names.
2026-12-01 18:31:40 +00:00
Group int `json:"group,omitempty" doc:"The region's choke group, which other regions' OffBy names."`
2026-12-01 18:19:11 +00:00
// OffBy is the group whose notes stop this region while it plays, or nil
// if none does.
2026-12-01 18:31:40 +00:00
OffBy * int `json:"offBy,omitempty" doc:"The group whose notes stop this region while it plays, or nil if none does."`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
Polyphony * int `json:"polyphony,omitempty" doc:"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." minimum:"1"`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
Conditions [] Condition `json:"conditions,omitempty" doc:"The ranges of controllers, pitch bend, aftertouch and channel that the region plays within, all of which must hold when a note starts."`
Keyswitch * Keyswitch `json:"keyswitch,omitempty" doc:"The keyswitches the region needs, or nil if it needs none."`
2026-12-01 18:19:11 +00:00
// Modulations are the controllers that move the region's settings, which
// hold their values with no controller.
2026-12-01 18:31:40 +00:00
Modulations [] Modulation `json:"modulations,omitempty" doc:"The controllers that move the region's settings while a note plays. The settings hold their values with no controller."`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
Width * float64 `json:"width,omitempty" doc:"The stereo width, in percent, or nil for the sample's own." minimum:"-100" maximum:"100"`
BendUp * float64 `json:"bendUp,omitempty" doc:"How far pitch bend moves the pitch up at its end, in cents, or nil for the SFZ default."`
BendDown * float64 `json:"bendDown,omitempty" doc:"How far pitch bend moves the pitch down at its end, in cents, or nil for the SFZ default."`
BendStep * float64 `json:"bendStep,omitempty" doc:"The size of the steps of pitch bend, in cents, or nil for the SFZ default."`
2026-12-01 18:19:11 +00:00
// NotePolyphony is how many voices of the region's group may play the
// same key at once, or nil if it sets none.
2026-12-01 18:31:40 +00:00
NotePolyphony * int `json:"notePolyphony,omitempty" doc:"How many voices of the region's group may play the same key at once, or nil if it sets none." minimum:"1"`
2026-12-01 18:19:11 +00:00
// OffMode is how the region stops when it is stopped by a group or past a
// voice limit: fast, time or normal.
2026-12-01 18:31:40 +00:00
OffMode string `json:"offMode" doc:"How the region stops when it is stopped by a group or past a voice limit: fast, time or normal."`
2026-12-01 18:19:11 +00:00
// OffTime is how long a region whose OffMode is time takes to stop, in
// seconds.
2026-12-01 18:31:40 +00:00
OffTime float64 `json:"offTime" doc:"How long a region whose OffMode is time takes to stop, in seconds." minimum:"0"`
2026-12-01 18:19:11 +00:00
}
// Instrument is an SFZ instrument as typed regions, with its settings taken at
// the starting controller values.
type Instrument struct {
2026-12-01 18:31:40 +00:00
Regions [] Region `json:"regions" doc:"The regions that the typed model has, in the order of the file."`
2026-12-01 18:19:11 +00:00
// 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.
2026-12-01 18:31:40 +00:00
Unread map [ string ] int `json:"unread,omitempty" doc:"For each opcode or header the typed model has no field for, how many regions have it, or how often a header appears."`
2026-12-01 18:19:11 +00:00
// Skipped counts the regions that have no typed region, by why.
2026-12-01 18:31:40 +00:00
Skipped map [ string ] int `json:"skipped,omitempty" doc:"How many regions have no typed region, by the reason."`
2026-12-01 18:19:11 +00:00
// Controls are the controllers that the instrument names, sets or uses.
2026-12-01 18:31:40 +00:00
Controls [] Control `json:"controls,omitempty" doc:"The controllers that the instrument names, sets or uses."`
2026-12-01 18:19:11 +00:00
// Keyswitches are its keyswitch keys, or nil if it has none.
2026-12-01 18:31:40 +00:00
Keyswitches * KeyswitchRange `json:"keyswitches,omitempty" doc:"The keys that are keyswitches, or nil if the instrument has none."`
2026-12-01 18:42:17 +00:00
// sources are the flattened regions that the typed ones were read from, in
// the same order, and eval reads a region from opcodes as they were.
sources [] FlatRegion
eval evaluator
2026-12-01 18:19:11 +00:00
}
// 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
)