History

python/demos/demo_waveform_plot.py: plot more samples, add hop_size parameter, add set_xlabels_sample2time

Paul Brossier committed 13 years ago

Commit 1eee4051 parent b429b681 file · +10 −6Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/demos/demo_waveform_plot.py+10−6
@@ -4,15 +4,15 @@ import sys · 3 unmodified lines
4from aubio import pvoc, source4from aubio import pvoc, source
5from numpy import zeros, hstack5from numpy import zeros, hstack
6 6
7def get_waveform_plot(filename, samplerate = 0, ax = None):7def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None):
8 import matplotlib.pyplot as plt8 import matplotlib.pyplot as plt
9 if not ax:9 if not ax:
10 fig = plt.figure()10 fig = plt.figure()
11 ax = fig.add_subplot(111)11 ax = fig.add_subplot(111)
12 hop_s = 4096 # block size12 hop_s = block_size
13 13
14 allsamples_max = zeros(0,)14 allsamples_max = zeros(0,)
15 downsample = 2**3 # to plot n samples / hop_s15 downsample = 2**4 # to plot n samples / hop_s
16 16
17 a = source(filename, samplerate, hop_s) # source file17 a = source(filename, samplerate, hop_s) # source file
18 if samplerate == 0: samplerate = a.samplerate18 if samplerate == 0: samplerate = a.samplerate
@@ -25,8 +25,6 @@ def get_waveform_plot(filename, samplerate = 0, ax = None): · 6 unmodified lines
25 allsamples_max = hstack([allsamples_max, new_maxes])25 allsamples_max = hstack([allsamples_max, new_maxes])
26 total_frames += read26 total_frames += read
27 if read < hop_s: break27 if read < hop_s: break
28 print samples.reshape(hop_s/downsample, downsample).shape
29
30 allsamples_max = (allsamples_max > 0) * allsamples_max28 allsamples_max = (allsamples_max > 0) * allsamples_max
31 allsamples_max_times = [ ( float (t) / downsample ) * hop_s for t in range(len(allsamples_max)) ]29 allsamples_max_times = [ ( float (t) / downsample ) * hop_s for t in range(len(allsamples_max)) ]
32 30
@@ -34,12 +32,18 @@ def get_waveform_plot(filename, samplerate = 0, ax = None): · 1 unmodified line
34 ax.plot(allsamples_max_times, -allsamples_max, '-b')32 ax.plot(allsamples_max_times, -allsamples_max, '-b')
35 ax.axis(xmin = allsamples_max_times[0], xmax = allsamples_max_times[-1])33 ax.axis(xmin = allsamples_max_times[0], xmax = allsamples_max_times[-1])
36 34
37 if allsamples_max_times[-1] / float(samplerate) > 60:35 set_xlabels_sample2time(ax, allsamples_max_times[-1], samplerate)
36 return ax
37
38def set_xlabels_sample2time(ax, latest_sample, samplerate):
39 if latest_sample / float(samplerate) > 60:
38 ax.set_xlabel('time (mm:ss)')40 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)41 ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50)
40 else:42 else:
41 ax.set_xlabel('time (ss.mm)')43 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)44 ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50)
45
46
43if __name__ == '__main__':47if __name__ == '__main__':
44 import matplotlib.pyplot as plt48 import matplotlib.pyplot as plt
45 if len(sys.argv) < 2:49 if len(sys.argv) < 2:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t