Skip to content

Python Instruments and Live MIDI API ​

Rendering an arrangement through the built-in synthesizer, a SoundFont or your own instrument, and playing instruments live through the realtime engine. Project is on Projects and the engine on Realtime Engine; this page holds the instrument tasks, the SynthPatch rules and the generated RealtimeEngine signatures for MIDI. The instrument records and string tokens are on Python Instrument Types.

Where each task lives ​

TaskAPIGuide
Render MIDI through the built-in synthesizerProject.bounce(synth_instruments=[ProjectInstrumentBinding(instrument=patch, destination_id=...)]), synth_preset_names(), synth_preset_patch(name), SynthPatchBuilt-in Synthesizer, Bouncing Projects
Render MIDI through a SoundFontProject.load_sound_font(data), Project.bounce(sf2_instruments=[ProjectSf2InstrumentBinding(instrument=config, destination_id=..., sound_font_uri=...)])SoundFont Player
Host your own instrument during a bounceProject.bounce(instruments=[ProjectInstrumentBinding(instrument=instrument, destination_id=...)]) with the ExternalInstrument protocol — a render(channels, num_frames) callback plus optional prepare/on_event hooks and latency_samples. Node and WASM take the same callback form in the instruments bounce option.Bouncing Projects
Play instruments live from MIDI events and replace destination MIDI FXRealtimeEngine.set_synth_instrument(...), RealtimeEngine.load_sound_font(...), RealtimeEngine.set_midi_fx(...), the engine's MIDI input queue (each push takes offset_in_block_samples, an offset into the next processed block rather than an absolute clock position), and push_midi_program(destination_id, group, channel, program, bank_valid=..., bank_msb=..., bank_lsb=...) / push_midi_input_program(...) (a program change with an optional bank in one call; the SoundFont player and a synth bound with GM programs follow the bank)MIDI Input
Send a destination to external MIDI hardwareset_midi_destination_external(...), set_external_midi_clock_enabled(...), drain_external_midi(...), external_midi_dropped_count()Project Editing: MIDI

Lifecycle ​

Create ​

Instruments are bound, not constructed on their own: pass synth_instruments, sf2_instruments, builtin_instruments or instruments to Project.bounce(...), or call set_synth_instrument, set_sf2_instrument, load_sound_font and set_midi_fx on a RealtimeEngine. A SynthPatch comes from synth_preset_patch(name) or is built by hand.

Use ​

The engine's MIDI input queue is the live path: each push takes offset_in_block_samples, an offset into the next processed block rather than an absolute clock position, and the control calls queue commands for the audio side. A bounce is offline.

Release ​

The objects that hold instruments are Project and RealtimeEngine; their close() releases the instruments (Releasing Native Handles).

Errors ​

An unknown preset name, a malformed patch, or a numeric SynthPatch field that is non-finite or outside the range synth_patch_param_info() publishes is refused by name as SonareValueError, not clamped. See Error Contract.

SynthPatch and presets ​

For synth preset introspection, synth_preset_patch(name) returns a named catalog preset as a SynthPatch (it raises SonareError for unknown names and accepts a 'va:' routing prefix) so you can inspect and tweak fields before binding it. synth_enum_tables() returns the runtime enum-name tables (dict[str, tuple[str, ...]]) for validating SynthModRouting source/destination names against the loaded build. Besides the engine mode, waveform, filter and modulation tables it carries sample_loop_modes, sample_key_tracks, retriggers, lfo_shapes, part_rig_modes and sample_desc_loop_modes.

Every numeric SynthPatch field defaults to None, meaning "keep the base preset's value". A field you never set therefore reads back as None, not 0.0, and any value you do supply is a real override — including an explicit 0, so SynthPatch(preset="warm-pad", amp_sustain=0) genuinely zeroes the amp sustain instead of being indistinguishable from leaving it unset. A patch obtained from synth_preset_patch(name) comes back fully populated with the preset's concrete values, so its numeric fields are never None. Enum fields (engine_mode, waveform, filter_model, filter_output, body, retrigger, lfo_shape) keep 0 / "default" as their keep-the-base value. mod_routings=None keeps the base mod matrix, an empty tuple clears it, and a non-empty tuple replaces it. lfo_shape is "triangle", "sine" or "random" (a smoothed sample-and-hold seeded per voice) and applies to the vibrato and tremolo LFOs at every destination; filter_model="svf4Pole" is an unsaturated 24 dB/octave lowpass. A numeric field that is non-finite or outside the range synth_patch_param_info() publishes is refused by name, not clamped.

