comparison src/melodyTriangle.cpp @ 12:317637282293

Compressed keyboard handling code, added calibration key 'c'.
author samer
date Mon, 30 Jan 2012 00:00:15 +0000
parents 066104f9a2e2
children 9e31c0507e65
comparison
equal deleted inserted replaced
11:066104f9a2e2 12:317637282293
38 x2=ofGetWidth()/2-triangleHeight/sqrt(3); 38 x2=ofGetWidth()/2-triangleHeight/sqrt(3);
39 y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2; 39 y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2;
40 x3=ofGetWidth()/2+triangleHeight/sqrt(3); 40 x3=ofGetWidth()/2+triangleHeight/sqrt(3);
41 y3=y2; 41 y3=y2;
42 42
43 sendCalibrate();
44 for (int i=0;i<numVoices;i++){
45 voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30);
46 }
47 voiceGrabbed=-1;
48 }
49
50 void melodyTriangle::sendCalibrate(){
43 ofxOscMessage m; 51 ofxOscMessage m;
44 m.setAddress( "/calibrate" ); 52 m.setAddress( "/calibrate" );
45 m.addIntArg( x1 ); 53 m.addIntArg( x1 );
46 m.addIntArg( y1 ); 54 m.addIntArg( y1 );
47 m.addIntArg( x2 ); 55 m.addIntArg( x2 );
48 m.addIntArg( y2 ); 56 m.addIntArg( y2 );
49 m.addIntArg( x3 ); 57 m.addIntArg( x3 );
50 m.addIntArg( y3 ); 58 m.addIntArg( y3 );
51 sender.sendMessage( m ); 59 sender.sendMessage( m );
52 printf("sent /calibrate %i %i %i %i %i %i\n",x1,y1,x2,y2,x3,y3); 60 printf("sent /calibrate %i %i %i %i %i %i\n",x1,y1,x2,y2,x3,y3);
53 61 }
54 for (int i=0;i<numVoices;i++){ 62
55 63 void melodyTriangle::sendPeriod(int id, int num, int den){
56 voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30); 64 ofxOscMessage m;
57 } 65 m.setAddress("/period");
58 voiceGrabbed=-1; 66 m.addIntArg(id);
67 m.addIntArg(num);
68 m.addIntArg(den);
69 sender.sendMessage(m);
70 printf("sent /period %i %i %i\n",id,num,den);
71 }
72
73 void melodyTriangle::sendShift(int id, int num, int den){
74 ofxOscMessage m;
75 m.setAddress("/shift");
76 m.addIntArg(id);
77 m.addIntArg(num);
78 m.addIntArg(den);
79 sender.sendMessage(m);
80 printf("sent /shift %i %i %i\n",id,num,den);
81 }
82
83 void melodyTriangle::sendOctave(int id, int oct){
84 ofxOscMessage m;
85 m.setAddress("/octave");
86 m.addIntArg(id);
87 m.addIntArg(oct);
88 sender.sendMessage(m);
89 printf("sent /octave %i %i\n",id,oct);
59 } 90 }
60 91
61 //-------------------------------------------------------------- 92 //--------------------------------------------------------------
62 void melodyTriangle::update(){ 93 void melodyTriangle::update(){
63 while( receiver.hasWaitingMessages() ) 94 while( receiver.hasWaitingMessages() )
197 } 228 }
198 }; 229 };
199 230
200 //let's draw our triangle 231 //let's draw our triangle
201 ofSetLineWidth(2); 232 ofSetLineWidth(2);
202 ofSetColor(128,128,128); 233 ofSetColor(96,96,96);
203 ofFill(); 234 ofFill();
204 ofTriangle(x1, y1, x2, y2, x3, y3); 235 ofTriangle(x1, y1, x2, y2, x3, y3);
205 if (constrained) ofSetColor(255,128,128); 236 if (constrained) ofSetColor(255,96,96);
206 237
207 // draw smooth edge, brighter if a token is constrained 238 // draw smooth edge, brighter if a token is constrained
208 ofNoFill(); 239 ofNoFill();
209 ofTriangle(x1, y1, x2, y2, x3, y3); 240 ofTriangle(x1, y1, x2, y2, x3, y3);
210 241
218 249
219 //-------------------------------------------------------------- 250 //--------------------------------------------------------------
220 void melodyTriangle::keyPressed (int key){ 251 void melodyTriangle::keyPressed (int key){
221 //printf("key %i",key); 252 //printf("key %i",key);
222 if (enableKeys){ 253 if (enableKeys){
223 if (key == 'a'){ 254 switch (key) {
224 for (int i=0; i<numVoices; i++){ 255 case ' ': {
225 if ((*voices[i]).isInVoice(mouseX,mouseY)){ 256 ofxOscMessage m;
226 257 m.setAddress( "/marker" );
227 (*voices[i]).isActive=!(*voices[i]).isActive; 258 sender.sendMessage(m);
228 ///start id 259 printf("sent /marker\n");
229 ///stop id 260 break;
230 if ((*voices[i]).isActive){ 261 }
231 ofxOscMessage m; 262
232 ///track id x y left right top bottom area 263 case '1':
233 m.setAddress( "/start" ); 264 case '2':
234 m.addIntArg( (*voices[i]).id ); 265 case '3':
235 sender.sendMessage( m ); 266 case '4': {
236 printf("sent /start %i \n",(*voices[i]).id); 267 int tempo=30 + 30*(key-'1');
237 }else { 268 ofxOscMessage m;
238 ofxOscMessage m; 269 m.setAddress( "/tempo" );
239 ///track id x y left right top bottom area 270 m.addIntArg(tempo);
240 m.setAddress( "/stop" ); 271 sender.sendMessage( m );
241 m.addIntArg( (*voices[i]).id ); 272 printf("sent /tempo %d\n",tempo);
242 sender.sendMessage( m ); 273 }
243 printf("sent /stop %i \n",(*voices[i]).id); 274 break;
244 275
276 case 'c': sendCalibrate(); break;
277
278 default: { // otherwise, send key to all active voices
279 for (int i=0; i<numVoices; i++){
280 if (voices[i]->isInVoice(mouseX,mouseY)){
281 Voice *v=voices[i];
282 switch (key) {
283 case 'a': {
284 ofxOscMessage m;
285 const char *addr = v->isActive ? "/stop" : "/start";
286 v->isActive=!v->isActive;
287 m.setAddress(addr);
288 m.addIntArg(v->id );
289 sender.sendMessage( m );
290 printf("sent %s %i \n",addr,v->id);
291 break;
292 }
293 case OF_KEY_LEFT: sendShift(v->id,-1,2); break;
294 case OF_KEY_RIGHT: sendShift(v->id,1,2); break;
295 case OF_KEY_UP: sendPeriod(v->id,1,2); break;
296 case OF_KEY_DOWN: sendPeriod(v->id,2,1); break;
297 case '.': sendPeriod(v->id,1,3); break;
298 case ',': sendPeriod(v->id,3,1); break;
299 case '+': sendOctave(v->id, ++v->octave); break;
300 case '-': sendOctave(v->id, --v->octave); break;
301 }
245 } 302 }
246
247 } 303 }
248 } 304 }
249 } 305 }
250 if (key == OF_KEY_UP){
251 for (int i=0; i<numVoices; i++){
252 if ((*voices[i]).isInVoice(mouseX,mouseY)){
253
254 ofxOscMessage m;
255 ///track id x y left right top bottom area
256 m.setAddress( "/period" );
257 m.addIntArg( (*voices[i]).id );
258 m.addIntArg(1);
259 m.addIntArg(2);
260 sender.sendMessage( m );
261 printf("sent /period %i %i %i\n",(*voices[i]).id,1,2);
262
263 }
264 }
265 }
266 if (key == OF_KEY_DOWN){
267 for (int i=0; i<numVoices; i++){
268 if ((*voices[i]).isInVoice(mouseX,mouseY)){
269 ofxOscMessage m;
270 ///track id x y left right top bottom area
271 m.setAddress( "/period" );
272 m.addIntArg( (*voices[i]).id );
273 m.addIntArg(2);
274 m.addIntArg(1);
275 sender.sendMessage( m );
276 printf("sent /period %i %i %i\n",(*voices[i]).id,2,1);
277 }
278 }
279 }
280
281 if (key == '.'){
282 for (int i=0; i<numVoices; i++){
283 if ((*voices[i]).isInVoice(mouseX,mouseY)){
284 ofxOscMessage m;
285 ///track id x y left right top bottom area
286 m.setAddress( "/period" );
287 m.addIntArg( (*voices[i]).id );
288 m.addIntArg(1);
289 m.addIntArg(3);
290 sender.sendMessage( m );
291 printf("sent /period %i %i %i\n",(*voices[i]).id,1,3);
292 }
293 }
294
295 }
296 if (key == ','){
297 for (int i=0; i<numVoices; i++){
298 if ((*voices[i]).isInVoice(mouseX,mouseY)){
299 ofxOscMessage m;
300 ///track id x y left right top bottom area
301 m.setAddress( "/period" );
302 m.addIntArg( (*voices[i]).id );
303 m.addIntArg(3);
304 m.addIntArg(1);
305 sender.sendMessage( m );
306 printf("sent /period %i %i %i\n",(*voices[i]).id,3,1);
307 }
308 }
309
310 }
311 if (key == OF_KEY_LEFT){
312 for (int i=0; i<numVoices; i++){
313 if ((*voices[i]).isInVoice(mouseX,mouseY)){
314
315 ofxOscMessage m;
316 ///track id x y left right top bottom area
317 m.setAddress( "/shift" );
318 m.addIntArg( (*voices[i]).id );
319 m.addIntArg(-1);
320 m.addIntArg(2);
321 sender.sendMessage( m );
322 printf("sent /shift %i %i %i\n",(*voices[i]).id,-1,2);
323
324 }
325 }
326 }
327 if (key == OF_KEY_RIGHT){
328 for (int i=0; i<numVoices; i++){
329 if ((*voices[i]).isInVoice(mouseX,mouseY)){
330
331
332 ofxOscMessage m;
333 ///track id x y left right top bottom area
334 m.setAddress( "/shift" );
335 m.addIntArg( (*voices[i]).id );
336 m.addIntArg(1);
337 m.addIntArg(2);
338 sender.sendMessage( m );
339 printf("sent /shift %i %i %i\n",(*voices[i]).id,1,2);
340
341 }
342 }
343 }
344 if (key == '1'){
345
346 ofxOscMessage m;
347 ///track id x y left right top bottom area
348 m.setAddress( "/tempo" );
349 m.addIntArg(30);
350 sender.sendMessage( m );
351 printf("sent /tempo 30\n");
352
353 }
354 if (key == '2'){
355 ofxOscMessage m;
356 ///track id x y left right top bottom area
357 m.setAddress( "/tempo" );
358 m.addIntArg(60);
359 sender.sendMessage( m );
360 printf("sent /tempo 60\n");
361 }
362 if (key == '3'){
363 ofxOscMessage m;
364 ///track id x y left right top bottom area
365 m.setAddress( "/tempo" );
366 m.addIntArg(90);
367 sender.sendMessage( m );
368 printf("sent /tempo 90\n");
369 }
370 if (key == '4'){
371 ofxOscMessage m;
372 ///track id x y left right top bottom area
373 m.setAddress( "/tempo" );
374 m.addIntArg(120);
375 sender.sendMessage( m );
376 printf("sent /tempo 120\n");
377 }
378 if (key == '+'){
379 for (int i=0; i<numVoices; i++){
380 if ((*voices[i]).isInVoice(mouseX,mouseY)){
381
382 (*voices[i]).octave++;
383 ofxOscMessage m;
384 ///track id x y left right top bottom area
385 m.setAddress( "/octave" );
386 m.addIntArg( (*voices[i]).id );
387 m.addIntArg((*voices[i]).octave);
388
389 sender.sendMessage( m );
390 printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave);
391
392 }
393 }
394 }
395
396 if (key == '-'){
397 for (int i=0; i<numVoices; i++){
398 if ((*voices[i]).isInVoice(mouseX,mouseY)){
399
400 (*voices[i]).octave--;
401 ofxOscMessage m;
402 ///track id x y left right top bottom area
403 m.setAddress( "/octave" );
404 m.addIntArg( (*voices[i]).id );
405 m.addIntArg((*voices[i]).octave);
406
407 sender.sendMessage( m );
408 printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave);
409
410 }
411 }
412 }
413 }
414 if (key == ' '){
415
416 ofxOscMessage m;
417 ///track id x y left right top bottom area
418 m.setAddress( "/marker" );
419 sender.sendMessage(m);
420 printf("sent /marker\n");
421 } 306 }
422 } 307 }
423 308
424 //-------------------------------------------------------------- 309 //--------------------------------------------------------------
425 void melodyTriangle::keyReleased (int key){ 310 void melodyTriangle::keyReleased (int key){