Created
January 23, 2013 10:54
-
-
Save nariakiiwatani/4604358 to your computer and use it in GitHub Desktop.
OpenSoundControl send/receive utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Osc.h" | |
#include "ofxOscSender.h" | |
#include "ofxOscReceiver.h" | |
namespace osc { | |
ofxOscSender sender_; | |
ofxOscReceiver receiver_; | |
map<string, ofEvent<ofxOscMessage>> events_; | |
} | |
void osc::setupSender(const string& hostname, int port) | |
{ | |
sender_.setup(hostname, port); | |
} | |
void osc::setupReceiver(int port) { | |
receiver_.setup(port); | |
} | |
void osc::send(const string& address, const char* fmt, ...) | |
{ | |
va_list ap; | |
va_start(ap, fmt); | |
ofxOscMessage msg; | |
msg.setAddress(address); | |
while(*fmt) { | |
switch(*fmt++) { | |
case 'i': msg.addIntArg(va_arg(ap, int)); break; | |
case 'f': msg.addFloatArg(va_arg(ap, double)); break; | |
case 'l': msg.addInt64Arg(va_arg(ap, long)); break; | |
case 's': msg.addStringArg(va_arg(ap, char*)); break; | |
} | |
} | |
sender_.sendMessage(msg); | |
va_end(ap); | |
} | |
void osc::send(const string& address, int num, const int* arg) | |
{ | |
ofxOscMessage msg; | |
msg.setAddress(address); | |
while(num--) { | |
msg.addIntArg(*arg++); | |
} | |
sender_.sendMessage(msg); | |
} | |
void osc::send(const string& address, const vector<int>& arg) | |
{ | |
ofxOscMessage msg; | |
msg.setAddress(address); | |
vector<int>::const_iterator it = arg.begin(); | |
vector<int>::const_iterator end = arg.end(); | |
while(it != end) { | |
msg.addIntArg(*it++); | |
} | |
sender_.sendMessage(msg); | |
} | |
void osc::send(const string& address, int num, const float* arg) | |
{ | |
ofxOscMessage msg; | |
msg.setAddress(address); | |
while(num--) { | |
msg.addFloatArg(*arg++); | |
} | |
sender_.sendMessage(msg); | |
} | |
void osc::send(const string& address, const vector<float>& arg) | |
{ | |
ofxOscMessage msg; | |
msg.setAddress(address); | |
vector<float>::const_iterator it = arg.begin(); | |
vector<float>::const_iterator end = arg.end(); | |
while(it != end) { | |
msg.addFloatArg(*it++); | |
} | |
sender_.sendMessage(msg); | |
} | |
void osc::receive() { | |
if(receiver_.hasWaitingMessages()) { | |
ofxOscMessage msg; | |
while(receiver_.getNextMessage(&msg)) { | |
map<string, ofEvent<ofxOscMessage>>::iterator it = events_.find(msg.getAddress()); | |
if(it != events_.end()) { | |
(*it).second.notify(NULL, msg); | |
} | |
} | |
} | |
} | |
/* EOF */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* @送るとき | |
* sendすれば送られます。 | |
* フォーマット文字列は以下が使用可です。 | |
* i: int, f: float, l: long, s: string | |
* | |
* @受け取るとき | |
* addReceiverにアドレスを指定して登録してください。 | |
* 指定したアドレスにメッセージを受け取ると、登録した関数が呼ばれます。 | |
* (第二引数以降はofAddListenerを参考にしてください) | |
*/ | |
#pragma once | |
#include "ofTypes.h" | |
#include "ofEvents.h" | |
#include "ofxOscMessage.h" | |
#include <map> | |
namespace osc { | |
// Sender用 | |
void setupSender(const string& hostname, int port); | |
void send(const string& address, const char* fmt, ...); | |
void send(const string& address, const vector<int>& arg); | |
void send(const string& address, const vector<float>& arg); | |
void send(const string& address, const vector<string>& arg); | |
void send(const string& address, int num, const int* arg); | |
void send(const string& address, int num, const float* arg); | |
// Receiver用 | |
void setupReceiver(int port); | |
template<class T> | |
void addReceiveEvent(string address, T* obj, void (T::*method)(ofxOscMessage& msg)); | |
template<class T> | |
void removeReceiveEvent(string address, T* obj, void (T::*method)(ofxOscMessage& msg)); | |
// 以下さわんないでいいです | |
extern map<string, ofEvent<ofxOscMessage>> events_; | |
void receive(); | |
} | |
template<class T> | |
void osc::addReceiveEvent(string address, T* obj, void (T::*method)(ofxOscMessage& msg)) | |
{ | |
events_[address] += Poco::delegate(obj, method); | |
} | |
template<class T> | |
void osc::removeReceiveEvent(string address, T* obj, void (T::*method)(ofxOscMessage& msg)) | |
{ | |
events_[address] -= Poco::delegate(obj, method); | |
} | |
/* EOF */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment