LibROSA
とは、音楽やオーディオ解析が出来るPythonパッケージです。
Brian McFee氏らにより開発され、現在もアップデートされています。
現在のバージョンは0.8.0です。(2020年12月1日現在)
では、実際に使っていきましょう。
https://librosa.github.io/librosa/index.html
LibROSAでできること
- オーディオファイル入出力とデジタル信号処理
- 情報の表示機能
- 特徴抽出や特徴量操作による楽曲データの変形
- オンセット特定、ハミング検索、断片検索など
- ビートとテンポの数値化や抽出
- スペクトログラム分離、スペクトラム情報として取得
- エフェクトをかける
- 出力、外部ファイルとして保存
- テンポを考慮したセグメント分け
- 各種ユーティリティ
- フィルタ機能
インストール方法
pipでインストールする
pip install librosa
Anaconda環境の場合
conda install -c conda-forge librosa
Exampleをダウンロードする
git clone https://github.com/librosa/data.git /path/to/librosa-data
関数librosa.util.list_examples()は、各トラックの説明を確認することができます。
※バージョン0.9.0では非推奨となるため、ご自身で所定のフォルダへダウンロードする必要があります。
関数librosa.util.example_info()は、指定されたトラックのメタデータ及びライセンス情報を確認できます。下記の表は提供されているExampleの楽曲の説明です。
Key | Full name | Description |
---|---|---|
brahms | Brahms – Hungarian Dance #5 | A short performance of this piece, with soft note onsets and variable tempo. |
choice | Admiral Bob – Choice | A short drum and bass loop, good for demonstrating decomposition methods. |
fishin | Karissa Hobbs – Let’s Go Fishin’ | A folk/pop song with verse/chorus/verse structure and vocals. |
nutcracker | Tchaikovsky – Dance of the Sugar Plum Fairy | Orchestral piece included to demonstrate tempo and harmony features. |
trumpet | Mihai Sorohan – Trumpet loop | Monophonic trumpet recording, good for demonstrating pitch features. |
vibeace | Kevin Macleod – Vibe Ace | A vibraphone, piano, and bass combo. Previously the only included example. |
LibROSAを使ってみる
では、pythonコードでlibrosaをインポートしexampleのオーディをファイルのスペクトラムを表示してみます。
メルスケールのスペクトグラムを表示する
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as ms
ms.use('seaborn-muted')
%matplotlib inline
import librosa
import librosa.display
audio = librosa.util.example_audio_file()
y, sr = librosa.load(audio)
S = librosa.feature.melspectrogram(y, sr=sr, n_mels=128)
log_S = librosa.power_to_db(S, ref=np.max)
plt.figure(figsize=(10, 4))
librosa.display.specshow(log_S, sr=sr, x_axis='time', y_axis='mel')
plt.title('mel power spectrogram')
plt.colorbar(format='%+02.0f dB')

リズムや和音を検出して分離する
exampleのオーディをファイルをそれぞれ和音、リズムに分離し、波形を表示するサンプルコードはこちらの通りです。
# 和音とリズムに分離します。
y_harmonic, y_percussive = librosa.effects.hpss(y)
# 和音、リズムの波形を取得する
S_harmonic = librosa.feature.melspectrogram(y_harmonic, sr=sr)
S_percussive = librosa.feature.melspectrogram(y_percussive, sr=sr)
# パワースペクトログラムをdBに変換する
log_Sh = librosa.power_to_db(S_harmonic, ref=np.max)
log_Sp = librosa.power_to_db(S_percussive, ref=np.max)
plt.figure(figsize=(10, 4))
# メルスケールのスペクトログラムを表示します。
# 和音
plt.subplot(2,1,1)
librosa.display.specshow(log_Sh, sr=sr, y_axis='mel')
plt.title('mel power spectrogram (Harmonic)')
plt.colorbar(format='%+02.0f dB')
# リズム
plt.subplot(2,1,2)
librosa.display.specshow(log_Sp, sr=sr, x_axis='time', y_axis='mel')
plt.title('mel power spectrogram (Percussive)')
plt.colorbar(format='%+02.0f dB')

ビートとテンポを取得する
https://librosa.org/doc/latest/tutorial.html
# Beat tracking example
import librosa
# 1. Get the file path to an included audio example
filename = librosa.example('nutcracker')
# 2. Load the audio as a waveform `y`
# Store the sampling rate as `sr`
y, sr = librosa.load(filename)
# 3. Run the default beat tracker
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
print('Estimated tempo: {:.2f} beats per minute'.format(tempo))
# 4. Convert the frame indices of beat events into timestamps
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
ライブラリをインポートします。
import librosa
オーディオファイルをexampleから取得します
くるみ割り人形
filename = librosa.example('nutcracker')
ファイルネームを指定し、読み込みます。
変数yは波形、srはサンプリングレートが返ってきます。
y, sr = librosa.load(filename)
関数librosa.beat.beat_track()に引数、波形とサンプリングレートを渡します。
tempoにテンポ、beat_framesにビートが返ってきます。
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
7.43 8.29 9.218 10.124 ...
ライブラリの使用例
ライブラリで利用できるAPIを実装したサンプルソースがこちらのリンクよりダウンロードが可能です。
Advanced examples — librosa 0.8.0 documentation
最後に
オーディオ解析ライブラリ、LibROSAを使用して波形の表示、分離を行いました。
機能が豊富でとても使いやすいのでぜひ利用してみましょう。