source: python/demos/demo_waveform_plot.py @ 07364a5

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 07364a5 was 4120fbc, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/demos: python3 and double precision compatibility

  • Property mode set to 100755
File size: 2.0 KB
RevLine 
[5d5d6b9]1#! /usr/bin/env python
2
3import sys
[8fb567c]4from aubio import source
[5d5d6b9]5from numpy import zeros, hstack
6
[7079887]7def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None, downsample = 2**4):
[5d5d6b9]8    import matplotlib.pyplot as plt
9    if not ax:
10        fig = plt.figure()
11        ax = fig.add_subplot(111)
[1eee405]12    hop_s = block_size
[5d5d6b9]13
14    allsamples_max = zeros(0,)
[7079887]15    downsample = downsample  # 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
[4120fbc]24        new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0)
[5d5d6b9]25        allsamples_max = hstack([allsamples_max, new_maxes])
26        total_frames += read
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)) ]
30
31    ax.plot(allsamples_max_times,  allsamples_max, '-b')
32    ax.plot(allsamples_max_times, -allsamples_max, '-b')
[daa0d5d]33    ax.axis(xmin = allsamples_max_times[0], xmax = allsamples_max_times[-1])
[5d5d6b9]34
[1eee405]35    set_xlabels_sample2time(ax, allsamples_max_times[-1], samplerate)
36    return ax
37
38def set_xlabels_sample2time(ax, latest_sample, samplerate):
[1e90588]39    ax.axis(xmin = 0, xmax = latest_sample)
[1eee405]40    if latest_sample / float(samplerate) > 60:
[daa0d5d]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)
43    else:
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)
[1eee405]46
47
[5d5d6b9]48if __name__ == '__main__':
[daa0d5d]49    import matplotlib.pyplot as plt
[5d5d6b9]50    if len(sys.argv) < 2:
[4120fbc]51        print("Usage: %s <filename>" % sys.argv[0])
[5d5d6b9]52    else:
53        for soundfile in sys.argv[1:]:
54            get_waveform_plot(soundfile)
55            # display graph
[daa0d5d]56            plt.show()
Note: See TracBrowser for help on using the repository browser.