[5d5d6b9] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import pvoc, source |
---|
| 5 | from numpy import zeros, hstack |
---|
| 6 | |
---|
| 7 | def get_waveform_plot(filename, samplerate = 0, ax = None): |
---|
| 8 | import matplotlib.pyplot as plt |
---|
| 9 | if not ax: |
---|
| 10 | fig = plt.figure() |
---|
| 11 | ax = fig.add_subplot(111) |
---|
[daa0d5d] | 12 | hop_s = 4096 # block size |
---|
[5d5d6b9] | 13 | |
---|
| 14 | allsamples_max = zeros(0,) |
---|
[daa0d5d] | 15 | downsample = 2**3 # to plot n samples / hop_s |
---|
[5d5d6b9] | 16 | |
---|
| 17 | a = source(filename, samplerate, hop_s) # source file |
---|
| 18 | if samplerate == 0: samplerate = a.samplerate |
---|
| 19 | |
---|
| 20 | total_frames = 0 |
---|
| 21 | while True: |
---|
| 22 | samples, read = a() |
---|
| 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]) |
---|
| 26 | total_frames += read |
---|
| 27 | if read < hop_s: break |
---|
[daa0d5d] | 28 | print samples.reshape(hop_s/downsample, downsample).shape |
---|
[5d5d6b9] | 29 | |
---|
| 30 | allsamples_max = (allsamples_max > 0) * allsamples_max |
---|
| 31 | allsamples_max_times = [ ( float (t) / downsample ) * hop_s for t in range(len(allsamples_max)) ] |
---|
| 32 | |
---|
| 33 | ax.plot(allsamples_max_times, allsamples_max, '-b') |
---|
| 34 | ax.plot(allsamples_max_times, -allsamples_max, '-b') |
---|
[daa0d5d] | 35 | ax.axis(xmin = allsamples_max_times[0], xmax = allsamples_max_times[-1]) |
---|
[5d5d6b9] | 36 | |
---|
[daa0d5d] | 37 | if allsamples_max_times[-1] / float(samplerate) > 60: |
---|
| 38 | ax.set_xlabel('time (mm:ss)') |
---|
| 39 | ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50) |
---|
| 40 | else: |
---|
| 41 | ax.set_xlabel('time (ss.mm)') |
---|
| 42 | ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50) |
---|
[5d5d6b9] | 43 | if __name__ == '__main__': |
---|
[daa0d5d] | 44 | import matplotlib.pyplot as plt |
---|
[5d5d6b9] | 45 | if len(sys.argv) < 2: |
---|
| 46 | print "Usage: %s <filename>" % sys.argv[0] |
---|
| 47 | else: |
---|
| 48 | for soundfile in sys.argv[1:]: |
---|
| 49 | get_waveform_plot(soundfile) |
---|
| 50 | # display graph |
---|
[daa0d5d] | 51 | plt.show() |
---|