Mercurial > hg > screen-ui
view src/melodyTriangle.cpp @ 8:3d9e0db254dc
Added OSC receive
author | Henrik Ekeus <hekeus@eecs.qmul.ac.uk> |
---|---|
date | Thu, 26 Jan 2012 23:42:13 +0000 |
parents | 38f63c4300d7 |
children | a8f71b5bdb0e |
line wrap: on
line source
#include "melodyTriangle.h" #include <GLUT/GLUT.h> /* /birth id /death id /start id /stop id /track id x y left right top bottom area /tempo */ melodyTriangle::melodyTriangle(const char *host, int port, int numVoices, bool enableKeys,int voiceIdOffset,int receivePort){ printf("in constructor: %s %i %i %i %i %i\n",host,port,numVoices,enableKeys,voiceIdOffset,receivePort); this->numVoices=numVoices; this->enableKeys=enableKeys; this->voiceIdOffset=voiceIdOffset; //voices=*Voice[numVoices]; sender.setup( host,port ); receiver.setup( receivePort ); } //-------------------------------------------------------------- void melodyTriangle::setup(){ //voices = new Voice[NUMVOICES]; ofSetCircleResolution(100); ofBackground(0,0,0); ofSetWindowTitle("Melody Triangle"); triangleHeight=ofGetHeight()*0.75; ofSetFrameRate(40); // if vertical sync is off, we can go a bit fast... this caps the framerate at 60fps. ofEnableSmoothing(); x1=ofGetWidth()/2; y1=(ofGetHeight()-triangleHeight)/2; x2=ofGetWidth()/2-triangleHeight/sqrt(3); y2=ofGetHeight()-(ofGetHeight()-triangleHeight)/2; x3=ofGetWidth()/2+triangleHeight/sqrt(3); y3=y2; ofxOscMessage m; m.setAddress( "/calibrate" ); m.addIntArg( x1 ); m.addIntArg( y1 ); m.addIntArg( x2 ); m.addIntArg( y2 ); m.addIntArg( x3 ); m.addIntArg( y3 ); sender.sendMessage( m ); printf("sent /calibrate %i %i %i %i %i %i\n",x1,y1,x2,y2,x3,y3); for (int i=0;i<numVoices;i++){ voices[i]=new Voice(i+1+voiceIdOffset,x2+15,y1+20+i*30); } voiceGrabbed=-1; } //-------------------------------------------------------------- void melodyTriangle::update(){ while( receiver.hasWaitingMessages() ) { // get the next message ofxOscMessage m; receiver.getNextMessage( &m ); string msg_string; msg_string = m.getAddress(); msg_string += ": "; for ( int i=0; i<m.getNumArgs(); i++ ) { // get the argument type msg_string += m.getArgTypeName( i ); msg_string += ":"; // display the argument - make sure we get the right type if( m.getArgType( i ) == OFXOSC_TYPE_INT32 ) msg_string += ofToString( m.getArgAsInt32( i ) ); else if( m.getArgType( i ) == OFXOSC_TYPE_FLOAT ) msg_string += ofToString( m.getArgAsFloat( i ) ); else if( m.getArgType( i ) == OFXOSC_TYPE_STRING ) msg_string += m.getArgAsString( i ); else msg_string += "unknown"; } cout<< msg_string << "\n"; } } bool melodyTriangle::isInTriangle(int x, int y){ if (x>x2 && x<x3 && y>y1 && y<y2){ //printf("in bounding box\n"); float dx=abs(x-x1); float dy=abs(y-y1); //printf("tan(30)- dx/dy: %f\n",tan(30*PI/180)-dx/dy); if (dx/dy < tan(30*PI/180)){ //printf("in triangle \n"); return true; }else { //printf("not in triangle \n"); return false; } }else{ //printf("not in bounding box \n"); return false; } } void melodyTriangle::sendStatus(Voice v){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/track2d" ); m.addIntArg( v.id ); m.addIntArg( v.posx ); m.addIntArg( v.posy ); sender.sendMessage( m ); printf("sent - /track2d %i %i %i\n",v.id,v.posx,v.posy); } //-------------------------------------------------------------- void melodyTriangle::draw(){ //let's draw our triangle ofSetColor(0,0,255); ofFill(); ofTriangle(x1, y1, x2, y2, x3, y3); bool sendStart=false; if (voiceGrabbed!=-1){ if (mouseX!=(*voices[voiceGrabbed]).posx || mouseY!=(*voices[voiceGrabbed]).posy){ //(*voices[voiceGrabbed]).posx=mouseX; //(*voices[voiceGrabbed]).posy=mouseY; if ((*voices[voiceGrabbed]).inTriangle && !isInTriangle(mouseX,mouseY)){ ///death id if (ofDist((*voices[voiceGrabbed]).posx, (*voices[voiceGrabbed]).posy, mouseX, mouseY) > (*voices[voiceGrabbed]).radius*2){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/death" ); m.addIntArg( (*voices[voiceGrabbed]).id ); sender.sendMessage( m ); printf("sent /death %i \n",(*voices[voiceGrabbed]).id); (*voices[voiceGrabbed]).posx=mouseX; (*voices[voiceGrabbed]).posy=mouseY; } else { //printf("e"); //On Edge } }else{ (*voices[voiceGrabbed]).posx=mouseX; (*voices[voiceGrabbed]).posy=mouseY; //(*voices[voiceGrabbed]).posx=(*voices[voiceGrabbed]).posx*0.9+mouseX*0.1; //(*voices[voiceGrabbed]).posy=(*voices[voiceGrabbed]).posy*0.9+mouseY*0.1; } if (!(*voices[voiceGrabbed]).inTriangle && isInTriangle(mouseX,mouseY)){ //birth id ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/birth" ); m.addIntArg( (*voices[voiceGrabbed]).id ); sender.sendMessage( m ); printf("sent /birth %i \n",(*voices[voiceGrabbed]).id); sendStart=true; } //(*voices[voiceGrabbed]).inTriangle = isInTriangle(mouseX,mouseY); (*voices[voiceGrabbed]).inTriangle = isInTriangle((*voices[voiceGrabbed]).posx,(*voices[voiceGrabbed]).posy); if ((*voices[voiceGrabbed]).inTriangle){ sendStatus(*voices[voiceGrabbed]); if (sendStart){ if ((*voices[voiceGrabbed]).isActive){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/start" ); m.addIntArg( (*voices[voiceGrabbed]).id ); sender.sendMessage( m ); printf("sent /start %i \n",(*voices[voiceGrabbed]).id); } } } } }; for (int i=0; i<numVoices; i++){ (*voices[i]).draw(); } } //-------------------------------------------------------------- void melodyTriangle::keyPressed (int key){ //printf("key %i",key); if (enableKeys){ if (key == 'a'){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ (*voices[i]).isActive=!(*voices[i]).isActive; ///start id ///stop id if ((*voices[i]).isActive){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/start" ); m.addIntArg( (*voices[i]).id ); sender.sendMessage( m ); printf("sent /start %i \n",(*voices[i]).id); }else { ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/stop" ); m.addIntArg( (*voices[i]).id ); sender.sendMessage( m ); printf("sent /stop %i \n",(*voices[i]).id); } } } } if (key == OF_KEY_UP){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/period" ); m.addIntArg( (*voices[i]).id ); m.addIntArg(1); m.addIntArg(2); sender.sendMessage( m ); printf("sent /period %i %i %i\n",(*voices[i]).id,1,2); } } } if (key == OF_KEY_DOWN){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/period" ); m.addIntArg( (*voices[i]).id ); m.addIntArg(2); m.addIntArg(1); sender.sendMessage( m ); printf("sent /period %i %i %i\n",(*voices[i]).id,2,1); } } } if (key == '.'){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/period" ); m.addIntArg( (*voices[i]).id ); m.addIntArg(1); m.addIntArg(3); sender.sendMessage( m ); printf("sent /period %i %i %i\n",(*voices[i]).id,1,3); } } } if (key == ','){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/period" ); m.addIntArg( (*voices[i]).id ); m.addIntArg(3); m.addIntArg(1); sender.sendMessage( m ); printf("sent /period %i %i %i\n",(*voices[i]).id,3,1); } } } if (key == OF_KEY_LEFT){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/shift" ); m.addIntArg( (*voices[i]).id ); m.addIntArg(-1); m.addIntArg(2); sender.sendMessage( m ); printf("sent /shift %i %i %i\n",(*voices[i]).id,-1,2); } } } if (key == OF_KEY_RIGHT){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/shift" ); m.addIntArg( (*voices[i]).id ); m.addIntArg(1); m.addIntArg(2); sender.sendMessage( m ); printf("sent /shift %i %i %i\n",(*voices[i]).id,1,2); } } } if (key == '1'){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/tempo" ); m.addIntArg(30); sender.sendMessage( m ); printf("sent /tempo 30\n"); } if (key == '2'){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/tempo" ); m.addIntArg(60); sender.sendMessage( m ); printf("sent /tempo 60\n"); } if (key == '3'){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/tempo" ); m.addIntArg(90); sender.sendMessage( m ); printf("sent /tempo 90\n"); } if (key == '4'){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/tempo" ); m.addIntArg(120); sender.sendMessage( m ); printf("sent /tempo 120\n"); } if (key == '+'){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ (*voices[i]).octave++; ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/octave" ); m.addIntArg( (*voices[i]).id ); m.addIntArg((*voices[i]).octave); sender.sendMessage( m ); printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave); } } } if (key == '-'){ for (int i=0; i<numVoices; i++){ if ((*voices[i]).isInVoice(mouseX,mouseY)){ (*voices[i]).octave--; ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/octave" ); m.addIntArg( (*voices[i]).id ); m.addIntArg((*voices[i]).octave); sender.sendMessage( m ); printf("sent /octave %i %i \n",(*voices[i]).id,(*voices[i]).octave); } } } } if (key == ' '){ ofxOscMessage m; ///track id x y left right top bottom area m.setAddress( "/marker" ); sender.sendMessage(m); printf("sent /marker\n"); } } //-------------------------------------------------------------- void melodyTriangle::keyReleased (int key){ } //-------------------------------------------------------------- void melodyTriangle::mouseMoved(int x, int y ){ for (int i=0; i<numVoices;i++){ if ((*voices[i]).isInVoice(x,y)){ (*voices[i]).highlight=true; }else { (*voices[i]).highlight=false; } } } //-------------------------------------------------------------- void melodyTriangle::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void melodyTriangle::mousePressed(int x, int y, int button){ for (int i=0; i<numVoices;i++){ if ((*voices[i]).isInVoice(x,y)){ voiceGrabbed=i; //printf("grabbed %i",voiceGrabbed); }else{ //printf("didnt grab %i",i); } } } //-------------------------------------------------------------- void melodyTriangle::mouseReleased(int x, int y, int button){ //printf("released %i",voiceGrabbed); voiceGrabbed=-1; } //-------------------------------------------------------------- void melodyTriangle::windowResized(int w, int h){ }