comparison src/midiEventHolder.cpp @ 6:6f5836d432ca

added confidence measure to each played event and associated match, but not really working yet
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 19 Aug 2011 19:42:01 +0100
parents 195907bb8bb7
children 75dcd1308658
comparison
equal deleted inserted replaced
5:195907bb8bb7 6:6f5836d432ca
45 bayesStruct.resetSpeedSize(200); 45 bayesStruct.resetSpeedSize(200);
46 bayesStruct.setRelativeSpeedScalar(0.01); 46 bayesStruct.setRelativeSpeedScalar(0.01);
47 bayesStruct.relativeSpeedPrior.getMaximum(); 47 bayesStruct.relativeSpeedPrior.getMaximum();
48 //bayesStruct.simpleExample(); 48 //bayesStruct.simpleExample();
49 49
50
51 speedWindowWidthMillis = 4000;
50 speedPriorValue = 1.0; 52 speedPriorValue = 1.0;
51 noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum); 53 noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum);
54
55 confidenceWeightingUsed = false;
52 56
53 printf("lookup index %f value %f\n", bayesStruct.prior.getLookupIndex(100, 30., 10.0), bayesStruct.prior.gaussianLookupTable[(int)bayesStruct.prior.getLookupIndex(100, 30., 10.0)]); 57 printf("lookup index %f value %f\n", bayesStruct.prior.getLookupIndex(100, 30., 10.0), bayesStruct.prior.gaussianLookupTable[(int)bayesStruct.prior.getLookupIndex(100, 30., 10.0)]);
54 } 58 }
55 59
56 60
199 int numberOfMatchesFound = findLocalMatches(pitch); 203 int numberOfMatchesFound = findLocalMatches(pitch);
200 setMatchLikelihoods(numberOfMatchesFound); 204 setMatchLikelihoods(numberOfMatchesFound);
201 bayesStruct.calculatePosterior(); 205 bayesStruct.calculatePosterior();
202 206
203 //having found matches we have matches for new note and matches for previous notes 207 //having found matches we have matches for new note and matches for previous notes
208 if (!confidenceWeightingUsed)
204 findLocalTempoPairs(); 209 findLocalTempoPairs();
210 else
211 findLocalTempoPairsWeightedForConfidence();
205 212
206 //bayesStruct.addGaussianNoiseToSpeedPosterior(10); 213 //bayesStruct.addGaussianNoiseToSpeedPosterior(10);
207 214
208 } 215 }
209 216
236 // printf("match times %i of %i::%f adding likelihood to %f\n", i, numberOfMatches, recordedEventTimes[matchesFound[i]], recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset); 243 // printf("match times %i of %i::%f adding likelihood to %f\n", i, numberOfMatches, recordedEventTimes[matchesFound[i]], recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset);
237 //this is the vent time since start of file 244 //this is the vent time since start of file
238 if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){ 245 if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){
239 // double confidenceMeasure = 0; 246 // double confidenceMeasure = 0;
240 // if (totalConfidence > 0) 247 // if (totalConfidence > 0)
241 // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence; 248 // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence;
242 249
243 bayesStruct.likelihood.addGaussianShape(recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset, likelihoodWidth, quantity);//* confidenceMeasure 250 bayesStruct.likelihood.addGaussianShape(recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset, likelihoodWidth, quantity);//* confidenceMeasure
244 }//end if 251 }//end if
245 } 252 }
246 bayesStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesStruct.likelihood.length); 253 bayesStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesStruct.likelihood.length);
257 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < startTime) 264 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < startTime)
258 startIndex++; 265 startIndex++;
259 266
260 } 267 }
261 268
269 IntVector v;
270 DoubleVector d;
271
262 double minimumConfidence = 0; 272 double minimumConfidence = 0;
263 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < endTime){ 273 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < endTime){
264 if (recordedNoteOnMatrix[startIndex][1] == notePitch){ 274 if (recordedNoteOnMatrix[startIndex][1] == notePitch){
275
265 matchesFound.push_back(startIndex); 276 matchesFound.push_back(startIndex);
277 v.push_back(startIndex);
266 double eventConfidence = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[startIndex]); 278 double eventConfidence = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[startIndex]);
267 if (eventConfidence > minimumConfidence){ 279 if (eventConfidence > minimumConfidence){
268 minimumConfidence = eventConfidence; 280 minimumConfidence = eventConfidence;
269 bestMatchIndex = startIndex; 281 bestMatchIndex = startIndex;
270 } 282 }
271 283 d.push_back(eventConfidence);
272 double confidence = bayesStruct.posterior.getValueAtMillis(mouseX); 284
285 double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX);
273 // recordedEventTimes[startIndex]); 286 // recordedEventTimes[startIndex]);
274 matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" ."; 287 matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" .";
275 } 288 }
276 startIndex++; 289 startIndex++;
277 } 290 }
280 // printf("%i MATCHES TO Note %i found\n", (int)matchesFound.size(), notePitch); 293 // printf("%i MATCHES TO Note %i found\n", (int)matchesFound.size(), notePitch);
281 int size = matchesFound.size(); 294 int size = matchesFound.size();
282 if (size > 0) 295 if (size > 0)
283 noteOnMatches[bestMatchIndex] = true; 296 noteOnMatches[bestMatchIndex] = true;
284 297
285 IntVector v; 298 v.insert(v.begin() , (int)size);
286 v.push_back(size); 299 d.insert(d.begin() , (double)size);
287 for (int i = 0;i < matchesFound.size();i++) 300
288 v.push_back(matchesFound[i]); 301 //v.push_back(size);
302 //d.push_back(size);
303 //for (int i = 0;i < matchesFound.size()+1;i++){
304 // v.push_back(matchesFound[i]);
305 // printf("match %i,[%i] is %i\n", startIndex, i, v[i]);
306 //}
307
289 308
290 matchMatrix.push_back(v); 309 matchMatrix.push_back(v);
310 matchConfidence.push_back(d);
291 311
292 return size; 312 return size;
293 } 313 }
294 314
295 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){ 315 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){
301 } 321 }
302 322
303 323
304 324
305 void midiEventHolder::findLocalTempoPairs(){ 325 void midiEventHolder::findLocalTempoPairs(){
306 326
307 int currentPlayedIndex = playedNoteOnMatrix.size()-1; 327 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
308 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]); 328 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
309 // printMatchesFound(); 329 // printMatchesFound();
310 // printMatchMatrix(); 330 // printMatchMatrix();
311 // printf("possible notes \n"); 331 // printf("possible notes \n");
312 332
313 333
314 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){ 334 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
315 335 //iterate through the recently matched events - even dodgy matches included
336 //size, index of match0, index of match1, ....
316 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1]; 337 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
317 338
318 int previousIndex = currentPlayedIndex-1; 339 int previousIndex = currentPlayedIndex-1;
319 340
320 while (previousIndex >= 0 && playedEventTimes[previousIndex] + 2000 > playedEventTimes[currentPlayedIndex]) { 341 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
321 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex]; 342 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
322 343
323 for (int k = 0;k < matchMatrix[previousIndex][0];k++){ 344 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
324 int recordedPreviousIndex = matchMatrix[previousIndex][k+1]; 345 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
325 346
326 347
327
328 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex]; 348 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
329 349
330 350
331 //we want the speed of the recording relative to that of the playing live 351 //we want the speed of the recording relative to that of the playing live
332 352
333 double speedRatio = recordedTimeDifference / playedTimeDifference; 353 double speedRatio = recordedTimeDifference / playedTimeDifference;
334 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){ 354 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
335 /* 355 /*
336 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]); 356 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
337 printf("[%i] :: ", recordedPreviousIndex); 357 printf("[%i] :: ", recordedPreviousIndex);
338 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference); 358 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
339 printf("update on speed ratio %f\n", speedRatio); 359 printf("update on speed ratio %f\n", speedRatio);
340 */ 360 */
341 // matchString += " speed: "+ofToString(speedRatio, 3); 361 // matchString += " speed: "+ofToString(speedRatio, 3);
342 // commented for debug 362 // commented for debug
343 363
344 bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match 364 bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match
345 365
346 } 366 }
347 // printf("\n"); 367 // printf("\n");
348 } 368 }
349 369
350 previousIndex--; 370 previousIndex--;
351 }//end while previousindex countdown 371 }//end while previousindex countdown
352 }//end for loop through possible current matches 372 }//end for loop through possible current matches
353 373
354 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate)); 374 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
355 } 375 }
376
377
378 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){
379
380 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
381 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
382 // printMatchesFound();
383 // printMatchMatrix();
384 // printf("possible notes \n");
385
386
387 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
388 //iterate through the recently matched events - even dodgy matches included
389 //size, index of match0, index of match1, ....
390 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
391
392 double currentMatchConfidence = matchConfidence[currentPlayedIndex][i+1];//new confidence
393
394 int previousIndex = currentPlayedIndex-1;
395
396 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
397 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
398
399 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
400 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
401
402 double previousMatchConfidence = matchConfidence[previousIndex][k+1];
403
404
405 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
406
407
408 //we want the speed of the recording relative to that of the playing live
409
410 double speedRatio = recordedTimeDifference / playedTimeDifference;
411 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
412
413 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
414 printf("[%i] :: ", recordedPreviousIndex);
415 printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
416 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
417 printf("update on speed ratio %f\n", speedRatio);
418
419 // matchString += " speed: "+ofToString(speedRatio, 3);
420 // commented for debug
421 double weighting = previousMatchConfidence * currentMatchConfidence * 0.1;
422 bayesStruct.updateTempoDistribution(speedRatio, weighting);//second paramter is confidence in the match
423
424 }
425 // printf("\n");
426 }
427
428 previousIndex--;
429 }//end while previousindex countdown
430 }//end for loop through possible current matches
431
432 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
433 }
434
356 435
357 436
358 void midiEventHolder::updatePlayPosition(){ 437 void midiEventHolder::updatePlayPosition(){
359 438
360 //in actual fact if we are changing the speed of the play position 439 //in actual fact if we are changing the speed of the play position