1.WAVEファイルを読み込む
import librosa filename = "test.wav" y, sr = librosa.load(filename)
2.12半音の強さ(クロマグラム)を取得し、matplotlibで出力する
import matplotlib.pyplot as plt pos=20 #クロマグラムを取得する楽曲の位置を指定 y_harmonic, y_percussive = librosa.effects.hpss(y) #打楽器の音を除く chromagram = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr) #クロマグラムを取得 X=[_ for _ in range(0,12)] Y=[chromagram[_][pos] for _ in range(0,12)] plt.bar(X,Y, align="center") plt.xticks(X,["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]) plt.show()
3.クロマグラムを数値としても出力する
for i,_ in enumerate(["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]): print(_,chromagram[i][pos])
4.楽曲を通してのクロマグラムの遷移をmatplotlibで出力する
for i,_ in enumerate(["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]): plt.plot(chromagram[i],label=_) plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left') plt.show()
Pythonは何をするにしてもライブラリが充実していて良いですね
参考:http://librosa.github.io/librosa/tutorial.html?highlight=chromagram