aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/scripts
diff options
context:
space:
mode:
authorThomas Grill <xovo@users.sourceforge.net>2005-03-10 04:59:00 +0000
committerThomas Grill <xovo@users.sourceforge.net>2005-03-10 04:59:00 +0000
commitec67ac9af1c9a02130347c4ed6f32fa03f55eba2 (patch)
treed2a1f6e129e9179c695ec6c0f5c3b9dffa46815c /externals/grill/py/scripts
parent5e3101f8a1a5cf78f86635da6a78d8564aa85483 (diff)
more buffer functionality (support sequence and number protocols)
more examples preset sys.argv for module loading support for buffer objects (preliminary) svn path=/trunk/; revision=2615
Diffstat (limited to 'externals/grill/py/scripts')
-rw-r--r--externals/grill/py/scripts/buffer.py50
1 files changed, 32 insertions, 18 deletions
diff --git a/externals/grill/py/scripts/buffer.py b/externals/grill/py/scripts/buffer.py
index 4c81caab..19b6f5d3 100644
--- a/externals/grill/py/scripts/buffer.py
+++ b/externals/grill/py/scripts/buffer.py
@@ -7,12 +7,9 @@
"""This is an example script for the py/pyext object's buffer support.
-PD/Max buffers can be imported to and exported from numarray arrays.
+PD/Max buffers can be mapped to numarray arrays.
For numarray see http://numeric.scipy.org
-It will probably once be replaced by Numeric
-
-- _import(buffer): copy contents from the buffer to a new numarray object
-- _export(buffer,numarray): export contents of numarray object to the buffer
+It will probably once be replaced by Numeric(3)
"""
import sys
@@ -20,7 +17,7 @@ import sys
try:
import pyext
except:
- print "ERROR: This script must be loaded by the PD/Max pyext external"
+ print "ERROR: This script must be loaded by the PD/Max py/pyext external"
try:
from numarray import *
@@ -28,19 +25,36 @@ except:
print "Failed importing numarray module:",sys.exc_value
def mul(*args):
+ # create buffer objects
+ # as long as these variables live the underlying buffers are locked
c = pyext.Buffer(args[0])
- dst = c.array()
- dst[:] = 0
- a = pyext.Buffer(args[1]).array()
- b = pyext.Buffer(args[2]).array()
- dst += a*b
- c.dirty()
+ a = pyext.Buffer(args[1])
+ b = pyext.Buffer(args[2])
+
+ # slicing causes numarrays (mapped to buffers) to be created
+ # note the c[:] - to assign contents you must assign to a slice of the buffer
+ c[:] = a[:]*b[:]
def add(*args):
c = pyext.Buffer(args[0])
- dst = c.array()
- dst[:] = 0
- a = pyext.Buffer(args[1]).array()
- b = pyext.Buffer(args[2]).array()
- dst += a+b
- c.dirty()
+ a = pyext.Buffer(args[1])
+ b = pyext.Buffer(args[2])
+
+ # this is also possible, but is probably slower
+ # the + converts a into a numarray, the argument b is taken as a sequence
+ # depending on the implementation in numarray this may be as fast
+ # as above or not
+ c[:] = a+b
+
+def fadein(target):
+ a = pyext.Buffer(target)
+ # in place operations are ok
+ a *= arange(len(a),type=Float32)/len(a)
+
+def neg(target):
+ a = pyext.Buffer(target)
+ # in place transformation (see numarray ufuncs)
+ negative(a[:],a[:])
+ # must mark buffer content as dirty to update graph
+ # (no explicit assignment occurred)
+ a.dirty()