当前位置: 首页>>代码示例>>C++>>正文


C++ PacketReader::init方法代码示例

本文整理汇总了C++中PacketReader::init方法的典型用法代码示例。如果您正苦于以下问题:C++ PacketReader::init方法的具体用法?C++ PacketReader::init怎么用?C++ PacketReader::init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PacketReader的用法示例。


在下文中一共展示了PacketReader::init方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

/**
 * Fetches muse-io udp packets from port localhost:4545 and reads OSC paths:
 * /muse/elements/delta_relative
 * /muse/elements/theta_relative
 * /muse/elements/alpha_relative
 * /muse/elements/beta_relative
 * /muse/elements/gamma_relative
 *
 * Calculates mean delta, theta, alpha, beta and gamma values (and st.dev.)
 * from all channels and which /muse/elements/is_good value is >= 0.5.
 * If there are no good values at all, reports 0.5 value and maximum st.dev.
 * (of uniform distribution between [0,1])
 *
 * Also measures:
 * /muse/elements/experimental/concentration
 * /muse/elements/experimental/mellow
 */
int main(int argc, char** argv)
{
	const unsigned int port = 4545;

	std::cout << "MUSE TEST OSC (UDP PORT 4545) LISTENER" << std::endl;
	fflush(stdout);

	UdpSocket sock;
	sock.bindTo(port);

	if(!sock.isOk()) return -1; // failure [retry?]

	PacketReader pr;
	PacketWriter pw;

	std::vector<int> connectionQuality;
	bool hasConnection = false;
	float delta = 0.0f, theta = 0.0f, alpha = 0.0f, beta = 0.0f, gamma = 0.0f;

	while(sock.isOk()){

		if(hasConnection){
			float q = 0.0f;
			for(auto qi : connectionQuality)
				if(qi > 0) q++;
			q = q / connectionQuality.size();

			// printf("EEQ: D:%.2f T:%.2f A:%.2f B:%.2f G:%.2f [QUALITY: %.2f]\n", delta, theta, alpha, beta, gamma, q);
			printf("EEQ POWER: %.2f [QUALITY %.2f]\n",
					log(exp(delta)+exp(theta)+exp(alpha)+exp(beta)+exp(gamma)), q);

			fflush(stdout);
		}

		if(sock.receiveNextPacket(30)){
			pr.init(sock.packetData(), sock.packetSize());
			Message* msg;
			while(pr.isOk() && ((msg = pr.popMessage()) != 0)){
				Message::ArgReader r = msg->match("/muse/elements/is_good");

				if(r.isOk() == true){ // matched
					// there are 4 ints telling connection quality
					std::vector<int> quality;

					while(r.nbArgRemaining()){
						if(r.isInt32()){
							int32_t i;
							r = r.popInt32(i);
							quality.push_back((int)i);
						}
						else{
							r = r.pop();
						}
					}

					if(quality.size() > 0){
						connectionQuality = quality;
					}

					bool connection = false;

					for(auto q : quality)
						if(q > 0) connection = true;

					hasConnection = connection;
				}

				// gets relative frequency bands..

				r = msg->match("/muse/elements/delta_absolute");
				if(r.isOk()){
					float f1, f2, f3, f4;
					if(r.popFloat(f1).popFloat(f2).popFloat(f3).popFloat(f4).isOkNoMoreArgs()){
						std::vector<float> v;
						v.push_back(f1); v.push_back(f2); v.push_back(f3); v.push_back(f4);

						// calculates mean according to connection quality
						float mean = 0.0f;
						float samples = 0.0f;

						for(unsigned int i=0;i<connectionQuality.size() && i<v.size();i++){
							if(connectionQuality[i] > 0){
								mean += exp(v[i]);
//.........这里部分代码省略.........
开发者ID:cslr,项目名称:resonanz,代码行数:101,代码来源:muse-io-test.cpp

示例2: ParseOSC

void CBodyBasics::ParseOSC()
{

	// UDP stuff
	PacketReader pr;
	PacketWriter pw;

	if (recvsock.isOk() && recvsock.receiveNextPacket(1 /* timeout, in ms */)) {
		pr.init(recvsock.packetData(), recvsock.packetSize());
		oscpkt::Message *msg;
		while (pr.isOk() && (msg = pr.popMessage()) != 0) {

			// parse IP address
			std::string c = recvsock.packetOrigin().asString();
			std::string delim = ":";
			size_t pos = 0;
			std::string token;
			while ((pos = c.find(delim)) != std::string::npos) {
				token = c.substr(0, pos);
				c = token;
			}
			std::string s = "Server: received message from " + c + "\n";
			printFucker(s);

			//
			// parse messages
			//

			// CONNECT
			if (msg->match("/connect").isOkNoMoreArgs()) {
				// find empty client - check existing list
				int foundmatch = 0;
				for (int i = 0; i < clients.size(); i++)
				{
					if (clients[i].address == c) // already in list
					{
						printFucker("RECONNECTING TO HOST " + c + ":\n");
						clients[i].active = 1;
						foundmatch = 1;
						break;
					}
				}
				if (foundmatch == 0) // add new client to first free slot
				{
					for (int i = 0; i < clients.size(); i++)
					{
						if (clients[i].active == false) // add here
						{
							printFucker("ADDING CONNECTION TO HOST " + c + ":\n");
							clients[i].address = c;
							clients[i].active = true;
							clients[i].socket.connectTo(c, SENDPORT);
							foundmatch = 1;
							break;
						}
					}
				}
				if (foundmatch == 0) // out of slots
				{
					std::string s = "Out of slots!!!\n";
					printFucker(s);

				}

			}

			// DISCONNECT
			else if (msg->match("/disconnect").isOkNoMoreArgs()) {
				for (int i = 0; i < clients.size(); i++)
				{
					if (clients[i].address == c) // shut it off
					{
						printFucker("DISCONNECTING FROM HOST " + c + ":\n");
						clients[i].active = false;
						break;
					}
				}
			}

			// ADDQUERY
			else if (msg->match("/addQuery")){
				Message::ArgReader arg(msg->arg()); // reader for arguments
				KinectQuery kq; // new query obect

				if (arg.isStr()) // first value should be string
				{
					std::string firstString;
					arg.popStr(firstString);
					// figure out query type
					if (firstString == "joint") kq.type = 0;
					else if (firstString == "trigger") kq.type = 1;
					else if (firstString == "toggle") kq.type = 2;
					else if (firstString == "controller") kq.type = 3;
					else if (firstString == "motion") kq.type = 4;
					printFucker("query type: " + std::to_string(kq.type) + "\n");
				}

				// parse remaining args based on type

				// joint
//.........这里部分代码省略.........
开发者ID:rev3rend,项目名称:ITPKinect2OSC,代码行数:101,代码来源:TransmitData.cpp


注:本文中的PacketReader::init方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。