| | --- |
| | license: cc-by-sa-4.0 |
| | language: |
| | - en |
| | tags: |
| | - music |
| | - spectrogram |
| | size_categories: |
| | - n<1K |
| | --- |
| | |
| | ## Google/MusicCapsをスペクトログラムにしたデータ。 |
| |
|
| | ### データ作った方法 |
| | ```python |
| | from PIL import Image |
| | import IPython.display |
| | import cv2 |
| | |
| | # 1. wavファイルを解析 |
| | y, sr = librosa.load("wavファイルなど") |
| | |
| | # 2. フーリエ変換を適用して周波数成分を取得 |
| | D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) # librosaを用いてデータを作る |
| | image = Image.fromarray(np.uint8(D), mode='L') # 'L'は1チャンネルのグレースケールモードを指定します |
| | image.save('spectrogram_{}.png') |
| | ``` |
| |
|
| | ### ♪復元方法 |
| | ```python |
| | im = Image.open("pngファイル") |
| | db_ud = np.uint8(np.array(im)) |
| | amp = librosa.db_to_amplitude(db_ud) |
| | print(amp.shape) |
| | # (1025, 861)は20秒のwavファイルをスペクトログラムにした場合 |
| | # (1025, 431)は10秒のwavファイルをスペクトログラムにした場合 |
| | # (1025, 216)は5秒のwavファイルをスペクトログラムにした場合 |
| | |
| | y_inv = librosa.griffinlim(amp*200) |
| | display(IPython.display.Audio(y_inv, rate=sr)) |
| | ``` |
| |
|