1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | from aubio import pvoc, source |
---|
5 | from numpy import array, arange, zeros, shape, log10, vstack |
---|
6 | from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks |
---|
7 | |
---|
8 | def get_spectrogram(filename): |
---|
9 | samplerate = 44100 |
---|
10 | win_s = 512 # fft window size |
---|
11 | hop_s = win_s / 2 # hop size |
---|
12 | fft_s = win_s / 2 + 1 # spectrum bins |
---|
13 | |
---|
14 | a = source(filename, samplerate, hop_s) # source file |
---|
15 | pv = pvoc(win_s, hop_s) # phase vocoder |
---|
16 | specgram = zeros([0, fft_s], dtype='float32') # numpy array to store spectrogram |
---|
17 | |
---|
18 | # analysis |
---|
19 | while True: |
---|
20 | samples, read = a() # read file |
---|
21 | specgram = vstack((specgram,pv(samples).norm)) # store new norm vector |
---|
22 | if read < a.hop_size: break |
---|
23 | |
---|
24 | # plotting |
---|
25 | imshow(log10(specgram.T + .001), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r) |
---|
26 | axis([0, len(specgram), 0, len(specgram[0])]) |
---|
27 | # show axes in Hz and seconds |
---|
28 | time_step = hop_s / float(samplerate) |
---|
29 | total_time = len(specgram) * time_step |
---|
30 | print "total time: %0.2fs" % total_time, |
---|
31 | print ", samplerate: %.2fkHz" % (samplerate / 1000.) |
---|
32 | n_xticks = 10 |
---|
33 | n_yticks = 10 |
---|
34 | xticks_pos = [ x / float(n_xticks) * len(specgram) for x in range(n_xticks) ] |
---|
35 | xticks_str = [ "%.2f" % (x * total_time / float(n_xticks)) for x in range(n_xticks) ] |
---|
36 | xticks( xticks_pos , xticks_str ) |
---|
37 | yticks_pos = [ y / float(n_yticks) * len(specgram[0]) for y in range(n_yticks) ] |
---|
38 | yticks_str = [ "%.2f" % (y * samplerate / 2000. / float(n_yticks)) for y in range(n_yticks) ] |
---|
39 | yticks( yticks_pos , yticks_str ) |
---|
40 | ylabel('Frequency (kHz)') |
---|
41 | xlabel('Time (s)') |
---|
42 | |
---|
43 | if __name__ == '__main__': |
---|
44 | if len(sys.argv) < 2: |
---|
45 | print "Usage: %s <filename>" % sys.argv[0] |
---|
46 | else: |
---|
47 | for soundfile in sys.argv[1:]: |
---|
48 | get_spectrogram(soundfile) |
---|
49 | # display graph |
---|
50 | show() |
---|