summaryrefslogtreecommitdiff
path: root/src/testApp.cpp
blob: 28d3ef24b20ec5ffb186541cd8bf81d64a547a39 (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
#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(){
}