Skip to content

Python ノート編集 API ​

libsonare Python パッケージで、音声から取り出したノートと打撃イベントを編集する関数と、ノート単位で編集できるポリフォニック解析のリファレンスです。エフェクト関数は Python エフェクト API にあります。インストール、クイックスタート、その他の API ファミリーは Python API の索引ページを参照してください。

ノートと打撃イベントの編集 ​

これらはモジュールレベルの関数で、音声から抽出したノートや打撃イベントを扱います。ワークフローは 音声内の音符を編集する を参照してください。extract_notes が返したノートを render_notes に渡すと、編集を聴けます。

python
# Extract editable note objects from audio and a caller-supplied F0 track.
def extract_notes(
    samples: Sequence[float] | list[float] | np.ndarray,
    sample_rate: int,
    f0_hz: Sequence[float] | list[float] | np.ndarray,
    frame_rate: float,
    *,
    voiced: Sequence[int] | list[int] | np.ndarray | None = None,
    voiced_prob: Sequence[float] | list[float] | np.ndarray | None = None,
    segmentation_threshold_cents: float | None = None,
    min_note_ms: float | None = None,
    reference_hz: float | None = None,
    voiced_threshold: float | None = None,
) -> list[NoteObject]: ...

# Render edited note objects over their source audio.
def render_notes(
    samples: Sequence[float] | list[float] | np.ndarray,
    sample_rate: int,
    notes: Sequence[NoteObject | NoteObjectInput],
    *,
    f0_hz: Sequence[float] | list[float] | np.ndarray | None = None,
    voiced: Sequence[int] | list[int] | np.ndarray | None = None,
    frame_rate: float | None = None,
    fade_ms: float | None = None,
    vibrato_cutoff_hz: float | None = None,
) -> np.ndarray: ...

# Split one note in two at a track frame, returning the whole new note set.
def split_note(
    samples: Sequence[float] | list[float] | np.ndarray,
    sample_rate: int,
    f0_hz: Sequence[float] | list[float] | np.ndarray,
    frame_rate: float,
    notes: Sequence[NoteObject | NoteSetEntry],
    index: int,
    frame: int,
    *,
    voiced: Sequence[int] | list[int] | np.ndarray | None = None,
    voiced_prob: Sequence[float] | list[float] | np.ndarray | None = None,
    segmentation_threshold_cents: float | None = None,
    min_note_ms: float | None = None,
    reference_hz: float | None = None,
    voiced_threshold: float | None = None,
) -> list[NoteObject]: ...

# Join a run of notes into one, returning the whole new note set.
def merge_notes(
    samples: Sequence[float] | list[float] | np.ndarray,
    sample_rate: int,
    f0_hz: Sequence[float] | list[float] | np.ndarray,
    frame_rate: float,
    notes: Sequence[NoteObject | NoteSetEntry],
    first: int,
    last: int,
    *,
    voiced: Sequence[int] | list[int] | np.ndarray | None = None,
    voiced_prob: Sequence[float] | list[float] | np.ndarray | None = None,
    segmentation_threshold_cents: float | None = None,
    min_note_ms: float | None = None,
    reference_hz: float | None = None,
    voiced_threshold: float | None = None,
) -> list[NoteObject]: ...

# Split one note's pitch curve into a centre, a drift and a vibrato.
def decompose_note_pitch(
    f0_hz: Sequence[float] | list[float] | np.ndarray,
    frame_rate: float,
    median_hz: float | None,
    *,
    voiced: Sequence[int] | list[int] | np.ndarray | None = None,
    vibrato_cutoff_hz: float | None = None,
) -> PitchDecompositionResult: ...

# Read one track of an in-memory Standard MIDI File as a reference melody.
def note_targets_from_smf(data: bytes | bytearray, *, track_index: int = 0) -> list[NoteTarget]: ...

# Write each note's ``pitch_semitones`` from the target it overlaps.
def assign_note_targets(
    notes: Sequence[NoteObject],
    sample_rate: int,
    targets: Sequence[NoteTarget],
    *,
    unmatched_policy: str = 'leave',
    min_overlap_ratio: float | None = None,
    max_correction_semitones: float | None = None,
) -> AssignNoteTargetsResult: ...

# Extract editable percussive events from audio.
def extract_percussive_events(
    samples: Sequence[float] | list[float] | np.ndarray,
    sample_rate: int,
    *,
    n_fft: int | None = None,
    hop_length: int | None = None,
    hpss_kernel_harmonic: int | None = None,
    hpss_kernel_percussive: int | None = None,
    onset_wait: int | None = None,
    onset_delta: float | None = None,
    max_event_ms: float | None = None,
    min_percussive_ratio: float | None = None,
) -> list[PercussiveEvent]: ...

