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