summaryrefslogtreecommitdiff
path: root/src/testApp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/testApp.cpp')
-rwxr-xr-xsrc/testApp.cpp234
1 files changed, 234 insertions, 0 deletions
diff --git a/src/testApp.cpp b/src/testApp.cpp
new file mode 100755
index 0000000..28d3ef2
--- /dev/null
+++ b/src/testApp.cpp
@@ -0,0 +1,234 @@
+#include "testApp.h"
+
+//--------------------------------------------------------------
+void testApp::setup(){
+ loadSettings("settings.xml");
+ //sender.setup(host.c_str(),port);
+ sender.setup("192.168.15.6",57117);
+
+ bInvert=true;
+ threshold = 80;
+ //
+
+ gw=640;
+ gh=480;
+
+ vidGrabber.setVerbose(true);
+ vidGrabber.initGrabber(gw,gh); //base grab size
+
+ colorImg.allocate(gw,gh);
+ grayImage.allocate(gw,gh);
+ grayBg.allocate(gw,gh);
+ grayDiff.allocate(gw,gh);
+
+ bLearnBakground = true;
+
+
+ bFlip=true;
+
+ if (boundaries.size()==0) {
+ boundaries.push_back(boundary());
+ }
+ selectedBoundary=0;
+
+}
+
+//--------------------------------------------------------------
+void testApp::update(){
+ vidGrabber.grabFrame();
+ bool bNewFrame = vidGrabber.isFrameNew();
+ if (bNewFrame){
+ colorImg.setFromPixels(vidGrabber.getPixels(), gw,gh);
+ if (bFlip) colorImg.mirror(false,true);
+ grayImage = colorImg;
+ if (bInvert) grayImage.invert();
+ if (bLearnBakground == true){
+ grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg (operator overloading)
+ bLearnBakground = false;
+ }
+ grayDiff.absDiff(grayBg, grayImage);
+ grayDiff.threshold(threshold);
+ contourFinder.findContours(grayDiff, 20, (gw*gh)/3, 10, true); // find holes
+ }
+
+ //generate midi
+
+ for (int i = 0; i < contourFinder.nBlobs; i++){
+ for (int j=0;j<boundaries.size();j++) {
+ if (boundaries[j].contains(contourFinder.blobs[i].centroid)) {
+ sendNote(boundaries[j].note);
+ //printf("sending %i\n",boundaries[j].note);
+ }
+ }
+ }
+
+}
+
+//--------------------------------------------------------------
+void testApp::draw(){
+ ofSetColor(255,255,255);
+ colorImg.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
+
+ for (int i = 0; i < contourFinder.nBlobs; i++){
+ contourFinder.blobs[i].draw(0,0); //(ofGetHeight()-gh)/2);
+ }
+
+ for (int i=0;i<boundaries.size();i++) {
+ if (selectedBoundary==i) ofSetColor(255,0,0);
+ else ofSetColor(180,0,0);
+ boundaries[i].draw();
+ }
+}
+
+//--------------------------------------------------------------
+void testApp::keyPressed(int key){
+
+ switch (key){
+ case ' ':
+ bLearnBakground = true;
+ break;
+ case '+':
+ threshold ++;
+ if (threshold > 255) threshold = 255;
+ break;
+ case '-':
+ threshold --;
+ if (threshold < 0) threshold = 0;
+ break;
+ case 'i':
+ bInvert=!bInvert;
+ break;
+ case 'f':
+ bFlip=!bFlip;
+ break;
+ case 's':
+ saveSettings("settings.xml");
+ break;
+ case 'a':
+ boundaries.push_back(boundary());
+ selectedBoundary=boundaries.size()-1;
+ break;
+ case 'z':
+ boundaries[selectedBoundary].undo();
+ break;
+ case 'q':
+ boundaries.erase(boundaries.begin()+selectedBoundary);
+ if (selectedBoundary==boundaries.size()) selectedBoundary=0;
+ break;
+ case OF_KEY_PAGE_UP:
+ selectedBoundary--;
+ if (selectedBoundary<0) selectedBoundary=boundaries.size()-1;
+ break;
+ case OF_KEY_PAGE_DOWN:
+ selectedBoundary=(selectedBoundary+1)%boundaries.size();
+ break;
+ case OF_KEY_LEFT:
+ boundaries[selectedBoundary].note--;
+ break;
+ case OF_KEY_RIGHT:
+ boundaries[selectedBoundary].note++;
+ break;
+
+ }
+}
+
+//--------------------------------------------------------------
+void testApp::sendNote(int note){
+ ofxOscMessage m;
+ m.setAddress("/osc/midi/out/noteOn");
+ m.addIntArg(channel);
+ m.addIntArg(note);
+ m.addIntArg(127);
+ m.addIntArg(1);
+ sender.sendMessage(m);
+}
+
+//--------------------------------------------------------------
+void testApp::keyReleased(int key){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseMoved(int x, int y ){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseDragged(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void testApp::mousePressed(int x, int y, int button){
+ boundaries[selectedBoundary].add(ofPoint(x,y,0));
+
+}
+
+//--------------------------------------------------------------
+void testApp::mouseReleased(int x, int y, int button){
+
+}
+
+//--------------------------------------------------------------
+void testApp::windowResized(int w, int h){
+
+}
+
+//--------------------------------------------------------------
+void testApp::gotMessage(ofMessage msg){
+
+}
+
+//--------------------------------------------------------------
+void testApp::dragEvent(ofDragInfo dragInfo){
+
+}
+void testApp::loadSettings(string filename){
+ if( !XML.loadFile(filename) ){
+ printf("unable to load %s check data/ folder\n",filename.c_str());
+ }else{
+ host=XML.getAttribute("figgis","host","127.0.0.1",0);
+ port=XML.getAttribute("figgis","port",5151,0);
+ channel=XML.getAttribute("figgis","channel",1,0);
+ bInvert=(XML.getAttribute("figgis","invert",0,0)==1);
+ threshold = XML.getAttribute("figgis","threshold",80,0);
+ if(XML.pushTag("boundaries")) {
+ for (int i=0;i<XML.getNumTags("boundary");i++){
+ boundaries.push_back(boundary(XML.getAttribute("boundary","note",40,i)));
+ selectedBoundary=boundaries.size()-1;
+ XML.pushTag("boundary",i);
+ for (int j=0;j<XML.getNumTags("point");j++){
+ boundaries[selectedBoundary].add(ofPoint(XML.getAttribute("point","x",0.0,j),XML.getAttribute("point","y",0.0,j),0.0));
+ }
+ XML.popTag();
+ }
+ XML.popTag();
+ }
+ printf("loaded %s\n",filename.c_str());
+ }
+}
+void testApp::saveSettings(string filename){
+ XML.setAttribute("figgis","invert",bInvert,0);
+ XML.setAttribute("figgis","threshold",threshold,0);
+ if (XML.tagExists("boundaries")) XML.removeTag("boundaries");
+ XML.addTag("boundaries");
+ if(XML.pushTag("boundaries")) {
+ for (int i=0;i<boundaries.size();i++){
+ XML.addTag("boundary");
+ XML.setAttribute("boundary","note",boundaries[i].note,i);
+ if(XML.pushTag("boundary",i)) {
+ for (int j=0;j<boundaries[i].points.size();j++){
+ XML.addTag("point");
+ XML.setAttribute("point","x",boundaries[i].points[j].x,j);
+ XML.setAttribute("point","y",boundaries[i].points[j].y,j);
+ }
+ XML.popTag();
+ }
+ }
+ XML.popTag();
+ }
+ XML.saveFile(filename);
+ printf("saved %s\n",filename.c_str());
+}
+void testApp::exit(){
+} \ No newline at end of file