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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
- from __future__ import division, print_function
- from os import listdir
- from os.path import isfile, join
- from matplotlib import pyplot as plt
- import scipy.io.wavfile as wav
- def wav_to_spectrogram(audio_path, save_path, spectrogram_dimensions=(64, 64), noverlap=16, cmap='gray_r'):
- """ Creates a spectrogram of a wav file.
- :param audio_path: path of wav file
- :param save_path: path of spectrogram to save
- :param spectrogram_dimensions: number of pixels the spectrogram should be. Defaults (64,64)
- :param noverlap: See http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
- :param cmap: the color scheme to use for the spectrogram. Defaults to 'gray_r'
- :return:
- """
- sample_rate, samples = wav.read(audio_path)
- fig = plt.figure()
- fig.set_size_inches((spectrogram_dimensions[0]/fig.get_dpi(), spectrogram_dimensions[1]/fig.get_dpi()))
- ax = plt.Axes(fig, [0., 0., 1., 1.])
- ax.set_axis_off()
- fig.add_axes(ax)
- ax.specgram(samples, cmap=cmap, Fs=2, noverlap=noverlap)
- ax.xaxis.set_major_locator(plt.NullLocator())
- ax.yaxis.set_major_locator(plt.NullLocator())
- fig.savefig(save_path, bbox_inches="tight", pad_inches=0)
- def dir_to_spectrogram(audio_dir, spectrogram_dir, spectrogram_dimensions=(64, 64), noverlap=16, cmap='gray_r'):
- """ Creates spectrograms of all the audio files in a dir
- :param audio_dir: path of directory with audio files
- :param spectrogram_dir: path to save spectrograms
- :param spectrogram_dimensions: tuple specifying the dimensions in pixes of the created spectrogram. default:(64,64)
- :param noverlap: See http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
- :param cmap: the color scheme to use for the spectrogram. Defaults to 'gray_r'
- :return:
- """
- file_names = [f for f in listdir(audio_dir) if isfile(join(audio_dir, f)) and '.wav' in f]
- for file_name in file_names:
- print(file_name)
- audio_path = audio_dir + file_name
- spectogram_path = spectrogram_dir + file_name.replace('.wav', '.png')
- wav_to_spectrogram(audio_path, spectogram_path, spectrogram_dimensions=spectrogram_dimensions, noverlap=noverlap, cmap=cmap)
- if __name__ == '__main__':
- audio_dir = "/Users/Jackson/development/free-spoken-digit-dataset/recordings/"
- spectrogram_dir = "/Users/Jackson/development/free-spoken-digit-dataset/spectrograms/"
- dir_to_spectrogram(audio_dir, spectrogram_dir)
|