andrew@0
|
1 /*
|
andrew@0
|
2 * midiEventHolder.cpp
|
andrew@0
|
3 * midiCannamReader3
|
andrew@0
|
4 *
|
andrew@0
|
5 * Created by Andrew on 19/07/2011.
|
andrew@0
|
6 * Copyright 2011 QMUL. All rights reserved.
|
andrew@0
|
7 *
|
andrew@0
|
8 */
|
andrew@0
|
9 //hello
|
andrew@0
|
10
|
andrew@0
|
11 #include "midiEventHolder.h"
|
andrew@0
|
12
|
andrew@0
|
13 midiEventHolder::midiEventHolder(){
|
andrew@0
|
14 // recordedNoteOnIndex = 0;
|
andrew@0
|
15
|
andrew@0
|
16 width = ofGetWidth();
|
andrew@0
|
17 height = ofGetHeight();
|
andrew@0
|
18 screenWidth= &width;
|
andrew@0
|
19 screenHeight = &height;
|
andrew@0
|
20
|
andrew@0
|
21 ticksPerScreen = 4000;
|
andrew@0
|
22 tickLocation = 0;
|
andrew@0
|
23 pulsesPerQuarternote = 240;
|
andrew@0
|
24 noteArrayIndex = 0;
|
andrew@0
|
25 noteMinimum = 30;
|
andrew@0
|
26 noteMaximum = 96;
|
andrew@0
|
27
|
andrew@4
|
28 minimumMatchSpeed = 0.0;
|
andrew@4
|
29 maximumMatchSpeed = 2.0;
|
andrew@2
|
30
|
andrew@0
|
31 likelihoodWidth = 100;
|
andrew@2
|
32 likelihoodToNoiseRatio = 0.02;
|
andrew@0
|
33
|
andrew@9
|
34 bayesStruct.speedLikelihoodNoise = 0.1;//was 0.05
|
andrew@2
|
35 bayesStruct.speedDecayWidth = 20;
|
andrew@2
|
36 bayesStruct.speedDecayAmount = 10;
|
andrew@2
|
37
|
andrew@3
|
38 speedPriorValue = 1.0;
|
andrew@2
|
39
|
andrew@2
|
40 matchWindowWidth = 8000;//window size for matching in ms
|
andrew@0
|
41
|
andrew@1
|
42 bayesStruct.resetSize(matchWindowWidth);
|
andrew@2
|
43 bayesStruct.setPositionDistributionScalar(1);
|
andrew@2
|
44
|
andrew@0
|
45 bayesStruct.resetSpeedSize(200);
|
andrew@0
|
46 bayesStruct.setRelativeSpeedScalar(0.01);
|
andrew@0
|
47 bayesStruct.relativeSpeedPrior.getMaximum();
|
andrew@2
|
48 //bayesStruct.simpleExample();
|
andrew@2
|
49
|
andrew@6
|
50
|
andrew@6
|
51 speedWindowWidthMillis = 4000;
|
andrew@3
|
52 speedPriorValue = 1.0;
|
andrew@0
|
53 noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum);
|
andrew@4
|
54
|
andrew@6
|
55 confidenceWeightingUsed = false;
|
andrew@6
|
56
|
andrew@9
|
57 drawPhaseMode = true;
|
andrew@9
|
58
|
andrew@4
|
59 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)]);
|
andrew@0
|
60 }
|
andrew@0
|
61
|
andrew@0
|
62
|
andrew@0
|
63
|
andrew@0
|
64 void midiEventHolder::reset(){
|
andrew@2
|
65 //called when we start playing
|
andrew@2
|
66
|
andrew@0
|
67 noteArrayIndex = 0;
|
andrew@0
|
68 tickLocation = 0;
|
andrew@0
|
69 lastPeriodUpdateTime = ofGetElapsedTimeMillis();
|
andrew@0
|
70 bayesStruct.lastEventTime = ofGetElapsedTimeMillis();
|
andrew@0
|
71 numberOfScreensIn = 0;
|
andrew@0
|
72 // recordedNoteOnIndex = 0;
|
andrew@0
|
73 bayesStruct.setNewDistributionOffsets(0);
|
andrew@0
|
74 bayesStruct.posterior.offset = 0;
|
andrew@0
|
75
|
andrew@0
|
76 playedEventTimes.clear();
|
andrew@0
|
77 playedNoteOnMatrix.clear();
|
andrew@0
|
78 matchMatrix.clear();
|
andrew@5
|
79 bestMatchIndex = 0;
|
andrew@0
|
80
|
andrew@0
|
81 bayesStruct.resetSpeedToOne();
|
andrew@3
|
82 bayesStruct.setSpeedPrior(speedPriorValue);
|
andrew@5
|
83 setMatchedNotesBackToFalse();
|
andrew@5
|
84 }
|
andrew@5
|
85
|
andrew@5
|
86 void midiEventHolder::setMatchedNotesBackToFalse(){
|
andrew@5
|
87 for (int i = 0;i < noteOnMatches.size();i++)
|
andrew@5
|
88 noteOnMatches[i] = false;
|
andrew@0
|
89 }
|
andrew@0
|
90
|
andrew@1
|
91 void midiEventHolder::clearAllEvents(){
|
andrew@1
|
92 recordedNoteOnMatrix.clear();
|
andrew@1
|
93 matchesFound.clear();
|
andrew@1
|
94 noteOnMatches.clear();
|
andrew@1
|
95 recordedEventTimes.clear();
|
andrew@1
|
96
|
andrew@1
|
97 //played events:
|
andrew@1
|
98 playedEventTimes.clear();
|
andrew@1
|
99 playedNoteOnMatrix.clear();
|
andrew@1
|
100 matchMatrix.clear();
|
andrew@1
|
101 }
|
andrew@1
|
102
|
andrew@0
|
103 void midiEventHolder::printNotes(){
|
andrew@0
|
104 printf("RECORDED MATRIX");
|
andrew@0
|
105 for (int i = 0;i < recordedNoteOnMatrix.size();i++){
|
andrew@0
|
106 printf("%i :: %i @ %f\n", recordedNoteOnMatrix[i][0], recordedNoteOnMatrix[i][1], recordedEventTimes[i]);
|
andrew@0
|
107 }
|
andrew@0
|
108 }
|
andrew@0
|
109
|
andrew@0
|
110
|
andrew@0
|
111 double midiEventHolder::getEventTimeTicks(double millis){
|
andrew@0
|
112 return (millis * pulsesPerQuarternote / period);
|
andrew@0
|
113 }
|
andrew@0
|
114
|
andrew@0
|
115 double midiEventHolder::getEventTimeMillis(double ticks){
|
andrew@0
|
116 return (period * ticks / (double) pulsesPerQuarternote);
|
andrew@0
|
117 }
|
andrew@0
|
118
|
andrew@0
|
119 void midiEventHolder::newNoteOnEvent(int pitch, int velocity, double timePlayed){
|
andrew@0
|
120
|
andrew@0
|
121 //MOVE INTO BAYESSTRUCT?? XXX
|
andrew@0
|
122 //bayesStruct.copyPriorToPosterior();
|
andrew@0
|
123 //why was this here??
|
andrew@0
|
124 bayesStruct.prior.copyFromDynamicVector(bayesStruct.posterior);//try the otehr way
|
andrew@0
|
125 //bayesStruct.copyPriorToPosterior();
|
andrew@0
|
126 //need to get new MAP position and set the offset of the arrays
|
andrew@0
|
127 //currently bestEstimate is the approx for the new MAP position
|
andrew@0
|
128
|
andrew@0
|
129
|
andrew@0
|
130 //add the new event to our played information matrix
|
andrew@0
|
131 IntVector v;
|
andrew@0
|
132 v.push_back(pitch);
|
andrew@0
|
133 v.push_back(velocity);
|
andrew@0
|
134 playedNoteOnMatrix.push_back(v);
|
andrew@0
|
135
|
andrew@0
|
136
|
andrew@0
|
137 //would update the arrays at this point to show where out current location (phase) and tempo is.
|
andrew@2
|
138 // double timeNow = ofGetElapsedTimeMillis() - startTime;
|
andrew@2
|
139 double timeNow = timePlayed;// - startTime;
|
andrew@2
|
140 recentNoteOnTime = timePlayed;
|
andrew@0
|
141
|
andrew@2
|
142 // printf("Max time %f OF time %f \n", timePlayed, timeNow);
|
andrew@0
|
143
|
andrew@0
|
144 playedEventTimes.push_back(timePlayed);
|
andrew@0
|
145
|
andrew@2
|
146 // double timeDifference = ofGetElapsedTimeMillis() - bayesStruct.lastEventTime;
|
andrew@2
|
147 double timeDifference = timePlayed - bayesStruct.lastEventTime;
|
andrew@0
|
148
|
andrew@9
|
149 // printf("note %i played at %f and last event %f\n", pitch, timePlayed, bayesStruct.lastEventTime);
|
andrew@0
|
150 //addnoise to the tempo distribution
|
andrew@2
|
151 //bayesStruct.decaySpeedDistribution(timeDifference);
|
andrew@2
|
152 if (timeDifference > 50){
|
andrew@2
|
153 bayesStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10 / 100.);
|
andrew@2
|
154 // bayesStruct.addTriangularNoiseToSpeedPosterior(timeDifference * 10 / 100.);
|
andrew@2
|
155 }
|
andrew@2
|
156
|
andrew@2
|
157 bayesStruct.updateTmpBestEstimate(timeDifference);// debug - didnt work bayesStruct.bestEstimate = bayesStruct.tmpBestEstimate;
|
andrew@2
|
158 bayesStruct.updateBestEstimate();
|
andrew@2
|
159 bayesStruct.lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
|
andrew@0
|
160
|
andrew@0
|
161 // double newMAPestimateTime = bayesStruct.posterior.getIndexInRealTerms(bayesStruct.posterior.MAPestimate);
|
andrew@0
|
162 //was offset + bayesStruct.posterior.MAPestimate; but this doesnt include scalar to convert to millis
|
andrew@0
|
163
|
andrew@0
|
164 timeString = "Pitch:"+ofToString(pitch);
|
andrew@0
|
165 timeString += ", time now:"+ofToString(timeNow, 1);
|
andrew@0
|
166 timeString += " TD "+ofToString(timeDifference, 1);
|
andrew@0
|
167 timeString += " offset "+ofToString(bayesStruct.posterior.offset , 0);
|
andrew@0
|
168 timeString += " map Est: "+ofToString(bayesStruct.posterior.MAPestimate, 0);
|
andrew@0
|
169 // timeString += " Previous time" + ofToString(newMAPestimateTime,0);
|
andrew@9
|
170 timeString += " speedMap "+ofToString(bayesStruct.relativeSpeedPosterior.integratedEstimate, 2);
|
andrew@9
|
171 timeString += " :: "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.integratedEstimate), 2);
|
andrew@0
|
172
|
andrew@0
|
173 // newMAPestimateTime += (timeDifference * bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
|
andrew@0
|
174 // timeString += " : Predicted MAP time" + ofToString(newMAPestimateTime,0);
|
andrew@0
|
175
|
andrew@0
|
176 //then we recalculate the window start based on MAP being central
|
andrew@0
|
177 //then we do the matches on these and the likelihood on these.
|
andrew@0
|
178
|
andrew@0
|
179 bayesStruct.setNewDistributionOffsets(max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2)));
|
andrew@0
|
180 // bayesStruct.prior.offset = max(0.,newMAPestimateTime - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
|
andrew@0
|
181
|
andrew@0
|
182 timeString += " \n : new offset " + ofToString(bayesStruct.prior.offset , 0);
|
andrew@0
|
183 timeString += " \n best estimate "+ofToString(bayesStruct.bestEstimate, 1);
|
andrew@10
|
184 timeString += " error "+ofToString(minimumMatchError, 0);
|
andrew@9
|
185 timeString += " map "+ofToString(bayesStruct.relativeSpeedPosterior.integratedEstimate, 1);
|
andrew@11
|
186 timeString += " rel speed "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.integratedEstimate), 2);
|
andrew@0
|
187
|
andrew@0
|
188
|
andrew@0
|
189 //be able to draw the prior in correct location relative to the midi notes
|
andrew@2
|
190 //this calculates the cross update of all possible speeds and all possible positions
|
andrew@0
|
191 bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, timeDifference);
|
andrew@0
|
192
|
andrew@0
|
193
|
andrew@0
|
194 timeString += " new OFF "+ofToString(bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2), 1);
|
andrew@0
|
195 timeString += " notearrayindex "+ofToString(noteArrayIndex, 0);
|
andrew@0
|
196 //when this is off teh screen there is a problem somehow XXX
|
andrew@0
|
197 bayesStruct.posterior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
|
andrew@0
|
198 // bayesStruct.prior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
|
andrew@0
|
199 //trying to switch to prior
|
andrew@0
|
200
|
andrew@2
|
201 //bayesStruct.lastEventTime = ofGetElapsedTimeMillis();
|
andrew@2
|
202 bayesStruct.lastEventTime = timePlayed;
|
andrew@0
|
203
|
andrew@0
|
204 //do the cross update to find current posterior for location
|
andrew@1
|
205 // totalConfidence= 0;
|
andrew@0
|
206 int numberOfMatchesFound = findLocalMatches(pitch);
|
andrew@0
|
207 setMatchLikelihoods(numberOfMatchesFound);
|
andrew@0
|
208 bayesStruct.calculatePosterior();
|
andrew@0
|
209
|
andrew@0
|
210 //having found matches we have matches for new note and matches for previous notes
|
andrew@12
|
211 //if (!confidenceWeightingUsed)
|
andrew@0
|
212 findLocalTempoPairs();
|
andrew@12
|
213 //else
|
andrew@12
|
214 //findLocalTempoPairsWeightedForConfidence();
|
andrew@0
|
215
|
andrew@2
|
216 //bayesStruct.addGaussianNoiseToSpeedPosterior(10);
|
andrew@0
|
217
|
andrew@0
|
218 }
|
andrew@0
|
219
|
andrew@0
|
220 int midiEventHolder::findLocalMatches(int notePitch){
|
andrew@0
|
221
|
andrew@0
|
222 //here we find the matches to the new note within appropriate range
|
andrew@0
|
223
|
andrew@1
|
224 matchString = "";
|
andrew@0
|
225
|
andrew@0
|
226 windowStartTime = max(0.0,(bayesStruct.bestEstimate - matchWindowWidth/2));//was playPositionInMillis
|
andrew@0
|
227 int numberOfMatches = findMatch(notePitch, windowStartTime, windowStartTime + matchWindowWidth);
|
andrew@5
|
228
|
andrew@0
|
229
|
andrew@1
|
230 matchString += " pitch: "+ofToString(notePitch)+" matches "+ofToString(numberOfMatches)+" win start "+ofToString(windowStartTime);
|
andrew@1
|
231
|
andrew@0
|
232 return numberOfMatches;
|
andrew@0
|
233
|
andrew@0
|
234
|
andrew@0
|
235 }
|
andrew@0
|
236
|
andrew@0
|
237
|
andrew@0
|
238 void midiEventHolder::setMatchLikelihoods(int numberOfMatches){
|
andrew@0
|
239 //reset the offset to match the prior
|
andrew@0
|
240 bayesStruct.likelihood.offset = bayesStruct.prior.offset;
|
andrew@0
|
241 bayesStruct.likelihood.zero();//set to zero
|
andrew@0
|
242
|
andrew@2
|
243 double quantity = likelihoodToNoiseRatio / numberOfMatches;
|
andrew@0
|
244
|
andrew@0
|
245 for (int i = 0;i < numberOfMatches && matchesFound[i] >= 0 && matchesFound[i] < recordedEventTimes.size();i++){
|
andrew@0
|
246 // printf("match times %i of %i::%f adding likelihood to %f\n", i, numberOfMatches, recordedEventTimes[matchesFound[i]], recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset);
|
andrew@0
|
247 //this is the vent time since start of file
|
andrew@0
|
248 if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){
|
andrew@1
|
249 // double confidenceMeasure = 0;
|
andrew@1
|
250 // if (totalConfidence > 0)
|
andrew@6
|
251 // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence;
|
andrew@2
|
252
|
andrew@2
|
253 bayesStruct.likelihood.addGaussianShape(recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset, likelihoodWidth, quantity);//* confidenceMeasure
|
andrew@0
|
254 }//end if
|
andrew@0
|
255 }
|
andrew@2
|
256 bayesStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesStruct.likelihood.length);
|
andrew@0
|
257 }
|
andrew@0
|
258
|
andrew@0
|
259 int midiEventHolder::findMatch(const int& notePitch, const int& startTime, const int& endTime){
|
andrew@0
|
260
|
andrew@0
|
261 matchesFound.clear();
|
andrew@0
|
262 int startIndex = 0;
|
andrew@10
|
263
|
andrew@0
|
264 if (recordedEventTimes.size() > 0){
|
andrew@0
|
265
|
andrew@0
|
266 //get to the right range of events to check in
|
andrew@0
|
267 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < startTime)
|
andrew@0
|
268 startIndex++;
|
andrew@0
|
269
|
andrew@0
|
270 }
|
andrew@0
|
271
|
andrew@6
|
272 IntVector v;
|
andrew@6
|
273 DoubleVector d;
|
andrew@10
|
274 double tmpError = 100000.;//v high error
|
andrew@6
|
275
|
andrew@5
|
276 double minimumConfidence = 0;
|
andrew@0
|
277 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < endTime){
|
andrew@0
|
278 if (recordedNoteOnMatrix[startIndex][1] == notePitch){
|
andrew@6
|
279
|
andrew@0
|
280 matchesFound.push_back(startIndex);
|
andrew@6
|
281 v.push_back(startIndex);
|
andrew@5
|
282 double eventConfidence = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[startIndex]);
|
andrew@5
|
283 if (eventConfidence > minimumConfidence){
|
andrew@5
|
284 minimumConfidence = eventConfidence;
|
andrew@5
|
285 bestMatchIndex = startIndex;
|
andrew@5
|
286 }
|
andrew@6
|
287 d.push_back(eventConfidence);
|
andrew@5
|
288
|
andrew@6
|
289 double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX);
|
andrew@1
|
290 // recordedEventTimes[startIndex]);
|
andrew@1
|
291 matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" .";
|
andrew@10
|
292
|
andrew@10
|
293 if (abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate) < tmpError){
|
andrew@10
|
294 //record the error between expected and observed times
|
andrew@10
|
295 tmpError = abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate);
|
andrew@12
|
296 minimumMatchError = tmpError;//recordedEventTimes[startIndex] - bayesStruct.bestEstimate;
|
andrew@10
|
297 }
|
andrew@10
|
298
|
andrew@0
|
299 }
|
andrew@0
|
300 startIndex++;
|
andrew@0
|
301 }
|
andrew@5
|
302
|
andrew@5
|
303
|
andrew@0
|
304 // printf("%i MATCHES TO Note %i found\n", (int)matchesFound.size(), notePitch);
|
andrew@0
|
305 int size = matchesFound.size();
|
andrew@5
|
306 if (size > 0)
|
andrew@5
|
307 noteOnMatches[bestMatchIndex] = true;
|
andrew@5
|
308
|
andrew@6
|
309 v.insert(v.begin() , (int)size);
|
andrew@6
|
310 d.insert(d.begin() , (double)size);
|
andrew@6
|
311
|
andrew@6
|
312 //v.push_back(size);
|
andrew@6
|
313 //d.push_back(size);
|
andrew@6
|
314 //for (int i = 0;i < matchesFound.size()+1;i++){
|
andrew@6
|
315 // v.push_back(matchesFound[i]);
|
andrew@6
|
316 // printf("match %i,[%i] is %i\n", startIndex, i, v[i]);
|
andrew@6
|
317 //}
|
andrew@6
|
318
|
andrew@0
|
319
|
andrew@0
|
320 matchMatrix.push_back(v);
|
andrew@6
|
321 matchConfidence.push_back(d);
|
andrew@0
|
322
|
andrew@0
|
323 return size;
|
andrew@0
|
324 }
|
andrew@0
|
325
|
andrew@0
|
326 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){
|
andrew@0
|
327 for (int i = 0;i < matchesFound.size();i++){
|
andrew@0
|
328 if (matchesFound[i] == tmpIndex)
|
andrew@0
|
329 return true;
|
andrew@0
|
330 }
|
andrew@0
|
331 return false;
|
andrew@0
|
332 }
|
andrew@0
|
333
|
andrew@0
|
334
|
andrew@0
|
335
|
andrew@0
|
336 void midiEventHolder::findLocalTempoPairs(){
|
andrew@6
|
337
|
andrew@0
|
338 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
|
andrew@6
|
339 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
|
andrew@6
|
340 // printMatchesFound();
|
andrew@6
|
341 // printMatchMatrix();
|
andrew@6
|
342 // printf("possible notes \n");
|
andrew@9
|
343 bool needToUpdate = false;
|
andrew@9
|
344 bayesStruct.setLikelihoodToConstant();
|
andrew@0
|
345
|
andrew@0
|
346
|
andrew@0
|
347 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
|
andrew@6
|
348 //iterate through the recently matched events - even dodgy matches included
|
andrew@6
|
349 //size, index of match0, index of match1, ....
|
andrew@9
|
350
|
andrew@9
|
351
|
andrew@0
|
352 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
|
andrew@0
|
353
|
andrew@0
|
354 int previousIndex = currentPlayedIndex-1;
|
andrew@9
|
355
|
andrew@0
|
356
|
andrew@6
|
357 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
|
andrew@0
|
358 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
|
andrew@6
|
359
|
andrew@0
|
360 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
|
andrew@0
|
361 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
|
andrew@0
|
362
|
andrew@6
|
363
|
andrew@0
|
364 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
|
andrew@6
|
365
|
andrew@0
|
366
|
andrew@0
|
367 //we want the speed of the recording relative to that of the playing live
|
andrew@0
|
368
|
andrew@0
|
369 double speedRatio = recordedTimeDifference / playedTimeDifference;
|
andrew@0
|
370 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
|
andrew@12
|
371 //adding in a prior that prefers 1
|
andrew@12
|
372 double priorWeighting = sin(speedRatio * PI/2);
|
andrew@12
|
373
|
andrew@12
|
374
|
andrew@6
|
375 /*
|
andrew@6
|
376 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
|
andrew@6
|
377 printf("[%i] :: ", recordedPreviousIndex);
|
andrew@6
|
378 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
|
andrew@6
|
379 printf("update on speed ratio %f\n", speedRatio);
|
andrew@6
|
380 */
|
andrew@1
|
381 // matchString += " speed: "+ofToString(speedRatio, 3);
|
andrew@6
|
382 // commented for debug
|
andrew@2
|
383
|
andrew@9
|
384 //bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match
|
andrew@9
|
385 double amount = (1-bayesStruct.speedLikelihoodNoise)/10;
|
andrew@12
|
386 amount *= priorWeighting;
|
andrew@9
|
387 bayesStruct.updateTempoLikelihood(speedRatio, amount);
|
andrew@9
|
388 needToUpdate = true;
|
andrew@0
|
389 }
|
andrew@6
|
390 // printf("\n");
|
andrew@0
|
391 }
|
andrew@0
|
392
|
andrew@0
|
393 previousIndex--;
|
andrew@0
|
394 }//end while previousindex countdown
|
andrew@0
|
395 }//end for loop through possible current matches
|
andrew@0
|
396
|
andrew@9
|
397 if (needToUpdate)
|
andrew@9
|
398 bayesStruct.updateTempoDistribution();
|
andrew@9
|
399
|
andrew@2
|
400 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
|
andrew@0
|
401 }
|
andrew@0
|
402
|
andrew@0
|
403
|
andrew@6
|
404 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){
|
andrew@9
|
405 bool needToUpdate = false;
|
andrew@6
|
406
|
andrew@6
|
407 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
|
andrew@6
|
408 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
|
andrew@6
|
409 // printMatchesFound();
|
andrew@6
|
410 // printMatchMatrix();
|
andrew@6
|
411 // printf("possible notes \n");
|
andrew@6
|
412
|
andrew@9
|
413 bayesStruct.setLikelihoodToConstant();
|
andrew@6
|
414
|
andrew@6
|
415 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
|
andrew@6
|
416 //iterate through the recently matched events - even dodgy matches included
|
andrew@6
|
417 //size, index of match0, index of match1, ....
|
andrew@6
|
418 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
|
andrew@6
|
419
|
andrew@6
|
420 double currentMatchConfidence = matchConfidence[currentPlayedIndex][i+1];//new confidence
|
andrew@6
|
421
|
andrew@6
|
422 int previousIndex = currentPlayedIndex-1;
|
andrew@6
|
423
|
andrew@6
|
424 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
|
andrew@6
|
425 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
|
andrew@6
|
426
|
andrew@6
|
427 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
|
andrew@6
|
428 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
|
andrew@6
|
429
|
andrew@6
|
430 double previousMatchConfidence = matchConfidence[previousIndex][k+1];
|
andrew@6
|
431
|
andrew@6
|
432
|
andrew@6
|
433 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
|
andrew@6
|
434
|
andrew@6
|
435
|
andrew@6
|
436 //we want the speed of the recording relative to that of the playing live
|
andrew@6
|
437
|
andrew@6
|
438 double speedRatio = recordedTimeDifference / playedTimeDifference;
|
andrew@6
|
439 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
|
andrew@6
|
440
|
andrew@6
|
441 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
|
andrew@6
|
442 printf("[%i] :: ", recordedPreviousIndex);
|
andrew@6
|
443 printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
|
andrew@6
|
444 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
|
andrew@6
|
445 printf("update on speed ratio %f\n", speedRatio);
|
andrew@6
|
446
|
andrew@6
|
447 // matchString += " speed: "+ofToString(speedRatio, 3);
|
andrew@6
|
448 // commented for debug
|
andrew@9
|
449 double weighting = previousMatchConfidence * currentMatchConfidence ;
|
andrew@9
|
450 double amount = (1-bayesStruct.speedLikelihoodNoise)*weighting/10;
|
andrew@9
|
451 bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match
|
andrew@9
|
452 needToUpdate = true;
|
andrew@6
|
453 }
|
andrew@6
|
454 // printf("\n");
|
andrew@6
|
455 }
|
andrew@6
|
456
|
andrew@6
|
457 previousIndex--;
|
andrew@6
|
458 }//end while previousindex countdown
|
andrew@6
|
459 }//end for loop through possible current matches
|
andrew@6
|
460
|
andrew@9
|
461 if (needToUpdate)
|
andrew@9
|
462 bayesStruct.updateTempoDistribution();
|
andrew@6
|
463 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
|
andrew@6
|
464 }
|
andrew@6
|
465
|
andrew@6
|
466
|
andrew@6
|
467
|
andrew@0
|
468 void midiEventHolder::updatePlayPosition(){
|
andrew@0
|
469
|
andrew@0
|
470 //in actual fact if we are changing the speed of the play position
|
andrew@0
|
471 //we will need to update this via the file
|
andrew@0
|
472
|
andrew@0
|
473 double timeDifference = ofGetElapsedTimeMillis() - lastPeriodUpdateTime;
|
andrew@0
|
474 //this is time diff in milliseconds
|
andrew@0
|
475 //then we have
|
andrew@0
|
476 double quarterNoteIntervals = (timeDifference / period);
|
andrew@0
|
477 tickLocation = quarterNoteIntervals * pulsesPerQuarternote;
|
andrew@0
|
478
|
andrew@0
|
479 playPositionInMillis = timeDifference;//based on updating from when we change period
|
andrew@0
|
480 //this to be added
|
andrew@0
|
481
|
andrew@0
|
482 bayesStruct.updateBestEstimate();
|
andrew@0
|
483
|
andrew@0
|
484 }
|
andrew@0
|
485
|
andrew@0
|
486
|
andrew@9
|
487 void midiEventHolder::drawMidiFile(){
|
andrew@9
|
488
|
andrew@0
|
489 //draws midi file on scrolling screen
|
andrew@0
|
490 int size = recordedNoteOnMatrix.size();
|
andrew@0
|
491 if (size > 0){
|
andrew@0
|
492
|
andrew@0
|
493 numberOfScreensIn = floor(bayesStruct.bestEstimate / getEventTimeMillis(ticksPerScreen));//rpounds down on no screens in
|
andrew@0
|
494
|
andrew@9
|
495 // numberOfScreensIn = tickLocation / ticksPerScreen;//rounds down
|
andrew@0
|
496 timeOffsetForScreen = getEventTimeMillis(numberOfScreensIn * ticksPerScreen);
|
andrew@0
|
497
|
andrew@11
|
498 while (noteArrayIndex < recordedNoteOnMatrix.size()-1 && tickLocation > recordedNoteOnMatrix[noteArrayIndex][0] )
|
andrew@0
|
499 noteArrayIndex++;
|
andrew@0
|
500
|
andrew@0
|
501
|
andrew@0
|
502 while (noteArrayIndex > 0 && noteArrayIndex < size && tickLocation < recordedNoteOnMatrix[noteArrayIndex][0])
|
andrew@0
|
503 noteArrayIndex--;
|
andrew@0
|
504
|
andrew@0
|
505 //need to start where we currently are in file
|
andrew@0
|
506 int maxNoteIndexToPrint = noteArrayIndex;
|
andrew@11
|
507 int minNoteIndexToPrint = min(size-1,noteArrayIndex);//not needed as changed above
|
andrew@0
|
508
|
andrew@0
|
509 while (maxNoteIndexToPrint < recordedNoteOnMatrix.size() && recordedNoteOnMatrix[maxNoteIndexToPrint][0] < (numberOfScreensIn+1)*ticksPerScreen )
|
andrew@0
|
510 maxNoteIndexToPrint++;
|
andrew@0
|
511
|
andrew@11
|
512 while (minNoteIndexToPrint > 0 && recordedNoteOnMatrix[minNoteIndexToPrint][0] > numberOfScreensIn*ticksPerScreen)//&& minNoteIndexToPrint < size
|
andrew@0
|
513 minNoteIndexToPrint--;
|
andrew@0
|
514
|
andrew@0
|
515 for (int tmpIndex = max(0,minNoteIndexToPrint);tmpIndex < min(maxNoteIndexToPrint, (int)recordedNoteOnMatrix.size());tmpIndex++){
|
andrew@0
|
516
|
andrew@0
|
517 if (checkIfMatchedNote(tmpIndex))
|
andrew@0
|
518 ofSetColor(0,0,255);
|
andrew@5
|
519 else if(noteOnMatches[tmpIndex]){
|
andrew@9
|
520 ofSetColor(255,0,255);
|
andrew@5
|
521 }else{
|
andrew@0
|
522 ofSetColor(255,255,255);
|
andrew@5
|
523 }
|
andrew@5
|
524
|
andrew@9
|
525
|
andrew@9
|
526
|
andrew@9
|
527 // XXX replace ofgetwidth below
|
andrew@0
|
528 //if (tmpIndex >= 0 && tmpIndex < size)
|
andrew@0
|
529 int xLocation = (float)(recordedNoteOnMatrix[tmpIndex][0] - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen;
|
andrew@0
|
530 int duration = (float)(recordedNoteOnMatrix[tmpIndex][3]*(*screenWidth))/(float)ticksPerScreen;
|
andrew@0
|
531
|
andrew@0
|
532
|
andrew@0
|
533 int yLocation = (*screenHeight) - ((recordedNoteOnMatrix[tmpIndex][1] - noteMinimum )*(*screenHeight)/ (float)(noteMaximum - noteMinimum));
|
andrew@0
|
534 ofRect(xLocation,yLocation, duration, noteHeight);
|
andrew@0
|
535
|
andrew@0
|
536 }
|
andrew@0
|
537
|
andrew@0
|
538
|
andrew@0
|
539 int xLocation;// = getLocationFromTicks(tickLocation);
|
andrew@9
|
540 // ofLine(xLocation, 0, xLocation, (*screenHeight));
|
andrew@0
|
541
|
andrew@0
|
542 //orange line at best estimate
|
andrew@0
|
543 xLocation = getLocationFromMillis(bayesStruct.bestEstimate);
|
andrew@0
|
544 ofSetColor(250,100,0);
|
andrew@0
|
545 ofLine(xLocation, 0, xLocation, (*screenHeight));
|
andrew@0
|
546
|
andrew@2
|
547 xLocation = getLocationFromMillis(bayesStruct.tmpBestEstimate);
|
andrew@2
|
548 ofSetColor(250,100,0);
|
andrew@2
|
549 ofLine(xLocation, 0, xLocation, (*screenHeight));
|
andrew@2
|
550
|
andrew@0
|
551
|
andrew@0
|
552 //lines where matching window start and end are
|
andrew@0
|
553 ofSetColor(0,100,255);
|
andrew@0
|
554 xLocation = getLocationFromMillis(windowStartTime);
|
andrew@0
|
555 ofLine(xLocation, 0, xLocation, (*screenHeight));
|
andrew@0
|
556 xLocation = getLocationFromMillis(windowStartTime+matchWindowWidth);
|
andrew@0
|
557 ofLine(xLocation, 0, xLocation, (*screenHeight));
|
andrew@9
|
558
|
andrew@0
|
559
|
andrew@11
|
560 int maxSize = recordedNoteOnMatrix[size-1][0];
|
andrew@11
|
561
|
andrew@11
|
562 string indexString = "num screens in "+ofToString(numberOfScreensIn)+"; min index to print "+ofToString(minNoteIndexToPrint)+", max index to print "+ofToString(maxNoteIndexToPrint);
|
andrew@11
|
563 indexString += " size "+ofToString(size)+" tick loc "+ofToString(tickLocation)+" max size "+ofToString(maxSize);
|
andrew@11
|
564 ofDrawBitmapString(indexString, 20, 40);
|
andrew@11
|
565
|
andrew@0
|
566 }
|
andrew@0
|
567
|
andrew@0
|
568 ofDrawBitmapString(ofToString(timeOffsetForScreen, 1), 20,20);
|
andrew@0
|
569
|
andrew@0
|
570 ofDrawBitmapString(timeString, 20, 60);
|
andrew@0
|
571
|
andrew@11
|
572
|
andrew@9
|
573 }
|
andrew@9
|
574
|
andrew@9
|
575 void midiEventHolder::drawFile(){
|
andrew@9
|
576 drawMidiFile();
|
andrew@9
|
577
|
andrew@9
|
578
|
andrew@0
|
579 // bayesStruct.drawArrays();
|
andrew@0
|
580
|
andrew@0
|
581 // ofSetColor(200,200,0);
|
andrew@0
|
582 // bayesStruct.prior.drawConstrainedVector(0, bayesStruct.prior.arraySize, 400, 800);
|
andrew@0
|
583
|
andrew@0
|
584 //need to draw arrays within correct timescope
|
andrew@9
|
585 if (drawPhaseMode)
|
andrew@0
|
586 bayesStruct.drawArraysRelativeToTimeframe(timeOffsetForScreen, timeOffsetForScreen + getEventTimeMillis(ticksPerScreen));
|
andrew@0
|
587
|
andrew@1
|
588 if (drawTempoMode)
|
andrew@1
|
589 bayesStruct.drawTempoArrays();
|
andrew@0
|
590
|
andrew@1
|
591
|
andrew@1
|
592 ofSetColor(0, 0, 0);
|
andrew@0
|
593 ofDrawBitmapString(matchString, 20, ofGetHeight() - 20);
|
andrew@0
|
594
|
andrew@1
|
595 double confidence = bayesStruct.posterior.getValueAtMillis(mouseX);
|
andrew@2
|
596 /*
|
andrew@2
|
597 string mouseString = "mouseX "+ofToString(confidence, 3)+" .";
|
andrew@1
|
598 ofDrawBitmapString(mouseString, 20 , ofGetHeight() - 40);
|
andrew@2
|
599 */
|
andrew@2
|
600 string mouseString = "updateCounter "+ofToString(bayesStruct.updateCounter);
|
andrew@2
|
601 ofDrawBitmapString(mouseString, 20 , ofGetHeight() - 40);
|
andrew@2
|
602
|
andrew@2
|
603 string infostring = "speed "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate), 3);
|
andrew@2
|
604 ofDrawBitmapString(infostring, 20 , ofGetHeight() - 60);
|
andrew@0
|
605 }
|
andrew@0
|
606
|
andrew@0
|
607 int midiEventHolder::getLocationFromTicks(double tickPosition){
|
andrew@0
|
608 return (int)((float)(tickPosition - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen);
|
andrew@0
|
609 }
|
andrew@0
|
610
|
andrew@0
|
611 int midiEventHolder::getLocationFromMillis(double millisPosition){
|
andrew@0
|
612 //(getEventTimeTicks(windowStartTime+matchWindowWidth) - numberOfScreensIn*ticksPerScreen)*(*screenWidth) / (double)ticksPerScreen
|
andrew@0
|
613 return (millisPosition - timeOffsetForScreen)*(*screenWidth)/getEventTimeMillis(ticksPerScreen);
|
andrew@0
|
614 }
|
andrew@0
|
615
|
andrew@0
|
616
|
andrew@0
|
617 void midiEventHolder::exampleCrossUpdate(){
|
andrew@0
|
618
|
andrew@0
|
619 bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, 200);
|
andrew@0
|
620
|
andrew@0
|
621 }
|
andrew@0
|
622
|
andrew@0
|
623
|
andrew@0
|
624 void midiEventHolder::setStartPlayingTimes(){
|
andrew@0
|
625 lastPeriodUpdateTime = ofGetElapsedTimeMillis();
|
andrew@0
|
626 startTime = lastPeriodUpdateTime;
|
andrew@0
|
627
|
andrew@2
|
628 /*
|
andrew@2
|
629 bayesStruct.lastEventTime = 0;//ofGetElapsedTimeMillis();
|
andrew@2
|
630 bayesStruct.bestEstimate = 0;
|
andrew@0
|
631 bayesStruct.resetArrays();
|
andrew@2
|
632 bayesStruct.lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
|
andrew@2
|
633 */
|
andrew@2
|
634 bayesStruct.setStartPlaying();
|
andrew@0
|
635 matchString = "";
|
andrew@0
|
636 }
|
andrew@0
|
637
|
andrew@0
|
638
|
andrew@0
|
639 void midiEventHolder::printMatchMatrix(){
|
andrew@0
|
640 printf("match matrix:\n");
|
andrew@0
|
641 for (int i = 0;i < matchMatrix.size();i++){
|
andrew@0
|
642 for (int k = 0;k < matchMatrix[i].size();k++){
|
andrew@0
|
643 printf("%i , ", matchMatrix[i][k]);
|
andrew@0
|
644 }
|
andrew@0
|
645 printf("\n");
|
andrew@0
|
646 }
|
andrew@0
|
647
|
andrew@0
|
648
|
andrew@0
|
649 } |