Python Mixer クラス API
Mixer は、ストリップごとのステレオブロックに対してセンド、バス、VCA グループ、メーター、オートメーションを扱う、シーンベースの永続ステレオミキサーです。ワンショットの mix_stereo(...) とミキシングの作業手順はこのページの冒頭にあります。ルーティングの考え方は ミキシングエンジン、シーンの形式は ミキシングシーン JSON を参照してください。
ミキシング API
Python からも libsonare のミキシングエンジンを使えます。ステムを一括でレンダリングするだけなら mix_stereo(...)、センド、バス、オートメーション、メーター、シーンの保存が必要ならシーンドキュメントから作る Mixer(Mixer(scene=...)。JSON テキストは Mixer.from_json(json, sample_rate=...))を使います。組み込みのシーンプリセット一覧は mixing_scene_preset_names() で取得できます。
import libsonare as sonare
print(sonare.mixing_scene_preset_names())
scene = sonare.mixing_scene_preset("vocalReverbSend")
offline = sonare.mix_stereo(
[(vocal_l, vocal_r), (music_l, music_r)],
sample_rate=48000,
input_trim_db=[3, 0],
fader_db=[-3, -12],
pan=[0, -0.2],
width=[1, 0.9],
)
with sonare.Mixer(sample_rate=48000, scene=scene, max_block_size=512) as mixer:
print(mixer.scene_warnings()) # 未宣言バスへのセンドなど、回復可能な警告
print(mixer.latency_samples) # ドライ/ウェットの位置合わせに使う、コンパイル済みグラフのレイテンシ
block = mixer.process_stereo([vocal_block_l, music_block_l], [vocal_block_r, music_block_r])
meter = mixer.strip_meter(0, tap="postFader")
mixer.schedule_fader_automation(0, 48000 * 8, -6, curve="sCurve")# Render a small stereo mixer scene from per-strip left/right buffers.
def mix_stereo(
strips: Sequence[tuple[Sequence[float], Sequence[float]]],
sample_rate: int,
fader_db: Sequence[float] | None = None,
pan: Sequence[float] | None = None,
pan_mode: Sequence[PanMode] | PanMode = 'balance',
width: Sequence[float] | None = None,
muted: Sequence[bool] | None = None,
input_trim_db: Sequence[float] | None = None,
) -> MixResult: ...
# Return built-in mixer scene preset identifiers.
def mixing_scene_preset_names() -> list[str]: ...mixer.process_stereo(...) は MixerStereoResult の frozen dataclass を返します。.left と .right(float32 配列)、.sample_rate(int)を持ち、Node/WASM の {left, right, sampleRate} と同じ形です。入力チャンネルがストリップより少ない場合、残りのストリップは無音になります。入力チャンネルがストリップより多い場合は SonareValueError が送出され、メッセージにはストリップ数が入ります(left_inputs must hold at most 2 inputs, one per strip)。
Mixer.set_pan_law(strip, pan_law) と RealtimeEngine.set_strip_pan_law(ref, pan_law) は PanLaw のトークンをそのまま受け取ります。const3Db、const4.5Db、const6Db、linear0Db のいずれかです。別の綴りは SonareValueError、数値は TypeError になります。
ルーティングの考え方、シーンプリセット、リアルタイム処理の注意点は ミキシングエンジン を参照してください。
ミキサーのその他のエントリポイント
| やりたいこと | API | ガイド |
|---|---|---|
| 信号の終わりで True Peak メーターの保留ウィンドウを反映する | Mixer.flush_meters() | ミキシングエンジン |
| 未宣言バスへのセンドなど、回復可能なミキサーシーン警告を読む | Mixer.scene_warnings() | ミキシングシーン JSON |
Mixer のライフサイクルとメンバーのシグネチャは以降に続きます。
Mixer のライフサイクル
Mixer(sample_rate=..., scene=None, max_block_size=None) は空のミキサーか、シーンのマッピングから作ったミキサーを返します。Mixer.from_json(document, *, sample_rate, max_block_size=None) はシーンの JSON テキストを読み込みます。JSON 文書の構文や内容に問題がある場合は、コード INVALID_FORMAT の SonareError になります。scene= で渡したマッピングに未知のキーがある場合は、scene. 以下のパスを示す SonareValueError になります。max_block_size はすべての process 呼び出しと drain 呼び出しのフレーム数の上限で、省略するとライブラリの既定値になり、明示的な 0 は拒否されます。ストリップはインデックス [0, strip_count)(読み取り専用の strip_count プロパティ)か文字列 ID(StripRef)で指定します。ストリップのハンドルはネイティブのミキサーが所有するので、ストリップはミキサーが開いている間だけ使えます。
close() は何度呼んでも安全で、with はブロックを抜けるときにミキサーを閉じ、ガベージコレクトされるミキサーは自分自身を閉じます。閉じたミキサーに対するほかの呼び出しは INVALID_STATE の SonareError になります。close() はすべてのストリップを無効にするため、その結果を close() の後まで持ち越さないでください。
Mixer のメンバー
作成、シリアライズ、解放
# Build a mixer from a scene document, or an empty one when ``scene`` is absent.
def __init__(
self,
*,
sample_rate: int,
scene: MixSceneDocument | None = None,
max_block_size: int | None = None,
) -> None: ...
# Build a mixer from a scene's JSON text, as :meth:`to_json` writes it.
@classmethod
def from_json(
cls,
document: str,
*,
sample_rate: int,
max_block_size: int | None = None,
) -> Mixer: ...
# Non-fatal warnings captured when this mixer was built from scene JSON.
def scene_warnings(self) -> list[str]: ...
# Rebuild the graph while preserving automation position and queued events.
def compile(self) -> None: ...
# Serialize the current scene (strips, buses, sends, connections) as JSON text.
def to_json(self) -> str: ...
# Release the underlying native mixer.
def close(self) -> None: ...
# Return the object itself for ``with`` use.
def __enter__(self) -> Mixer: ...
# Close the object on leaving the ``with`` block; never suppresses exceptions.
def __exit__(self, *_exc: object) -> None: ...ストリップ、バス、VCA グループ
# The largest ``num_samples`` one process call accepts.
@property
def max_block_size(self) -> int: ...
# Return the number of strips in the mixer.
@property
def strip_count(self) -> int: ...
# Add a channel strip to the mixer topology.
def add_strip(
self,
id: str,
*,
enabled: bool = True,
lufs: bool = True,
true_peak: bool = True,
true_peak_oversample: int | None = None,
) -> int: ...
# Add a bus to the mixer topology.
def add_bus(self, id: str, role: MixBusRole = 'aux') -> None: ...
# Remove a bus by id from the mixer topology.
def remove_bus(self, id: str) -> None: ...
# Send a strip or bus into a submix bus, or back to the master.
def set_output_bus(self, source_id: str, bus_id: str) -> None: ...
# Return the number of buses in the mixer topology.
@property
def bus_count(self) -> int: ...
# Add a VCA group with the given id, gain offset, and strip members.
def add_vca_group(
self,
id: str,
gain_db: float = 0.0,
members: Sequence[str] | None = None,
) -> None: ...
# Remove a VCA group by id from the mixer topology.
def remove_vca_group(self, id: str) -> None: ...
# Set an existing VCA group's gain in dB.
def set_vca_group_gain_db(self, id: str, gain_db: float) -> None: ...
# Replace an existing VCA group's strip membership.
def set_vca_group_members(self, id: str, members: Sequence[str]) -> None: ...
# Return the number of VCA groups in the mixer topology.
@property
def vca_group_count(self) -> int: ...
# Return the index of the strip with ``id``.
def strip_by_id(self, id: str) -> int: ...ストリップの操作とセンド
# Set a strip's solo state (takes effect without a recompile).
def set_soloed(self, strip: StripRef, soloed: bool) -> None: ...
# Mark a strip solo-safe so other strips' solo never implied-mutes it.
def set_solo_safe(self, strip: StripRef, solo_safe: bool) -> None: ...
# Invert the polarity of the left and/or right channel of a strip.
def set_polarity_invert(self, strip: StripRef, invert_left: bool, invert_right: bool) -> None: ...
# Set a strip's pan law (a :data:`PanLaw` token such as ``"const3Db"``).
def set_pan_law(self, strip: StripRef, pan_law: PanLaw) -> None: ...
# Set a per-strip channel delay in samples, moving it later than the other strips.
def set_channel_delay_samples(self, strip: StripRef, delay_samples: int) -> None: ...
# Set a strip's direct VCA trim in dB (persisted as the scene's ``vcaOffsetDb``).
def set_vca_offset_db(self, strip: StripRef, offset_db: float) -> None: ...
# Set independent left/right pan positions in [-1, 1] for a strip (dual-pan mode).
def set_dual_pan(self, strip: StripRef, left: float, right: float) -> None: ...
# Set a strip's surround pan position (used when feeding a >2-channel bus).
def set_surround_pan(
self,
strip: StripRef,
*,
azimuth: float = 0.0,
divergence: float = 0.0,
lfe: float = 0.0,
) -> None: ...
# Set a strip's fader gain in dB (takes effect without a recompile).
def set_fader_db(self, strip: StripRef, db: float) -> None: ...
# Set a strip's input trim in dB.
def set_input_trim_db(self, strip: StripRef, db: float) -> None: ...
# Set a strip's pan position.
def set_pan(self, strip: StripRef, pan: float, pan_mode: PanMode | None = None) -> None: ...
# Set a strip's stereo width in [0, 2] (0 mono, 1 unchanged, 2 widest).
def set_width(self, strip: StripRef, width: float) -> None: ...
# Set a strip's mute state (takes effect without a recompile).
def set_muted(self, strip: StripRef, muted: bool) -> None: ...
# Snap a strip's gain smoothers to the values already set on it.
def settle(self, strip: StripRef) -> None: ...
# Add a send from a strip to a bus and return its send index.
def add_send(
self,
strip: StripRef,
send_id: str,
destination_bus_id: str,
send_db: float = 0.0,
timing: SendTiming = 'postFader',
) -> int: ...
# Set the level in dB of an existing send on a strip (by send index).
def set_send_db(self, strip: StripRef, index: int, db: float) -> None: ...
# Remove a strip's send by add-order index.
def remove_send(self, strip: StripRef, index: int) -> None: ...メーター
# Read a per-strip meter snapshot at the given tap point.
def strip_meter(self, strip: StripRef, tap: MeterTap = 'postFader') -> MixMeterSnapshot: ...
# Read the post-insert meter for a compiled bus, including master.
def bus_meter(self, bus_id: str) -> MixMeterSnapshot: ...
# Turn the master-output meter on or off and choose what it measures.
def configure_meter(
self,
enabled: bool = True,
*,
true_peak_oversample: int = 4,
measure_lufs: bool = True,
measure_true_peak: bool = True,
) -> None: ...
# Read the master-output meter.
def meter_snapshot(self) -> MixMeterSnapshot: ...
# Return the blocks in which the strip discarded recursive state.
def strip_non_finite_discard_count(self, strip: StripRef) -> int: ...
# Return the blocks in which the bus discarded recursive state.
def bus_non_finite_discard_count(self, bus_id: str) -> int: ...
# Read up to ``max_points`` of the latest goniometer (vectorscope) data.
def read_goniometer_latest(self, strip: StripRef, max_points: int) -> list[GoniometerPoint]: ...
# Mark the end of the signal for every strip, bus and master meter.
def flush_meters(self) -> None: ...オートメーションと処理
# Schedule a sample-accurate fader (dB) automation event on a strip.
def schedule_fader_automation(
self,
strip: StripRef,
sample_pos: int,
fader_db: float,
curve: AutomationCurve = 'linear',
) -> None: ...
# Schedule a sample-accurate pan automation event on a strip.
def schedule_pan_automation(
self,
strip: StripRef,
sample_pos: int,
pan: float,
curve: AutomationCurve = 'linear',
) -> None: ...
# Schedule a sample-accurate stereo-width automation event on a strip.
def schedule_width_automation(
self,
strip: StripRef,
sample_pos: int,
width: float,
curve: AutomationCurve = 'linear',
) -> None: ...
# Schedule a sample-accurate send-level (dB) automation event on a strip.
def schedule_send_automation(
self,
strip: StripRef,
send_index: int,
sample_pos: int,
db: float,
curve: AutomationCurve = 'linear',
) -> None: ...
# Schedule a sample-accurate insert-parameter automation event.
def schedule_insert_automation(
self,
strip_index: StripRef,
insert_index: int,
param_id: int,
sample_pos: int,
value: float,
curve: AutomationCurve = 'linear',
) -> None: ...
# Mix one block of per-strip stereo audio into the stereo master.
def process_stereo(
self,
left_inputs: Sequence[Sequence[float]],
right_inputs: Sequence[Sequence[float]],
) -> MixerStereoResult: ...
# Return the longest audible reverb/delay path to the master, in samples.
@property
def tail_samples(self) -> int: ...
# Return the compiled mixer graph's latency in samples.
@property
def latency_samples(self) -> int: ...
# Drain ``num_samples`` of trailing effect-tail audio with no new input.
def drain_tail_stereo(self, num_samples: int) -> MixerStereoResult: ...結果型とストリップ参照
# Stereo master output of :meth:`Mixer.process_stereo`.
@dataclass(frozen=True, slots=True, eq=False)
class MixerStereoResult:
left: npt.NDArray[np.float32]
right: npt.NDArray[np.float32]
sample_rate: int
StripRef = int | strシーンプリセットとミキシングアシスタント
mixing_scene_preset(name) は、組み込みのシーンプリセットをシーンドキュメントのマッピングとして返します。そのまま Mixer(scene=...) に渡せます。文字列でない名前には TypeError、mixing_scene_preset_names() に載っていない名前には SonareValueError を送出します。シーンの形式は ミキシングシーン JSON を参照してください。
# Return a built-in mixer scene preset as a scene document mapping.
def mixing_scene_preset(name: str) -> MixSceneDocument: ...mixing_assistant_suggest(tracks, *, sample_rate, ...) は MixAssistantTrack のリストを解析し、提案するシーンとその根拠を MixAssistantResult として返します。音声が成立しないトラックは、呼び出しを失敗させる代わりに除外され、usable が false で exclusion_reason 付きで報告されます。mixing_assistant_source_class_names() はアシスタントが報告しうるソースクラスの識別子を列挙し、mixing_assistant_source_class_from_name(name) はそのひとつを enum 値に解決します(一覧にない名前は SonareValueError)。ワークフローは ミキシングアシスタント にあります。
# Analyse ``tracks`` and return a suggested scene with the reasoning behind it.
def mixing_assistant_suggest(
tracks: Sequence[MixAssistantTrack],
*,
sample_rate: int,
target_track_lufs: float | None = None,
suggestion_strength: float | None = None,
eq_max_cut_db: float | None = None,
mix_bus_headroom_dbtp: float | None = None,
tempo_bpm: float | None = None,
enable_structure: bool | None = None,
enable_gain: bool | None = None,
enable_balance: bool | None = None,
enable_eq: bool | None = None,
enable_dynamics: bool | None = None,
enable_image: bool | None = None,
enable_high_pass: bool | None = None,
n_fft: int | None = None,
hop_length: int | None = None,
) -> MixAssistantResult: ...
# Return the source-class identifiers the assistant can report.
def mixing_assistant_source_class_names() -> list[str]: ...
# Resolve a source-class identifier to its enum value.
def mixing_assistant_source_class_from_name(name: str) -> int: ...