summaryrefslogtreecommitdiff
path: root/src/testApp.cpp
blob: 1ca45e4d36775c202e620de9a5f0bcf7600e68ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
	loadSettings("settings.xml");
	//sender.setup(host.c_str(),port);
	sender.setup("169.254.113.20",57117);

	bInvert=true;
	threshold = 80;
	//

	gw=720;
	gh=576;

	vidGrabber.setVerbose(true);
	bool bGrab=false;
	//vidGrabber.initGrabber(gw,gh); //base grab size

	colorImg.allocate(gw,gh);
	grayImage.allocate(gw,gh);
	grayBg.allocate(gw,gh);
	grayDiff.allocate(gw,gh);

	mode=COLOUR;

	bLearnBakground = true;


	bFlip=true;

	if (boundaries.size()==0) {
		boundaries.push_back(boundary());
	}
	selectedBoundary=0;

//    ofAddListener(ofEvents().fileDragEvent, this, &testApp::fileDragEvent);
	/*
	guiWin=new guiWindow();
	//gui window stuff
	ofxFenster* win2=ofxFensterManager::get()->createFenster(0, 0,  200, 400, OF_WINDOW);

	//ofAddListener(win2->events.windowResized, this, &testApp::windowEvent);

	win2->setWindowTitle("config");
	win2->addListener(guiWin);
	guiWin->setup();
	guiWin->setParent(this);

*/
}

//--------------------------------------------------------------
void testApp::update(){
	if (bGrab) { 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);
	switch(mode){
		case COLOUR:
			colorImg.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
			break;
		case GREY:
			grayImage.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
			break;
		case DIFF:
			grayDiff.draw(0,0,gw,gh); //(ofGetHeight()-gh)/2,gw,ofGetHeight()+((gh-ofGetHeight())/2));
			break;
	}

	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){

    //make in fenster, drag/drop support
    //drag sound file onto a sound area, its name becomes name of area
    //keys to play/stop (test)
    //overall sound level control
    //fine tune image detection

	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;
		*/
		case '1':
		case '2':
		case '3':
			mode=key-48;
			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);
	//printf("sent %i\n",note);
}

//--------------------------------------------------------------
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, ofxFenster* win){
    dragEvent(dragInfo);
}
void testApp::dragEvent(ofDragInfo dragInfo) {
    printf("got draginfo: %f,%f,%f\n",dragInfo.position.x,dragInfo.position.y,dragInfo.position.z);
    int sta=dragInfo.files[0].find_last_of("\\/")+1;
    int len=(dragInfo.files[0].find_last_of(".")+4)-sta;
	string filename=dragInfo.files[0].substr(sta,len);
	printf("loading %s\n",filename.c_str());
	for (int i=0;i<boundaries.size();i++) {
		boundaries[i].checkfile(dragInfo.position,filename);
	}
}
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","filename","",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","filename",boundaries[i].filename,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(){
}