4 from aubio import source
5 from numpy import zeros, hstack
7 def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None, downsample = 2**4):
8 import matplotlib.pyplot as plt
11 ax = fig.add_subplot(111)
14 allsamples_max = zeros(0,)
15 downsample = downsample # to plot n samples / hop_s
17 a = source(filename, samplerate, hop_s) # source file
18 if samplerate == 0: samplerate = a.samplerate
23 # keep some data to plot it later
24 new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0)
25 allsamples_max = hstack([allsamples_max, new_maxes])
27 if read < hop_s: break
28 allsamples_max = (allsamples_max > 0) * allsamples_max
29 allsamples_max_times = [ ( float (t) / downsample ) * hop_s for t in range(len(allsamples_max)) ]
31 ax.plot(allsamples_max_times, allsamples_max, '-b')
32 ax.plot(allsamples_max_times, -allsamples_max, '-b')
33 ax.axis(xmin = allsamples_max_times[0], xmax = allsamples_max_times[-1])
35 set_xlabels_sample2time(ax, allsamples_max_times[-1], samplerate)
38 def set_xlabels_sample2time(ax, latest_sample, samplerate):
39 ax.axis(xmin = 0, xmax = latest_sample)
40 if latest_sample / float(samplerate) > 60:
41 ax.set_xlabel('time (mm:ss)')
42 ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50)
44 ax.set_xlabel('time (ss.mm)')
45 ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50)
48 if __name__ == '__main__':
49 import matplotlib.pyplot as plt
51 print("Usage: %s <filename>" % sys.argv[0])
53 for soundfile in sys.argv[1:]:
54 get_waveform_plot(soundfile)