Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Got the printHook callback working and also in the Python demo. Added…

… runme scripts for Java and Python to simplify using them.
  • Loading branch information...
commit 16edca8b6d4a1b2d516f90893f2c1a66011176a0 1 parent afc6572
chrism authored
View
BIN  libs/Darwin-i386/libjnizengarden.jnilib
Binary file not shown
View
BIN  libs/Darwin-i386/libzengarden.dylib
Binary file not shown
View
5 pyExampleGarden.py
@@ -26,6 +26,11 @@
zg = pyZenGarden(filename, "pd-patches", BLOCKSIZE, CHANNELS, CHANNELS, 22050)
zg.prepare()
+def myPrintHook(instring):
+ print "Python from Pd -> ", instring,
+
+zg.setPrintHook(myPrintHook)
+
#s = pygame.mixer.Sound("dubstep.ogg")
s = pygame.sndarray.make_sound(zeros((BLOCKSIZE, CHANNELS)))
a = pygame.sndarray.samples(s)
View
11 runme-java.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+if [ "$1" == "" ]
+then
+ patch=pd-patches/simple_osc.pd
+else
+ patch=$1
+fi
+
+java -Djava.library.path=./libs/`./src/platform`/ -jar ZenGarden.jar $patch
+
View
10 runme-python.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ "$1" == "" ]
+then
+ patch=pd-patches/simple_osc.pd
+else
+ patch=$1
+fi
+
+LD_LIBRARY_PATH=./libs/`./src/platform`/ DYLD_LIBRARY_PATH=./libs/`./src/platform`/ python pyExampleGarden.py $patch
View
15 src/MessagePrint.cpp
@@ -34,17 +34,26 @@ MessagePrint::MessagePrint(char *name, PdGraph *pdGraph, char *initString) : Mes
}
MessagePrint::~MessagePrint() {
- // nothing to do
+ free(this->name);
}
void MessagePrint::processMessage(int inletIndex, PdMessage *message) {
if (inletIndex == 0) {
+ char *full = NULL;
char *out = message->toString();
+
if (this->name) {
- printf("%s: %s\n", name, out);
+ int len = strlen(out) + strlen(this->name) + 5;
+ full = (char *)malloc(len);
+ snprintf(full, len, "%s: %s\n", name, out);
} else {
- printf("%s\n", out);
+ int len = strlen(out) + 2;
+ full = (char *)malloc(len);
+ snprintf(full, len, "%s\n", out);
}
+
+ pdGraph->printHook(full);
+ free(full);
free(out);
}
}
View
19 src/PdGraph.cpp
@@ -69,6 +69,8 @@ PdGraph::PdGraph(FILE *fp, char *directory, char *libraryDirectory, int blockSiz
this->directory = StaticUtils::copyString(directory);
+ this->printHook = defaultPrintHook;
+
nodeList = new List();
adcList = new List();
dacList = new List();
@@ -437,22 +439,39 @@ void PdGraph::process(float *audioInput, float *audioOutput) {
}
}
+void PdGraph::setPrintHook(void(*printHookIn)(char *)) {
+ printHook=printHookIn;
+}
+
/* Expose our PdGraph object with a pure C interface */
extern "C" {
+ /** Create a new ZenGarden graph to evaluate Pd patches */
PdGraph *NewPdGraph(char *directory, char *filename, char *libraryDirectory, int blockSize, int numInputChannels, int numOutputChannels, int sampleRate) {
return PdGraph::newInstance(directory, filename, libraryDirectory, blockSize, numInputChannels, numOutputChannels, sampleRate);
}
+ /** Destroy an existing ZenGarden graph */
void DeletePdGraph(PdGraph *pdGraph) {
delete pdGraph;
}
+ /** Init function to be called after adding objects to the graph and before processing */
void PrepareForProcessingPdGraph(PdGraph *pdGraph) {
pdGraph->prepareForProcessing();
}
+ /** Call this every frame to put and get audio data into and out of the graph */
void ProcessPdGraph(PdGraph *pdGraph, float *audioInput, float *audioOutput) {
pdGraph->process(audioInput, audioOutput);
}
+
+ void defaultPrintHook(char *incoming) {
+ printf(incoming);
+ }
+
+ /** Set a callback which accepts a char* and does something with the results of all Pd [print] objects. */
+ void SetPrintHook(PdGraph *pdGraph, void(*printHookIn)(char *)) {
+ pdGraph->setPrintHook(printHookIn);
+ }
}
View
11 src/PdGraph.h
@@ -56,6 +56,12 @@ class PdGraph : public PdNodeInterface {
void process(float *inputBuffer, float *outputBuffer);
+ /**
+ * Callbacks to interface with the outside world.
+ */
+ void setPrintHook(void(*printHookIn)(char *));
+ void (*printHook)(char *);
+
private:
PdGraph(FILE *fp, char *directory, char *libraryDirectory, int blockSize, int numInputChannels, int numOutputChannels, int sampleRate);
List *flatten();
@@ -87,6 +93,11 @@ class PdGraph : public PdNodeInterface {
List *subgraphList;
List *orderedEvaluationList; // an ordered list of the object to be evaluated
+
};
+extern "C" {
+ void defaultPrintHook(char *incoming);
+}
+
#endif // _PD_GRAPH_H_
View
1  src/PdObject.cpp
@@ -558,7 +558,6 @@ PdObject *PdObject::newInstance(char *objectType, char *objectInitString, int bl
return new MessagePowToDb(objectInitString);
} else if (strcmp("print", token) == 0) {
token = strtok(NULL, " ");
- printf("token: %s\n", token);
if (token == NULL) {
return new MessagePrint("print", pdGraph, objectInitString);
} else {
View
7 src/pyZenGarden.py
@@ -1,4 +1,4 @@
-from ctypes import *
+from ctypes import cdll, CFUNCTYPE, c_float, c_char, c_char_p
from os import path, sep
from sys import platform
@@ -38,6 +38,11 @@ def prepare(self):
def process(self):
self.zg.ProcessPdGraph(self.g, self.inBlock, self.outBlock)
+
+ def setPrintHook(self, fn):
+ PRINT_HOOK_FUNC = CFUNCTYPE(None, c_char_p)
+ printHook = PRINT_HOOK_FUNC(fn)
+ self.zg.SetPrintHook(self.g, printHook)
if __name__ == "__main__":
import doctest
Please sign in to comment.
Something went wrong with that request. Please try again.