comparison src/melodyTriangle.cpp @ 6:d879a30556f8

re-intital commit
author Henrik Ekeus <hekeus@eecs.qmul.ac.uk>
date Wed, 25 Jan 2012 16:30:07 +0000
parents
children 38f63c4300d7
comparison
equal deleted inserted replaced
5:ae8a02700c9b 6:d879a30556f8
1 #include "melodyTriangle.h"
2 #include <GLUT/GLUT.h>
3
4 /*
5 /birth id
6 /death id
7 /start id
8 /stop id
9 /track id x y left right top bottom area
10 /tempo
11
12
13 */
14 melodyTriangle::melodyTriangle(const char *host, int port, int numVoices, bool enableKeys,int voiceIdOffset){
15 printf("in constructor: %s %i %i %i\n",host,port,numVoices,enableKeys);
16 this->numVoices=numVoices;
17 this->enableKeys=enableKeys;
18 this->voiceIdOffset=voiceIdOffset;
19 //voices=*Voice[numVoices];
20 sender.setup( host,port );
21 }
22
23 //--------------------------------------------------------------
24 void melodyTriangle::setup(){
25 //voices = new Voice[NUMVOICES];
26
27 ofSetCircleResolution(100);
28 ofBackground(0,0,0);
29 ofSetWindowTitle("Melody Triangle");
30 triangleHeight=ofGetHeight()*0.75;
31 ofSetFrameRate(40); // if vertical sync is off, we can go a bit fast... this caps the framerate at 60fps.
32 ofEnableSmoothing();
33 x1=ofGetWidth()/2;
34 y1=(ofGetHeight()-triangleHeight)/2;
35 x2=ofGetWidth()/2-triangleHeight/sqrt(3);
36 y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2;
37 x3=ofGetWidth()/2+triangleHeight/sqrt(3);
38 y3=y2;
39
40 ofxOscMessage m;
41 m.setAddress( "/calibrate" );
42 m.addIntArg( x1 );
43 m.addIntArg( y1 );
44 m.addIntArg( x2 );
45 m.addIntArg( y2 );
46 m.addIntArg( x3 );
47 m.addIntArg( y3 );
48 sender.sendMessage( m );
49 printf("sent /calibrate %i %i %i %i %i %i\n",x1,y1,x2,y2,x3,y3);
50
51 for (int i=0;i<numVoices;i++){
52
53 voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30);
54 }
55 voiceGrabbed=-1;
56 }
57
58 //--------------------------------------------------------------
59 void melodyTriangle::update(){
60 }
61
62 bool melodyTriangle::isInTriangle(int x, int y){
63 if (x>x2 && x<x3 && y>y1 && y<y2){
64 //printf("in bounding box\n");
65 float dx=abs(x-x1);
66 float dy=abs(y-y1);
67 //printf("tan(30)- dx/dy: %f\n",tan(30*PI/180)-dx/dy);
68
69 if (dx/dy < tan(30*PI/180)){
70
71 //printf("in triangle \n");
72 return true;
73 }else {
74 //printf("not in triangle \n");
75 return false;
76 }
77
78 }else{
79 //printf("not in bounding box \n");
80 return false;
81 }
82 }
83
84 void melodyTriangle::sendStatus(Voice v){
85
86 ofxOscMessage m;
87 ///track id x y left right top bottom area
88 m.setAddress( "/track2d" );
89 m.addIntArg( v.id );
90 m.addIntArg( v.posx );
91 m.addIntArg( v.posy );
92 sender.sendMessage( m );
93 printf("sent - /track2d %i %i %i\n",v.id,v.posx,v.posy);
94
95 }
96
97 //--------------------------------------------------------------
98 void melodyTriangle::draw(){
99
100
101 //let's draw our triangle
102 ofSetColor(0,0,255);
103
104 ofFill();
105 ofTriangle(x1, y1, x2, y2, x3, y3);
106 bool sendStart=false;
107 if (voiceGrabbed!=-1){
108 if (mouseX!=(*voices[voiceGrabbed]).posx || mouseY!=(*voices[voiceGrabbed]).posy){
109 //(*voices[voiceGrabbed]).posx=mouseX;
110 //(*voices[voiceGrabbed]).posy=mouseY;
111 if ((*voices[voiceGrabbed]).inTriangle && !isInTriangle(mouseX,mouseY)){
112 ///death id
113
114 if (ofDist((*voices[voiceGrabbed]).posx, (*voices[voiceGrabbed]).posy, mouseX, mouseY) > (*voices[voiceGrabbed]).radius*2){
115
116 ofxOscMessage m;
117 ///track id x y left right top bottom area
118 m.setAddress( "/death" );
119 m.addIntArg( (*voices[voiceGrabbed]).id );
120 sender.sendMessage( m );
121
122 printf("sent /death %i \n",(*voices[voiceGrabbed]).id);
123 (*voices[voiceGrabbed]).posx=mouseX;
124 (*voices[voiceGrabbed]).posy=mouseY;
125
126 } else {
127 //printf("e");
128 //On Edge
129 }
130
131 }else{
132 (*voices[voiceGrabbed]).posx=mouseX;
133 (*voices[voiceGrabbed]).posy=mouseY;
134 //(*voices[voiceGrabbed]).posx=(*voices[voiceGrabbed]).posx*0.9+mouseX*0.1;
135 //(*voices[voiceGrabbed]).posy=(*voices[voiceGrabbed]).posy*0.9+mouseY*0.1;
136
137
138 }
139 if (!(*voices[voiceGrabbed]).inTriangle && isInTriangle(mouseX,mouseY)){
140 //birth id
141
142 ofxOscMessage m;
143 ///track id x y left right top bottom area
144 m.setAddress( "/birth" );
145 m.addIntArg( (*voices[voiceGrabbed]).id );
146 sender.sendMessage( m );
147
148 printf("sent /birth %i \n",(*voices[voiceGrabbed]).id);
149 sendStart=true;
150 }
151
152
153 //(*voices[voiceGrabbed]).inTriangle = isInTriangle(mouseX,mouseY);
154
155 (*voices[voiceGrabbed]).inTriangle = isInTriangle((*voices[voiceGrabbed]).posx,(*voices[voiceGrabbed]).posy);
156
157 if ((*voices[voiceGrabbed]).inTriangle){
158 sendStatus(*voices[voiceGrabbed]);
159 if (sendStart){
160 if ((*voices[voiceGrabbed]).isActive){
161 ofxOscMessage m;
162 ///track id x y left right top bottom area
163 m.setAddress( "/start" );
164 m.addIntArg( (*voices[voiceGrabbed]).id );
165 sender.sendMessage( m );
166 printf("sent /start %i \n",(*voices[voiceGrabbed]).id);
167 }
168 }
169 }
170
171 }
172 };
173 for (int i=0; i<numVoices; i++){
174 (*voices[i]).draw();
175 }
176
177
178 }
179
180
181 //--------------------------------------------------------------
182 void melodyTriangle::keyPressed (int key){
183 //printf("key %i",key);
184 if (enableKeys){
185 if (key == 'a'){
186 for (int i=0; i<numVoices; i++){
187 if ((*voices[i]).isInVoice(mouseX,mouseY)){
188
189 (*voices[i]).isActive=!(*voices[i]).isActive;
190 ///start id
191 ///stop id
192 if ((*voices[i]).isActive){
193 ofxOscMessage m;
194 ///track id x y left right top bottom area
195 m.setAddress( "/start" );
196 m.addIntArg( (*voices[i]).id );
197 sender.sendMessage( m );
198 printf("sent /start %i \n",(*voices[i]).id);
199 }else {
200 ofxOscMessage m;
201 ///track id x y left right top bottom area
202 m.setAddress( "/stop" );
203 m.addIntArg( (*voices[i]).id );
204 sender.sendMessage( m );
205 printf("sent /stop %i \n",(*voices[i]).id);
206
207 }
208
209 }
210 }
211 }
212 if (key == OF_KEY_UP){
213 for (int i=0; i<numVoices; i++){
214 if ((*voices[i]).isInVoice(mouseX,mouseY)){
215
216 ofxOscMessage m;
217 ///track id x y left right top bottom area
218 m.setAddress( "/period" );
219 m.addIntArg( (*voices[i]).id );
220 m.addIntArg(1);
221 m.addIntArg(2);
222 sender.sendMessage( m );
223 printf("sent /period %i %i %i\n",(*voices[i]).id,1,2);
224
225 }
226 }
227 }
228 if (key == OF_KEY_DOWN){
229 for (int i=0; i<numVoices; i++){
230 if ((*voices[i]).isInVoice(mouseX,mouseY)){
231 ofxOscMessage m;
232 ///track id x y left right top bottom area
233 m.setAddress( "/period" );
234 m.addIntArg( (*voices[i]).id );
235 m.addIntArg(2);
236 m.addIntArg(1);
237 sender.sendMessage( m );
238 printf("sent /period %i %i %i\n",(*voices[i]).id,2,1);
239 }
240 }
241 }
242
243 if (key == '.'){
244 for (int i=0; i<numVoices; i++){
245 if ((*voices[i]).isInVoice(mouseX,mouseY)){
246 ofxOscMessage m;
247 ///track id x y left right top bottom area
248 m.setAddress( "/period" );
249 m.addIntArg( (*voices[i]).id );
250 m.addIntArg(1);
251 m.addIntArg(3);
252 sender.sendMessage( m );
253 printf("sent /period %i %i %i\n",(*voices[i]).id,1,3);
254 }
255 }
256
257 }
258 if (key == ','){
259 for (int i=0; i<numVoices; i++){
260 if ((*voices[i]).isInVoice(mouseX,mouseY)){
261 ofxOscMessage m;
262 ///track id x y left right top bottom area
263 m.setAddress( "/period" );
264 m.addIntArg( (*voices[i]).id );
265 m.addIntArg(3);
266 m.addIntArg(1);
267 sender.sendMessage( m );
268 printf("sent /period %i %i %i\n",(*voices[i]).id,3,1);
269 }
270 }
271
272 }
273 if (key == OF_KEY_LEFT){
274 for (int i=0; i<numVoices; i++){
275 if ((*voices[i]).isInVoice(mouseX,mouseY)){
276
277 ofxOscMessage m;
278 ///track id x y left right top bottom area
279 m.setAddress( "/shift" );
280 m.addIntArg( (*voices[i]).id );
281 m.addIntArg(-1);
282 m.addIntArg(2);
283 sender.sendMessage( m );
284 printf("sent /shift %i %i %i\n",(*voices[i]).id,-1,2);
285
286 }
287 }
288 }
289 if (key == OF_KEY_RIGHT){
290 for (int i=0; i<numVoices; i++){
291 if ((*voices[i]).isInVoice(mouseX,mouseY)){
292
293
294 ofxOscMessage m;
295 ///track id x y left right top bottom area
296 m.setAddress( "/shift" );
297 m.addIntArg( (*voices[i]).id );
298 m.addIntArg(1);
299 m.addIntArg(2);
300 sender.sendMessage( m );
301 printf("sent /shift %i %i %i\n",(*voices[i]).id,1,2);
302
303 }
304 }
305 }
306 if (key == '1'){
307
308 ofxOscMessage m;
309 ///track id x y left right top bottom area
310 m.setAddress( "/tempo" );
311 m.addIntArg(30);
312 sender.sendMessage( m );
313 printf("sent /tempo 30\n");
314
315 }
316 if (key == '2'){
317 ofxOscMessage m;
318 ///track id x y left right top bottom area
319 m.setAddress( "/tempo" );
320 m.addIntArg(60);
321 sender.sendMessage( m );
322 printf("sent /tempo 60\n");
323 }
324 if (key == '3'){
325 ofxOscMessage m;
326 ///track id x y left right top bottom area
327 m.setAddress( "/tempo" );
328 m.addIntArg(90);
329 sender.sendMessage( m );
330 printf("sent /tempo 90\n");
331 }
332 if (key == '4'){
333 ofxOscMessage m;
334 ///track id x y left right top bottom area
335 m.setAddress( "/tempo" );
336 m.addIntArg(120);
337 sender.sendMessage( m );
338 printf("sent /tempo 120\n");
339 }
340 if (key == '+'){
341 for (int i=0; i<numVoices; i++){
342 if ((*voices[i]).isInVoice(mouseX,mouseY)){
343
344 (*voices[i]).octave++;
345 ofxOscMessage m;
346 ///track id x y left right top bottom area
347 m.setAddress( "/octave" );
348 m.addIntArg( (*voices[i]).id );
349 m.addIntArg((*voices[i]).octave);
350
351 sender.sendMessage( m );
352 printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave);
353
354 }
355 }
356 }
357
358 if (key == '-'){
359 for (int i=0; i<numVoices; i++){
360 if ((*voices[i]).isInVoice(mouseX,mouseY)){
361
362 (*voices[i]).octave--;
363 ofxOscMessage m;
364 ///track id x y left right top bottom area
365 m.setAddress( "/octave" );
366 m.addIntArg( (*voices[i]).id );
367 m.addIntArg((*voices[i]).octave);
368
369 sender.sendMessage( m );
370 printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave);
371
372 }
373 }
374 }
375 }
376 if (key == ' '){
377
378 ofxOscMessage m;
379 ///track id x y left right top bottom area
380 m.setAddress( "/marker" );
381 sender.sendMessage(m);
382 printf("sent /marker\n");
383 }
384 }
385
386 //--------------------------------------------------------------
387 void melodyTriangle::keyReleased (int key){
388
389 }
390
391 //--------------------------------------------------------------
392 void melodyTriangle::mouseMoved(int x, int y ){
393 for (int i=0; i<numVoices;i++){
394 if ((*voices[i]).isInVoice(x,y)){
395 (*voices[i]).highlight=true;
396 }else {
397 (*voices[i]).highlight=false;
398 }
399
400 }
401
402
403 }
404
405 //--------------------------------------------------------------
406 void melodyTriangle::mouseDragged(int x, int y, int button){
407
408 }
409
410 //--------------------------------------------------------------
411 void melodyTriangle::mousePressed(int x, int y, int button){
412
413 for (int i=0; i<numVoices;i++){
414 if ((*voices[i]).isInVoice(x,y)){
415 voiceGrabbed=i;
416 //printf("grabbed %i",voiceGrabbed);
417 }else{
418 //printf("didnt grab %i",i);
419 }
420 }
421 }
422
423
424
425 //--------------------------------------------------------------
426 void melodyTriangle::mouseReleased(int x, int y, int button){
427 //printf("released %i",voiceGrabbed);
428 voiceGrabbed=-1;
429 }
430
431 //--------------------------------------------------------------
432 void melodyTriangle::windowResized(int w, int h){
433
434 }