# Render edited percussive events over their source audio.
def render_percussive_events(
    samples: Sequence[float] | list[float] | np.ndarray,
    sample_rate: int,
    events: Sequence[PercussiveEvent | PercussiveEventInput],
    *,
    n_fft: int | None = None,
    hop_length: int | None = None,
    hpss_kernel_harmonic: int | None = None,
    hpss_kernel_percussive: int | None = None,
    fade_ms: float | None = None,
) -> np.ndarray: ...
関数説明
extract_notes音声と、呼び出し側が用意したフレームごとの F0 トラックから、編集可能な NoteObject を抽出
render_notes編集したノートを元の音声の上にレンダリング。編集が恒等のノートは再合成されません
split_note1 つのノートをトラックのフレーム位置で 2 つに分け、新しいノート集合全体を返す
merge_notesfirst から last までのノートを 1 つに結合し、新しいノート集合全体を返す。結果は最初のノートの編集を引き継ぎます
decompose_note_pitch1 つのノートのピッチ曲線を、中心・ドリフト・ビブラートに分解。render_notes にも同じ vibrato_cutoff_hz を渡します
note_targets_from_smfメモリ上の標準 MIDI ファイルの 1 トラックを、参照メロディ(NoteTarget のリスト)として読み取る。時刻はファイルのテンポマップに従います
assign_note_targets各ノートの pitch_semitones を、最も長く重なるターゲットから書き込み、テイクを書かれたメロディに追従させる。入力のノートは変更されません
extract_percussive_events音声だけから編集可能な打撃イベントを抽出。オンセットは打撃成分の上で検出します
render_percussive_events編集した打撃イベントを元の音声の上にレンダリング。各打撃の打撃成分だけが動きます

ポリフォニック解析 ​

PolyphonicAnalysis は 1 回のポリフォニック解析を保持し、ノートごとに編集して、音声にレンダーし直せます。

python
# One polyphonic analysis, editable note by note and renderable back to audio.
class PolyphonicAnalysis:
    # Analyse ``samples`` into editable notes.
    def __init__(
        self,
        samples: Sequence[float] | list[float] | np.ndarray,
        sample_rate: int,
        *,
        n_fft: int | None = None,
        hop_length: int | None = None,
        win_length: int | None = None,
        cent_ref_hz: float | None = None,
        cents_per_bin: float | None = None,
        cent_max_hz: float | None = None,
        tonality: bool = True,
        salience_harmonics: int | None = None,
        f0_min_hz: float | None = None,
        f0_max_hz: float | None = None,
        salience_alpha_hz: float | None = None,
        salience_beta_hz: float | None = None,
        salience_inharmonicity: float | None = None,
        max_polyphony: int | None = None,
        min_frame_peak_ratio: float | None = None,
        min_separation_cents: float | None = None,
        subtraction_factor: float | None = None,
        max_jump_cents: float | None = None,
        min_ridge_peak_ratio: float | None = None,
        min_ridge_duration_ms: float | None = None,
        mask_harmonics: int | None = None,
        claim_lobes: float | None = None,
        inharmonicity: float | None = None,
        estimate_inharmonicity: bool = False,
        inharmonicity_min_partials: int | None = None,
        inharmonicity_max_residual_bins: float | None = None,
        inharmonicity_max_stretch: float | None = None,
        window_frames: int | None = None,
        min_partial_separation: float | None = None,
        max_fit_residual: float | None = None,
        max_weight_modulus: float | None = None,
        max_refine_hz: float | None = None,
        f0_tolerance_cents: float | None = None,
        segmentation_threshold_cents: float | None = None,
        min_note_ms: float | None = None,
        reference_hz: float | None = None,
        reattack_ratio: float | None = None,
    ) -> None: ...
    # Analyse ``samples`` into editable notes and return the analysis.
    @classmethod
    def analyze(
        cls,
        samples: Sequence[float] | list[float] | np.ndarray,
        sample_rate: int,
        **config: object,
    ) -> Self: ...
    # Release the native analysis.
    def close(self) -> None: ...
    # Return self so the object works as a context manager.
    def __enter__(self) -> PolyphonicAnalysis: ...
    # Close on leaving the ``with`` block; never suppresses an exception.
    def __exit__(self, *_exc: object) -> None: ...
    # Number of notes, which is also the number of claim sets.
    @property
    def note_count(self) -> int: ...
    # Number of STFT frames the analysis ran over.
    @property
    def frame_count(self) -> int: ...
    # Copy the notes out, in the order their claim sets are held in.
    def notes(self) -> list[NoteObject]: ...
    # Per-frame voice count, before tracking dropped anything.
    def polyphony(self) -> np.ndarray: ...
    # The stretch fitted for each note, where the fit was asked for.
    def note_inharmonicity(self) -> np.ndarray: ...
    # One note's F0 in Hz, per frame over its own span.
    def note_f0(self, note: int) -> np.ndarray: ...
    # One note's linear RMS, per frame over its own span.
    def note_amplitude(self, note: int) -> np.ndarray: ...
    # One note's salience, per frame over its own span.
    def note_salience(self, note: int) -> np.ndarray: ...
    # One note's amplitude envelope points, as last set.
    def note_envelope(self, note: int) -> np.ndarray: ...
    # Replace one note's pending edit.
    def set_note_edit(self, note: int, edit: NoteEdit | NoteEditInput | None = None) -> None: ...
    # Render the analysis back to audio with whatever edits its notes carry.
    def render(
        self,
        *,
        fade_ms: float | None = None,
        vibrato_cutoff_hz: float | None = None,
    ) -> np.ndarray: ...