Mercurial > hg > midi-score-follower
diff 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 |
line wrap: on
line diff
--- a/hackday/testApp.cpp Sat Dec 03 17:19:43 2011 +0000 +++ b/hackday/testApp.cpp Sat Dec 03 21:09:13 2011 +0000 @@ -13,7 +13,7 @@ midiIn.listPorts(); midiIn.openPort(2); - transpose = 12; + transpose = 24; noteInStream.transposeVal = &transpose; noteInStream.startTime = &midiEvents.startTime;//point start time of note in stream to the same time in MIDI events @@ -35,6 +35,8 @@ receiver.setup( PORT ); + + sender.setup( HOST, SEND_PORT ); screenWidth = ofGetWidth(); screenHeight = ofGetHeight(); @@ -42,13 +44,16 @@ midiEvents.screenHeight = &screenHeight; midiEvents.drawTempoMode = false; ofSetFrameRate(30); + + midiEvents.ticksPerScreen += 4000; + lastScoreIndexSent = 0; + } //-------------------------------------------------------------- void testApp::update(){ if (playing){ - midiEvents.updatePlayPosition(); - // midiEvents.bayesStruct.updateBestEstimate(); + midiEvents.updatePlayPosition();//this fn calls midiEvents.bayesStruct.updateBestEstimate(); } // drawer.tickLocation+=20; @@ -122,9 +127,72 @@ } }//end while osc + + checkNewScoreNote(); } + +void testApp::checkNewScoreNote(){ + if (lastScoreIndexSent != midiEvents.bestMatchIndex){ + //then we send out new note + sendNoteToMuseScore(); + lastScoreIndexSent = midiEvents.bestMatchIndex; + findMeasure(); + } +} + +void testApp::findMeasure(){ + int ticks = midiEvents.recordedNoteOnMatrix[midiEvents.bestMatchIndex][0]; + int tmpMeasure = lastMeasureSent; + + while (lastMeasureSent > 0 && midiEvents.measureVector[lastMeasureSent] > ticks) { + lastMeasureSent--; + } + + while (lastMeasureSent < midiEvents.measureVector.size() && midiEvents.measureVector[lastMeasureSent] < ticks) { + lastMeasureSent++; + } + if (lastMeasureSent != tmpMeasure){ + sendMeasureToMuseScore(); + } + + +} + +void testApp::sendNoteToMuseScore(){ + int ticks = midiEvents.recordedNoteOnMatrix[midiEvents.bestMatchIndex][0]; + int pitch = midiEvents.recordedNoteOnMatrix[midiEvents.bestMatchIndex][1]; + printf("sending to muse score %i, %i \n", ticks, pitch); + + ofxOscMessage m; + m.setAddress( "/plugin" ); + m.addStringArg( "coloronenote.js" ); + m.addStringArg("mytick"); + m.addIntArg( ticks ); + m.addStringArg("mypitch"); + m.addIntArg( pitch); + sender.sendMessage( m ); + + // /plugin coloronenote.js mytick 100 mypitch 56; +} + + + +void testApp::sendMeasureToMuseScore(){ + + printf("sending measure to muse score %i \n", lastMeasureSent); + + ofxOscMessage m; + m.setAddress( "/select-measure" ); + m.addIntArg(lastMeasureSent); + sender.sendMessage( m ); + + // /select-measure 6 + // /plugin coloronenote.js mytick 100 mypitch 56; +} + + void testApp::newMessage(ofxMidiEventArgs &args){ int pitch; @@ -148,9 +216,10 @@ v.push_back(tickTime); v.push_back(pitch); v.push_back(args.byteTwo); + v.push_back(200);//tmp time til note off happens noteInStream.midiInputEvents.push_back(v); noteInStream.midiInputTimes.push_back(timeNow - firstNoteTime); - printf("NOTE %i at time %f at tick time %i\n", pitch, (timeNow - firstNoteTime), tickTime); + //printf("NOTE %i at time %f at tick time %i\n", pitch, (timeNow - firstNoteTime), tickTime); } // cout << "MIDI message [port: " << args.port << ", channel: " << args.channel << ", status: " << args.status << ", byteOne: " << pitch << ", byteTwo: " << args.byteTwo << ", timestamp: " << args.timestamp << "]" << endl; @@ -161,6 +230,13 @@ midiEvents.drawFile(); + string info = "Measure "; + info += ofToString(lastMeasureSent); + info += " Last note "; + info += ofToString(lastScoreIndexSent); + + ofSetHexColor(0x000000); + ofDrawBitmapString(info, 20, 20); midiEvents.drawMidiFile(noteInStream.midiInputEvents); } @@ -294,6 +370,9 @@ void testApp::stopPlaying(){ playing = false; liveInputPlaying = false; + lastScoreIndexSent = 0; + midiEvents.bestMatchIndex = 0; + sendNoteToMuseScore(); } bool testApp::getFilenameFromDialogBox(string* fileNameToSave){