SPB Git

Repository Public

instruments.md 8.7 KB

# Instruments

Every track is played by an instrument. An instrument is an engine — a synthesis method implemented in the runtime — with values for its parameters. Sonata has 16 engines and the standard library names 83 instruments built on them.

# Standard instruments

Use one by naming it as the type of a track:

sonata
composition "A Small Ensemble" {
    tempo 84bpm
    instrument flute: Flute {
        play A5 duration 1/2
        play G5 duration 1/2
    }
    instrument guitar: NylonGuitar {
        arpeggio(chord(A2, minor) + chord(A3, minor), step: 1/8)
        arpeggio(chord(G2, major) + chord(G3, major), step: 1/8)
    }
    instrument bass: DoubleBass {
        play A1 duration 1/1
    }
}

The families: pianos, plucked strings, bowed strings, flutes, clarinets, brass, organs, basses, synthesizers, FM instruments, drums and cymbals, mallets and bells, and voices. The complete list with the sound of each is in Standard instruments, and the Library space lets you browse and hear them.

# What the engines are

Engine Method
piano modal synthesis of inharmonic strings struck by a hammer, with a soundboard
plucked_string waveguide string (extended Karplus–Strong) with a body resonator
bowed_string waveguide string driven by a bow-friction model
flute, clarinet, brass waveguide tubes with a jet, reed or lip excitation
organ additive synthesis with nine drawbars
bass electric (waveguide string, pickup) or synthesizer bass
synth subtractive: two oscillators, filter, envelopes, LFO
fm four-operator frequency modulation
kick, snare, hihat drum synthesis
modal_percussion banks of damped modes: bars, bells, tubes, glass
voice formant synthesis of sung vowels
patch a signal graph you write yourself

These are models: approximations chosen to be musically useful, not exact simulations of physical instruments. Each engine states its method and its limits in the engine reference and in Documentation/acoustics/. In particular, the flute and brass engines are driven at the pitch of the note rather than finding it by themselves, so they do not overblow.

# Defining an instrument

An instrument definition chooses an engine with model and sets parameters. It is written at the top level of a file.

sonata
instrument WarmKeys {
    model fm
    preset epiano
    feedback 0.1
    envelope adsr(attack: 4ms, decay: 1.2s, sustain: 0.3, release: 400ms)
}

composition "A Defined Instrument" {
    instrument keys: WarmKeys {
        chord [C3, G3, E4, B4] duration 1/1
    }
}
  • model m selects the engine.
  • preset p starts from one of the engine's presets.
  • name value sets a parameter. Lines apply in order, so a parameter written after preset overrides the preset.
  • Parameters with a dot in their name can be written directly — envelope.attack 4ms — or grouped as a slot: envelope adsr(attack: 4ms, …) chooses the kind adsr and sets its members.

Every name, unit and range is checked against the engine's parameter table. A time needs a unit, a ratio must be between 0 and 1, an unknown parameter is refused with a suggestion:

sonata,error=S2023
instrument Wrong {
    model plucked_string
    dampening 0.02
}

composition "Unknown Parameter" {
    instrument a: Wrong {
        play C4
    }
}

# Deriving an instrument

instrument B: A { } starts from another instrument and changes some parameters. The engine cannot be changed by a derived instrument.

sonata
instrument DarkGrand: GrandPiano {
    brightness 0.2
    hammer.hardness 0.3
}

instrument SlowStrings: ConcertStrings {
    envelope adsr(attack: 600ms, decay: 200ms, sustain: 0.9, release: 1.5s)
    vibrato.depth 10cents
}

composition "Derived Instruments" {
    tempo 60bpm
    instrument piano: DarkGrand {
        chord [D2, A2, F3] duration 1/1
    }
    instrument strings: SlowStrings {
        gain -6dB
        chord [D4, F4, A4] duration 1/1
    }
}

# A physical-model instrument

The plucked-string engine exposes the parts of its model: where the string is plucked, how much it is damped, and a body made of resonant modes.

sonata
instrument CrystalHarp {
    model plucked_string
    damping 0.015
    excitation pluck(position: 0.35)
    resonance modal(frequencies: [440Hz, 880Hz, 1320Hz])
    envelope adsr(attack: 5ms, decay: 300ms, sustain: 0.6, release: 2s)
}

