source: python/demos/demo_pitch.py @ 5c6b264

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

python/demos/: minor plot improvements

  • Property mode set to 100755
File size: 3.1 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import source, pitch, freqtomidi
5
6if len(sys.argv) < 2:
7    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
8    sys.exit(1)
9
10filename = sys.argv[1]
11
12downsample = 1
13samplerate = 44100 / downsample
14if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
15
16win_s = 4096 / downsample # fft size
17hop_s = 512  / downsample # hop size
18
19s = source(filename, samplerate, hop_s)
20samplerate = s.samplerate
21
22tolerance = 0.8
23
24pitch_o = pitch("yin", win_s, hop_s, samplerate)
25pitch_o.set_unit("freq")
26pitch_o.set_tolerance(tolerance)
27
28pitches = []
29confidences = []
30
31# total number of frames read
32total_frames = 0
33while True:
34    samples, read = s()
35    pitch = pitch_o(samples)[0]
36    #pitch = int(round(pitch))
37    confidence = pitch_o.get_confidence()
38    #if confidence < 0.8: pitch = 0.
39    print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
40    pitches += [pitch]
41    confidences += [confidence]
42    total_frames += read
43    if read < hop_s: break
44
45if 0: sys.exit(0)
46
47#print pitches
48from numpy import array, ma
49import matplotlib.pyplot as plt
50from demo_waveform_plot import get_waveform_plot, set_xlabels_sample2time
51
52skip = 1
53
54pitches = array(pitches[skip:])
55confidences = array(confidences[skip:])
56times = [t * hop_s for t in range(len(pitches))]
57
58fig = plt.figure()
59
60ax1 = fig.add_subplot(311)
61ax1 = get_waveform_plot(filename, samplerate = samplerate, block_size = hop_s, ax = ax1)
62plt.setp(ax1.get_xticklabels(), visible = False)
63ax1.set_xlabel('')
64
65def array_from_text_file(filename, dtype = 'float'):
66    import os.path
67    from numpy import array
68    filename = os.path.join(os.path.dirname(__file__), filename)
69    return array([line.split() for line in open(filename).readlines()],
70        dtype = dtype)
71
72ax2 = fig.add_subplot(312, sharex = ax1)
73import sys, os.path
74ground_truth = os.path.splitext(filename)[0] + '.f0.Corrected'
75if os.path.isfile(ground_truth):
76    ground_truth = array_from_text_file(ground_truth)
77    true_freqs = ground_truth[:,2]
78    true_freqs = ma.masked_where(true_freqs < 2, true_freqs)
79    true_times = float(samplerate) * ground_truth[:,0]
80    ax2.plot(true_times, true_freqs, 'r')
81    ax2.axis( ymin = 0.9 * true_freqs.min(), ymax = 1.1 * true_freqs.max() )
82# plot raw pitches
83ax2.plot(times, pitches, '--g')
84# plot cleaned up pitches
85cleaned_pitches = pitches
86#cleaned_pitches = ma.masked_where(cleaned_pitches < 0, cleaned_pitches)
87#cleaned_pitches = ma.masked_where(cleaned_pitches > 120, cleaned_pitches)
88cleaned_pitches = ma.masked_where(confidences < tolerance, cleaned_pitches)
89ax2.plot(times, cleaned_pitches, '.-')
90#ax2.axis( ymin = 0.9 * cleaned_pitches.min(), ymax = 1.1 * cleaned_pitches.max() )
91#ax2.axis( ymin = 55, ymax = 70 )
92plt.setp(ax2.get_xticklabels(), visible = False)
93ax2.set_ylabel('f0 (Hz)')
94
95# plot confidence
96ax3 = fig.add_subplot(313, sharex = ax1)
97# plot the confidence
98ax3.plot(times, confidences)
99# draw a line at tolerance
100ax3.plot(times, [tolerance]*len(confidences))
101ax3.axis( xmin = times[0], xmax = times[-1])
102ax3.set_ylabel('condidence')
103set_xlabels_sample2time(ax3, times[-1], samplerate)
104plt.show()
105#plt.savefig(os.path.basename(filename) + '.svg')
Note: See TracBrowser for help on using the repository browser.