blob: 1ea9ce545593893ab919ff1d63028839cc42a941 (
plain)
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
|
import sys
import librosa
import numpy
import scipy
import matplotlib.pyplot as plt
if len(sys.argv) < 3:
print "python spectrum.py in.aiff out.png"
sys.exit()
IN_FILENAME = sys.argv[1]
OUT_FILENAME = sys.argv[2]
N_FFT = 2048
def read_audio_spectum(filename):
x, fs = librosa.load(filename, 44100)
S = librosa.stft(x, N_FFT)
S = numpy.log1p(numpy.abs(S))
return S, fs
a_spectrum, fs = read_audio_spectum(IN_FILENAME)
a_spectrum = numpy.flipud(a_spectrum)
print a_spectrum.shape
fig = plt.figure(figsize=(5, int(len(a_spectrum[0])/100)))
img = plt.imshow(a_spectrum)
img.set_cmap('hot')
plt.set_cmap('hot')
plt.axis('off')
plt.savefig(OUT_FILENAME, transparent=True, bbox_inches='tight', pad_inches=0)
|