|
1 |
| -# py/pyext - python script objects for PD and MaxMSP |
2 |
| -# |
3 |
| -# Copyright (c) 2002-2005 Thomas Grill (gr@grrrr.org) |
4 |
| -# For information on usage and redistribution, and for a DISCLAIMER OF ALL |
5 |
| -# WARRANTIES, see the file, "license.txt," in this distribution. |
6 |
| -# |
7 |
| - |
8 |
| -"""This is an example script for the py/pyext object's buffer support. |
9 |
| -
|
10 |
| -PD/Max buffers can be mapped to Python arrays. |
11 |
| -Currently, there are three implementations: |
12 |
| -Numeric, numarray and Numeric3 (for all of them see http://numeric.scipy.org) |
13 |
| -""" |
14 |
| - |
15 |
| -import sys |
16 |
| - |
17 |
| -try: |
18 |
| - import pyext |
19 |
| -except: |
20 |
| - print "ERROR: This script must be loaded by the PD/Max py/pyext external" |
21 |
| - |
22 |
| -try: |
23 |
| - from numarray import * |
24 |
| -except: |
25 |
| - print "Failed importing numarray module:",sys.exc_value |
26 |
| - |
27 |
| -def mul(*args): |
28 |
| - # create buffer objects |
29 |
| - # as long as these variables live the underlying buffers are locked |
30 |
| - c = pyext.Buffer(args[0]) |
31 |
| - a = pyext.Buffer(args[1]) |
32 |
| - b = pyext.Buffer(args[2]) |
33 |
| - |
34 |
| - # slicing causes Python arrays (mapped to buffers) to be created |
35 |
| - # note the c[:] - to assign contents you must assign to a slice of the buffer |
36 |
| - c[:] = a[:]*b[:] |
37 |
| - |
38 |
| -def add(*args): |
39 |
| - c = pyext.Buffer(args[0]) |
40 |
| - a = pyext.Buffer(args[1]) |
41 |
| - b = pyext.Buffer(args[2]) |
42 |
| - |
43 |
| - # this is also possible, but is probably slower |
44 |
| - # the + converts a into a Python array, the argument b is taken as a sequence |
45 |
| - # depending on the implementation this may be as fast |
46 |
| - # as above or not |
47 |
| - c[:] = a+b |
48 |
| - |
49 |
| -def fadein(target): |
50 |
| - a = pyext.Buffer(target) |
51 |
| - # in place operations are ok |
52 |
| - a *= arange(len(a),type=Float32)/len(a) |
53 |
| - |
54 |
| -def neg(target): |
55 |
| - a = pyext.Buffer(target) |
56 |
| - # in place transformation (see Python array ufuncs) |
57 |
| - negative(a[:],a[:]) |
58 |
| - # must mark buffer content as dirty to update graph |
59 |
| - # (no explicit assignment occurred) |
60 |
| - a.dirty() |
| 1 | +# py/pyext - python script objects for PD and MaxMSP |
| 2 | +# |
| 3 | +# Copyright (c) 2002-2005 Thomas Grill (gr@grrrr.org) |
| 4 | +# For information on usage and redistribution, and for a DISCLAIMER OF ALL |
| 5 | +# WARRANTIES, see the file, "license.txt," in this distribution. |
| 6 | +# |
| 7 | + |
| 8 | +"""This is an example script for the py/pyext object's buffer support. |
| 9 | +
|
| 10 | +PD/Max buffers can be mapped to Python arrays. |
| 11 | +Currently, there are three implementations: |
| 12 | +Numeric, numarray and Numeric3 (for all of them see http://numeric.scipy.org) |
| 13 | +""" |
| 14 | + |
| 15 | +import sys |
| 16 | + |
| 17 | +try: |
| 18 | + import pyext |
| 19 | +except: |
| 20 | + print "ERROR: This script must be loaded by the PD/Max py/pyext external" |
| 21 | + |
| 22 | +try: |
| 23 | + from numarray import * |
| 24 | +except: |
| 25 | + print "Failed importing numarray module:",sys.exc_value |
| 26 | + |
| 27 | +def mul(*args): |
| 28 | + # create buffer objects |
| 29 | + # as long as these variables live the underlying buffers are locked |
| 30 | + c = pyext.Buffer(args[0]) |
| 31 | + a = pyext.Buffer(args[1]) |
| 32 | + b = pyext.Buffer(args[2]) |
| 33 | + |
| 34 | + # slicing causes Python arrays (mapped to buffers) to be created |
| 35 | + # note the c[:] - to assign contents you must assign to a slice of the buffer |
| 36 | + c[:] = a[:]*b[:] |
| 37 | + |
| 38 | +def add(*args): |
| 39 | + c = pyext.Buffer(args[0]) |
| 40 | + a = pyext.Buffer(args[1]) |
| 41 | + b = pyext.Buffer(args[2]) |
| 42 | + |
| 43 | + # this is also possible, but is probably slower |
| 44 | + # the + converts a into a Python array, the argument b is taken as a sequence |
| 45 | + # depending on the implementation this may be as fast |
| 46 | + # as above or not |
| 47 | + c[:] = a+b |
| 48 | + |
| 49 | +def fadein(target): |
| 50 | + a = pyext.Buffer(target) |
| 51 | + # in place operations are ok |
| 52 | + a *= arange(len(a),type=Float32)/len(a) |
| 53 | + |
| 54 | +def neg(target): |
| 55 | + a = pyext.Buffer(target) |
| 56 | + # in place transformation (see Python array ufuncs) |
| 57 | + negative(a[:],a[:]) |
| 58 | + # must mark buffer content as dirty to update graph |
| 59 | + # (no explicit assignment occurred) |
| 60 | + a.dirty() |
0 commit comments