void OSCServer::oscMessageReceived (const OSCMessage& message){ // we're realtime here, defer if needed if(createClientIfNeeded(message)) return; if(OSCAddressPattern("/All/*").matches(message.getAddressPattern().toString())){ StringArray res; res.addTokens(message.getAddressPattern().toString(),"/",""); res.removeRange(1, 1); String current = res.joinIntoString("/"); for(auto &c:clients){ for(auto & b:c->boundedParams){ StringArray res2; res2.addTokens(b->address.toString(),"/",""); // currently /Tracks/i/i/... res2.removeRange(1, 3); String curS = res2.joinIntoString("/"); if(curS == current){ float val; if(getValueFromMessage(message, val)){ b->setValue(val); } } } } } else{ for(auto &c:clients){ for(auto & b:c->boundedParams){ if(message.getAddressPattern() == b->address){ float val; if(getValueFromMessage(message, val)){ b->setValue(val); } else{ DBG("message format not supported : "+String( message[0].getType())); } } } } } }
bool OSCServer::createClientIfNeeded(const OSCMessage &m){ // m. // TODO what clever protocol should we implement if(m.getAddressPattern().matches(OSCAddress("/Hi"))){ String host = m[0].getString(); if(host == "localhost"){ host = "127.0.0.1"; } int port = m[1].getInt32(); for(auto & c:clients){ if(c->host==host && c->port == port) return false; } OSCClient * cli = new OSCClient(host,port,true); for(auto & t:owner->tracks){ for(auto & p :t.get()->getParameters()){ cli->bindParameter(t, p, "/Tracks/"+String(*t->groupID) + "/" +String(*t->trackNum) + "/" + p->getName(50) ); } } clients.add(cli); return true; } else if(m.getAddressPattern().matches(OSCAddress("/Bye"))){ String host = m[0].getString(); if(host == "localhost"){ host = "127.0.0.1"; } int port = m[1].getInt32(); for(auto & c:clients){ if(c->host==host && c->port == port){ clients.removeObject(c); break; } } } return false; }
void Ambix_rotatorAudioProcessor::oscMessageReceived (const OSCMessage& message) { if (message.getAddressPattern() == OSCAddressPattern("/rotation")) { // /rotation [pitch] [yaw] [roll] float val[3]; for (int i=0; i < jmin(3,message.size()); i++) { val[i] = 0.5f; // get the value wheter it is a int or float value if (message[i].getType() == OSCTypes::float32) { val[i] = (float)message[i].getFloat32(); } else if (message[i].getType() == OSCTypes::int32) { val[i] = (float)message[i].getInt32(); } } setParameterNotifyingHost(Ambix_rotatorAudioProcessor::PitchParam, jlimit(0.f, 1.f, val[0]/360.f+0.5f)); setParameterNotifyingHost(Ambix_rotatorAudioProcessor::YawParam, jlimit(0.f, 1.f, val[1]/360.f+0.5f)); setParameterNotifyingHost(Ambix_rotatorAudioProcessor::RollParam, jlimit(0.f, 1.f, val[2]/360.f+0.5f)); } else if (message.getAddressPattern() == OSCAddressPattern("/head_pose")) { // /head_pose [User_ID] [x] [y] [z] [pitch] [yaw] [roll] float val[7]; for (int i=4; i < jmin(7,message.size()); i++) { val[i] = 0.5f; // get the value wheter it is a int or float value if (message[i].getType() == OSCTypes::float32) { val[i] = (float)message[i].getFloat32(); } else if (message[i].getType() == OSCTypes::int32) { val[i] = (float)message[i].getInt32(); } } setParameterNotifyingHost(Ambix_rotatorAudioProcessor::PitchParam, jlimit(0.f, 1.f, val[4]/360.f+0.5f)); setParameterNotifyingHost(Ambix_rotatorAudioProcessor::YawParam, jlimit(0.f, 1.f, val[5]/360.f+0.5f)); setParameterNotifyingHost(Ambix_rotatorAudioProcessor::RollParam, jlimit(0.f, 1.f, val[6]/360.f+0.5f)); } // debug the message #if 0 std::cout << "osc message received: " << message.getAddressPattern().toString() << " "; for (int i=0; i<message.size(); i++) { if (message[i].getType() == OSCTypes::float32) { std::cout << "[f] " << message[i].getFloat32() << " "; } else if (message[i].getType() == OSCTypes::int32) { std::cout << "[i] " << message[i].getInt32() << " "; } else if (message[i].getType() == OSCTypes::string) std::cout << "[s] " << message[i].getString() << " "; } std::cout << std::endl; #endif }
void TuioClient::oscMessageReceived (const OSCMessage& message) { if( message.getAddressPattern() == "/tuio/2Dcur" ) { String cmd; cmd = message[0].getString(); if (cmd == "set") { int32 s_id; float xpos, ypos, xspeed, yspeed, maccel; s_id=message[1].getInt32(); xpos=message[2].getFloat32(); ypos=message[3].getFloat32(); xspeed =message[4].getFloat32(); yspeed=message[5].getFloat32(); maccel=message[6].getFloat32(); std::list<TuioCursor*>::iterator tcur; for (tcur=cursorList.begin(); tcur!= cursorList.end(); tcur++) if((*tcur)->getSessionID()==(long)s_id) break; if (tcur==cursorList.end()) { TuioCursor *addCursor = new TuioCursor((long)s_id,-1,xpos,ypos); frameCursors.push_back(addCursor); } else if ( ((*tcur)->getX()!=xpos) || ((*tcur)->getY()!=ypos) || ((*tcur)->getXSpeed()!=xspeed) || ((*tcur)->getYSpeed()!=yspeed) || ((*tcur)->getMotionAccel()!=maccel) ) { int id = (*tcur)->getCursorID(); TuioCursor *updateCursor = new TuioCursor((long)s_id,id,xpos,ypos); updateCursor->update(xpos,ypos,xspeed,yspeed,maccel); frameCursors.push_back(updateCursor); } } else if (cmd=="alive") { aliveCursorList.clear(); for(int i = 1; i < message.size() ; i++) { aliveCursorList.push_back(message[i].getInt32()); } } else if( cmd== "fseq" ){ int32 fseq; fseq = message[1].getInt32(); bool lateFrame = false; if (fseq>0) { if (fseq>currentFrame) currentTime = TuioTime::getSessionTime(); if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq; else lateFrame = true; } else if ((TuioTime::getSessionTime().getTotalMilliseconds()-currentTime.getTotalMilliseconds())>100) { currentTime = TuioTime::getSessionTime(); } if (!lateFrame) { // lockCursorList(); // find the removed cursors first for (std::list<TuioCursor*>::iterator tcur=cursorList.begin(); tcur != cursorList.end(); tcur++) { std::list<long>::iterator iter = find(aliveCursorList.begin(), aliveCursorList.end(), (*tcur)->getSessionID()); if (iter == aliveCursorList.end()) { (*tcur)->remove(currentTime); frameCursors.push_back(*tcur); } } // unlockCursorList(); for (std::list<TuioCursor*>::iterator iter=frameCursors.begin(); iter != frameCursors.end(); iter++) { TuioCursor *tcur = (*iter); int c_id = -1; TuioCursor *frameCursor = NULL; switch (tcur->getTuioState()) { case TUIO_REMOVED: frameCursor = tcur; frameCursor->remove(currentTime); for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) (*listener)->removeTuioCursor(frameCursor); // lockCursorList(); for (std::list<TuioCursor*>::iterator delcur=cursorList.begin(); delcur!=cursorList.end(); delcur++) { if((*delcur)->getSessionID()==frameCursor->getSessionID()) { cursorList.erase(delcur); break; } } if (frameCursor->getCursorID()==maxCursorID) { maxCursorID = -1; delete frameCursor; if (cursorList.size()>0) { std::list<TuioCursor*>::iterator clist; for (clist=cursorList.begin(); clist != cursorList.end(); clist++) { c_id = (*clist)->getCursorID(); if (c_id>maxCursorID) maxCursorID=c_id; } freeCursorBuffer.clear(); for (std::list<TuioCursor*>::iterator flist=freeCursorList.begin(); flist != freeCursorList.end(); flist++) { TuioCursor *freeCursor = (*flist); if (freeCursor->getCursorID()>maxCursorID) delete freeCursor; else freeCursorBuffer.push_back(freeCursor); } freeCursorList = freeCursorBuffer; } else { for (std::list<TuioCursor*>::iterator flist=freeCursorList.begin(); flist != freeCursorList.end(); flist++) { TuioCursor *freeCursor = (*flist); delete freeCursor; } freeCursorList.clear(); } } else if (frameCursor->getCursorID()<maxCursorID) { freeCursorList.push_back(frameCursor); } // unlockCursorList(); break; case TUIO_ADDED: // lockCursorList(); c_id = (int)cursorList.size(); if (((int)(cursorList.size())<=maxCursorID) && ((int)(freeCursorList.size())>0)) { std::list<TuioCursor*>::iterator closestCursor = freeCursorList.begin(); for(std::list<TuioCursor*>::iterator iter = freeCursorList.begin();iter!= freeCursorList.end(); iter++) { if((*iter)->getDistance(tcur)<(*closestCursor)->getDistance(tcur)) closestCursor = iter; } TuioCursor *freeCursor = (*closestCursor); c_id = freeCursor->getCursorID(); freeCursorList.erase(closestCursor); delete freeCursor; } else maxCursorID = c_id; frameCursor = new TuioCursor(currentTime,tcur->getSessionID(),c_id,tcur->getX(),tcur->getY()); cursorList.push_back(frameCursor); delete tcur; // unlockCursorList(); for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++){ (*listener)->addTuioCursor(frameCursor); } break; default: // lockCursorList(); std::list<TuioCursor*>::iterator iter; for (iter=cursorList.begin(); iter != cursorList.end(); iter++) { if((*iter)->getSessionID()==tcur->getSessionID()) { frameCursor = (*iter); break; } } if ( (tcur->getX()!=frameCursor->getX() && tcur->getXSpeed()==0) || (tcur->getY()!=frameCursor->getY() && tcur->getYSpeed()==0) ) frameCursor->update(currentTime,tcur->getX(),tcur->getY()); else frameCursor->update(currentTime,tcur->getX(),tcur->getY(),tcur->getXSpeed(),tcur->getYSpeed(),tcur->getMotionAccel()); delete tcur; // unlockCursorList(); for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) (*listener)->updateTuioCursor(frameCursor); } } for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) (*listener)->refresh(currentTime); } else { for (std::list<TuioCursor*>::iterator iter=frameCursors.begin(); iter != frameCursors.end(); iter++) { TuioCursor *tcur = (*iter); delete tcur; } } frameCursors.clear(); } } else if( message.getAddressPattern()== "/tuio/2Dobj" ){ // String cmd = message[0].getString(); // // if (cmd=="set") { // // int32 s_id, c_id; // float xpos, ypos, angle, xspeed, yspeed, rspeed, maccel, raccel; // s_id=message[1].getInt32(); // c_id=message[2].getInt32(); // xpos=message[3].getFloat32(); // ypos=message[4].getFloat32(); // angle=message[5].getFloat32(); // xspeed =message[6].getFloat32(); // yspeed=message[7].getFloat32(); // rspeed=message[8].getFloat32(); // maccel=message[9].getFloat32(); // raccel=message[10].getFloat32(); // // lockObjectList(); // std::list<TuioObject*>::iterator tobj; // for (tobj=objectList.begin(); tobj!= objectList.end(); tobj++) // if((*tobj)->getSessionID()==(long)s_id) break; // // if (tobj == objectList.end()) { // // TuioObject *addObject = new TuioObject((long)s_id,(int)c_id,xpos,ypos,angle); // frameObjects.push_back(addObject); // // } else if ( ((*tobj)->getX()!=xpos) || ((*tobj)->getY()!=ypos) || ((*tobj)->getAngle()!=angle) || ((*tobj)->getXSpeed()!=xspeed) || ((*tobj)->getYSpeed()!=yspeed) || ((*tobj)->getRotationSpeed()!=rspeed) || ((*tobj)->getMotionAccel()!=maccel) || ((*tobj)->getRotationAccel()!=raccel) ) { // // TuioObject *updateObject = new TuioObject((long)s_id,(*tobj)->getSymbolID(),xpos,ypos,angle); // updateObject->update(xpos,ypos,angle,xspeed,yspeed,rspeed,maccel,raccel); // frameObjects.push_back(updateObject); // // } // unlockObjectList(); // // } // else if (cmd=="alive") { // // aliveObjectList.clear(); // for(int i = 1 ; i < message.size() ; i++){ // aliveObjectList.push_back(message[i].getInt32()); // } // // } else if (cmd == "fseq") { // // int32 fseq; // fseq = message[1].getInt32(); // bool lateFrame = false; // if (fseq>0) { // if (fseq>currentFrame) currentTime = TuioTime::getSessionTime(); // if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq; // else lateFrame = true; // } else if ((TuioTime::getSessionTime().getTotalMilliseconds()-currentTime.getTotalMilliseconds())>100) { // currentTime = TuioTime::getSessionTime(); // } // // if (!lateFrame) { // // lockObjectList(); // //find the removed objects first // for (std::list<TuioObject*>::iterator tobj=objectList.begin(); tobj != objectList.end(); tobj++) { // std::list<long>::iterator iter = find(aliveObjectList.begin(), aliveObjectList.end(), (*tobj)->getSessionID()); // if (iter == aliveObjectList.end()) { // (*tobj)->remove(currentTime); // frameObjects.push_back(*tobj); // } // } // unlockObjectList(); // // for (std::list<TuioObject*>::iterator iter=frameObjects.begin(); iter != frameObjects.end(); iter++) { // TuioObject *tobj = (*iter); // // TuioObject *frameObject = NULL; // switch (tobj->getTuioState()) { // case TUIO_REMOVED: // frameObject = tobj; // frameObject->remove(currentTime); // // for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) // (*listener)->removeTuioObject(frameObject); // // lockObjectList(); // for (std::list<TuioObject*>::iterator delobj=objectList.begin(); delobj!=objectList.end(); delobj++) { // if((*delobj)->getSessionID()==frameObject->getSessionID()) { // objectList.erase(delobj); // break; // } // } // unlockObjectList(); // break; // case TUIO_ADDED: // // lockObjectList(); // frameObject = new TuioObject(currentTime,tobj->getSessionID(),tobj->getSymbolID(),tobj->getX(),tobj->getY(),tobj->getAngle()); // objectList.push_back(frameObject); // unlockObjectList(); // // for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) // (*listener)->addTuioObject(frameObject); // // break; // default: // // lockObjectList(); // std::list<TuioObject*>::iterator iter; // for (iter=objectList.begin(); iter != objectList.end(); iter++) { // if((*iter)->getSessionID()==tobj->getSessionID()) { // frameObject = (*iter); // break; // } // } // if(iter==objectList.end()) break; // // if ( (tobj->getX()!=frameObject->getX() && tobj->getXSpeed()==0) || (tobj->getY()!=frameObject->getY() && tobj->getYSpeed()==0) ) // frameObject->update(currentTime,tobj->getX(),tobj->getY(),tobj->getAngle()); // else // frameObject->update(currentTime,tobj->getX(),tobj->getY(),tobj->getAngle(),tobj->getXSpeed(),tobj->getYSpeed(),tobj->getRotationSpeed(),tobj->getMotionAccel(),tobj->getRotationAccel()); // unlockObjectList(); // // for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) // (*listener)->updateTuioObject(frameObject); // // } // delete tobj; // } // // for (std::list<TuioListener*>::iterator listener=listenerList.begin(); listener != listenerList.end(); listener++) // (*listener)->refresh(currentTime); // // } else { // for (std::list<TuioObject*>::iterator iter=frameObjects.begin(); iter != frameObjects.end(); iter++) { // TuioObject *tobj = (*iter); // delete tobj; // } // } // // frameObjects.clear(); // } } }
void runTest() { beginTest ("Basic usage"); { OSCMessage msg ("/test/param0"); expectEquals (msg.size(), 0); expect (msg.getAddressPattern().toString() == "/test/param0"); const int numTestArgs = 4; const int testInt = 42; const float testFloat = 3.14159f; const String testString = "Hello, World!"; const uint8 testBlobData[5] = { 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; const MemoryBlock testBlob (testBlobData, sizeof (testBlobData)); msg.addInt32 (testInt); msg.addFloat32 (testFloat); msg.addString (testString); msg.addBlob (testBlob); expectEquals (msg.size(), numTestArgs); expectEquals (msg[0].getType(), OSCTypes::int32); expectEquals (msg[1].getType(), OSCTypes::float32); expectEquals (msg[2].getType(), OSCTypes::string); expectEquals (msg[3].getType(), OSCTypes::blob); expect (msg[0].isInt32()); expect (msg[1].isFloat32()); expect (msg[2].isString()); expect (msg[3].isBlob()); expectEquals (msg[0].getInt32(), testInt); expectEquals (msg[1].getFloat32(), testFloat); expectEquals (msg[2].getString(), testString); expect (msg[3].getBlob() == testBlob); expect (msg.begin() + numTestArgs == msg.end()); OSCArgument* arg = msg.begin(); expect (arg->isInt32()); expectEquals (arg->getInt32(), testInt); ++arg; expect (arg->isFloat32()); expectEquals (arg->getFloat32(), testFloat); ++arg; expect (arg->isString()); expectEquals (arg->getString(), testString); ++arg; expect (arg->isBlob()); expect(arg->getBlob() == testBlob); ++arg; expect (arg == msg.end()); } beginTest ("Initialisation with argument list (C++11 only)"); { int testInt = 42; float testFloat = 5.5; String testString = "Hello, World!"; { OSCMessage msg ("/test", testInt); expect (msg.getAddressPattern().toString() == String ("/test")); expectEquals (msg.size(), 1); expect (msg[0].isInt32()); expectEquals (msg[0].getInt32(), testInt); } { OSCMessage msg ("/test", testFloat); expect (msg.getAddressPattern().toString() == String ("/test")); expectEquals (msg.size(), 1); expect (msg[0].isFloat32()); expectEquals (msg[0].getFloat32(), testFloat); } { OSCMessage msg ("/test", testString); expect (msg.getAddressPattern().toString() == String ("/test")); expectEquals (msg.size(), 1); expect (msg[0].isString()); expectEquals (msg[0].getString(), testString); } { OSCMessage msg ("/test", testInt, testFloat, testString, testFloat, testInt); expect (msg.getAddressPattern().toString() == String ("/test")); expectEquals (msg.size(), 5); expect (msg[0].isInt32()); expect (msg[1].isFloat32()); expect (msg[2].isString()); expect (msg[3].isFloat32()); expect (msg[4].isInt32()); expectEquals (msg[0].getInt32(), testInt); expectEquals (msg[1].getFloat32(), testFloat); expectEquals (msg[2].getString(), testString); expectEquals (msg[3].getFloat32(), testFloat); expectEquals (msg[4].getInt32(), testInt); } } }