History

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

Paul Brossier committed 13 years ago

Commit 5d5d6b91 parent 2cedc835 files · +149 −68Browse files

Showing the changes to python (5 of 5 files). Show all

Unified · Split

5 files
  1. python/demos/demo_onset.py +1 −0
  2. python/demos/demo_pitch.py +37 −0
  3. python/demos/demo_simple_robot_voice.py +1 −0
  4. python/demos/demo_specdesc.py +68 −68
  5. python/demos/demo_waveform_plot.py +42 −0
python/demos/demo_onset.py+1−0
@@ -17,6 +17,7 @@ if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) · 16 unmodified lines
17 17
18s = source(filename, samplerate, hop_s)18s = source(filename, samplerate, hop_s)
19samplerate = s.samplerate19samplerate = s.samplerate
20
20o = onset("default", win_s, hop_s, samplerate)21o = onset("default", win_s, hop_s, samplerate)
21 22
22# onset detection delay, in samples23# onset detection delay, in samples
python/demos/demo_pitch.py+37−0
@@ -0,0 +1,37 @@
1#! /usr/bin/env python
2
3import sys
4from aubio import source, pitch
5
6win_s = 1024 # fft size
7hop_s = win_s # hop size
8
9if len(sys.argv) < 2:
10 print "Usage: %s <filename> [samplerate]" % sys.argv[0]
11 sys.exit(1)
12
13filename = sys.argv[1]
14
15samplerate = 0
16if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
17
18s = source(filename, samplerate, hop_s)
19samplerate = s.samplerate
20
21pitch_o = pitch("default", win_s, hop_s, samplerate)
22pitch_o.set_unit("midi")
23
24pitches = []
25
26
27# total number of frames read
28total_frames = 0
29while True:
30 samples, read = s()
31 pitch = pitch_o(samples)[0]
32 print "%f %f" % (total_frames / float(samplerate), pitch)
33 #pitches += [pitches]
34 total_frames += read
35 if read < hop_s: break
36
37#print pitches
python/demos/demo_simple_robot_voice.py+1−0
@@ -19,6 +19,7 @@ if __name__ == '__main__': · 18 unmodified lines
19 while read:19 while read:
20 samples, read = f()20 samples, read = f()
21 spectrum = pv(samples) # compute spectrum21 spectrum = pv(samples) # compute spectrum
22 #spectrum.norm *= .8 # reduce amplitude a bit
22 spectrum.phas[:] = 0. # zero phase23 spectrum.phas[:] = 0. # zero phase
23 new_samples = pv.rdo(spectrum) # compute modified samples24 new_samples = pv.rdo(spectrum) # compute modified samples
24 g(new_samples, read) # write to output25 g(new_samples, read) # write to output
python/demos/demo_specdesc.py+68−68
@@ -1,84 +1,84 @@
1#! /usr/bin/env python1#! /usr/bin/env python
2 2
3from numpy import random, sin, arange, ones, zeros3import sys
4from math import pi4from aubio import fvec, source, pvoc, specdesc
5from aubio import fvec, onset5from numpy import hstack
6 6
7def build_sinusoid(length, freqs, samplerate):7win_s = 512 # fft size
8 return sin( 2. * pi * arange(length) * freqs / samplerate)8hop_s = win_s / 4 # hop size
9 9
10def run_onset(p, input_vec):10if len(sys.argv) < 2:
11 f = fvec (p.hop_size)11 print "Usage: %s <filename> [samplerate]" % sys.argv[0]
12 cands = []12 sys.exit(1)
13 count = 0
14 for vec_slice in input_vec.reshape((-1, p.hop_size)):
15 f[:] = vec_slice
16 cands.append(o(f))
17 return cands
18 13
19methods = ['default',14filename = sys.argv[1]
20 'energy',
21 'complex',
22 'phase',
23 'specdiff',
24 'kl',
25 'mkl',
26 'specflux',
27 'centroid',
28 'spread',
29 'skewness',
30 'kurtosis',
31 'slope',
32 'decrease',
33 'rolloff',
34 ]
35 15
36cands = {}16samplerate = 0
37buf_size = 204817if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
38hop_size = 512
39samplerate = 44100
40sin_length = (samplerate * 10) % 512 * 512
41freqs = zeros(sin_length)
42 18
43partition = sin_length / 819s = source(filename, samplerate, hop_s)
44pointer = 020samplerate = s.samplerate
45 21
46pointer += partition22pv = pvoc(win_s, hop_s)
47freqs[pointer: pointer + partition] = 440
48 23
49pointer += partition24methods = ['default', 'energy', 'hfc', 'complex', 'phase', 'specdiff', 'kl', 'mkl',
50pointer += partition25 'specflux', 'centroid', 'spread', 'skewness', 'kurtosis', 'slope', 'decrease',
51freqs[ pointer : pointer + partition ] = 74026 'rolloff', ]
52 27
53pointer += partition28all_descs = {}
54freqs[ pointer : pointer + partition ] = 148029o = {}
55
56pointer += partition
57pointer += partition
58freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
59
60a = build_sinusoid(sin_length, freqs, samplerate)
61 30
62for method in methods:31for method in methods:
63 o = onset(method, buf_size, hop_size, samplerate)32 cands = []
64 cands[method] = run_onset(o, a)33 all_descs[method] = fvec(0)
34 o[method] = specdesc(method, win_s)
35
36total_frames = 0
37downsample = 2
65 38
66print "done computing"39while True:
40 samples, read = s()
41 fftgrain = pv(samples)
42 print "%f" % ( total_frames / float(samplerate) ),
43 for method in methods:
44 specdesc_val = o[method](fftgrain)[0]
45 all_descs[method] = hstack ( [all_descs[method], specdesc_val] )
46 print "%f" % specdesc_val,
47 print
48 total_frames += read
49 if read < hop_s: break
67 50
68if 1:51if 1:
69 from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot52 print "done computing, now plotting"
70 subplot (211)53 import matplotlib.pyplot as plt
71 legend(methods+['ground truth'], 'upper right')54 from demo_waveform_plot import get_waveform_plot
72 xlabel('time (s)')55 fig = plt.figure()
73 ylabel('amplitude')56 plt.rc('lines',linewidth='.8')
74 ramp = arange(0, sin_length).astype('float') / samplerate57 wave = plt.axes([0.1, 0.75, 0.8, 0.19])
75 plot(ramp, a, ':')58 get_waveform_plot(filename, samplerate, ax = wave )
76 subplot (212)59 wave.yaxis.set_visible(False)
77 ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate60 wave.xaxis.set_visible(False)
78 for method in methods:
79 plot(ramp, cands[method],'.-')
80 legend(methods, 'upper right')
81 xlabel('time (s)')
82 ylabel('spectral descriptor value')
83 show()
84 61
62 all_desc_times = [ x * hop_s for x in range(len(all_descs["default"])) ]
63 n_methods = len(methods)
64 for i, method in enumerate(methods):
65 #ax = fig.add_subplot (n_methods, 1, i)
66 #plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
67 ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_methods), 0.8, 0.65 / n_methods], sharex = wave )
68 ax.plot(all_desc_times, all_descs[method], '-', label = method)
69 #ax.set_ylabel(method, rotation = 0)
70 ax.xaxis.set_visible(False)
71 ax.yaxis.set_visible(False)
72 ax.axis(xmax = all_desc_times[-1], xmin = all_desc_times[0])
73 ax.annotate(method, xy=(-10, 10), xycoords='axes points',
74 horizontalalignment='right', verticalalignment='bottom',
75 )
76 if all_desc_times[-1] / float(samplerate) > 60:
77 plt.xlabel('time (mm:ss)')
78 ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50)
79 else:
80 plt.xlabel('time (ss.mm)')
81 ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50)
82 #plt.ylabel('spectral descriptor value')
83 ax.xaxis.set_visible(True)
84 plt.show()
python/demos/demo_waveform_plot.py+42−0
@@ -0,0 +1,42 @@
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()

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