Monday, 5 September 2011

[Pd] py / pyext

Finally I am back and relatively sane after being in Edinburgh for over two weeks performing every night. Whilst I was there I was trying to find out an easier, or at least more conventional, way of writing logic in Pd. Originally I was looking for a Perl plugin / extension but there are none of these, but there is one for Python. Not being a great Python programmer I wasn't massively keen, but in my past life as a programmer at Getty Images I had written some Python CGI scripts for querying a (huge) Oracle database, so, apart from being used to programming in Java and Perl and other bits and pieces of C based languages and therefore more than prone to slamming a ";" at the end of every line and using curly braces (whilst also indenting properly it can be a bit weird(sometimes, when you can dothis too){and then do something else on the same line;} it seems a very nice language.

I had some initial problems installing the py/pyext library, probably due to not doing an RTFM and ended up Finking, Porting different versions of Python, compiling one using gcc along with compiling Flext and py/pyext from scratch too. In the end, no matter what I did to my environment variables either in OS X or Pd, py/pyext would only load and, more importantly work, when I used the library I had compiled with the version of Python that the library wanted (which is the preinstalled Apple build of Python 2.5.1). No matter what I did py/pyext wanted that Apple build so it must have hardcoded paths to the Apple Python Framework stuff - even changing where the symbolic link to python was pointing didn't help (thankfully it didn't break anything either).

Anyway I got there in the end and played around a little bit with it, although as the patch above more than suggests I haven't got very far with it yet. The Python code is:

import pyext
import sys

class trythis(pyext._class):
    _inlets=3
    _outlets=4

    def _anything_1(self, *args):
        for x in args:
            if x % 2 == 0:
                self._outlet(1) 
            elif x % 3 == 0:
                self._outlet(2)
            elif x % 4 == 0:
                self._outlet(3)
            else:
                self._outlet(4)

...which does nothing clever at all. It just takes a Pd list which py/pyext turns into a Python array then does a modulus on each number and outputs a [o] where relevant. The [mksnd] sub patches just output a bleep at nice intervals with a shortish [adsr~] envelope, doesn't sound interesting but it's sort of nice.

Also, there is a mismatch between inputs and outs - sort of. If you wanted to route a message straight through from input 0 to output 0 it wouldn't happen - there is no output 0 (as far as I can see) the first output is output 1... I am going to look at the C API for this!

It's ok though Pd still rocks and Thomas Grill knows what he is doing. I don't.

No comments:

Post a Comment