音頻特征提取——librosa工具包使用

分類:IT技術 時間:2017-05-07

作者:桂。

時間:2017-05-06  11:20:47

鏈接:http://www.cnblogs.com/xingshansi/p/6816308.html 


前言

 本文主要記錄librosa工具包的使用,librosa在音頻、樂音信號的分析中經常用到,是python的一個工具包,這裏主要記錄它的相關內容以及安裝步驟,用的是python3.5以及win8.1環境。

一、MIR簡介

音樂信息檢索(Music information retrieval,MIR)主要翻譯自wikipedia.

MIR是從音樂中檢索信息的跨學科科學,該領域需要心理學、樂理學、信號處理、機器學習等學科的背景知識。

目前MIR的商業應用主要包括:

  • 推薦系統
    目前音樂推薦的應用很多,但很少是基於MIR技術實現的,現在主流技術是通過人工標記或者用戶的評論以及收聽歷史等簡介數據進行分類判斷,進而實現推薦,但事實上不同音樂本身的相似性是很多的
  • 背景音樂分離及樂器識別
    如實現歌聲與背景音樂的分離,並從背景音樂中識別出是何種樂器在演奏
  • 自動錄音
    例如根據音樂自動轉換成MIDI文件或者樂譜
  • 音樂分類
    根據音樂的產地、藝術家身份、音樂節奏等特征,借助機器學習方法進行音樂分類
  • 自動生成音樂
    利用數據庫訓練模式,讓機器自主創造音樂

MIR領域涉及到的知識包括:

  • 語料庫:沒有音樂庫,利用機器學習挖掘歷史出統計規律,是不夠現實的
  • 特征提取:例如常見的MFCC,是音色的一種度量,另外和弦、和聲、節奏等音樂的特性,都需要合適的特征來進行表征
  • 統計學習方法以及機器學習的相關知識

MIR用到的相關工具包可以參考isMIR主頁。

 

二、Librosa功能簡介

librosa對於MIR來講就是特征提取的工具,當然一般音頻分析也可以借用librosa。

  A-主要功能

更多細節可以參考其主頁。

  • 音頻處理

load:讀取文件,可以是wav、mp3等格式;resample:重采樣;get_duration:計算音頻時長;autocorrelate:自相關函數;zero crossings:過零率;

  • 頻譜特性

stft:短時傅裏葉變換;istft:逆短時傅裏葉變換;ifgram:瞬時頻率;cqt:音樂中常用的CQT算法(constant-Q transform);hybrid cqt:混合CQT變換;fmt:快速梅林變換;interp harmonics:主要計算時頻信號中諧波的能量;salience:諧波顯示功能;phase vocoder:相位聲碼;magphase:相位幅值

  • 幅度

就是一些數值不同度量的轉化。

  • 時頻轉化

這個比較直觀,就不啰嗦了。

  • Pitch and tuning(音調和曲調?清楚的麻煩說一下二者具體區別)

  • Dynamic Time Warping

就是DWT,動態時間規整。

以上只是一部分,其他的功能還有很多:

例如常用的MFCC提取就是Feature extraction中的一個函數而已。

  B-常用功能

比如讀取一個音頻信號:

import librosa
# 1. Get the file path to the included audio example
filepath = 'C:\\Users\\Nobleding\\Documents\\FileRecv\\'
filename =filepath+'bluesky.wav'
# 2. Load the audio as a waveform `y`
#    Store the sampling rate as `sr`
y, sr = librosa.load(filename,sr=None)

  load默認的采樣率是22050,如果需要讀取原始采樣率,需要.load(filename,sr=None)而不是load(filename)

例如讀取一段音頻,判斷節奏,並畫出時頻特性:

# Beat tracking example
#from __future__ import print_function
import librosa
import matplotlib.pyplot as plt
import librosa.display

# 1. Get the file path to the included audio example
# Sonify detected beat events
y, sr = librosa.load(librosa.util.example_audio_file())
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
y_beats = librosa.clicks(frames=beats, sr=sr)

# Or generate a signal of the same length as y
y_beats = librosa.clicks(frames=beats, sr=sr, length=len(y))

# Or use timing instead of frame indices
times = librosa.frames_to_time(beats, sr=sr)
y_beat_times = librosa.clicks(times=times, sr=sr)

# Or with a click frequency of 880Hz and a 500ms sample
y_beat_times880 = librosa.clicks(times=times, sr=sr,
                                 click_freq=880, click_duration=0.5)

# Display click waveform next to the spectrogram
plt.figure()
S = librosa.feature.melspectrogram(y=y, sr=sr)
ax = plt.subplot(2,1,2)
librosa.display.specshow(librosa.power_to_db(S, ref=np.max),
                         x_axis='time', y_axis='mel')
plt.subplot(2,1,1, sharex=ax)
librosa.display.waveplot(y_beat_times, sr=sr, label='Beat clicks')
plt.legend()
plt.xlim(15, 30)
plt.tight_layout()

關於可視化多說兩句,librosa.display模塊並不默認包含在librosa中,所以開頭兩句都要有:

import librosa
import librosa.display

  例如這個時候想顯示語譜圖:

import librosa
import matplotlib.pyplot as plt
import numpy as np
import librosa.display

# 1. Get the file path to the included audio example
filepath = 'C:\\Users\\Nobleding\\Documents\\FileRecv\\'
filename =filepath+'bluesky1.wav'
# 2. Load the audio as a waveform `y`
#    Store the sampling rate as `sr`
y, sr = librosa.load(filename,sr=None)

plt.figure(figsize=(12, 8))
D = librosa.amplitude_to_db(librosa.stft(y), ref=np.max)
plt.subplot(4, 2, 1)
librosa.display.specshow(D, y_axis='linear')
plt.colorbar(format='%+2.0f dB')
plt.title('Linear-frequency power spectrogram')

例如想觀察CQT變換:

CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
plt.subplot(4, 2, 3)
librosa.display.specshow(CQT, y_axis='cqt_note')
plt.colorbar(format='%+2.0f dB')
plt.title('Constant-Q power spectrogram (note)')

  其他以此類推。

MFCC提取:

import librosa
import librosa.display

# 1. Get the file path to the included audio example
# Sonify detected beat events
y, sr = librosa.load(librosa.util.example_audio_file())
librosa.feature.mfcc(y=y, sr=sr)

librosa在YouTube上有簡要的教程。

 

三、librosa的安裝

libsora對應的鏈接點擊這裏。安裝報錯兩個:

關於Microsoft visual c++ 14.0 :

解決思路是:

  • Download Microsoft Visual C++ Build Tools 2015
  • Install this, making sure in the install options to select the “Windows SDK” appropriate for your version of Windows. Windows 7 systems should use Windows 8.1 SDK.

找到visual C++下載頁面,點擊這裏:

安裝完成後,安裝resampy。

關於resampy(同樣依賴microsoft visual c++ 14.0):

resampy是采樣率轉化工具,github關於resampy的安裝包點擊這裏。

 cd到對應文件夾,我放在了\pkgs\lib文件夾內,輸入:

pip install resampy

  可以看到resampy已經成功安裝:

進一步安裝librosa,同樣放在\pkgs\lib文件夾內,cd到對應目錄,輸入:

pip install librosa

  即可完成librosa的安裝。

 

參考:

  •  librosa:http://librosa.github.io/librosa/core.html

Tags: 學習方法 心理學 python 藝術家 背景音樂

文章來源:


ads
ads

相關文章
ads

相關文章

ad