librosa は, 音声,音楽の機能をもった Python のパッケージである. 主な機能としては,音源分離(music source separation),スペクトログラム, 音声ファイルの読み込み,テンポ(tempo)の推定がある.
【目次】
【文献】
McFee, Brian, Colin Raffel, Dawen Liang, Daniel PW Ellis, Matt McVicar, Eric Battenberg, and Oriol Nieto. “librosa: Audio and music signal analysis in python.” In Proceedings of the 14th python in science conference, pp. 18-25. 2015.
【関連する外部ページ】
【サイト内の関連ページ】
Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
コマンドプロンプトを管理者として実行: 別ページ »で説明
試してみたが,「pip install librosa」でインストールするよりも,下の手順の方がトラブルが少なそう.
cd %HOMEPATH% rmdir /s /q librosa git clone --recursive https://github.com/librosa/librosa cd librosa python setup.py develop
librosa の公式チュートリアル 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) print(beat_times)
このプログラムは beat feature の算出を行う.
# Feature extraction example import numpy as np import librosa # Load the example clip y, sr = librosa.load(librosa.ex('nutcracker')) # Set the hop length; at 22050 Hz, 512 samples ~= 23ms hop_length = 512 # Separate harmonics and percussives into two waveforms y_harmonic, y_percussive = librosa.effects.hpss(y) # Beat track on the percussive signal tempo, beat_frames = librosa.beat.beat_track(y=y_percussive, sr=sr) # Compute MFCC features from the raw signal mfcc = librosa.feature.mfcc(y=y, sr=sr, hop_length=hop_length, n_mfcc=13) # And the first-order differences (delta features) mfcc_delta = librosa.feature.delta(mfcc) # Stack and synchronize between beat events # This time, we'll use the mean value (default) instead of median beat_mfcc_delta = librosa.util.sync(np.vstack([mfcc, mfcc_delta]), beat_frames) # Compute chroma features from the harmonic signal chromagram = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr) # Aggregate chroma features between beat events # We'll use the median value of each feature between beat frames beat_chroma = librosa.util.sync(chromagram, beat_frames, aggregate=np.median) # Finally, stack all beat-synchronous features together beat_features = np.vstack([beat_chroma, beat_mfcc_delta]) print(beat_features)
次のプログラムは,librosa に付属の音声データである trumpet について,パワースペクトログラムを表示する. 次のプログラムでは,横軸は時間,縦軸は線形スケール(linear scale)で表された周波数である パワースペクトログラムを表示する. stft は short-time Fourier transform を行う. そして,その振幅により色をプロットする.
ここのプログラムのソースコードは, http://librosa.org/doc/main/auto_examples/plot_display.html#sphx-glr-auto-examples-plot-display-py のものを使用(ISC ライセンス).
import numpy as np %matplotlib inline import matplotlib.pyplot as plt import warnings warnings.filterwarnings('ignore') # Suppress Matplotlib warnings import librosa import librosa.display y, sr = librosa.load(librosa.ex('trumpet')) D = librosa.stft(y) # STFT of y S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max) plt.figure() librosa.display.specshow(S_db, x_axis='time', y_axis='linear', sr=sr) plt.colorbar()