pyrun Opcodes

pyrun — Run a Python statement or block of statements.

Syntax

pyrun "statement"
pyruni "statement"
pylrun "statement"
pylruni "statement"
pyrunt ktrigger, "statement"
pylrunt ktrigger, "statement"

Description

Execute the specified Python statement at k-time (pyrun and pylrun) or i-time (pyruni and pylruni).

The statement is executed in the global environment for pyrun and pyruni or the local environment for pylrun and pylruni.

These opcodes perform no message passing. However, since the statement have access to the main namespace and the private namespace, it can interact with objects previously created in that environment.

The "local" version of the pyrun opcodes are useful when the code ran by different instances of an instrument should not interact.

Example of the pyrun Opcode Group

Example 706. Orchestra

sr=44100
kr=4410
ksmps=10
nchnls=1

;If you're not running CsoundAC you need the following line
;to initialize the python interpreter
;pyinit

pyruni "import random"

instr 1
        ; This message is stored in the main namespace
        ; and is the same for every instance
        pyruni  "message = 'a global random number: %f' % random.random()"
        pyrun   "print message"

        ; This message is stored in the private namespace
        ; and is different for different instances
        pylruni "message = 'a private random number: %f' % random.random()"
        pylrun  "print message"
endin

Example 707. Score

i1 0 0.1

Running this score you should get intermixed pairs of messages from the two instances of instrument 1.

The first message of each pair is stored into the main namespace and so the second instance overwrites the message of the first instance. The result is that first message will be the same for both instances.

The second message is different for the two instances, being stored in the private namespace.

Credits

Copyright (c) 2002 by Maurizio Umberto Puxeddu. All rights reserved. Portions copyright (c) 2004 and 2005 by Michael Gogins. This document has been updated Sunday 25 July 2004 and 1 February 2005 by Michael Gogins.