File size: 1,002 Bytes
6a4e8bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import numpy as np
import librosa.display
import matplotlib.pyplot as plt
import os
# rename files in ./data/*
def create_spectrogram(audio_file, image_file):
y, sr = librosa.load(audio_file)
plt.figure(figsize=(12, 8))
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
librosa.display.specshow(D, y_axis='linear')
plt.colorbar(format='%+2.0f dB')
plt.savefig(image_file, bbox_inches='tight', pad_inches=0.0)
plt.close()
def create_pngs_from_wavs(input_path, output_path):
if not os.path.exists(output_path):
os.makedirs(output_path)
dir = os.listdir(input_path)
for i, file in enumerate(dir):
input_file = os.path.join(input_path, file)
output_file = os.path.join(output_path, file.replace('.wav', '.png'))
create_spectrogram(input_file, output_file)
create_pngs_from_wavs('./data/a/', './data/a_png/')
create_pngs_from_wavs('./data/b/', './data/b_png/')
create_pngs_from_wavs('./data/c/', './data/c_png/')
|