Python OpcodesIntroductionUsing the Python opcode family, you can interact with a Python3 interpreter embedded in Csound in five ways:Initialize the Python interpreter (the pyinit opcodes),Run a statement (the pyrun opcodes),Execute a script (the pyexec opcodes),Invoke a callable and pass arguments (the pycall opcodes),Evaluate an expression (the pyeval opcodes), orChange the value of a Python object, possibly creating a new
Python object (the pyassign opcodes);and you can do any of these things:At i-time or at k-time,In the global Python namespace, or in a namespace specific
to an individual instance of a Csound instrument (local or "l" context),And can you can retrieve from 0 to 8 return values from
callables that accept N parameters....this means that there are many Python-related opcodes. But
all of these opcodes share the same py prefix, and have a regular
naming scheme:
"py" + [optional context prefix] + [action name] + [optional x-time suffix]Orchestra SyntaxBlocks of Python code, and indeed entire scripts, can be
embedded in Csound orchestras using the {{ and }} directives to
enclose the script, as follows:sr=44100
kr=4410
ksmps=10
nchnls=1
pyinit
giSinusoid ftgen 0, 0, 8192, 10, 1
pyruni {{
import random
pool = [(1 + i/10.0) ** 1.2 for i in range(100)]
def get_number_from_pool(n, p):
if random.random() < p:
i = int(random.random() * len(pool))
pool[i] = n
return random.choice(pool)
}}
instr 1
k1 oscil 1, 3, giSinusoid
k2 pycall1 "get_number_from_pool", k1 + 2, p4
printk 0.01, k2
endinCreditsCopyright (c) 2002 by Maurizio Umberto Puxeddu. All rights
reserved.Portions copyright (c) 2004 and 2005 by Michael Gogins.