Python エフェクト API
libsonare Python パッケージのオーディオエフェクトとエフェクト関数のリファレンスです。ルーム音響は Python ルーム音響 API、リアルタイムボイスチェンジャーとストリーミング EQ は Python ストリーミング API にあります。フレーミング、正規化、テンポグラムのヘルパーは Python メータリングと信号ヘルパー API、ノートと打撃イベントの編集は Python ノート編集 API にあります。インストール、クイックスタート、その他の API ファミリーは Python API の索引ページを参照してください。
オーディオエフェクト
from libsonare import Audio
audio = Audio.from_file("music.mp3")
# HPSS(倍音成分/打撃成分の分離)
hpss_result = audio.hpss()
harmonic = audio.harmonic()
percussive = audio.percussive()
# タイムストレッチ / ピッチシフト
stretched = audio.time_stretch(speed_ratio=1.5) # 1.5倍速
shifted = audio.pitch_shift(pitch_semitones=2.0) # 2半音上げ
# 正規化と無音トリム
normalized = audio.normalize(target_db=-3.0)
trimmed = audio.trim(threshold_db=-60.0)
# リサンプリング
resampled = audio.resample(target_sample_rate=44100)時間/周波数の領域指定編集には spectral_edit(samples, sample_rate, [SpectralRegionOp(...)]) を使います。詳しくは スペクトル編集 を参照してください。
# Region-based spectral editing (STFT -> per-op bin/frame masking -> iSTFT).
def spectral_edit(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
ops: Sequence[SpectralRegionOp],
*,
n_fft: int | None = None,
hop_length: int | None = None,
window: WindowType = 'hann',
heal_radius_frames: int | None = None,
) -> NDArray[np.float32]: ...エフェクト関数
# Perform harmonic-percussive source separation.
def hpss(
samples: Sequence[float] | list[float],
sample_rate: int,
kernel_harmonic: int = 31,
kernel_percussive: int = 31,
n_fft: int = _DEFAULT_EFFECT_N_FFT,
hop_length: int | None = None,
hard_mask: bool = False,
with_residual: bool = False,
) -> HpssResult: ...
# Extract the harmonic component of audio.
def harmonic(samples: Sequence[float] | list[float], sample_rate: int) -> np.ndarray: ...
# Extract the percussive component of audio.
def percussive(samples: Sequence[float] | list[float], sample_rate: int) -> np.ndarray: ...
# Time-stretch audio without changing pitch.
def time_stretch(
samples: Sequence[float] | list[float],
sample_rate: int,
speed_ratio: float,
n_fft: int = _DEFAULT_EFFECT_N_FFT,
hop_length: int | None = None,
) -> np.ndarray: ...
# Shift the pitch of audio.
def pitch_shift(
samples: Sequence[float] | list[float],
sample_rate: int,
pitch_semitones: float,
n_fft: int = _DEFAULT_EFFECT_N_FFT,
hop_length: int | None = None,
) -> np.ndarray: ...| 関数 | 説明 |
|---|---|
hpss | 倍音成分/打撃成分の分離(HPSS)。既定は kernel_harmonic=31、kernel_percussive=31、n_fft=2048、hop_length=None(n_fft の 4 分の 1)、hard_mask=False。with_residual=True で residual も埋まります |
harmonic | 倍音成分を抽出 |
percussive | 打撃成分を抽出 |
time_stretch | ピッチを変えずにテンポ変更。既定は n_fft=2048、hop_length=None(n_fft の 4 分の 1) |
pitch_shift | テンポを変えずにピッチ変更。既定は n_fft=2048、hop_length=None(n_fft の 4 分の 1) |
# Pitch-correct audio from a current MIDI note to a target MIDI note.
def pitch_correct_to_midi(
samples: Sequence[float] | list[float],
sample_rate: int,
current_midi: float,
target_midi: float,
) -> np.ndarray: ...
# Contour-following pitch correction toward a fixed MIDI note OR a scale.
def pitch_correct_timevarying(
samples: Sequence[float] | list[float],
sample_rate: int,
f0_hz: Sequence[float] | list[float],
hop_length: int = 512,
*,
target_mode: PitchTargetMode = 'midi',
target_midi: float = 69.0,
scale_root: int = 0,
scale_mode_mask: int | KeyMode | None = None,
scale_reference_midi: float | None = None,
retune_amount: float | None = None,
max_correction_semitones: float | None = None,
retune_speed_ms: float | None = None,
vibrato_threshold_cents: float | None = None,
voiced: Sequence[int] | list[int] | None = None,
voiced_prob: Sequence[float] | list[float] | None = None,
) -> np.ndarray: ...
# Snap the voiced pitch of a monophonic recording to a scale, offline.
def auto_tune(
samples: Sequence[float] | list[float],
sample_rate: int,
key: str | Key | Mapping[str, object] | tuple[PitchClass, KeyMode] = 'detect',
*,
retune_amount: float | None = None,
retune_speed_ms: float | None = None,
vibrato_threshold_cents: float | None = None,
max_correction_semitones: float | None = None,
scale_reference_midi: float | None = None,
) -> AutoTuneResult: ...
# Apply a voice-change effect with independent pitch and formant control.
def voice_change(
samples: Sequence[float] | list[float],
sample_rate: int,
pitch_semitones: float = 0.0,
formant_factor: float = 1.0,
formant_mode: FormantMode = 'relative',
) -> np.ndarray: ...
# Apply the realtime voice changer to a buffer offline.
def realtime_voice_changer_render(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
config: VoicePresetId | RealtimeVoiceChangerPreset | RealtimeVoiceChangerConfig | None = 'neutralMonitor',
num_channels: int = 1,
) -> np.ndarray: ...| 関数 | 説明 |
|---|---|
pitch_correct_to_midi | 目標 MIDI ノートへピッチ補正 |
pitch_correct_timevarying | 輪郭に沿うピッチ補正。フレームごとの f0_hz の輪郭に沿って有声フレームを寄せ、ビブラートやドリフトを平坦化せず保持します。補正先は固定のノート(target_mode="midi")またはスケールの最も近い構成音(target_mode="scale")です。scale_mode_mask には 12 ビットの整数マスクまたは KeyMode 文字列を指定します。None は C メジャーを選びます。リチューン系のつまみで自然さと機械的な効き方を調整できる |
auto_tune | 単音のテイクを、検出した("detect")または指定した(Key、{"root", "mode"}、(root, mode))キーに合わせる。.samples が補正後の音声、.key が使われたキー |
voice_change | ピッチとフォルマントを独立にシフト。formant_mode は "relative"(既定。formant_factor は [0.55, 1.65] の範囲でピッチシフトの後にかけるワープ)か "absolute"(formant_factor はピッチシフトに関係なく入力に対するフォルマントのシフト量。1.0 ならフォルマントは元の位置のまま)。範囲外の relative の係数と、ワープ formant_factor / 2**(pitch_semitones / 12) が同じ範囲を外れる absolute の指定は、到達できる formant_factor の範囲を示した SonareValueError になります |
realtime_voice_changer_render | リアルタイム音声プリセットチェーンで 1 回レンダリング。config はプリセット ID、設定マッピング、RealtimeVoiceChangerConfig のいずれかで、2 チャンネルはインターリーブです |
# Time-stretch a single note region without changing pitch.
def note_stretch(
samples: Sequence[float] | list[float],
sample_rate: int,
onset_sample: int | None = None,
offset_sample: int | None = None,
stretch_ratio: float = 1.0,
*,
onset_sec: float | None = None,
offset_sec: float | None = None,
) -> np.ndarray: ...
# Move a note region to a new onset without changing its duration.
def note_move(
samples: Sequence[float] | list[float],
sample_rate: int,
onset_sample: int | None = None,
offset_sample: int | None = None,
target_onset_sample: int | None = None,
*,
onset_sec: float | None = None,
offset_sec: float | None = None,
target_onset_sec: float | None = None,
) -> np.ndarray: ...| 関数 | 説明 |
|---|---|
note_stretch | 単一ノート区間をその場でストレッチ |
note_move | ノート区間の長さを変えずに、新しいオンセット位置へ移動。target_onset_sample と target_onset_sec のどちらか一方が必須 |
# Normalize audio so its peak or RMS level reaches ``target_db``.
def normalize(
samples: Sequence[float] | list[float],
sample_rate: int,
*,
mode: NormalizeMode = 'peak',
target_db: float | None = None,
) -> np.ndarray: ...
# Normalize a stereo pair on one gain measured across both channels.
def normalize_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
mode: NormalizeMode = 'peak',
target_db: float | None = None,
) -> NormalizeStereoResult: ...
# Reorder / concatenate a signal by interval slices (librosa.effects.remix).
def remix(
samples: Sequence[float] | list[float],
intervals: npt.ArrayLike,
sr: int = 22050,
align_zeros: bool = True,
) -> np.ndarray: ...
# Resolve the cut points :func:`remix` would use, without cutting.
def remix_aligned_intervals(
samples: Sequence[float] | list[float],
sample_rate: int,
intervals: npt.ArrayLike,
align_zeros: bool = True,
) -> np.ndarray: ...
# Trim silence from the beginning and end of audio.
def trim(
samples: Sequence[float] | list[float],
sample_rate: int,
threshold_db: float = -60.0,
frame_length: int = _DEFAULT_TRIM_FRAME_LENGTH,
hop_length: int = _DEFAULT_TRIM_HOP_LENGTH,
) -> np.ndarray: ...
# Resample audio to a different sample rate.
def resample(
samples: Sequence[float] | list[float],
sample_rate: int,
target_sample_rate: int,
) -> np.ndarray: ...| 関数 | 説明 |
|---|---|
normalize | 目標レベルへ正規化。mode は "peak"(既定、target_db の既定は 0.0)または "rms"(target_db の既定は -20.0) |
normalize_stereo | ペア共通の 1 つのゲインで正規化。mode と target_db は normalize と同じです |
remix | (n, 2) の区間スライスで並べ替え/連結。align_zeros は既定 True |
remix_aligned_intervals | remix が使う切り出し位置だけを解決する(切り出しはしない)。align_zeros は既定 True |
trim | 無音区間をトリム(既定: -60.0 dB、frame_length=2048、hop_length=512) |
resample | 目標サンプルレートへリサンプリング |
編集位置はサンプル数か秒(*_sec)のどちらか一方で指定します。両方を渡すと拒否されます。SpectralRegionOp も同様に start_sec / end_sec を受け取ります。
trim(...) は単純なしきい値ベースの編集ヘルパーです。Python メータリングと信号ヘルパー APIの trim_silence(...) はフレーム RMS と top_db を使い、トリム後の音声と元音源上のサンプル範囲を返します。
ステレオペアの正規化
normalize_stereo(...) は JavaScript 側と同じく mode("peak" または "rms")を取ります。レベルをペア全体で測り、1 つの共通ゲインを両チャンネルに適用します。これがステレオイメージを保つ仕組みです。チャンネルごとに自前のゲインで正規化すると、2 つのピークが揃うまで小さい側が持ち上がり、レベルではなく定位バランスが変わってしまいます。両チャンネルが無音なら SonareValueError になります。浮動小数点の出力はフルスケールを超えることがあります。結果は left/right に加えて、ペア共通のサンプル数 length を保持します。
どちらかのチャンネルが空の場合、および 2 つのチャンネルの長さが一致しない場合は拒否されます。C のエントリポイントはペアに対して 1 つのサンプルレートを取るので、2 つのチャンネルがサンプルレートで食い違うことはありません。
result = sonare.normalize_stereo(left, right, 48000, target_db=-3.0)
print(result.length)マルチチャンネル素材を共通のフレームで切る
remix(...)(align_zeros の既定は True)はスライス境界を信号のゼロクロス位置に合わせますが、これは信号ごとの判断です。remix をチャンネルごとに呼ぶと各チャンネルが別々のフレームに合わせられ、ステレオ素材がずれていきます。remix_aligned_intervals(...) は 1 つのチャンネルから切り出し位置を 1 セットだけ解決し(クランプ済みの (start, end) 行を並べた (n, 2) 配列)、remix に align_zeros=False を渡すことで同じフレームで全チャンネルを切り出せるようにします。
位置合わせでスライスが消えないよう、ガードが 2 つあります。符号変化がまったくない信号(無音、DC オフセット、あらゆる定数)は位置合わせされません。また、内容があったのに位置合わせ後に空へ潰れるスライスは、元の境界を保ちます。
cuts = sonare.remix_aligned_intervals(left, 48000, [(0, 48000), (96000, 144000)])
left_out = sonare.remix(left, cuts, sr=48000, align_zeros=False)
right_out = sonare.remix(right, cuts, sr=48000, align_zeros=False)