Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Python + pygame example for interfacing with ZenGarden

  • Loading branch information...
commit 5c34c9de94ebda98ab8fa99ae2ffb5d417dc037f 1 parent b45e04e
chrism authored
View
1  .gitignore
@@ -11,3 +11,4 @@ Xcode/Xcode.xcodeproj/mhroth.pbxuser
Xcode/Xcode.xcodeproj/mhroth.mode1v3
src/main.cpp
.*.swp
+*.pyc
View
BIN  ZenGarden.jar
Binary file not shown
View
17 examples/wobbly.pd
@@ -0,0 +1,17 @@
+#N canvas 312 136 450 300 10;
+#X obj 115 248 dac~;
+#X obj 124 195 osc~ 440;
+#X obj 125 152 *~ 50;
+#X obj 125 173 +~ 440;
+#X obj 125 131 osc~ 0.5;
+#X obj 143 32 osc~ 0.37123;
+#X obj 141 82 +~ 0.5;
+#X obj 141 59 *~ 0.25;
+#X connect 1 0 0 0;
+#X connect 1 0 0 1;
+#X connect 2 0 3 0;
+#X connect 3 0 1 0;
+#X connect 4 0 2 0;
+#X connect 5 0 7 0;
+#X connect 6 0 4 0;
+#X connect 7 0 6 0;
View
BIN  libs/Darwin-i386/libjnizengarden.jnilib
Binary file not shown
View
49 pyExampleGarden.py
@@ -0,0 +1,49 @@
+import sys
+sys.path.append("src")
+
+from time import sleep
+
+from Numeric import zeros
+import pygame
+from pygame import locals
+
+from pyZenGarden import pyZenGarden
+
+BLOCKSIZE = 256
+CHANNELS = 2
+
+# initialise the pygame mixer
+pygame.mixer.init(22050, -16, CHANNELS, BLOCKSIZE)
+pygame.init()
+
+# if the user gives us a pd file, load that instead
+if len(sys.argv) == 2:
+ filename = sys.argv[1]
+else:
+ filename = "pd-patches/simple_osc.pd"
+
+# initialise zengarden with our Pd file
+zg = pyZenGarden(filename, "pd-patches", BLOCKSIZE, CHANNELS, CHANNELS, 22050)
+zg.prepare()
+
+#s = pygame.mixer.Sound("dubstep.ogg")
+s = pygame.sndarray.make_sound(zeros((BLOCKSIZE, CHANNELS)))
+a = pygame.sndarray.samples(s)
+#a[0][0] = pow(2, 16) / 2
+
+c = pygame.mixer.find_channel()
+c.set_endevent(locals.USEREVENT)
+
+s_ranger = xrange(BLOCKSIZE)
+c_ranger = xrange(CHANNELS)
+
+c.play(s)
+
+while 1:
+ if pygame.event.get(locals.USEREVENT):
+ zg.process()
+ for x in c_ranger:
+ for y in s_ranger:
+ a[y][x] = zg.outBlock[y + x * BLOCKSIZE] * (pow(2, 16) / 2 - 1)
+ c.play(s)
+
View
36 src/pyZenGarden.py
@@ -0,0 +1,36 @@
+from ctypes import *
+from os import path, sep
+
+class pyZenGarden:
+ def __init__(self, pdFile, libraryPath, blockSize, inChannels, outChannels, sampleRate):
+ """
+ Interface to the ZenGarden Pd patch processing library.
+
+ >>> zg = pyZenGarden("../pd-patches/simple_osc.pd", "../pd-patches", 64, 2, 2, 22050)
+ >>> zg.prepare()
+ Ordered evaluation list (2/2 active objects):
+ osc~
+ dac~
+ >>> zg.process()
+ >>> print [s for s in zg.outBlock[:5]]
+ [1.0, 0.99215036630630493, 0.96872472763061523, 0.93009084463119507, 0.87685519456863403]
+ """
+ self.zg = cdll.LoadLibrary("libs/Darwin-i386/libzengarden.dylib")
+ filename = path.basename(pdFile)
+ directory = path.dirname(pdFile)
+ self.g = self.zg.NewPdGraph(directory + sep, filename, libraryPath + sep, blockSize, inChannels, outChannels, sampleRate)
+ t_inBlock = c_float * (inChannels * blockSize)
+ self.inBlock = t_inBlock()
+ t_outBlock = c_float * (outChannels * blockSize)
+ self.outBlock = t_outBlock()
+
+ def prepare(self):
+ self.zg.PrepareForProcessingPdGraph(self.g)
+
+ def process(self):
+ self.zg.ProcessPdGraph(self.g, self.inBlock, self.outBlock)
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()
+
Please sign in to comment.
Something went wrong with that request. Please try again.