Python Note Editing API
Editing notes and percussive events extracted from audio, and polyphonic analysis that is editable note by note, in the libsonare Python package. The effects functions are on Python Effects API, and installation, quick start, and the other API families on the Python API index.
Note and percussive event editing
These module-level functions work on notes and percussive events extracted from audio; Edit Notes Inside Audio covers the workflow. Pass the notes from extract_notes back to render_notes to hear the edits.
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: ...| Function | Description |
|---|---|
extract_notes | Extract editable NoteObjects from audio and a caller-supplied per-frame F0 track |
render_notes | Render edited notes over their source audio; a note whose edit is the identity is not resynthesized |
split_note | Split one note in two at a track frame and return the whole new note set |
merge_notes | Join a run of notes (first to last) into one and return the whole new note set; the result takes the first note's edit |
decompose_note_pitch | Split one note's pitch curve into a centre, a drift and a vibrato; pass render_notes the same vibrato_cutoff_hz |
note_targets_from_smf | Read one track of an in-memory Standard MIDI File as a reference melody (NoteTarget list); times follow the file's tempo map |
assign_note_targets | Write each note's pitch_semitones from the target it overlaps longest, so a take follows a written melody; the input notes are not modified |
extract_percussive_events | Extract editable percussive events from audio alone, detecting onsets on the percussive component |
render_percussive_events | Render edited percussive events over their source audio; only the percussive signal of each hit moves |
Polyphonic analysis
PolyphonicAnalysis holds one polyphonic analysis that is editable note by note and renderable back to audio.
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: ...