int lua_pdcontext_open(lua_State * L) { PdSynth * self = SynthBinding<PdSynth>::checkto(L, 1); PdGraph * graph = NULL; if (lua_gettop(L) > 1) { const char * filepath = luaL_checkstring(L, 2); // todo: check that filepath terminates with a slash... const char * filename = luaL_checkstring(L, 3); graph = zg_new_graph(self->pd(), (char *)filepath, (char *)filename); } else { // just an empty graph graph = zg_new_empty_graph(self->pd()); } if (graph) { zg_attach_graph(self->pd(), graph); } //return Glue<PdGraph>::create(L); return 0; }
JNIEXPORT jlong JNICALL Java_me_rjdj_zengarden_ZenGarden_loadPdPatch( JNIEnv *env, jobject jobj, jstring jdirectory, jstring jfilename, jint blockSize, jint numInputChannels, jint numOutputChannels, jfloat sampleRate) { PdGraph *pdGraph = NULL; char *cdirectory = (char *) env->GetStringUTFChars(jdirectory, NULL); char *cfilename = (char *) env->GetStringUTFChars(jfilename, NULL); pdGraph = zg_new_graph(cdirectory, cfilename, blockSize, numInputChannels, numOutputChannels, sampleRate); env->ReleaseStringUTFChars(jdirectory, cdirectory); env->ReleaseStringUTFChars(jfilename, cfilename); if (pdGraph == NULL) { env->ThrowNew(env->FindClass("me/rjdj/zengarden/NativeLoadException"), "PdGraph is NULL. Is the filename correct? Does the file exist? Are all of the referenced objects implemented?"); return (jlong) 0; } // initialise the PureDataMobile native variables PureDataMobileNativeVars *pdmnv = (PureDataMobileNativeVars *) malloc(sizeof(PureDataMobileNativeVars)); pdmnv->pdGraph = pdGraph; pdmnv->finputBuffer = (float *) malloc(blockSize * numInputChannels * sizeof(float)); pdmnv->foutputBuffer = (float *) malloc(blockSize * numOutputChannels * sizeof(float)); pdmnv->blockSize = blockSize; pdmnv->numInputChannels = numInputChannels; pdmnv->numOutputChannels = numOutputChannels; pdmnv->shortToFloatLookupTable = (float *) malloc(32768 * sizeof(float)); pdmnv->numBytesInBlock = blockSize * sizeof(float); // register the callback pdmnv->zgObject = env->NewGlobalRef(jobj); zg_register_callback(pdGraph, java_callback, pdmnv->zgObject); // define the input (microphone) waveshaper // linear for (int i = 0; i < 32768; i++) { pdmnv->shortToFloatLookupTable[i] = ((float) i) / 32767.0f; } return (jlong) pdmnv; }
int main(int argc, char * const argv[]) { const int blockSize = 64; const int numInputChannels = 2; const int numOutputChannels = 2; const float sampleRate = 22050.0f; // pass directory and filename of the patch to load PdContext *context = zg_new_context(numInputChannels, numOutputChannels, blockSize, sampleRate, callbackFunction, NULL); PdGraph *graph = zg_new_graph(context, "/Users/mhroth/workspace/ZenGarden/test/", "MessageMessageBox.pd"); if (graph == NULL) { zg_delete_context(context); return 1; } zg_attach_graph(context, graph); float *inputBuffers = (float *) calloc(numInputChannels * blockSize, sizeof(float)); float *outputBuffers = (float *) calloc(numOutputChannels * blockSize, sizeof(float)); timeval start, end; gettimeofday(&start, NULL); for (int i = 0; i < NUM_ITERATIONS; i++) { zg_process(context, inputBuffers, outputBuffers); } gettimeofday(&end, NULL); double elapsedTime = (end.tv_sec - start.tv_sec) * 1000.0; // sec to ms elapsedTime += (end.tv_usec - start.tv_usec) / 1000.0; // us to ms printf("Runtime is: %i iterations in %f milliseconds == %f iterations/second.\n", NUM_ITERATIONS, elapsedTime, ((double) NUM_ITERATIONS)*1000.0/elapsedTime); double simulatedTime = ((double) blockSize / (double) sampleRate) * (double) NUM_ITERATIONS * 1000.0; // milliseconds printf("Runs in realtime: %s (x%.3f)\n", (simulatedTime >= elapsedTime) ? "YES" : "NO", simulatedTime/elapsedTime); zg_delete_context(context); free(inputBuffers); free(outputBuffers); return 0; }
void ofxZenGarden::load(string patchFile, int inputs, int outputs, int samplerate, int bufferSize) { patchFile = ofToDataPath(patchFile); running = false; hasADC = false; blockSize = bufferSize; numInputChannels = inputs; numOutputChannels = outputs; sampleRate = samplerate; path = patchFile; if(inputBuffer!=NULL) delete [] inputBuffer; if(outputBuffer!=NULL) delete [] outputBuffer; outputBuffer = new float[numOutputChannels*blockSize]; inputBuffer = new float[numInputChannels*blockSize]; char file[256]; char dir[512]; getDirAndFile(patchFile.c_str(), dir, file); if(context!=NULL) { delete context; context = NULL; } printf("Starting graph with in: %d out: %d sr: %d bs: %d\n", numInputChannels, numOutputChannels, sampleRate, blockSize); context = zg_new_context(numInputChannels, numOutputChannels, blockSize, sampleRate, callbackFunction, NULL); ZGGraph *graph = zg_new_graph(context, dir, file); if (graph == NULL) { printf("PdGraph could not be created. Is the filename correct?\n"); zg_delete_context(context); return; } zg_attach_graph(context, graph); // look through the pd file and see if there's a dac~ in it string line; ifstream myfile (patchFile.c_str()); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); if(line.find("adc~;")!=-1) { hasADC = true; break; } } myfile.close(); } if(hasADC) { printf("Has input\n"); } else { printf("No input\n"); } running = true; }