Python Vocal Edit Types
The records that VocalEditSession, VocalEditDraft, VocalRenderSnapshot and VocalRenderJob take and return, and the VocalEditError they raise. The session, draft and render calls are on Python Projects API, and Offline Vocal Editing walks through the flow.
All records are frozen dataclasses. Sample positions count samples of the analysed source unless a field says it is an output position; NDArray fields are copies.
Analysis and capabilities
VocalEditSession(samples, sample_rate) analyses mono audio into a VocalAnalysisResult. The VocalAnalysis inside it holds the frame-wise f0_hz and voiced arrays together with the settings the analysis was made with. source_sha256 and analysis_sha256 are hexadecimal SHA-256 digests of the source and of the analysis. VocalEditSession also accepts an existing VocalAnalysis through its analysis argument. VocalCapabilities reports what the loaded engine supports; gate the feature on capabilities().features.vocal_edit and vocal_edit_abi_version().
# Frame-wise pitch analysis of the source and the settings it was made with.
@dataclass(frozen=True, slots=True)
class VocalAnalysis:
frame_origin_sample: float
samples_per_frame: float
frame_length_samples: int
f0_hz: NDArray[np.float32]
voiced: NDArray[np.uint8]
algorithm_id: str
algorithm_version: int
fmin_hz: float = 65.0
fmax_hz: float = 2093.0
yin_threshold: float = 0.1
voiced_threshold: float = 0.5
centered: bool = True
segmentation_threshold_cents: float = 50.0
min_note_ms: float = 30.0
reference_hz: float = 440.0
# A pitch analysis with the digests and format of the source it describes.
@dataclass(frozen=True, slots=True)
class VocalAnalysisResult:
analysis: VocalAnalysis
source_sha256: str
analysis_sha256: str
source_length_samples: int
sample_rate: int
# What the loaded vocal-edit engine supports.
@dataclass(frozen=True, slots=True)
class VocalCapabilities:
abi_version: int
profile_id: int
monophonic_only: bool
analysis_cancellable: bool
minimum_formant_shift_semitones: float
maximum_formant_shift_semitones: floatRanges and state tokens
A VocalRange is half-open, so end_sample is one past the last sample and length is 0 for an empty or inverted range. A VocalStateToken names a session or draft state: revision counts committed edits (committed_revision is its alias), and a committed state has draft_id 0 and generation 0. Render and edit results carry the token of the state they came from.
# A half-open range of source or output samples.
@dataclass(frozen=True, slots=True)
class VocalRange:
start_sample: int
end_sample: int
# Number of samples in the range; 0 when empty or inverted.
@property
def length(self) -> int: ...
# Identifies a session or draft state by epoch, revision, draft and generation counters.
@dataclass(frozen=True, slots=True)
class VocalStateToken:
session_epoch: int
revision: int
draft_id: int
generation: int
request_id: int = 0
profile_id: int = 0
# Alias of ``revision``.
@property
def committed_revision(self) -> int: ...
# Whether the session has an edit to undo or redo.
@dataclass(frozen=True, slots=True)
class VocalHistory:
can_undo: bool
can_redo: boolNotes and pitch
session.notes() and draft.notes() return a VocalNotesResult: the detected VocalNote records and the VocalTransition crossfades between adjacent notes. A note's median_hz, center_midi and f0_stability are None when no pitch was measured. VocalNote.source_range() returns the source span as a VocalRange.
evaluate_pitch(note_id) returns a VocalPitchResult: per-frame arrays of source position, measured, target and effective pitch (MIDI note numbers), with voiced and has_target flags.
# One detected note with its source span, measured pitch and current edit.
@dataclass(frozen=True, slots=True)
class VocalNote:
id: int
source_start_sample: int
source_end_sample: int
analysis_frame_start: int
analysis_frame_end: int
median_hz: float | None
center_midi: float | None
f0_stability: float | None
amplitude: NDArray[np.float32]
edit: VocalNoteEdit
# The note's source span as a ``VocalRange``.
@property
def source_range(self) -> VocalRange: ...
# The notes of a vocal session or draft and the transitions between them.
@dataclass(frozen=True, slots=True)
class VocalNotesResult:
notes: tuple[VocalNote, ...]
transitions: tuple[VocalTransition, ...]
# The crossfade between two adjacent notes.
@dataclass(frozen=True, slots=True)
class VocalTransition:
left_note_id: int
right_note_id: int
left_window_samples: int
right_window_samples: int
strength: float
# Per-frame measured, target and effective pitch over a range.
@dataclass(frozen=True, slots=True)
class VocalPitchResult:
source_samples: NDArray[np.float64]
measured_midi: NDArray[np.float64]
target_midi: NDArray[np.float64]
effective_midi: NDArray[np.float64]
voiced: NDArray[np.uint8]
has_target: NDArray[np.uint8]
# One point of a pitch curve: a source position and its target pitch.
@dataclass(frozen=True, slots=True)
class VocalPitchPoint:
source_sample: float
target_midi: float
# Alias of ``target_midi``.
@property
def midi(self) -> float: ...Note edits
A VocalNoteEdit is the whole edit of one note, with the defaults of an untouched note. target_mode is "none", "center" (one pitch, target_midi) or "curve" (target_points, a sequence of VocalPitchPoint). formant_mode is "preserve" or "shift", and "shift" applies formant_shift_semitones within the range VocalCapabilities reports.
# The edit applied to one note: pitch target, correction, timing, gain and formant settings.
@dataclass(frozen=True, slots=True)
class VocalNoteEdit:
target_mode: VocalTargetMode = 'none'
target_midi: float = 0.0
target_points: Sequence[VocalPitchPoint] = ()
amount: float = 0.0
speed_ms: float = 0.0
max_correction_semitones: float = 12.0
transpose_semitones: float = 0.0
drift_scale: float = 1.0
vibrato_scale: float = 1.0
destination_start_sample: int = 0
destination_length_samples: int = 0
gain_db: float = 0.0
muted: bool = False
formant_mode: VocalFormantMode = 'preserve'
formant_shift_semitones: float = 0.0
amplitude_envelope: Sequence[float] = ()Edit operations
draft.apply(operations) takes an iterable of these records; a VocalOperation is any one of VocalSetNoteEdit, VocalSetNoteSourceSpan, VocalSplitNote, VocalMergeNotes, VocalSetTransition, VocalRemoveTransition and VocalResetNotes. VocalSplitNote and VocalSetNoteSourceSpan accept a position in samples or in seconds. VocalMergeNotes.merge_policy is "preserve" (the merged note keeps the first note's edit) or "reset" (it starts from defaults).
# Operation: replace a note's edit.
@dataclass(frozen=True, slots=True)
class VocalSetNoteEdit:
note_id: int
edit: VocalNoteEdit
# Set a note's source span and its destination placement.
@dataclass(frozen=True, slots=True)
class VocalSetNoteSourceSpan:
note_id: int
source_start_sample: int | None = None
source_end_sample: int | None = None
destination_start_sample: int | None = None
destination_length_samples: int | None = None
source_start_sec: float | None = None
source_end_sec: float | None = None
destination_start_sec: float | None = None
destination_length_sec: float | None = None
# Cut a note at ``source_sample`` or at ``source_sec`` seconds.
@dataclass(frozen=True, slots=True)
class VocalSplitNote:
note_id: int
source_sample: int | None = None
source_sec: float | None = None
# Operation: merge adjacent notes into one.
@dataclass(frozen=True, slots=True)
class VocalMergeNotes:
note_ids: Sequence[int]
policy: VocalMergePolicy = 'preserve'
# Operation: set the crossfade between two adjacent notes.
@dataclass(frozen=True, slots=True)
class VocalSetTransition:
transition: VocalTransition
# Operation: remove the crossfade between two adjacent notes.
@dataclass(frozen=True, slots=True)
class VocalRemoveTransition:
left_note_id: int
right_note_id: int
# Operation: restore notes to their analysed state.
@dataclass(frozen=True, slots=True)
class VocalResetNotes:
note_ids: Sequence[int]Results
apply, commit, undo and redo return a VocalEditResult: the new token, the output ranges that changed (dirty_ranges) and the VocalIdChange records for the note ids the call replaced. A render returns a VocalRenderResult whose samples start at output position start_sample; processed_ranges, cache_hit_units, dry_passed_frames and limited_correction_frames are the render statistics.
# Outcome of an applied edit: the new state, changed output ranges and id replacements.
@dataclass(frozen=True, slots=True)
class VocalEditResult:
token: VocalStateToken
dirty_ranges: tuple[VocalRange, ...]
id_changes: tuple[VocalIdChange, ...] = ()
# A note id retired by an operation and the ids that replace it.
@dataclass(frozen=True, slots=True)
class VocalIdChange:
operation_index: int
retired_id: int
new_ids: tuple[int, ...]
# Rendered audio with the state it came from and render statistics.
@dataclass(frozen=True, slots=True)
class VocalRenderResult:
samples: NDArray[np.float32]
start_sample: int
token: VocalStateToken
processed_ranges: tuple[VocalRange, ...]
cache_hit_units: int
dry_passed_frames: int
limited_correction_frames: intVocalErrorDetail is the structured detail carried by a vocal-edit error: it identifies the vocal reason and field, and records expected and actual values as text where available.
# Structured detail of a vocal-edit failure.
@dataclass(frozen=True, slots=True)
class VocalErrorDetail:
reason: VocalReason
field: str
expected: str
actual: str
expected_text: str
actual_text: strErrors
A failing vocal-edit call raises VocalEditError, a SonareError that also carries the native reason detail. reason is one of "none", "invalidInput", "revisionConflict", "sourceMismatch", "unsupported", "cancelled", "counterExhausted" and "invalidState"; "none" means no reason was recorded. field names the offending field, and expected and actual carry the numeric values when the detail has them (expected_text and actual_text otherwise).
try:
draft.commit(expected_revision=3)
except sonare.VocalEditError as error:
if error.reason == "revisionConflict":
print(error.field, error.expected, error.actual)# A native vocal-edit failure with structured vocal detail.
class VocalEditError(SonareError):
detail: VocalErrorDetail
def __init__(
self,
code: int,
message: str,
*,
reason: VocalReason = 'none',
field: str = '',
expected: int = 0,
actual: int = 0,
expected_text: str = '',
actual_text: str = '',
) -> None: ...
code: ErrorCode
entry: str | None
reason: ErrorReason | None
# The name of :attr:`code` shared by every binding, such as ``"InvalidParameter"``.
@property
def code_name(self) -> ErrorCodeName: ...