bool nextOSCPacket(FILE *file, OSC_Packet *packet, int64& outTime) { int32 msglen; if (fread(&msglen, 1, sizeof(int32), file) != sizeof(int32)) return true; // msglen is in network byte order msglen = OSCint((char*)&msglen); if (msglen > 1073741824){ throw std::runtime_error("OSC packet too long. > 2^30 bytes\n"); } packet->mData = (char *)realloc((void *)packet->mData, (size_t)msglen); if(!packet->mData) throw std::runtime_error("nextOSCPacket: realloc failed...\n"); size_t read = fread(packet->mData, 1, msglen, file); if (read != msglen) throw std::runtime_error("nextOSCPacket: invalid read of OSC packet\n"); if (strcmp(packet->mData, "#bundle")!=0) throw std::runtime_error("OSC packet not a bundle\n"); packet->mSize = msglen; outTime = OSCtime(packet->mData+8); return false; }
void PerformOSCBundle(int inSize, char* inData, PyrObject *replyObj) { // convert all data to arrays int64 oscTime = OSCtime(inData + 8); double seconds = OSCToElapsedTime(oscTime); VMGlobals *g = gMainVMGlobals; ++g->sp; SetObject(g->sp, g->process); ++g->sp; SetFloat(g->sp, seconds); ++g->sp; SetObject(g->sp, replyObj); PyrSlot *stackBase = g->sp; char *data = inData + 16; char* dataEnd = inData + inSize; while (data < dataEnd) { int32 msgSize = OSCint(data); data += sizeof(int32); PyrObject *arrayObj = ConvertOSCMessage(msgSize, data); ++g->sp; SetObject(g->sp, arrayObj); data += msgSize; } int numMsgs = g->sp - stackBase; runInterpreter(g, s_recvoscbndl, 3+numMsgs); }
void PerformOSCBundle(int inSize, char* inData, PyrObject *replyObj, int inPortNum) { // convert all data to arrays int64 oscTime = OSCtime(inData + 8); double seconds = OSCToElapsedTime(oscTime); VMGlobals *g = gMainVMGlobals; char *data = inData + 16; char* dataEnd = inData + inSize; while (data < dataEnd) { int32 msgSize = OSCint(data); data += sizeof(int32); if (IsBundle(data)) { PerformOSCBundle(msgSize, data, replyObj, inPortNum); } else // is a message { ++g->sp; SetObject(g->sp, g->process); ++g->sp; SetFloat(g->sp, seconds); ++g->sp; SetObject(g->sp, replyObj); ++g->sp; SetInt(g->sp, inPortNum); PyrObject *arrayObj = ConvertOSCMessage(msgSize, data); ++g->sp; SetObject(g->sp, arrayObj); runInterpreter(g, s_recvoscmsg, 5); } data += msgSize; } }
void dumpOSC(int mode, int size, char* inData) { if (mode & 1) { if (strcmp(inData, "#bundle") == 0) { char* data = inData + 8; printf("[ \"#bundle\", %lld, ", OSCtime(data)); data += 8; char* dataEnd = inData + size; while (data < dataEnd) { int32 msgSize = OSCint(data); data += sizeof(int32); printf("\n "); dumpOSCmsg(msgSize, data); data += msgSize; if (data < dataEnd) printf(","); } printf("\n]\n"); } else { dumpOSCmsg(size, inData); printf("\n"); } } if (mode & 2) hexdump(size, inData); }
static bool dumpOSCbndl(int indent, int size, char *inData) { char* data = inData + 8; char* dataEnd = inData + size; scprintf("[ \"#bundle\", %" PRIu64 ", ", OSCtime(data)); data += 8; while (data < dataEnd) { int contentPrinted; int32 msgSize = OSCint(data); data += sizeof(int32); scprintf("\n"); for (int i=0; i<indent+1; i++) scprintf(" "); if (!strcmp(data, "#bundle")) contentPrinted = dumpOSCbndl(indent+1, msgSize, data); else contentPrinted = dumpOSCmsg(msgSize, data, true); data += msgSize; if ( (data < dataEnd) && contentPrinted) scprintf(","); } scprintf("\n"); for (int i=0; i<indent; i++) scprintf(" "); scprintf("]"); return true; }
void dumpOSCmsg(int inSize, char* inData) { int size; char *data; if (inData[0]) { char *addr = inData; data = OSCstrskip(inData); size = inSize - (data - inData); scprintf("[ \"%s\",", addr); } else { scprintf("[ %d,", OSCint(inData)); data = inData + 4; size = inSize - 4; } sc_msg_iter msg(size, data); while (msg.remain()) { char c = msg.nextTag('i'); switch(c) { case 'i' : scprintf(" %d", msg.geti()); break; case 'f' : scprintf(" %g", msg.getf()); break; case 'd' : scprintf(" %g", msg.getd()); break; case 's' : scprintf(" \"%s\"", msg.gets()); break; case 'b' : scprintf(" DATA[%d]", msg.getbsize()); msg.skipb(); break; case '[' : scprintf("["); msg.count++; break; case ']' : scprintf("]"); msg.count++; break; default : scprintf(" !unknown tag '%c' 0x%02x !", isprint(c)?c:'?', (unsigned char)c & 255); goto leave; } if (msg.remain() && (c!= '[')) scprintf(","); } leave: scprintf(" ]"); }
bool nextOSCPacket(FILE *file, OSC_Packet *packet, int64& outTime) { int32 msglen; if (!fread(&msglen, 1, sizeof(int32), file)) return true; // msglen is in network byte order msglen = OSCint((char*)&msglen); if (msglen > 8192) throw std::runtime_error("OSC packet too long. > 8192 bytes\n"); fread(packet->mData, 1, msglen, file); if (strcmp(packet->mData, "#bundle")!=0) throw std::runtime_error("OSC packet not a bundle\n"); packet->mSize = msglen; outTime = OSCtime(packet->mData+8); return false; }
void ConvertOSCBundle(int inSize, char* inData, PyrObject *replyObj) { // convert all data to arrays //int64 oscTime = OSCtime(inData + 8); //double seconds = OSCToElapsedTime(oscTime); VMGlobals *g = gMainVMGlobals; int numMsgs = 0; char *data = inData + 16; char* dataEnd = inData + inSize; while (data < dataEnd) { int32 msgSize = OSCint(data); data += sizeof(int32); PyrObject *arrayObj = ConvertOSCMessage(msgSize, data); ++g->sp; SetObject(g->sp, arrayObj); numMsgs++; data += msgSize; } }
jobject convertMessageToJava(JNIEnv* myEnv, char* inData, int inSize) { jclass oscMessageClass = myEnv->FindClass(OSC_MESSAGE_CLASS); if (!oscMessageClass) { scprintf("convertMessageToJava could not find the JAVA OSC representation"); return NULL; } if (inSize<=0 || !inData) { return NULL; } jmethodID oscConstructor = myEnv->GetMethodID(oscMessageClass, "<init>", "()V"); if (!oscConstructor) { scprintf("convertMessageToJava could not find a constructor for the JAVA OSC representation"); return NULL; } jobject oscObject = myEnv->NewObject(oscMessageClass, oscConstructor); jmethodID addInt = myEnv->GetMethodID(oscMessageClass, "add", "(I)Z"); jmethodID addStr = myEnv->GetMethodID(oscMessageClass, "add", "(Ljava/lang/String;)Z"); jmethodID addFlt = myEnv->GetMethodID(oscMessageClass, "add", "(F)Z"); jmethodID addLng = myEnv->GetMethodID(oscMessageClass, "add", "(J)Z"); // Did I steal this wholesale from dumpOSCmsg? Yes I did. -ajs 20100826 char * data; int size; if (inData[0]) { char *addr = inData; data = OSCstrskip(inData); size = inSize - (data - inData); jstring jaddr = myEnv->NewStringUTF(addr); myEnv->CallBooleanMethod(oscObject,addStr,jaddr); } else { myEnv->CallBooleanMethod(oscObject,addInt,OSCint(inData)); data = inData + 4; size = inSize - 4; } sc_msg_iter msg(size, data); bool ok(true); while (msg.remain() && ok) { char c = msg.nextTag('i'); jstring jstr; switch(c) { case 'i' : myEnv->CallBooleanMethod(oscObject,addInt,msg.geti()); break; case 'f' : myEnv->CallBooleanMethod(oscObject,addFlt,msg.getf()); break; case 's' : jstr = myEnv->NewStringUTF(msg.gets()); myEnv->CallBooleanMethod(oscObject,addStr,jstr); break; default : scprintf("convertMessageToJava unknown/unimplemented tag '%c' 0x%02x", isprint(c)?c:'?', (unsigned char)c & 255); ok = false; break; } } return oscObject; }
static bool dumpOSCmsg(int inSize, char* inData, bool skipStatus = false) { int size; const char *data; if (inData[0]) { const char *addr = inData; if (skipStatus) { if (strcmp(addr, "/status") == 0 || strcmp(addr, "status") == 0) // skip /status messages return false; // nothing has been printed } data = OSCstrskip(inData); size = inSize - (data - inData); scprintf("[ \"%s\",", addr); } else { scprintf("[ %d,", OSCint(inData)); data = inData + 4; size = inSize - 4; } sc_msg_iter msg(size, data); while (msg.remain()) { char c = msg.nextTag('i'); switch(c) { case 'i' : scprintf(" %d", msg.geti()); break; case 'f' : scprintf(" %g", msg.getf()); break; case 'd' : scprintf(" %g", msg.getd()); break; case 's' : scprintf(" \"%s\"", msg.gets()); break; case '[' : scprintf(" ["); msg.count ++; break; case ']' : scprintf(" ]"); msg.count ++; break; case 'b' : scprintf(" DATA[%zu]", msg.getbsize()); msg.skipb(); break; case 'm' : { char midi [4]; msg.getb (midi, 4); scprintf(" MIDI[0x%02x 0x%02x 0x%02x 0x%02x]", midi[0], midi[1], midi[2], midi[3]); break; } case 'c' : scprintf(" %c", (char)msg.geti()); break; case 't' : scprintf(" %" PRId64 "", msg.gett()); break; case 'T' : scprintf(" true"); msg.count ++; break; case 'F' : scprintf(" false"); msg.count ++; break; case 'I' : scprintf(" infinitum"); msg.count ++; break; case 'N' : scprintf(" nil"); msg.count ++; break; default : scprintf(" !unknown tag '%c' 0x%02x !", isprint(c)?c:'?', (unsigned char)c & 255); goto leave; } if (msg.remain() && (c!= '[')) scprintf(","); } leave: scprintf(" ]"); return true; // something has been printed }