composition "Crystal Harp" {
    tempo 72bpm
    instrument harp: CrystalHarp {
        for n in [A3, E4, A4, C#5, E5] {
            play n duration 1/4
        }
    }
}

The pluck position works as on a real string: plucking at a fraction p of the length removes the harmonics that have a node there (every third harmonic at p = 1/3). For an ideal string the fundamental is f = (1 / 2L) · √(T / μ); the engine does not ask for length, tension and mass, it tunes its delay line to the frequency of the note and lets you shape the losses and the excitation.

A mallet instrument can be given its own set of modes:

sonata
instrument Plate {
    model modal_percussion
    // Mode frequencies as ratios of the fundamental.
    partials [1, 2.3, 3.9, 5.6, 7.7]
    decay 1.8
    hardness 0.7
}

composition "Custom Modes" {
    instrument plate: Plate {
        play C4 duration 1/2
        play G4 duration 1/2
    }
}

# Patch graphs

With model patch you build the instrument yourself as a signal graph. The graph describes one voice; the runtime plays as many voices as there are simultaneous notes.

sonata
instrument SoftSaw {
    model patch
    graph {
        node osc = saw(freq: note.freq) + saw(freq: note.freq * 1.006) * 0.7
        node env = adsr(attack: 20ms, decay: 300ms, sustain: 0.7, release: 500ms)
        node tone = lowpass(in: osc, cutoff: 600Hz + env * 2500, q: 1.2)
        out tone * env * note.velocity * 0.4
    }
}

composition "A Patch" {
    tempo 90bpm
    instrument pad: SoftSaw {
        chord [C3, G3, E4] duration 1/1
    }
}
  • node name = expression names a signal. out expression is the output of the voice; a graph has exactly one.
  • A call such as saw(…) or lowpass(…) creates a node. Ports are given by name; a port left out takes its default. All node kinds and their ports are in Patch nodes.
  • note.freq, note.pitch, note.velocity, note.gate and note.time are the inputs of the note being played.
  • +, - and * between signals mix and modulate them. Next to a signal, a value with a unit is its number in the base unit: hertz, seconds, semitones, decibels.
  • The envelope multiplied into the output decides when a voice ends: when it reaches zero after the note is released.

A node can only read nodes defined above it, with one exception that makes feedback possible: the in port of a delay may read a node defined later.

sonata
instrument Pluck {
    model patch
    release 1.5s
    graph {
        node burst = noise() * adsr(attack: 0ms, decay: 4ms, sustain: 0, release: 4ms)
        // A string tuned to A3: a delay of one period of 220 Hz (4.545 ms), fed back
        // through a gentle low-pass.
        node string = delay(in: looped, time: 4.545ms)
        node looped = burst + lowpass(in: string, cutoff: 3000Hz) * 0.985
        out looped * adsr(attack: 0ms, decay: 1.5s, sustain: 0, release: 300ms)
    }
}

composition "Feedback Through a Delay" {
    instrument pluck: Pluck {
        play A3 duration 1/1
    }
}

A loop that does not pass through a delay has no meaning — a sample cannot depend on itself — and is refused:

sonata,error=G5002
instrument Loop {
    model patch
    graph {
        node a = lowpass(in: b, cutoff: 500Hz)
        node b = a * 0.5
        out b
    }
}

composition "Not Causal" {
    instrument x: Loop {
        play C4
    }
}

The example Examples/patch_drum_machine.sonata builds a kick, a snare and a hi-hat this way, and Examples/subtractive_patch.sonata a filtered lead and a modal bell.

# Parameters that move

Parameters marked automatable in the reference can change during the piece, with automate instrument.NAME:

sonata
composition "A Moving Filter" {
    tempo 120bpm
    instrument pad: SynthPad {
        gain -8dB
        automate instrument.filter.cutoff {
            0bars: 300Hz
            2bars: 5000Hz exp
        }
        chord [D3, A3, F4] duration 2bars
    }
}

More in Mixing. The Instrument Lab space (Instrument Lab) edits these parameters with sliders, plays the result from a keyboard and writes the definition for you.

Files main
Menu

Theme