-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunPolyAlignment.m
362 lines (307 loc) · 12.7 KB
/
runPolyAlignment.m
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
function [estimatedOns estimatedOffs]=runPolyAlignment(audiofile, midifile, meansCovarsMat, voiceType)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% estimatedOns estimatedOffs]=runPolyAlignment(audiofile, midifile)
%
% Description: Main function for runing polyphonic MIDI-audio alignment
% An intial DTW alignment is refined to estimate asychroncies
% between notated simultaneities
%
% Note that this current version assumes that each note ends
% immediately before it starts again (i.e., no rests)
%
% Inputs:
% audiofile - audio file file
% midifile - midi file
% meansCovarsMat - specifies means and covariance matrix to use
% voiceType - vector indicating which voice (or instrument) to use for
% each musical line
%
% Outputs:
% estimatedOns - cell array of onset times
% estimatedOffs - cell array of offset times
%
% Dependencies:
% Ellis, D. P. W. 2003. Dynamic Time Warp (DTW) in Matlab. Available
% from: http://www.ee.columbia.edu/~dpwe/resources/matlab/dtw/
% Ellis, D. P. W. 2008. Aligning MIDI scores to music audio. Available
% from: http://www.ee.columbia.edu/~dpwe/resources/matlab/alignmidiwav/
% Toiviainen, P. and T. Eerola. 2006. MIDI Toolbox. Available from:
% https://www.jyu.fi/hum/laitokset/musiikki/en/research/coe/materials
% /miditoolbox/
% Murphy, K. 1998. Hidden Markov Model (HMM) Toolbox for Matlab.
% Available from http://www.cs.ubc.ca/~murphyk/Software/HMM/hmm.html
%
% Automatic Music Performance Analysis and Analysis Toolkit (AMPACT)
% http://www.ampact.org
% (c) copyright 2014 Johanna Devaney (j@devaney.ca), all rights reserved.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% if no arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 4
voiceType = [2 1 1 1];
end
if nargin < 3
meansCovarsMat='polySingingMeansCovars.mat';
end
if nargin < 2
midifile = 'polyExample.mid';
end
if nargin < 1
audiofile = 'polyExample.wav';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Initial DTW alignment stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read MIDI file
nmatAll=midi2nmat(midifile);
if min(nmatAll(:,3)) == 0
nmatAll(:,3)=nmatAll(:,3)+1;
end
for i = sort(unique(nmatAll(:,3)))'
nmat{i} = nmatAll(nmatAll(:,3)==i,:);
end
maxNotes=max(nmatAll(:,3));
%%%%%%%% Initialize HMM variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% needs to be here for calculations in initial DTW alignment
% starting state for HMM
for i = 1 : maxNotes
startingState{i} = [1; zeros(3^i-1,1)];
end
% get transition matrix for HMM
[notes trans] = genPolyTrans(50, 0, 5);
for i = 1 : maxNotes
notesInd{i} = cat(1, notes{i}{:})';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% run DTW alignment using composite midifile
[align,spec] = runDTWAlignment(audiofile, midifile, 0.025);
% calculate how many voices change at each transition
%nmatAll(:,1)=floor(nmatAll(:,1)*1000)/1000;
[uniqueBeats, idx1, idx2] = unique(onset(nmatAll), 'first');
uniqueAlignOns = align.nmat(idx1, 1);
onsetMap = zeros(length(uniqueBeats),maxNotes);
for i = 1 : length(uniqueBeats)
%num = 1;
for j = 1:maxNotes
if sum(onset(nmat{j}) == uniqueBeats(i))
onsetMap(i,j) = 1;
end
%num = num + 1;
end
end
% create new onset map using alignment values
% THIS IS CURRENTLY ASSUMING THAT THERE ARE NO NOTATED RESTS
for i = 1 : size(onsetMap,1) % number of onsets
for j = 1 : size(onsetMap,2) % number of voices
if onsetMap(i,j) == 1,
onsMap2(i,j) = uniqueAlignOns(i);
end
end
lv2(i) = find(onsetMap(i,:), 1, 'first');
onVals(i)=onsMap2(i,lv2(i));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Audio analysis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set paramters for audio analysis
offset1=0.125;
offset2=0.125;
[audio,sr]=wavread(audiofile);
audio=resample(audio,1,2);
sr = sr/2;
tuning=estimateTuning(audio);
parameter.winLenSTMSP=441;
parameter.shiftFB = tuning;
% create a matrix of the notes in the audio in midi note numbers for each
% transition, as defined by onsetMap
for i = 1 : maxNotes
idxCell{i}=1;
pitches{1}(i,3)=nmat{i}(1,4)+tuning;
end
for i = 2 : size(onsetMap,1)
for j = 1 : maxNotes
if onsetMap(i,j) == 1
pitches{i}(j,1)=nmat{j}(idxCell{j},4)+tuning;
pitches{i}(j,2)=0;
try
pitches{i}(j,3)=nmat{j}(idxCell{j}+1,4)+tuning;
end
idxCell{j}=idxCell{j}+1;
else
pitches{i}(j,1)=pitches{i-1}(j,3)+tuning;
pitches{i}(j,2)=pitches{i-1}(j,3)+tuning;
try
pitches{i}(j,3)=pitches{i-1}(j,3)+tuning;
end
end
end
end
% get means and covars for the singing voice
% differentiate for different voices
load(meansCovarsMat)
for i = 1 : size(nmat,2)
[meansSeed{i} covarsSeed{i} versions]=genMeansCovars(notes, vals{i},voiceType);
end
% set the harmonics that are going to be considered
harmonics=[-1 0 1];
harmonics2=[-1 0 1 12 19 24 28 31 36];
% run audio analysis
fpitchAll=audio_to_pitch_via_FB(audio,parameter);
hop = length(audio)/size(fpitchAll,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% initialize indexing cell array
for i = 1 : maxNotes
idxCell{i}=1;
end
for i = 1 : length(onsetMap)
%for i = 2 : length(onsetMap)-1
numVoices = sum(onsetMap(i,:),2);
try
fpitch{i}=fpitchAll(:,round((onVals(i)-offset1)*sr/hop):round((onVals(i)+offset2)*sr/hop));
catch
fpitch{i}=fpitchAll(:,max(1,round((onVals(i)-offset1)*sr/hop)):end);
end
numFrames(i)=size(fpitch{i},2);
lengthSignal(i)=length(audio(max(1,round((onVals(i)-offset1)*sr)):max(round((onVals(i)+offset2)*sr),1)));
[a,b,c]=find(onsetMap(i,:), size(nmat,2));
num = 1;
for j = b
obs{i}(num,:)=db(sum(fpitch{i}(nmat{j}(idxCell{j},4)+harmonics,:)));
if sum(onsetMap(i+1:end,j))~=0
% db of sum fpitch vals - no harmonics
obs{i}(num+1,:)=db(sum(fpitch{i}(nmat{j}(idxCell{j}+1,4)+harmonics,:)));
% alternative features
% % db of mean fpitch vals - no harmonics
% db(mean(fpitch{i}(nmat{j}(idxCell{j},4)+harmonics,:)));
% db(mean(fpitch{i}(nmat{j}(idxCell{j}+1,4)+harmonics,:)));
%
% % db of mean fpitch vals - harmonics
% db(mean(fpitch{i}(nmat{j}(idxCell{j},4)+harmonics2,:)));
% db(mean(fpitch{i}(nmat{j}(idxCell{j}+1,4)+harmonics2,:)));
%
% % db of sum fpitch vals - harmonics
% db(sum(fpitch{i}(nmat{j}(idxCell{j},4)+harmonics2,:)));
% db(sum(fpitch{i}(nmat{j}(idxCell{j}+1,4)+harmonics2,:)));
idxCell{j}=idxCell{j}+1;
else
obs{i}(num+1,:)=db(sum(fpitch{i}(nmat{j}(idxCell{j},4)+harmonics,:)));
% numVoices = numVoices-1;
% b = b(b~=j);
end
num = num + 2;
end
if numVoices
for j = 1 : size(versions{numVoices},1)
if all(versions{numVoices}(j,:)==b);
idx = j;
end
end
% get appropriate trans, meansSeed, covarsSeed, and calculate mixmat
curTrans = trans{numVoices};
curMeansSeed = meansSeed{3}{numVoices}{idx};
curCovarsSeed = covarsSeed{3}{numVoices}{idx};
mixmat = ones(length(curMeansSeed),1);
sState = startingState{numVoices};
states = [1 2 3];
if i == 1
curTrans = curTrans(sum(notesInd{numVoices}==1,1)<1,sum(notesInd{numVoices}==1,1)<1);
curMeansSeed = curMeansSeed(:,sum(notesInd{numVoices}==1,1)<1);
curCovarsSeed = curCovarsSeed(:,:,sum(notesInd{numVoices}==1,1)<1);
mixmat = mixmat(sum(notesInd{numVoices}==1,1)<1);
sState = sState(sum(notesInd{numVoices}==1,1)<1);
sState(1) = 1;
notesIndTmp{i}=notesInd{numVoices}(:,sum(notesInd{4}==1,1)<1);
states = [2 3];
% curTrans = curTrans(sum(notesInd{numVoices}~=3)>(maxNotes-1),:);
% curMeansSeed = curMeansSeed(:,sum(notesInd{numVoices}~=3)>(maxNotes-1));
% curCovarsSeed = curCovarsSeed(:,:,sum(notesInd{numVoices}~=3)>(maxNotes-1));
% mixmat = mixmat(sum(notesInd{numVoices}~=3)>(maxNotes-1));
% sState = sState(sum(notesInd{numVoices}~=3)>(maxNotes-1));
% notesIndTmp=notesInd{maxNotes}(:,sum(notesInd{numVoices}~=3)>(maxNotes-1));
elseif i == length(onsetMap)
curTrans = curTrans(sum(notesInd{numVoices}<3,1)>(numVoices-1),:);
curMeansSeed = curMeansSeed(:,sum(notesInd{numVoices}<3,1)>(numVoices-1));
curCovarsSeed = curCovarsSeed(:,:,sum(notesInd{numVoices}<3,1)>(numVoices-1));
mixmat = mixmat(sum(notesInd{numVoices}<3,1)>(numVoices-1));
sState = sState(sum(notesInd{numVoices}<3,1)>(numVoices-1));
states = [1 2];
notesIndTmp{i}=notesInd{numVoices}(:,sum(notesInd{numVoices}<3,1)>(numVoices-1));
else
notesIndTmp{i}=notesInd{numVoices};
end
like1{i} = mixgauss_prob(obs{i}, curMeansSeed, curCovarsSeed, mixmat,1);
like1{i}(:,1)=[1; zeros(length(like1{i}(:,end))-1,1)];
like1{i}(:,end)=[zeros(length(like1{i}(:,end))-1,1); 1];
vpath1{i}=viterbi_path(sState, curTrans, like1{i});
end
% for each note
% i is the note
% b(j) is the voice
for j = 1 : numVoices
try
noteVals{i}{j}=notesIndTmp{i}(j,vpath1{i});
end
for m = states
try
notePos{i}{j}(m)=find(noteVals{i}{j}==m,1,'last');
catch
notePos{i}{j}(m)=notePos{i}{j}(m-1);
end
end
end
end
% % last note
numVoices=maxNotes;
curTrans = trans{numVoices};
idxEnd=sum(notesInd{numVoices}<3,1)>(numVoices-1);
curTrans = curTrans(idxEnd,idxEnd);
curMeansSeed = meansSeed{3}{numVoices}{1};
curMeansSeed = curMeansSeed(:,idxEnd);
curCovarsSeed = covarsSeed{3}{numVoices}{1};
curCovarsSeed = curCovarsSeed(:,:,idxEnd);
mixmat = ones(length(curMeansSeed),1);
%mixmat = mixmat(sum(notesInd{numVoices}<3,1)>(numVoices-1));
sState = startingState{numVoices};
sState = sState(1:length(mixmat));
states = [1 2];
lastOffset=length(onsetMap)+1;
notesIndTmp{lastOffset}=notesInd{numVoices}(:,idxEnd);
fpitch{lastOffset}=fpitchAll(:,round((onVals(end)+offset1)*sr/hop):end);
numFrames(lastOffset)=size(fpitch{lastOffset},2);
lengthSignal(lastOffset)=length(audio(max(1,round((onVals(end)+offset1)*sr)):end));
num = 1;
for note = 1 : numVoices
obs{lastOffset}(num,:)=db(sum(fpitch{lastOffset}(nmat{note}(idxCell{note},4)+harmonics,:)));
obs{lastOffset}(num+1,:)=db(sum(fpitch{lastOffset}(nmat{note}(idxCell{note},4)+harmonics,:)))
num = num + 2;
end
like1{lastOffset} = mixgauss_prob(obs{lastOffset}, curMeansSeed, curCovarsSeed, mixmat,1);
like1{lastOffset}(:,1)=[1; zeros(length(like1{lastOffset}(:,end))-1,1)];
like1{lastOffset}(:,end)=[zeros(length(like1{lastOffset}(:,end))-1,1); 1];
vpath1{lastOffset}=viterbi_path(sState, curTrans, like1{lastOffset});
for j = 1 : numVoices
noteVals{lastOffset}{j}=notesIndTmp{lastOffset}(j,vpath1{lastOffset});
for m = states
notePos{lastOffset}{j}(m)=find(noteVals{lastOffset}{j}==m,1,'last');
% catch
% notePos{lastOffset}{j}(m)=notePos{lastOffset}{j}(m-1);
% end
end
end
for i = 1 : length(onsetMap)
for j = find(onsetMap(i,:)): sum(onsetMap(i,:))
% if onsetMap(i,j) == 1 && sum(onsetMap(i+1:end,j))~=0
noteSecs{i}{j}=notePos{i}{j}*lengthSignal(i)/numFrames(i)/sr+onVals(i)-offset1;
if i > 1
% this doesn't work
estimatedOffs{j}(i-1) = noteSecs{i}{j}(1);
end
estimatedOns{j}(i) = noteSecs{i}{j}(2);
% else
% estimatedOffs{j}(i)=0;
% estimatedOns{j}(i)=0;
% end
end
end
for j = 1 : maxNotes
noteSecs{lastOffset}{j}=notePos{lastOffset}{j}*lengthSignal(lastOffset)/numFrames(lastOffset)/sr+onVals(end)+offset1;
estimatedOffs{j}(length(estimatedOns{j}))=noteSecs{lastOffset}{j}(1);
end