-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pitch_To_MIDI.py
179 lines (125 loc) · 5.13 KB
/
Pitch_To_MIDI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/env python
import sys
from aubio import source, pitch
import os
from mido.midifiles import MidiTrack, MidiFile
import mido
from abjad import *
# if len(sys.argv) < 2:
# print("Usage: %s <filename> [samplerate]" % sys.argv[0])
# sys.exit(1)
# filename = sys.argv[1]
file_index = "137"
filename = "./Content/Minsky/clips/" + file_index + ".wav"
downsample = 1
samplerate = 44100 // downsample
if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
win_s = 4096 // downsample # fft size
hop_s = 512 // downsample # hop size
s = source(filename, samplerate, hop_s)
samplerate = s.samplerate
tolerance = 0.00 # tolerance = 0.8
pitch_o = pitch("yin", win_s, hop_s, samplerate)
pitch_o.set_unit("midi")
pitch_o.set_tolerance(tolerance)
pitches = []
confidences = []
timez = []
# Open file
output_file_name = "./Content/Minsky/pitches/" + file_index + ".txt"
output_file = open(output_file_name, "w")
# total number of frames read
total_frames = 0
while True:
samples, read = s()
pitch = pitch_o(samples)[0]
#pitch = int(round(pitch))
confidence = pitch_o.get_confidence()
#if confidence < 0.8: pitch = 0.
print("%f %f %f" % (total_frames / float(samplerate), pitch, confidence))
# Write times and cleaned_pitches to a file (to use feed to MIDI synthesizer)
output_file.write("%f\t%f\t%f\n" % (total_frames / float(samplerate), pitch, confidence))
timez += [total_frames / float(samplerate)]
pitches += [pitch]
confidences += [confidence]
total_frames += read
if read < hop_s: break
if 0: sys.exit(0)
#print pitches
import os.path
from numpy import array, ma
import matplotlib.pyplot as plt
from demo_waveform_plot import get_waveform_plot, set_xlabels_sample2time
skip = 1
pitches = array(pitches[skip:])
confidences = array(confidences[skip:])
times = [t * hop_s for t in range(len(pitches))]
time = -1
with MidiFile() as mid:
track = MidiTrack()
mid.tracks.append(track)
track.append(mido.Message('program_change', program=12, time=0))
for t in range(len(pitches)):
# # Write times and cleaned_pitches to a file (to use feed to MIDI synthesizer)
# output_file.write("%f\t%f\t%f\n" % (total_frames / float(samplerate), pitch, confidence))
# Note
if time == -1:
time = total_frames / float(samplerate)
else:
time = (total_frames / float(samplerate)) - time
if pitches[t] < 0:
pitches[t] = 0
if pitches[t] > 127:
pitches[t] = 127
if timez[t] < 1:
timez[t] = 1
# track.append(mido.Message('note_on', note=int(pitch), velocity=int(confidence * 127.0), time=int(time)))
# track.append(mido.Message('note_off', note=int(pitch), velocity=int(127 - (confidence * 127.0)), time=32))
# track.append(mido.Message('note_on', note=int(pitch), velocity=int(confidence * 127.0), time=int(time)))
# track.append(mido.Message('note_off', note=int(pitch), velocity=int(127 - (confidence * 127.0)), time=32))
track.append(mido.Message('note_on', note=int(pitches[t]), velocity=int(confidences[t] * 127.0), time=int(timez[t])))
# track.append(mido.Message('note_off', note=int(pitches[t]), velocity=int(127 - (confidences[t] * 127.0)), time=0))
mid.save('./Content/Minsky/midi/' + file_index + '.mid')
fig = plt.figure()
ax1 = fig.add_subplot(311)
ax1 = get_waveform_plot(filename, samplerate = samplerate, block_size = hop_s, ax = ax1)
plt.setp(ax1.get_xticklabels(), visible = False)
ax1.set_xlabel('')
def array_from_text_file(filename, dtype = 'float'):
filename = os.path.join(os.path.dirname(__file__), filename)
return array([line.split() for line in open(filename).readlines()],
dtype = dtype)
ax2 = fig.add_subplot(312, sharex = ax1)
ground_truth = os.path.splitext(filename)[0] + '.f0.Corrected'
if os.path.isfile(ground_truth):
ground_truth = array_from_text_file(ground_truth)
true_freqs = ground_truth[:,2]
true_freqs = ma.masked_where(true_freqs < 2, true_freqs)
true_times = float(samplerate) * ground_truth[:,0]
ax2.plot(true_times, true_freqs, 'r')
ax2.axis( ymin = 0.9 * true_freqs.min(), ymax = 1.1 * true_freqs.max() )
# plot raw pitches
ax2.plot(times, pitches, '.g')
# plot cleaned up pitches
cleaned_pitches = pitches
#cleaned_pitches = ma.masked_where(cleaned_pitches < 0, cleaned_pitches)
#cleaned_pitches = ma.masked_where(cleaned_pitches > 120, cleaned_pitches)
cleaned_pitches = ma.masked_where(confidences < tolerance, cleaned_pitches)
ax2.plot(times, cleaned_pitches, '.-')
#ax2.axis( ymin = 0.9 * cleaned_pitches.min(), ymax = 1.1 * cleaned_pitches.max() )
#ax2.axis( ymin = 55, ymax = 70 )
plt.setp(ax2.get_xticklabels(), visible = False)
ax2.set_ylabel('f0 (midi)')
# Close output file
output_file.close()
# plot confidence
ax3 = fig.add_subplot(313, sharex = ax1)
# plot the confidence
ax3.plot(times, confidences)
# draw a line at tolerance
ax3.plot(times, [tolerance]*len(confidences))
ax3.axis( xmin = times[0], xmax = times[-1])
ax3.set_ylabel('condidence')
set_xlabels_sample2time(ax3, times[-1], samplerate)
# plt.show()
plt.savefig('./Content/Minsky/plots/' + file_index + '.svg')