source: python/demos/demo_waveform_plot.py @ 5d5d6b9

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

python/demos: add demo_pitch.py and demo_waveform_plot.py

  • Property mode set to 100755
File size: 1.3 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import pvoc, source
5from numpy import zeros, hstack
6
7def 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)
12    hop_s = 512                                        # fft window size
13
14    allsamples_max = zeros(0,)
15    downsample = 2  # to plot n samples / hop_s
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
28
29    allsamples_max = (allsamples_max > 0) * allsamples_max
30    allsamples_max_times = [ ( float (t) / downsample ) * hop_s for t in range(len(allsamples_max)) ]
31
32    ax.plot(allsamples_max_times,  allsamples_max, '-b')
33    ax.plot(allsamples_max_times, -allsamples_max, '-b')
34
35if __name__ == '__main__':
36    if len(sys.argv) < 2:
37        print "Usage: %s <filename>" % sys.argv[0]
38    else:
39        for soundfile in sys.argv[1:]:
40            get_waveform_plot(soundfile)
41            # display graph
42            show()
Note: See TracBrowser for help on using the repository browser.