金子邦彦研究室人工知能Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア)librosa のインストールと動作確認(音声処理)(Python を使用)(Windows 上)

librosa のインストールと動作確認(音声処理)(Python を使用)(Windows 上)

librosa は, 音声,音楽の機能をもった Python のパッケージである. 主な機能としては,音源分離(music source separation),スペクトログラム, 音声ファイルの読み込み,テンポ(tempo)の推定がある.

[image]

目次

  1. 前準備
  2. librosa のインストール(Python,pip を使用)(Windows 上)
  3. 動作確認のため動かしてみる(Windows 上)

    文献

    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.

    関連する外部ページ

前準備

Git のインストール(Windows 上)

サイト内の関連ページ

Windows での Git のインストール: 別ページ »で説明

関連する外部ページ

Git の公式ページ: https://git-scm.com/

Python のインストール(Windows 上)

サイト内の関連ページ

関連する外部ページ

Python の公式ページ: https://www.python.org/

librosa のインストール(Python,pip を使用)(Windows 上)

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. インストール

    試してみたが,「pip install librosa」でインストールするよりも,下の手順の方がトラブルが少なそう.

    cd %HOMEPATH%
    rmdir /s /q librosa
    git clone --recursive https://github.com/librosa/librosa
    cd librosa
    python setup.py develop
    

    [image]
    (以下省略)

動作確認のため動かしてみる(Windows 上)

librosa の公式チュートリアル https://librosa.org/doc/latest/tutorial.html に記載のプログラムを実行してみる.

  1. 次の Python プログラムを実行する

    Python プログラムの実行: 別ページ »で説明

    Python のまとめ: 別ページ »にまとめ

    このプログラムはビートの推定を行う.

    # 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)
    

    [image]
  2. 次の Python プログラムを実行する

    このプログラムは 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)
    

    [image]
  3. 次の Python プログラムを実行する.Matplotlib を使うので,Jupyter QtConsoleJupyter ノートブック (Jupyter Notebook) の利用が便利である.

    次のプログラムは,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()
    

    [image]