Python インストゥルメントとライブ MIDI API
内蔵シンセサイザー、SoundFont、自前のインストゥルメントを通したアレンジのレンダーと、リアルタイムエンジンを通したライブ演奏です。Project は プロジェクト、エンジンは リアルタイムエンジン にあります。このページには、インストゥルメントのタスク、SynthPatch の規則、MIDI 向けの RealtimeEngine の生成シグネチャを載せています。インストゥルメントのレコードと文字列トークンは Python インストゥルメントの型 にあります。
タスク別の参照先
| やりたいこと | API | ガイド |
|---|---|---|
| 内蔵シンセサイザー(NativeSynth)で MIDI をレンダリングする | Project.bounce(synth_instruments=[ProjectInstrumentBinding(instrument=patch, destination_id=...)])、synth_preset_names()、synth_preset_patch(name)、SynthPatch | 内蔵シンセサイザー、プロジェクトのバウンス |
| SoundFont で MIDI をレンダリングする | Project.load_sound_font(data)、Project.bounce(sf2_instruments=[ProjectSf2InstrumentBinding(instrument=config, destination_id=..., sound_font_uri=...)]) | SoundFont プレイヤー |
| バウンス中に自前のインストゥルメントをホストする | ExternalInstrument プロトコルを使う Project.bounce(instruments=[ProjectInstrumentBinding(instrument=instrument, destination_id=...)])。render(channels, num_frames) コールバックに加え、任意の prepare/on_event フックと latency_samples を持ちます。Node と WASM では、同じコールバック形式を instruments バウンスオプションに渡します。 | プロジェクトのバウンス |
| MIDI イベントからインストゥルメントをライブ演奏し、送出先(デスティネーション)の MIDI FX を差し替える | RealtimeEngine.set_synth_instrument(...)、RealtimeEngine.load_sound_font(...)、RealtimeEngine.set_midi_fx(...)、エンジンの MIDI 入力キュー(各 push は offset_in_block_samples を取り、絶対的なクロック位置ではなく、次に処理されるブロック内のオフセットを表します)、push_midi_program(destination_id, group, channel, program, bank_valid=..., bank_msb=..., bank_lsb=...) / push_midi_input_program(...)(バンク指定を含められるプログラムチェンジを 1 回の呼び出しで送る。SoundFont プレイヤーと GM プログラムを割り当てたシンセはバンクに従う) | MIDI 入力 |
| デスティネーションを外部 MIDI ハードウェアへ送る | set_midi_destination_external(...)、set_external_midi_clock_enabled(...)、drain_external_midi(...)、external_midi_dropped_count() | プロジェクト編集:MIDI |
ライフサイクル
作成
インストゥルメントは単独では作らず、バインドします。Project.bounce(...) に synth_instruments、sf2_instruments、builtin_instruments、instruments を渡すか、RealtimeEngine の set_synth_instrument、set_sf2_instrument、load_sound_font、set_midi_fx を呼びます。SynthPatch は synth_preset_patch(name) から得るか、手で組み立てます。
使用
ライブの経路はエンジンの MIDI 入力キューです。各 push は offset_in_block_samples、つまり絶対的なクロック位置ではなく、次に処理されるブロックの先頭からのオフセットを受け取り、コントロール呼び出しはオーディオ側へのコマンドとしてキューに積まれます。バウンスはオフラインです。
解放
インストゥルメントを保持するのは Project と RealtimeEngine で、それぞれの close() で解放されます(ネイティブハンドルの解放)。
エラー
未知のプリセット名、不正なパッチ、synth_patch_param_info() が公開する範囲の外やそもそも有限でない SynthPatch の数値フィールドは、クランプされず、名前を挙げて SonareValueError で拒否されます。エラー契約 を参照してください。
SynthPatch とプリセット
シンセプリセットの確認には synth_preset_patch(name) を使います。名前付きカタログプリセットを SynthPatch として返すので、バインドする前にフィールドを確認・調整できます(不明な名前では SonareError を送出し、'va:' ルーティング接頭辞も受け付けます)。synth_enum_tables() は実行時の enum 名テーブル(dict[str, tuple[str, ...]])を返し、SynthModRouting のソース/デスティネーション名を、読み込まれたビルドに対して検証できます。エンジンモード・波形・フィルター・モジュレーションのテーブルに加えて、sample_loop_modes、sample_key_tracks、retriggers、lfo_shapes、part_rig_modes、sample_desc_loop_modes も含みます。
SynthPatch の数値フィールドはすべて既定値が None で、「ベースプリセットの値を保つ」という意味です。設定していないフィールドを読むと 0.0 ではなく None が返り、値を渡せばそれは明示的な上書きになります。0 も同様なので、SynthPatch(preset="warm-pad", amp_sustain=0) は未設定として扱われず、アンプサステインを本当にゼロにします。synth_preset_patch(name) から得たパッチはプリセットの具体値で埋まって返るため、数値フィールドが None になることはありません。enum フィールド(engine_mode、waveform、filter_model、filter_output、body、retrigger、lfo_shape)では 0 / "default" が「ベースを保つ」を意味します。mod_routings=None はベースのモッドマトリクスを保ち、空タプルは消去、要素のあるタプルは置き換えです。lfo_shape は "triangle"・"sine"・"random"(ボイスごとにシードされる滑らかなサンプル&ホールド)で、ビブラートとトレモロの LFO にすべての宛先で効きます。filter_model="svf4Pole" は飽和しない 24 dB/オクターブのローパスです。synth_patch_param_info() が示す範囲の外や非有限の数値フィールドは、クランプされずに名前を挙げて拒否されます。
カタログとディスクリプタの関数
読み込まれたビルドのシンセカタログを読むモジュールレベルの関数です。ホストは名前や範囲をハードコードせずに済みます。synth_preset_names() はカタログのプリセット名を列挙し、synth_preset_patch(name) はそのひとつを SynthPatch として取得し、synth_enum_tables() は enum 名テーブルを返します。synth_patch_param_info() は SynthPatch の数値フィールドすべて(名前、受け付ける範囲、既定値)を記述し、synth_engine_param_info(engine_mode) はひとつのエンジンモードの engine_params のキーを記述します。min / max は閉じた範囲を持つフィールドにだけ現れ、範囲外の値はクランプされずに拒否されます。"default" と "sample" のモードにはエンジンセクションがないため、空のリストを返します。controller_profile_names() は RealtimeEngine.set_controller_profile が受け付ける名前を列挙します。
# 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 のリズムセットとバリエーション
synth_gs_drum_kit_name(program) は、リズムパートの program が選ぶ GS リズムセットの名前を返します。モジュール自身のトーンマップにそこでのセットがなければ None です。二つの ..._is_voiced_apart は、リズムセットまたはメロディックのバリエーションが、本来解決される先と異なる鳴り方をするかどうかを返します。独自のボイシングがあれば True、Standard(またはキャピタルトーン)とまったく同じ鳴り方なら False、そのプログラムに何もない場合や引数が [0, 127] の外にある場合は None です。ホストはこれを使って、音が実際に変わる選択肢だけを提示できます。
# 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 とインストゥルメントのメンバー
エンジンのインストゥルメントと MIDI のメンバーを、宣言順に並べています。クラスの残りは リアルタイムエンジン にあります。midi_cc_bindings() はライブバインディングの一覧を返すため、現在の件数には len(engine.midi_cc_bindings()) を使います。
# 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: ...