Skip to content

Python ボーカル編集の型 ​

VocalEditSession、VocalEditDraft、VocalRenderSnapshot、VocalRenderJob が受け取り、返すレコードと、これらが送出する VocalEditError です。セッション、ドラフト、レンダリングの呼び出しは Python プロジェクト API にあり、全体の流れは オフラインボーカル編集 で説明しています。

レコードはすべて frozen dataclass です。サンプル位置は、出力位置と明記されたフィールドを除き、解析したソースのサンプル数で数えます。NDArray のフィールドはコピーです。

解析とケイパビリティ ​

VocalEditSession(samples, sample_rate) はモノラル音声を解析して VocalAnalysisResult にします。その中の VocalAnalysis は、フレームごとの f0_hz と voiced の配列と、解析に使った設定を保持します。source_sha256 と analysis_sha256 は、ソースと解析結果の 16 進 SHA-256 ダイジェストです。VocalEditSession は、既存の VocalAnalysis を analysis 引数で受け取ることもできます。VocalCapabilities は読み込んだエンジンが対応する内容を報告します。機能の判定には capabilities().features.vocal_edit と vocal_edit_abi_version() を使います。

python
# 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: float

範囲と状態トークン ​

VocalRange は半開区間で、end_sample は最後のサンプルの 1 つ後です。length は、空または逆転した範囲では 0 です。VocalStateToken はセッションまたはドラフトの状態を示します。revision はコミット済みの編集の回数で(committed_revision はその別名)、コミット済みの状態では draft_id と generation が 0 です。レンダリング結果と編集結果は、元になった状態のトークンを持ちます。

python
# 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: bool

ノートとピッチ ​

session.notes() と draft.notes() は VocalNotesResult を返します。検出された VocalNote のレコードと、隣り合うノートの間の VocalTransition(クロスフェード)です。ノートの median_hz、center_midi、f0_stability は、ピッチを測定できなかったときは None です。VocalNote.source_range() はソース上の範囲を VocalRange で返します。

evaluate_pitch(note_id) は VocalPitchResult を返します。ソース位置、測定ピッチ、目標ピッチ、実効ピッチ(MIDI ノート番号)のフレームごとの配列と、voiced と has_target のフラグです。

python
# 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: ...

ノートの編集 ​

VocalNoteEdit は 1 つのノートの編集全体で、既定値は手を加えていないノートの状態です。target_mode は "none"、"center"(単一のピッチ target_midi)、"curve"(VocalPitchPoint の並びである target_points)のいずれかです。formant_mode は "preserve" か "shift" で、"shift" のときは VocalCapabilities が報告する範囲内で formant_shift_semitones が適用されます。

python
# 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] = ()

編集操作 ​

draft.apply(operations) は次のレコードのイテラブルを受け取ります。VocalOperation は VocalSetNoteEdit、VocalSetNoteSourceSpan、VocalSplitNote、VocalMergeNotes、VocalSetTransition、VocalRemoveTransition、VocalResetNotes のいずれかです。VocalSplitNote と VocalSetNoteSourceSpan は、位置をサンプル数でも秒でも指定できます。VocalMergeNotes.merge_policy は、"preserve"(結合したノートが最初のノートの編集を引き継ぐ)か "reset"(既定値から始める)です。

python
# 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]

結果 ​

apply、commit、undo、redo は VocalEditResult を返します。新しい token、変化した出力範囲(dirty_ranges)、呼び出しで置き換わったノート ID の VocalIdChange です。レンダリングは VocalRenderResult を返し、samples は出力位置 start_sample から始まります。processed_ranges、cache_hit_units、dry_passed_frames、limited_correction_frames はレンダリングの統計です。

python
# 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: int

VocalErrorDetail はボーカル編集エラーに含まれる構造化情報です。理由とフィールドを示し、利用可能な場合は期待値と実際の値をテキストで記録します。

python
# 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: str

エラー ​

ボーカル編集の呼び出しが失敗すると、SonareError であり、ネイティブの理由の詳細も持つ VocalEditError が送出されます。reason は "none"、"invalidInput"、"revisionConflict"、"sourceMismatch"、"unsupported"、"cancelled"、"counterExhausted"、"invalidState" のいずれかで、"none" は理由が記録されなかったことを表します。field は問題のあるフィールド名です。expected と actual は詳細が数値を持つときの値で、そうでなければ expected_text と actual_text に文字列で入ります。

python
try:
    draft.commit(expected_revision=3)
except sonare.VocalEditError as error:
    if error.reason == "revisionConflict":
        print(error.field, error.expected, error.actual)
python
# 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: ...