Mercurial > hg > midi-score-follower
comparison hackday/testApp.cpp @ 25:2a025ea7c793
hackday work to get live midi input, follow the notes, output measure, read measure in with midi file
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Sat, 03 Dec 2011 21:09:13 +0000 |
parents | 5a11b19906c7 |
children | 179365726f07 |
comparison
equal
deleted
inserted
replaced
24:5a11b19906c7 | 25:2a025ea7c793 |
---|---|
11 //ofBackground(255,255,255); | 11 //ofBackground(255,255,255); |
12 | 12 |
13 midiIn.listPorts(); | 13 midiIn.listPorts(); |
14 midiIn.openPort(2); | 14 midiIn.openPort(2); |
15 | 15 |
16 transpose = 12; | 16 transpose = 24; |
17 noteInStream.transposeVal = &transpose; | 17 noteInStream.transposeVal = &transpose; |
18 | 18 |
19 noteInStream.startTime = &midiEvents.startTime;//point start time of note in stream to the same time in MIDI events | 19 noteInStream.startTime = &midiEvents.startTime;//point start time of note in stream to the same time in MIDI events |
20 | 20 |
21 noteInStream.factor = &midiEvents.ticksFactor; | 21 noteInStream.factor = &midiEvents.ticksFactor; |
33 | 33 |
34 playing = false; | 34 playing = false; |
35 | 35 |
36 | 36 |
37 receiver.setup( PORT ); | 37 receiver.setup( PORT ); |
38 | |
39 sender.setup( HOST, SEND_PORT ); | |
38 | 40 |
39 screenWidth = ofGetWidth(); | 41 screenWidth = ofGetWidth(); |
40 screenHeight = ofGetHeight(); | 42 screenHeight = ofGetHeight(); |
41 midiEvents.screenWidth = &screenWidth; | 43 midiEvents.screenWidth = &screenWidth; |
42 midiEvents.screenHeight = &screenHeight; | 44 midiEvents.screenHeight = &screenHeight; |
43 midiEvents.drawTempoMode = false; | 45 midiEvents.drawTempoMode = false; |
44 ofSetFrameRate(30); | 46 ofSetFrameRate(30); |
47 | |
48 midiEvents.ticksPerScreen += 4000; | |
49 lastScoreIndexSent = 0; | |
50 | |
45 } | 51 } |
46 | 52 |
47 //-------------------------------------------------------------- | 53 //-------------------------------------------------------------- |
48 void testApp::update(){ | 54 void testApp::update(){ |
49 if (playing){ | 55 if (playing){ |
50 midiEvents.updatePlayPosition(); | 56 midiEvents.updatePlayPosition();//this fn calls midiEvents.bayesStruct.updateBestEstimate(); |
51 // midiEvents.bayesStruct.updateBestEstimate(); | |
52 } | 57 } |
53 // drawer.tickLocation+=20; | 58 // drawer.tickLocation+=20; |
54 | 59 |
55 // check for waiting messages | 60 // check for waiting messages |
56 while( receiver.hasWaitingMessages() ) | 61 while( receiver.hasWaitingMessages() ) |
120 midiEvents.minimumMatchSpeed = minSpeed; | 125 midiEvents.minimumMatchSpeed = minSpeed; |
121 } | 126 } |
122 } | 127 } |
123 | 128 |
124 }//end while osc | 129 }//end while osc |
125 | 130 |
126 } | 131 checkNewScoreNote(); |
132 | |
133 } | |
134 | |
135 | |
136 void testApp::checkNewScoreNote(){ | |
137 if (lastScoreIndexSent != midiEvents.bestMatchIndex){ | |
138 //then we send out new note | |
139 sendNoteToMuseScore(); | |
140 lastScoreIndexSent = midiEvents.bestMatchIndex; | |
141 findMeasure(); | |
142 } | |
143 } | |
144 | |
145 void testApp::findMeasure(){ | |
146 int ticks = midiEvents.recordedNoteOnMatrix[midiEvents.bestMatchIndex][0]; | |
147 int tmpMeasure = lastMeasureSent; | |
148 | |
149 while (lastMeasureSent > 0 && midiEvents.measureVector[lastMeasureSent] > ticks) { | |
150 lastMeasureSent--; | |
151 } | |
152 | |
153 while (lastMeasureSent < midiEvents.measureVector.size() && midiEvents.measureVector[lastMeasureSent] < ticks) { | |
154 lastMeasureSent++; | |
155 } | |
156 if (lastMeasureSent != tmpMeasure){ | |
157 sendMeasureToMuseScore(); | |
158 } | |
159 | |
160 | |
161 } | |
162 | |
163 void testApp::sendNoteToMuseScore(){ | |
164 int ticks = midiEvents.recordedNoteOnMatrix[midiEvents.bestMatchIndex][0]; | |
165 int pitch = midiEvents.recordedNoteOnMatrix[midiEvents.bestMatchIndex][1]; | |
166 printf("sending to muse score %i, %i \n", ticks, pitch); | |
167 | |
168 ofxOscMessage m; | |
169 m.setAddress( "/plugin" ); | |
170 m.addStringArg( "coloronenote.js" ); | |
171 m.addStringArg("mytick"); | |
172 m.addIntArg( ticks ); | |
173 m.addStringArg("mypitch"); | |
174 m.addIntArg( pitch); | |
175 sender.sendMessage( m ); | |
176 | |
177 // /plugin coloronenote.js mytick 100 mypitch 56; | |
178 } | |
179 | |
180 | |
181 | |
182 void testApp::sendMeasureToMuseScore(){ | |
183 | |
184 printf("sending measure to muse score %i \n", lastMeasureSent); | |
185 | |
186 ofxOscMessage m; | |
187 m.setAddress( "/select-measure" ); | |
188 m.addIntArg(lastMeasureSent); | |
189 sender.sendMessage( m ); | |
190 | |
191 // /select-measure 6 | |
192 // /plugin coloronenote.js mytick 100 mypitch 56; | |
193 } | |
194 | |
127 | 195 |
128 void testApp::newMessage(ofxMidiEventArgs &args){ | 196 void testApp::newMessage(ofxMidiEventArgs &args){ |
129 | 197 |
130 int pitch; | 198 int pitch; |
131 if (noteInStream.noteInReceived(args)){ | 199 if (noteInStream.noteInReceived(args)){ |
146 int tickTime = midiEvents.getEventTimeTicks(timeNow-firstNoteTime); | 214 int tickTime = midiEvents.getEventTimeTicks(timeNow-firstNoteTime); |
147 IntVector v; | 215 IntVector v; |
148 v.push_back(tickTime); | 216 v.push_back(tickTime); |
149 v.push_back(pitch); | 217 v.push_back(pitch); |
150 v.push_back(args.byteTwo); | 218 v.push_back(args.byteTwo); |
219 v.push_back(200);//tmp time til note off happens | |
151 noteInStream.midiInputEvents.push_back(v); | 220 noteInStream.midiInputEvents.push_back(v); |
152 noteInStream.midiInputTimes.push_back(timeNow - firstNoteTime); | 221 noteInStream.midiInputTimes.push_back(timeNow - firstNoteTime); |
153 printf("NOTE %i at time %f at tick time %i\n", pitch, (timeNow - firstNoteTime), tickTime); | 222 //printf("NOTE %i at time %f at tick time %i\n", pitch, (timeNow - firstNoteTime), tickTime); |
154 } | 223 } |
155 | 224 |
156 // cout << "MIDI message [port: " << args.port << ", channel: " << args.channel << ", status: " << args.status << ", byteOne: " << pitch << ", byteTwo: " << args.byteTwo << ", timestamp: " << args.timestamp << "]" << endl; | 225 // cout << "MIDI message [port: " << args.port << ", channel: " << args.channel << ", status: " << args.status << ", byteOne: " << pitch << ", byteTwo: " << args.byteTwo << ", timestamp: " << args.timestamp << "]" << endl; |
157 } | 226 } |
158 | 227 |
159 //-------------------------------------------------------------- | 228 //-------------------------------------------------------------- |
160 void testApp::draw(){ | 229 void testApp::draw(){ |
161 | 230 |
162 midiEvents.drawFile(); | 231 midiEvents.drawFile(); |
163 | 232 |
233 string info = "Measure "; | |
234 info += ofToString(lastMeasureSent); | |
235 info += " Last note "; | |
236 info += ofToString(lastScoreIndexSent); | |
237 | |
238 ofSetHexColor(0x000000); | |
239 ofDrawBitmapString(info, 20, 20); | |
164 midiEvents.drawMidiFile(noteInStream.midiInputEvents); | 240 midiEvents.drawMidiFile(noteInStream.midiInputEvents); |
165 | 241 |
166 } | 242 } |
167 | 243 |
168 //-------------------------------------------------------------- | 244 //-------------------------------------------------------------- |
292 } | 368 } |
293 | 369 |
294 void testApp::stopPlaying(){ | 370 void testApp::stopPlaying(){ |
295 playing = false; | 371 playing = false; |
296 liveInputPlaying = false; | 372 liveInputPlaying = false; |
373 lastScoreIndexSent = 0; | |
374 midiEvents.bestMatchIndex = 0; | |
375 sendNoteToMuseScore(); | |
297 } | 376 } |
298 | 377 |
299 bool testApp::getFilenameFromDialogBox(string* fileNameToSave){ | 378 bool testApp::getFilenameFromDialogBox(string* fileNameToSave){ |
300 //this uses a pointer structure within the loader and returns true if the dialogue box was used successfully | 379 //this uses a pointer structure within the loader and returns true if the dialogue box was used successfully |
301 // first, create a string that will hold the URL | 380 // first, create a string that will hold the URL |