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