Catalog and descriptor functions ​

Module-level functions that read the loaded build's synth catalogs, so a host does not hardcode names or ranges. synth_preset_names() lists the catalog preset names, synth_preset_patch(name) fetches one as a SynthPatch, and synth_enum_tables() returns the enum-name tables. synth_patch_param_info() describes every numeric SynthPatch field (name, accepted range, default), and synth_engine_param_info(engine_mode) describes the keys of one engine mode's engine_params: min / max appear only for a field with a closed range, a value outside it is refused rather than clamped, and the "default" and "sample" modes have no engine section and give an empty list. controller_profile_names() lists the names RealtimeEngine.set_controller_profile accepts.

python
# NativeSynth preset catalog names (``"sine"``, ``"sawLead"``, ``"ePiano"``, ``"drumKit"``, ...).
def synth_preset_names() -> list[str]: ...

# Fetch the named catalog preset as a :class:`SynthPatch` (the preset name plus the wrapper-section values), so hosts can inspect a preset and tweak fields before binding it.
def synth_preset_patch(name: str) -> SynthPatch: ...

# Canonical NativeSynth enum-name tables supplied by the C ABI.
def synth_enum_tables() -> dict[str, tuple[str, ...]]: ...

# Describe every numeric, boolean, and enum :class:`SynthPatch` wrapper field.
def synth_patch_param_info() -> list[SynthPatchParamInfo]: ...

# Describe every :attr:`SynthPatch.engine_params` key of ``engine_mode``.
def synth_engine_param_info(engine_mode: SynthEngineMode) -> list[SynthParamInfo]: ...

# Controller-profile preset names (``"gm"``, ``"breath"``, ``"breathAftertouch"``, ``"mpe"``, ...).
def controller_profile_names() -> list[str]: ...

GS rhythm sets and variations ​

synth_gs_drum_kit_name(program) returns the GS rhythm-set name a rhythm part's program selects, or None when the module's own tone map defines no set there. The two ..._is_voiced_apart queries tell whether a rhythm set or a melodic variation renders differently from the one it would otherwise resolve to: True when it has voicing of its own, False when it renders exactly as Standard (or resolves to the capital tone), and None when nothing sits at that program or an argument is outside [0, 127]. A host can use them to offer only the choices that change the sound.

python
# GS rhythm-set name a rhythm part's ``program`` selects (``"Standard"``, ``"Room"``, ``"Jazz"``, ...), or ``None`` when the module's own tone map defines no set there.
def synth_gs_drum_kit_name(program: int) -> str | None: ...

# Whether the GS rhythm set at ``program`` is voiced apart from Standard: ``True`` when at least one drum note differs, ``False`` when the set renders exactly as Standard, ``None`` when no set sits at ``program``.
def synth_gs_drum_kit_is_voiced_apart(program: int) -> bool | None: ...

# Whether melodic Bank Select ``bank`` on ``program`` is voiced apart from the capital tone: ``True`` when the bank has a patch of its own, ``False`` when it resolves to the capital, ``None`` when either argument is outside ``[0, 127]`` -- both are seven-bit MIDI values, so ``128`` is out of range rather than the drum bank here.
def synth_gs_variation_is_voiced_apart(bank: int, program: int) -> bool | None: ...

RealtimeEngine MIDI and instrument members ​

The instrument and MIDI members of the engine, in declaration order. The rest of the class is on Realtime Engine. midi_cc_bindings() returns the live binding rows; use len(engine.midi_cc_bindings()) for their current count.

python
# Replace MIDI schedules; None omits a loop period and an explicit zero is refused.
def set_midi_clips(self, clips: Sequence[EngineMidiClipSchedule]) -> None: ...

def set_midi_destination_external(self, destination_id: int, external: bool) -> None: ...

def set_external_midi_clock_enabled(self, enabled: bool) -> None: ...

@property
def external_midi_dropped_count(self) -> int: ...

def drain_external_midi(self, max_records: int = 1024) -> list[ExternalMidiEvent]: ...

