Created
October 11, 2019 13:34
-
-
Save evmcheb/1ee101789472c38fcab94b8b007278d7 to your computer and use it in GitHub Desktop.
leverage the librosa python library to extract a spectrogram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import librosa.display, os, gc | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def extract_spectrogram(fname, iname): | |
audio, sr = librosa.load(fname, res_type='kaiser_fast') | |
S = librosa.feature.melspectrogram(audio, sr=sr, n_mels=128) | |
log_S = librosa.power_to_db(S, ref=np.max) | |
fig = plt.figure(figsize=[1, 1]) | |
ax = fig.add_subplot(111) | |
fig.subplots_adjust(left=0,right=1,bottom=0,top=1) | |
ax.axis("off") | |
ax.axis("tight") | |
plt.margins(0) | |
librosa.display.specshow(log_S, sr=sr) | |
fig.savefig(iname, dpi=100, pad_inches=0) | |
plt.close(fig) | |
plt.close('all') | |
del audio, S, log_S, ax, fig | |
samples_folder = "soundscapes/" | |
images_folder = "images/" | |
already = os.listdir(images_folder) | |
d = os.listdir(samples_folder) | |
for i, f in enumerate(d): | |
extract_spectrogram(samples_folder+f, f"{images_folder}/{f.replace('.wav', '.png')}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment