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 localServerReplyFunc(struct ReplyAddress *inReplyAddr, char* inBuf, int inSize) { bool isBundle = IsBundle(inBuf); pthread_mutex_lock (&gLangMutex); if (compiledOK) { PyrObject *replyObj = ConvertReplyAddress(inReplyAddr); if (isBundle) { PerformOSCBundle(inSize, inBuf, replyObj); } else { PerformOSCMessage(inSize, inBuf, replyObj); } } pthread_mutex_unlock (&gLangMutex); }
void localServerReplyFunc(struct ReplyAddress *inReplyAddr, char* inBuf, int inSize) { bool isBundle = IsBundle(inBuf); gLangMutex.lock(); if (compiledOK) { PyrObject *replyObj = ConvertReplyAddress(inReplyAddr); if (isBundle) { PerformOSCBundle(inSize, inBuf, replyObj, gUDPport->RealPortNum()); } else { PerformOSCMessage(inSize, inBuf, replyObj, gUDPport->RealPortNum()); } } gLangMutex.unlock(); }
void ProcessOSCPacket(OSC_Packet* inPacket) { //post("recv '%s' %d\n", inPacket->mData, inPacket->mSize); inPacket->mIsBundle = IsBundle(inPacket->mData); pthread_mutex_lock (&gLangMutex); if (compiledOK) { PyrObject *replyObj = ConvertReplyAddress(&inPacket->mReplyAddr); if (compiledOK) { if (inPacket->mIsBundle) { PerformOSCBundle(inPacket->mSize, inPacket->mData, replyObj); } else { PerformOSCMessage(inPacket->mSize, inPacket->mData, replyObj); } } } pthread_mutex_unlock (&gLangMutex); FreeOSCPacket(inPacket); }
// takes ownership of inPacket void ProcessOSCPacket(OSC_Packet* inPacket, int inPortNum) { //post("recv '%s' %d\n", inPacket->mData, inPacket->mSize); inPacket->mIsBundle = IsBundle(inPacket->mData); gLangMutex.lock(); if (compiledOK) { PyrObject *replyObj = ConvertReplyAddress(&inPacket->mReplyAddr); if (compiledOK) { if (inPacket->mIsBundle) { PerformOSCBundle(inPacket->mSize, inPacket->mData, replyObj, inPortNum); } else { PerformOSCMessage(inPacket->mSize, inPacket->mData, replyObj, inPortNum); } } } gLangMutex.unlock(); FreeOSCPacket(inPacket); }
void World_NonRealTimeSynthesis(struct World *world, WorldOptions *inOptions) { if (inOptions->mLoadGraphDefs) { World_LoadGraphDefs(world); } int bufLength = world->mBufLength; int fileBufFrames = inOptions->mPreferredHardwareBufferFrameSize; if (fileBufFrames <= 0) fileBufFrames = 8192; int bufMultiple = (fileBufFrames + bufLength - 1) / bufLength; fileBufFrames = bufMultiple * bufLength; // batch process non real time audio if (!inOptions->mNonRealTimeOutputFilename) throw std::runtime_error("Non real time output filename is NULL.\n"); SF_INFO inputFileInfo, outputFileInfo; float *inputFileBuf = 0; float *outputFileBuf = 0; int numInputChannels = 0; int numOutputChannels; outputFileInfo.samplerate = inOptions->mPreferredSampleRate; numOutputChannels = outputFileInfo.channels = world->mNumOutputs; sndfileFormatInfoFromStrings(&outputFileInfo, inOptions->mNonRealTimeOutputHeaderFormat, inOptions->mNonRealTimeOutputSampleFormat); world->hw->mNRTOutputFile = sf_open(inOptions->mNonRealTimeOutputFilename, SFM_WRITE, &outputFileInfo); sf_command(world->hw->mNRTOutputFile, SFC_SET_CLIPPING, NULL, SF_TRUE); if (!world->hw->mNRTOutputFile) throw std::runtime_error("Couldn't open non real time output file.\n"); outputFileBuf = (float*)calloc(1, world->mNumOutputs * fileBufFrames * sizeof(float)); if (inOptions->mNonRealTimeInputFilename) { world->hw->mNRTInputFile = sf_open(inOptions->mNonRealTimeInputFilename, SFM_READ, &inputFileInfo); if (!world->hw->mNRTInputFile) throw std::runtime_error("Couldn't open non real time input file.\n"); inputFileBuf = (float*)calloc(1, inputFileInfo.channels * fileBufFrames * sizeof(float)); if (world->mNumInputs != (uint32)inputFileInfo.channels) scprintf("WARNING: input file channels didn't match number of inputs specified in options.\n"); numInputChannels = world->mNumInputs = inputFileInfo.channels; // force it. if (inputFileInfo.samplerate != (int)inOptions->mPreferredSampleRate) scprintf("WARNING: input file sample rate does not equal output sample rate.\n"); } else { world->hw->mNRTInputFile = 0; } FILE *cmdFile; if (inOptions->mNonRealTimeCmdFilename) { #ifdef _WIN32 cmdFile = fopen(inOptions->mNonRealTimeCmdFilename, "rb"); #else cmdFile = fopen(inOptions->mNonRealTimeCmdFilename, "r"); #endif } else cmdFile = stdin; if (!cmdFile) throw std::runtime_error("Couldn't open non real time command file.\n"); OSC_Packet packet; memset(&packet, 0, sizeof(packet)); packet.mData = (char *)malloc(8192); packet.mIsBundle = true; packet.mReplyAddr.mReplyFunc = null_reply_func; int64 schedTime; if (nextOSCPacket(cmdFile, &packet, schedTime)) throw std::runtime_error("command file empty.\n"); int64 prevTime = schedTime; World_SetSampleRate(world, inOptions->mPreferredSampleRate); World_Start(world); int64 oscTime = 0; double oscToSeconds = 1. / pow(2.,32.); double oscToSamples = inOptions->mPreferredSampleRate * oscToSeconds; int64 oscInc = (int64)((double)bufLength / oscToSamples); if(inOptions->mVerbosity >= 0) { printf("start time %g\n", schedTime * oscToSeconds); } bool run = true; int inBufStep = numInputChannels * bufLength; int outBufStep = numOutputChannels * bufLength; float* inputBuses = world->mAudioBus + world->mNumOutputs * bufLength; float* outputBuses = world->mAudioBus; int32* inputTouched = world->mAudioBusTouched + world->mNumOutputs; int32* outputTouched = world->mAudioBusTouched; for (; run;) { int bufFramesCalculated = 0; float* inBufPos = inputFileBuf; float* outBufPos = outputFileBuf; if (world->hw->mNRTInputFile) { int framesRead = sf_readf_float(world->hw->mNRTInputFile, inputFileBuf, fileBufFrames); if (framesRead < fileBufFrames) { memset(inputFileBuf + framesRead * numInputChannels, 0, (fileBufFrames - framesRead) * numInputChannels * sizeof(float)); } } for (int i=0; i<bufMultiple && run; ++i) { int bufCounter = world->mBufCounter; // deinterleave input to input buses if (inputFileBuf) { float *inBus = inputBuses; for (int j=0; j<numInputChannels; ++j, inBus += bufLength) { float *inFileBufPtr = inBufPos + j; for (int k=0; k<bufLength; ++k) { inBus[k] = *inFileBufPtr; inFileBufPtr += numInputChannels; } inputTouched[j] = bufCounter; } } // execute ready commands int64 nextTime = oscTime + oscInc; while (schedTime <= nextTime) { float diffTime = (float)(schedTime - oscTime) * oscToSamples + 0.5; float diffTimeFloor = floor(diffTime); world->mSampleOffset = (int)diffTimeFloor; world->mSubsampleOffset = diffTime - diffTimeFloor; if (world->mSampleOffset < 0) world->mSampleOffset = 0; else if (world->mSampleOffset >= bufLength) world->mSampleOffset = bufLength-1; PerformOSCBundle(world, &packet); if (nextOSCPacket(cmdFile, &packet, schedTime)) { run = false; break; } if(inOptions->mVerbosity >= 0) { printf("nextOSCPacket %g\n", schedTime * oscToSeconds); } if (schedTime < prevTime) { scprintf("ERROR: Packet time stamps out-of-order.\n"); run = false; goto Bail; } prevTime = schedTime; } World_Run(world); // interleave output to output buffer float *outBus = outputBuses; for (int j=0; j<numOutputChannels; ++j, outBus += bufLength) { float *outFileBufPtr = outBufPos + j; if (outputTouched[j] == bufCounter) { for (int k=0; k<bufLength; ++k) { *outFileBufPtr = outBus[k]; outFileBufPtr += numOutputChannels; } } else { for (int k=0; k<bufLength; ++k) { *outFileBufPtr = 0.f; outFileBufPtr += numOutputChannels; } } } bufFramesCalculated += bufLength; inBufPos += inBufStep; outBufPos += outBufStep; world->mBufCounter++; oscTime = nextTime; } Bail: // write output sf_writef_float(world->hw->mNRTOutputFile, outputFileBuf, bufFramesCalculated); } if (cmdFile != stdin) fclose(cmdFile); sf_close(world->hw->mNRTOutputFile); world->hw->mNRTOutputFile = 0; if (world->hw->mNRTInputFile) { sf_close(world->hw->mNRTInputFile); world->hw->mNRTInputFile = 0; } free(packet.mData); World_Cleanup(world,true); }