From e2e45966d7a3ca7673bdbaadef5b5e8a38b0ff78 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Thu, 11 Oct 2012 19:43:47 +0100 Subject: initial commit --- src/boundary.cpp | 79 +++++++++++++++++++ src/boundary.h | 28 +++++++ src/main.cpp | 16 ++++ src/testApp.cpp | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/testApp.h | 61 +++++++++++++++ 5 files changed, 418 insertions(+) create mode 100755 src/boundary.cpp create mode 100755 src/boundary.h create mode 100755 src/main.cpp create mode 100755 src/testApp.cpp create mode 100755 src/testApp.h (limited to 'src') diff --git a/src/boundary.cpp b/src/boundary.cpp new file mode 100755 index 0000000..c30f676 --- /dev/null +++ b/src/boundary.cpp @@ -0,0 +1,79 @@ +#include "boundary.h" + +boundary::boundary() +{ + note=40; //middle C +} + +boundary::boundary(int _note) +{ + note=_note; +} + +boundary::~boundary() +{ +} + + +void boundary::draw(){ + if (points.size()>1) { + for (int i=0;i0) { + points.erase(points.end()-1); + getCentroid(); + } +} + +void boundary::getCentroid(){ + float x=0; + float y=0; + for (int i=0;i min(p1.y,p2.y)) { + if (p.y <= max(p1.y,p2.y)) { + if (p.x <= max(p1.x,p2.x)) { + if (p1.y != p2.y) { + xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x; + if (p1.x == p2.x || p.x <= xinters) + counter++; + } + } + } + } + p1 = p2; + } + + if (counter % 2 == 0) + return false; + else + return true; +} diff --git a/src/boundary.h b/src/boundary.h new file mode 100755 index 0000000..a4ee28b --- /dev/null +++ b/src/boundary.h @@ -0,0 +1,28 @@ +#ifndef BOUNDARY_H +#define BOUNDARY_H + +#include "ofMain.h" + + + +class boundary +{ + public: + boundary(); + boundary(int _note); + virtual ~boundary(); + bool contains(ofPoint p); + void draw(); + void add(ofPoint p); + void undo(); + int note; + void getCentroid(); + vector points; + ofPoint centroid; + +}; + + + + +#endif // BOUNDARY_H diff --git a/src/main.cpp b/src/main.cpp new file mode 100755 index 0000000..e502a4d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,16 @@ +#include "ofMain.h" +#include "testApp.h" +#include "ofAppGlutWindow.h" + +//======================================================================== +int main( ){ + + ofAppGlutWindow window; + ofSetupOpenGL(&window, 640,480, OF_WINDOW); // <-------- setup the GL context + + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + ofRunApp( new testApp()); + +} 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 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 boundaries; + int selectedBoundary; + +}; -- cgit v1.2.3