# HG changeset patch # User samer # Date 1328206644 0 # Node ID 4dcc4312b5fa2f1ed6aea108d23d22ae2c05bb18 # Parent 95626beb91abf218970bd1fc5a78c1ad333a491f Bit of a tidy up, adding text display, added full screen toggle and window resize handler. diff -r 95626beb91ab -r 4dcc4312b5fa src/Voice.cpp --- a/src/Voice.cpp Wed Feb 01 16:17:32 2012 +0000 +++ b/src/Voice.cpp Thu Feb 02 18:17:24 2012 +0000 @@ -11,18 +11,10 @@ inline static double min(double x,double y) { return (xid=id; - posx=x; - posy=y; - isActive=true; - radius=12; - inTriangle=false; - octave=0; - highlight=false; - amplitude=0.6; - status=clear; -} +Voice::Voice(int id): + radius(12), isActive(true), + inTriangle(false), octave(0), highlight(false), + amplitude(0.6), status(clear), id(id), posx(0), posy(0) {} void Voice::draw(){ int r,g,b; @@ -52,6 +44,6 @@ } } -double Voice::louder() { return amplitude=min(1,amplitude*1.125); } -double Voice::quieter() { return amplitude/=1.125; } +double Voice::louder() { return amplitude=min(1,amplitude*1.0625); } +double Voice::quieter() { return amplitude/=1.0625; } diff -r 95626beb91ab -r 4dcc4312b5fa src/Voice.h --- a/src/Voice.h Wed Feb 01 16:17:32 2012 +0000 +++ b/src/Voice.h Thu Feb 02 18:17:24 2012 +0000 @@ -9,8 +9,7 @@ #include "ofMain.h" class Voice { public: - Voice(int id, int x, int y); - Voice() {}; + Voice(int id); enum status { clear, @@ -20,6 +19,7 @@ }; bool isInVoice(int x, int y) { return (ofDist(x, y, posx, posy)<=radius); }; + void setPos(int x, int y) { posx=x; posy=y; } double louder(); double quieter(); void draw(); diff -r 95626beb91ab -r 4dcc4312b5fa src/melodyTriangle.cpp --- a/src/melodyTriangle.cpp Wed Feb 01 16:17:32 2012 +0000 +++ b/src/melodyTriangle.cpp Thu Feb 02 18:17:24 2012 +0000 @@ -12,207 +12,62 @@ */ -melodyTriangle::melodyTriangle(const char *host, int port, int numVoices, bool enableKeys,int voiceIdOffset,int receivePort){ + +melodyTriangle::melodyTriangle(const char *host, int port, int numVoices, + bool enableKeys,int voiceIdOffset,int receivePort): + numVoices(numVoices), enableKeys(enableKeys), receivePort(receivePort), + display_msg(""), + display_frames(0) +{ 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]; + for (int i=0;isetPos(x2+voices[i]->radius+i*30,48); voiceGrabbed=-1; } -void melodyTriangle::sendCalibrate(){ - 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); -} - -void melodyTriangle::sendPeriod(int id, int num, int den){ - ofxOscMessage m; - m.setAddress("/period"); - m.addIntArg(id); - m.addIntArg(num); - m.addIntArg(den); - sender.sendMessage(m); - printf("sent /period %i %i %i\n",id,num,den); -} - -void melodyTriangle::sendShift(int id, int num, int den){ - ofxOscMessage m; - m.setAddress("/shift"); - m.addIntArg(id); - m.addIntArg(num); - m.addIntArg(den); - sender.sendMessage(m); - printf("sent /shift %i %i %i\n",id,num,den); -} - -void melodyTriangle::sendOctave(int id, int oct){ - ofxOscMessage m; - m.setAddress("/octave"); - m.addIntArg(id); - m.addIntArg(oct); - sender.sendMessage(m); - printf("sent /octave %i %i\n",id,oct); -} - -void melodyTriangle::sendAmplitude(int id, float amp){ - ofxOscMessage m; - m.setAddress("/amplitude"); - m.addIntArg(id); - m.addFloatArg(amp); - sender.sendMessage(m); - printf("sent /amplitude %i %1.3f\n",id,amp); -} - //-------------------------------------------------------------- void melodyTriangle::update(){ - while( receiver.hasWaitingMessages() ) - { + bool sendStart=false; + + while( receiver.hasWaitingMessages() ) { // get the next message ofxOscMessage m; receiver.getNextMessage( &m ); - if (m.getAddress()=="/notify") { - int id=m.getArgAsInt32(0)-1; - string st=m.getArgAsString(1); - - if (id>=0 && idstatus=Voice::clear; - else if (st=="pending") v->status=Voice::pending; - else if (st=="requested") v->status=Voice::waiting; - else cout << "** unrecognised voice status: " << st << ".\n"; - } else { - cout << "** voice id "<y2) { // off the bottom - clipped=true; - *y=y2; - if (*xx3) *x=x3; - } else { // have to be a bit cleverer - bool reflect=false; - if (*x dy*DX13) { - // (x,y) must be somewhere right of triangle now - clipped=true; - int dp=dx*DX13 + dy*DY13; - if (dp<0) { *x=x1; *y=y1; } // off the top - else if (dp>SQLEN13) { *x=x3; *y=y3; } // off the bottom right - else { // project onto right edge - *x=x1+dp*DX13/SQLEN13; - *y=y1+dp*DY13/SQLEN13; - } - } else { - clipped=false; - } - - if (reflect) *x=2*x1 - *x; // reflect back if necessary - } - return clipped; -} - - -void melodyTriangle::sendPosition(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(){ - bool constrained=false; - bool sendStart=false; - + constrained=false; if (voiceGrabbed!=-1){ Voice *vg=voices[voiceGrabbed]; if (mouseX!=vg->posx || mouseY!=vg->posy){ @@ -230,7 +85,7 @@ m.setAddress( "/death" ); m.addIntArg( vg->id ); sender.sendMessage( m ); - + printf("sent /death %i \n",vg->id); vg->posx=mouseX; vg->posy=mouseY; @@ -279,8 +134,12 @@ } } }; +} - //let's draw our triangle + + +//-------------------------------------------------------------- +void melodyTriangle::draw(){ ofSetLineWidth(2); ofSetColor(80,80,80); ofFill(); @@ -295,10 +154,54 @@ (*voices[i]).draw(); } - + // display message if any + if (display_frames>0) { + ofSetColor(220,220,220); + display_font.drawString(display_msg,x2,y1+32); + display_frames--; + } } -//-------------------------------------------------------------- +bool melodyTriangle::clipToTriangle(int *x, int *y) { + bool clipped; + + if (*y>y2) { // off the bottom + clipped=true; + *y=y2; + if (*xx3) *x=x3; + } else { // have to be a bit cleverer + bool reflect=false; + if (*x dy*DX13) { + // (x,y) must be somewhere right of triangle now + clipped=true; + int dp=dx*DX13 + dy*DY13; + if (dp<0) { *x=x1; *y=y1; } // off the top + else if (dp>SQLEN13) { *x=x3; *y=y3; } // off the bottom right + else { // project onto right edge + *x=x1+dp*DX13/SQLEN13; + *y=y1+dp*DY13/SQLEN13; + } + } else { + clipped=false; + } + + if (reflect) *x=2*x1 - *x; // reflect back if necessary + } + return clipped; +} + + + +//- Keyboard ---------------------------------------------------------- + +void melodyTriangle::keyReleased(int key){} void melodyTriangle::keyPressed (int key){ //printf("key %i",key); if (enableKeys){ @@ -324,7 +227,8 @@ } break; - case 'c': sendCalibrate(); break; + case 'c': sendReplyTo(); sendCalibrate(); break; + case 'f': ofToggleFullscreen(); break; default: { // otherwise, send key to all active voices for (int i=0; ihighlight = voices[i]->isInVoice(x,y); } } -//-------------------------------------------------------------- -void melodyTriangle::mouseDragged(int x, int y, int button){ - -} - -//-------------------------------------------------------------- void melodyTriangle::mousePressed(int x, int y, int button){ for (int i=0; i=0 && idstatus=Voice::clear; + else if (st=="pending") v->status=Voice::pending; + else if (st=="requested") v->status=Voice::waiting; + else cout << "** unrecognised voice status: " << st << ".\n"; + } else { + cout << "** voice id "<