-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
284 lines (249 loc) · 10 KB
/
audio.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from essentia.standard import MonoLoader, TuningFrequencyExtractor, \
PitchMelodia, PitchContourSegmentation, EqualLoudness, \
PredominantPitchMelodia
import madmom
import essentia
import librosa
from math import log
import numpy as np
import scipy.ndimage.filters as fi
import os.path
import json
import csv
import vamp
import sys
import pysptk
import matplotlib.pyplot as plt
# import crepe
class AudioProc():
def __init__(self, audiopath, respath):
self.filename = None
self.index = None
self.sig = None
self.path = audiopath
self.respath = respath
def setFilename(self, f, index = None, offset = 0, duration = None):
self.filename = f
self.index = index
def setOffset(self, offset, duration):
self.offset = offset
self.duration = duration
self.sig = None
def getSignal(self):
# Using librosa to load audio - allows to set duration / offset
# print 'reading audio', self.filename
self.sig, _ = librosa.load(self.filename, sr=44100,
offset = self.offset,
duration = self.duration,
mono = True)
@classmethod
def freq2midi(_, freq, tuning):
if freq == 0:
return 0
return 12 * log(freq / tuning, 2) + 69
def getODM(self, method, overwrite = False):
basefile = self.respath + '/' + method + '/' + ("%03d" % self.index) + \
".%d" % self.offset
contourfile = basefile + ".contour.csv"
notefile = basefile + ".notes.csv"
# if os.path.isfile(contourfile) and \
# os.path.isfile(notefile) and not overwrite:
# print('\treading transcription from file')
# (o, d, m) = ([], [], [])
# t = None
# with open(outfile, 'r') as cached:
# for line in cached:
# if t is None:
# t = float(line)
# continue
# v = line.split(', ')
# o.append(float(v[0]))
# d.append(float(v[1]))
# m.append(int(v[2]))
# return outfile, o, d, m, t
if self.sig is None:
self.getSignal()
# get tuning - return MODE of predictions
tuner = TuningFrequencyExtractor()
tf = tuner(self.sig)
tc = {}
for c in tf:
if c not in tc:
tc[c] = 0
tc[c] += 1
tuning = sorted( [(k, tc[k]) for k in tc],
key = lambda x: x[1],
reverse = True)[0][0]
if method == 'pitchmelodia':
elFilter = EqualLoudness()
audio = elFilter(self.sig)
melodia = PitchMelodia()
contour, confidence = melodia(audio)
segmenter = PitchContourSegmentation(tuningFrequency = int(tuning))
o, d, m = segmenter(contour, audio)
elif method == 'melodia':
elFilter = EqualLoudness()
audio = elFilter(self.sig)
melodia = PredominantPitchMelodia()
contour, confidence = melodia(audio)
segmenter = PitchContourSegmentation(tuningFrequency = int(tuning))
o, d, m = segmenter(contour, audio)
elif method == 'silvet':
notes = vamp.collect(self.sig, 44100, "silvet:silvet")['list']
(o, d, m) = ([], [], [])
for note in notes:
o.append(note['timestamp'])
d.append(note['duration'])
m.append(AudioProc.freq2midi(note['values'][0], tuning))
contour = None
elif method == 'pyin':
contour = vamp.collect(self.sig, 44100, "pyin:pyin", output="smoothedpitchtrack")['vector'][1]
notes = vamp.collect(self.sig, 44100, "pyin:pyin", output='notes')['list']
(o, d, m) = ([], [], [])
for note in notes:
o.append(note['timestamp'])
d.append(note['duration'])
m.append(AudioProc.freq2midi(note['values'][0], tuning))
# contour = None
elif method == 'swipe':
contour = pysptk.sptk.swipe(self.sig.astype('float64'),
44100, 128, max=2000)
segmenter = PitchContourSegmentation(tuningFrequency = int(tuning))
o, d, m = segmenter(essentia.array(contour), self.sig)
m[np.where(np.isneginf(m))] = 0
m[np.where(np.isnan(m))] = 0
elif method == 'crepe':
t_, contour, c_, a_ = crepe.predict(self.sig, 44100,
viterbi=True,
verbose=False)
# contour = []
# with open(contourfile, 'r') as infile:
# for line in infile:
# contour.append(float(line))
segmenter = PitchContourSegmentation(tuningFrequency = int(tuning), hopSize = 441)
o, d, m = segmenter(essentia.array(contour), self.sig)
else:
sys.exit(1)
if contour is not None:
np.savetxt(contourfile, contour, fmt = "%.3f")
with open(notefile, 'w') as output:
output.write("%f\n" % tuning)
for t in range(len(o)):
output.write('%.3f, %.3f, %.2f\n' % (o[t], d[t], m[t]))
# return notefile, o, d, map(int, m), tuning
return None, None, None, None, None
@classmethod
def processSpectrum(_, spectrum, freqs):
def freq2midi(freq):
if freq == 0:
return 0
return round(1200 * log(freq / 440., 2) + 6900)
toCents = np.vectorize(freq2midi, otypes = [np.int])
cents = toCents(freqs)
maxCent = np.max(cents)
counters = np.zeros(maxCent + 1)
result = np.zeros(maxCent + 1)
for c in range(len(cents)):
if cents[c] <= 0:
continue
result[ cents[c] ] += spectrum[c]
counters[ cents[c] ] += 1
counters[ np.where(counters == 0) ] = 1
result /= counters
g = fi.gaussian_filter1d(result, 15, mode = 'wrap')
ow = np.zeros(1200)
for c in range(maxCent):
ow[c % 1200] += g[c]
ow120 = ow[ range(0, 1200, 10) ]
s = sum(ow120)
ow120 /= s
return ow120.tolist()
def getHPCP(self):
s = madmom.audio.signal.Signal(self.filename, sample_rate = 44100, num_channels = 1,
start = self.offset, stop = self.offset + 12)
fs = madmom.audio.signal.FramedSignal(s, frame_size = 4096)
stfs = madmom.audio.stft.ShortTimeFourierTransform(fs)
spec = madmom.audio.spectrogram.Spectrogram(stfs)
hpcp = madmom.audio.chroma.HarmonicPitchClassProfile(spec, num_classes = 120)
print hpcp.shape
hpcp = np.sum(hpcp, axis = 0)
s = sum(hpcp)
hpcp /= s
plt.plot(hpcp)
plt.show()
sys.exit(0)
return hpcp
def getPCH(self, method, overwrite = False):
outfile = self.respath + '/pch/' + ("%03d" % self.index) + \
(".%d.%s.json" % (self.offset, method))
if os.path.isfile(outfile) and not overwrite:
print('reading', outfile)
with open(outfile, 'r') as datafile:
return outfile, json.load(datafile)
if self.sig is None:
self.getSignal()
if method == 'global':
rspec = np.fft.rfft(self.sig)
spectrum = np.abs(rspec)
freqs = np.fft.rfftfreq(len(self.sig), d = 1./44100)
cutOff = 0
for f in range(len(freqs)):
if freqs[f]>5000:
cutOff = f
break
freqs = freqs[:f]
spectrum = spectrum[:f]
elif method == 'local':
stft = np.abs(librosa.stft(self.sig, n_fft = 4096))
spectrum = np.sum(stft,axis = 1)
freqs = librosa.fft_frequencies(sr=44100, n_fft = 4096)
elif method == '5k':
stft = np.abs(librosa.stft(self.sig, n_fft = 4096))
spectrum = np.sum(stft,axis = 1)
freqs = librosa.fft_frequencies(sr=44100, n_fft = 4096)
cutOff = 0
for f in range(len(freqs)):
if freqs[f]>5000:
cutOff = f
break
freqs = freqs[:f]
spectrum = spectrum[:f]
elif method == 'deepChroma':
dcp = madmom.audio.chroma.DeepChromaProcessor()
chroma = dcp(self.sig)
# print chroma.shape
chroma = np.sum(chroma, axis = 0)
# print chroma.shape
pch1 = np.zeros((120))
for i in range(12):
pch1[i*10] = chroma[i]
pch = fi.gaussian_filter1d(pch1, 15, mode = 'wrap')
s = sum(pch)
pch /= s
elif method == 'HPCP':
pch = self.getHPCP()
else:
print('getPCH(): method should be \'local\' or \'global\'')
return None
# pch = AudioProc.processSpectrum(spectrum, freqs)
with open(outfile, 'w') as datafile:
json.dump(pch, datafile)
return outfile, pch
if __name__ == '__main__':
import sys
import glob
path = 'soundfiles'
transcriptpath = 'transcriptions'
ap = AudioProc(path, transcriptpath)
# algo = sys.argv[2]
with open(sys.argv[1], 'r') as refFile:
reader = csv.DictReader(refFile, delimiter = ',')
for row in reader:
print('processing tune', row['index'])
ap.setFilename('%s/%s' % (path, row['file']),
index = int(row['index']))
for t in ['t1', 't2', 't3', 't4']:
print('\tfrom offset', row[t])
ap.setOffset(int(row[t]), 12)
# notes_f, _, _, _, _ = ap.getODM(algo, overwrite = True)
pch_f, _ = ap.getPCH('HPCP', overwrite = True)