pyexec Opcodes

pyexec — Execute a script from a file at k-time or i-time (i suffix).

Syntax

pyexec "filename"
pyexeci "filename"
pylexec "filename"
pylexeci "filename"
pyexect ktrigger, "filename"
plyexect ktrigger, "filename"

Description

Execute a script from a file at k-time or i-time (i suffix).

This is not the same as calling the script with the system() call, since the code is executed by the embedded interpreter.

The code contained in the specified file is executed in the global environment for opcodes pyexec and pyexeci and in the private environment for the opcodes pylexec and pylexeci.

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

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

Example of the pyexec Opcode Group

Example 700. Orchestra (pyexec.orc)

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"

        pyexeci "pyexec1.py"

instr 1

        pyexec          "pyexec2.py"

        pylexeci        "pyexec3.py"
        pylexec         "pyexec4.py"

endin

Example 701. Score (pyexec.sco)

i1 0 0.01
i1 0 0.01

Example 702. The pyexec1.py Script

import time, os

print
print "Welcome to Csound!"

try:
    s = ', %s?' % os.getenv('USER')
except:
    s = '?'

print 'What sound do you want to hear today%s' % s
answer = raw_input()

Example 703. The pyexec2.py script

print 'your answer is "%s"' % answer

Example 704. The pyexec3.py script

message = 'a private random number: %f' % random.random()

Example 705. The pyexec4.py script

print message

If I run this example on my machine I get something like:

Using ../../csound.xmg
Csound Version 4.19 (Mar 23 2002)
Embedded Python interpreter version 2.2
orchname:  pyexec.orc
scorename: pyexec.sco
sorting score ...
        ... done
orch compiler:
11 lines read
        instr   1       
Csound Version 4.19 (Mar 23 2002)
displays suppressed

Welcome to Csound!
What sound do you want to hear today, maurizio?

then I answer

a sound

then Csound continues with the normal performance

your answer is "a sound"
a private random number: 0.884006
new alloc for instr 1:
your answer is "a sound"
a private random number: 0.884006
your answer is "a sound"
a private random number: 0.889868
your answer is "a sound"
a private random number: 0.884006
your answer is "a sound"
a private random number: 0.889868
your answer is "a sound"
a private random number: 0.884006
your answer is "a sound"
...

In the same instrument a message is created in the private namespace and printed, appearing different for each instance.

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.