[5d5d6b9] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
[8fb567c] | 4 | from aubio import source, pitch |
---|
[5d5d6b9] | 5 | |
---|
| 6 | if len(sys.argv) < 2: |
---|
[4120fbc] | 7 | print("Usage: %s <filename> [samplerate]" % sys.argv[0]) |
---|
[5d5d6b9] | 8 | sys.exit(1) |
---|
| 9 | |
---|
| 10 | filename = sys.argv[1] |
---|
| 11 | |
---|
[c54b2c0] | 12 | downsample = 1 |
---|
[4120fbc] | 13 | samplerate = 44100 // downsample |
---|
[5d5d6b9] | 14 | if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) |
---|
| 15 | |
---|
[4120fbc] | 16 | win_s = 4096 // downsample # fft size |
---|
| 17 | hop_s = 512 // downsample # hop size |
---|
[c54b2c0] | 18 | |
---|
[5d5d6b9] | 19 | s = source(filename, samplerate, hop_s) |
---|
| 20 | samplerate = s.samplerate |
---|
| 21 | |
---|
[c54b2c0] | 22 | tolerance = 0.8 |
---|
[5d5d6b9] | 23 | |
---|
[c54b2c0] | 24 | pitch_o = pitch("yin", win_s, hop_s, samplerate) |
---|
[193dcbb] | 25 | pitch_o.set_unit("midi") |
---|
[c54b2c0] | 26 | pitch_o.set_tolerance(tolerance) |
---|
[5d5d6b9] | 27 | |
---|
[c54b2c0] | 28 | pitches = [] |
---|
| 29 | confidences = [] |
---|
[5d5d6b9] | 30 | |
---|
| 31 | # total number of frames read |
---|
| 32 | total_frames = 0 |
---|
| 33 | while True: |
---|
| 34 | samples, read = s() |
---|
| 35 | pitch = pitch_o(samples)[0] |
---|
[c54b2c0] | 36 | #pitch = int(round(pitch)) |
---|
| 37 | confidence = pitch_o.get_confidence() |
---|
| 38 | #if confidence < 0.8: pitch = 0. |
---|
[4120fbc] | 39 | print("%f %f %f" % (total_frames / float(samplerate), pitch, confidence)) |
---|
[c54b2c0] | 40 | pitches += [pitch] |
---|
| 41 | confidences += [confidence] |
---|
[5d5d6b9] | 42 | total_frames += read |
---|
| 43 | if read < hop_s: break |
---|
| 44 | |
---|
[c54b2c0] | 45 | if 0: sys.exit(0) |
---|
| 46 | |
---|
[5d5d6b9] | 47 | #print pitches |
---|
[c9a9f86] | 48 | import os.path |
---|
[c54b2c0] | 49 | from numpy import array, ma |
---|
| 50 | import matplotlib.pyplot as plt |
---|
| 51 | from demo_waveform_plot import get_waveform_plot, set_xlabels_sample2time |
---|
| 52 | |
---|
| 53 | skip = 1 |
---|
| 54 | |
---|
| 55 | pitches = array(pitches[skip:]) |
---|
| 56 | confidences = array(confidences[skip:]) |
---|
| 57 | times = [t * hop_s for t in range(len(pitches))] |
---|
| 58 | |
---|
| 59 | fig = plt.figure() |
---|
[4f2c28c] | 60 | |
---|
[c54b2c0] | 61 | ax1 = fig.add_subplot(311) |
---|
| 62 | ax1 = get_waveform_plot(filename, samplerate = samplerate, block_size = hop_s, ax = ax1) |
---|
[4f2c28c] | 63 | plt.setp(ax1.get_xticklabels(), visible = False) |
---|
| 64 | ax1.set_xlabel('') |
---|
[c54b2c0] | 65 | |
---|
| 66 | def array_from_text_file(filename, dtype = 'float'): |
---|
| 67 | filename = os.path.join(os.path.dirname(__file__), filename) |
---|
| 68 | return array([line.split() for line in open(filename).readlines()], |
---|
| 69 | dtype = dtype) |
---|
| 70 | |
---|
| 71 | ax2 = fig.add_subplot(312, sharex = ax1) |
---|
| 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 |
---|
[193dcbb] | 81 | ax2.plot(times, pitches, '.g') |
---|
[c54b2c0] | 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 ) |
---|
[4f2c28c] | 90 | plt.setp(ax2.get_xticklabels(), visible = False) |
---|
[193dcbb] | 91 | ax2.set_ylabel('f0 (midi)') |
---|
[c54b2c0] | 92 | |
---|
| 93 | # plot confidence |
---|
| 94 | ax3 = fig.add_subplot(313, sharex = ax1) |
---|
| 95 | # plot the confidence |
---|
| 96 | ax3.plot(times, confidences) |
---|
| 97 | # draw a line at tolerance |
---|
| 98 | ax3.plot(times, [tolerance]*len(confidences)) |
---|
| 99 | ax3.axis( xmin = times[0], xmax = times[-1]) |
---|
[1a8ce10] | 100 | ax3.set_ylabel('confidence') |
---|
[c54b2c0] | 101 | set_xlabels_sample2time(ax3, times[-1], samplerate) |
---|
[4f2c28c] | 102 | plt.show() |
---|
| 103 | #plt.savefig(os.path.basename(filename) + '.svg') |
---|