# Queue a live control change; ``controller`` and ``value`` are 0..127,
def push_midi_cc(
    self,
    destination_id: int,
    group: int,
    channel: int,
    controller: int,
    value: int,
    render_frame: int | None = None,
) -> None: ...

# Queue a live SysEx frame (1..512 bytes including 0xF0 and 0xF7); an oversize payload
def push_midi_sysex(
    self,
    destination_id: int,
    data: bytes | bytearray | memoryview,
    render_frame: int | None = None,
) -> None: ...

# Queue a live raw UMP message of 1..4 words, most significant first; data messages (MT
def push_midi_ump(
    self,
    destination_id: int,
    words: Iterable[int],
    render_frame: int | None = None,
) -> None: ...

# Queue a live program change, with a bank when ``bank_valid``; ``program`` and bank bytes
def push_midi_program(
    self,
    destination_id: int,
    group: int,
    channel: int,
    program: int,
    bank_valid: bool = False,
    bank_msb: int = 0,
    bank_lsb: int = 0,
    render_frame: int | None = None,
) -> None: ...

# Queue a MIDI panic at the next block head when ``render_frame`` is None;
def push_midi_panic(self, render_frame: int | None = None) -> None: ...

# Bind the built-in polyphonic synth to ``destination_id``; ``config`` None installs the
def set_builtin_instrument(
    self,
    config: BuiltinSynthConfig | None = None,
    destination_id: int = 0,
) -> None: ...

# Bind the patch-driven NativeSynth to ``destination_id``; ``patch`` is a SynthPatch, a
def set_synth_instrument(
    self,
    patch: SynthPatch | str | None = None,
    destination_id: int = 0,
    *,
    sample_bank: SampleBank | None = None,
) -> None: ...

# Resolve a hosted instrument's continuous parameter to its automation id.
def resolve_instrument_automation_id(self, destination_id: int, param_name: str) -> int: ...

# Parse SoundFont 2 bytes into the engine, replacing any previous one; malformed input
def load_sound_font(self, data: bytes | bytearray | memoryview) -> None: ...

# Bind a SoundFont player to ``destination_id``; ``config`` None installs the defaults.
def set_sf2_instrument(
    self,
    config: Sf2InstrumentConfig | None = None,
    destination_id: int = 0,
) -> None: ...

# Clear any realtime instrument bound to ``destination_id``.
def clear_midi_instrument(self, destination_id: int = 0) -> None: ...

# Number of bound realtime MIDI instruments.
@property
def midi_instrument_count(self) -> int: ...

# Replace the instrument's controller profile with a named preset (see
def set_controller_profile(self, destination_id: int, preset_name: str) -> None: ...

# Add one controller binding mapping ``input`` (at ``index`` for control change) to
def bind_controller(
    self,
    destination_id: int,
    *,
    input: ControllerInput,
    index: int = 0,
    axis: ControllerAxis,
    lo: float = 0.0,
    hi: float = 1.0,
    curve: float = 1.0,
) -> None: ...

# Drop every binding of the instrument's controller profile.
def clear_controller_bindings(self, destination_id: int) -> None: ...

# Number of bindings the instrument's controller profile holds.
def controller_binding_count(self, destination_id: int) -> int: ...

# Say whether note-on velocity carries expression for this instrument.
def set_controller_velocity_meaningful(self, destination_id: int, meaningful: bool) -> None: ...

# Read back :meth:`set_controller_velocity_meaningful`.
def controller_velocity_meaningful(self, destination_id: int) -> bool: ...

# Say which note a channel-addressed value of one MPE dimension belongs to.
def set_controller_note_tracking(
    self,
    destination_id: int,
    dimension: MpeDimension,
    tracking: NoteTracking,
) -> None: ...

# Read back :meth:`set_controller_note_tracking` as its canonical name.
def controller_note_tracking(
    self,
    destination_id: int,
    dimension: MpeDimension,
) -> NoteTracking: ...

# Set what a channel does with an overlapping note-on (poly, mono retrigger or mono
def set_articulation(
    self,
    destination_id: int,
    channel: int,
    articulation: Articulation,
) -> None: ...

# Set one part's rig on the instrument bound to ``destination_id``; an unbound destination
def set_part_rig(
    self,
    destination_id: int,
    part: int,
    *,
    mode: PartRigMode,
    inserts: Sequence[Mapping[str, object]] | None = ...,
) -> None: ...

# Read back :meth:`set_articulation` as its canonical name.
def articulation(self, destination_id: int, channel: int) -> Articulation: ...

# Number of times a legato continuation was asked for and refused.
def legato_fallback_count(self, destination_id: int) -> int: ...

# Bind a 7/14-bit CC, RPN or NRPN descriptor to a live engine parameter.
def bind_midi_cc(self, binding: MidiCcBinding) -> None: ...

# Clear all live MIDI CC to parameter bindings.
def clear_midi_cc_bindings(self) -> None: ...

# Return a copied snapshot of the live MIDI CC to parameter bindings.
def midi_cc_bindings(self) -> list[MidiCcBinding]: ...

# Install or replace a live MIDI-FX insert for ``destination_id`` from a MidiFxConfig.
def set_midi_fx(self, destination_id: int, config: MidiFxConfig) -> None: ...

# Clear the live MIDI-FX insert for ``destination_id``.
def clear_midi_fx(self, destination_id: int = 0) -> None: ...

# Enable the engine-owned live MIDI input source for ``destination_id``.
def set_midi_input_source(self, destination_id: int = 0) -> None: ...

# Clear the engine-owned live MIDI input source.
def clear_midi_input_source(self) -> None: ...

# Number of events queued in the live MIDI input source.
@property
def midi_input_pending_count(self) -> int: ...

# Queue a note-on into the live MIDI input source; ``offset_in_block_samples`` is samples
def push_midi_input_note_on(
    self,
    group: int,
    channel: int,
    note: int,
    velocity: int,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a note-off into the live MIDI input source; ``offset_in_block_samples`` is samples
def push_midi_input_note_off(
    self,
    group: int,
    channel: int,
    note: int,
    velocity: int = 0,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a control change into the live MIDI input source; ``offset_in_block_samples`` is
def push_midi_input_cc(
    self,
    group: int,
    channel: int,
    controller: int,
    value: int,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a pitch bend (``bend14`` 0..16383, centre 8192) into the live MIDI input source;
def push_midi_input_pitch_bend(
    self,
    group: int,
    channel: int,
    bend14: int,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a channel pressure (0..127) into the live MIDI input source; requires
def push_midi_input_channel_pressure(
    self,
    group: int,
    channel: int,
    pressure: int,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a key pressure (``note`` and ``pressure`` 0..127) into the live MIDI input source;
def push_midi_input_poly_pressure(
    self,
    group: int,
    channel: int,
    note: int,
    pressure: int,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a program change into the live MIDI input source; requires
def push_midi_input_program(
    self,
    group: int,
    channel: int,
    program: int,
    bank_valid: bool = False,
    bank_msb: int = 0,
    bank_lsb: int = 0,
    offset_in_block_samples: int = 0,
) -> None: ...

# Queue a raw UMP message into the live MIDI input source; requires
def push_midi_input_ump(self, words: Iterable[int], offset_in_block_samples: int = 0) -> None: ...

# Queue a live note-on to a MIDI destination; ``render_frame`` is a
def push_midi_note_on(
    self,
    destination_id: int,
    group: int,
    channel: int,
    note: int,
    velocity: int,
    render_frame: int | None = None,
) -> None: ...

# Queue a live note-off to a MIDI destination; ``render_frame`` is a
def push_midi_note_off(
    self,
    destination_id: int,
    group: int,
    channel: int,
    note: int,
    velocity: int = 0,
    render_frame: int | None = None,
) -> None: ...

# Queue a live pitch bend (``bend14`` 0..16383, centre 8192; larger is refused) to a MIDI
def push_midi_pitch_bend(
    self,
    destination_id: int,
    group: int,
    channel: int,
    bend14: int,
    render_frame: int | None = None,
) -> None: ...

# Queue a live channel pressure (0..127) to a MIDI destination.
def push_midi_channel_pressure(
    self,
    destination_id: int,
    group: int,
    channel: int,
    pressure: int,
    render_frame: int | None = None,
) -> None: ...

# Queue a live key pressure (``note`` and ``pressure`` 0..127) to a MIDI destination.
def push_midi_poly_pressure(
    self,
    destination_id: int,
    group: int,
    channel: int,
    note: int,
    pressure: int,
    render_frame: int | None = None,
) -> None: ...