Last active
May 19, 2020 16:39
-
-
Save ilkermanap/983d5754bbf73b0114a769553a97f262 to your computer and use it in GitHub Desktop.
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 matplotlib.pyplot as plt | |
import librosa.display | |
import os | |
import glob | |
import numpy as np | |
#import pandas as pd | |
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas | |
import librosa | |
import sys | |
def save_spectrogram(filename, sample_length): | |
""" | |
filename of wav file | |
sample_length in seconds | |
""" | |
y, sr = librosa.load(filename) | |
slen = int(sr * sample_length) | |
#if slen < len(y): # requested sample length is larger then the file | |
window_size = 1024 | |
window = np.hanning(window_size) | |
for sample_start in range(0, len(y), slen): | |
audio_part = y[sample_start:sample_start + slen] | |
plt.figure(figsize=(12,5)) | |
plt.subplot(2, 1, 1) | |
X= librosa.stft(audio_part) | |
Xdb = librosa.amplitude_to_db(abs(X)) | |
librosa.display.specshow(X, sr=sr, y_axis='hz', x_axis='time') | |
plt.subplot(2, 1, 2) | |
librosa.display.specshow(Xdb, sr=sr, y_axis='log', x_axis='time') | |
plt.savefig(f"{filename}_{sample_start / sr}.png") | |
if __name__ == "__main__": | |
dirname = sys.argv[1] | |
sample_length = float(sys.argv[2]) | |
files = glob.glob(f"{dirname}/*.wav") | |
for fname in files: | |
save_spectrogram(fname, sample_length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment