Debian Codegraph @

csound5/opcodes/py.xml

raw symbols SCIP
1 <chapter id="py">
2
3 <title>Python Opcodes</title>
4 <section id="PythonOpcodes">
5 <title>Introduction</title>
6 <para>Using the Python opcode family, you can interact with a Python3 interpreter embedded in Csound in five ways:</para>
7 <orderedlist>
8 <listitem><simpara>Initialize the Python interpreter (the <link
9 linkend="pyinit"><citetitle>pyinit</citetitle></link> opcodes),</simpara></listitem>
10 <listitem><simpara>Run a statement (the <link
11 linkend="pyrun"><citetitle>pyrun</citetitle></link> opcodes),</simpara></listitem>
12 <listitem><simpara>Execute a script (the <link
13 linkend="pyexec"><citetitle>pyexec</citetitle></link> opcodes),</simpara></listitem>
14 <listitem><simpara>Invoke a callable and pass arguments (the <link
15 linkend="pycall"><citetitle>pycall</citetitle></link> opcodes),</simpara></listitem>
16 <listitem><simpara>Evaluate an expression (the <link
17 linkend="pyeval"><citetitle>pyeval</citetitle></link> opcodes), or</simpara></listitem>
18 <listitem><simpara>Change the value of a Python object, possibly creating a new
19 Python object (the <link
20 linkend="pyassign"><citetitle>pyassign</citetitle></link> opcodes);</simpara></listitem>
21 </orderedlist>
22 <para>and you can do any of these things:</para>
23 <orderedlist>
24 <listitem><simpara>At i-time or at k-time,</simpara></listitem>
25 <listitem><simpara>In the global Python namespace, or in a namespace specific
26 to an individual instance of a Csound instrument (local or "l" context),</simpara></listitem>
27 <listitem><simpara>And can you can retrieve from 0 to 8 return values from
28 callables that accept N parameters.</simpara></listitem>
29 </orderedlist>
30 <para>...this means that there are many Python-related opcodes. But
31 all of these opcodes share the same <emphasis>py</emphasis> prefix, and have a regular
32 naming scheme:</para>
33
34 <programlisting>
35 "py" + [optional context prefix] + [action name] + [optional x-time suffix]</programlisting>
36
37 </section>
38 <section id="PythonOpcodesSyntax">
39 <title>Orchestra Syntax</title>
40 <para>Blocks of Python code, and indeed entire scripts, can be
41 embedded in Csound orchestras using the {{ and }} directives to
42 enclose the script, as follows:</para>
43
44 <programlisting>
45 <emphasis role="ohdr">sr</emphasis><emphasis role="op">=</emphasis>44100
46 <emphasis role="ohdr">kr</emphasis><emphasis role="op">=</emphasis>4410
47 <emphasis role="ohdr">ksmps</emphasis><emphasis role="op">=</emphasis>10
48 <emphasis role="ohdr">nchnls</emphasis><emphasis role="op">=</emphasis>1
49 <emphasis role="opc">pyinit</emphasis>
50
51 giSinusoid <emphasis role="opc">ftgen</emphasis> 0, 0, 8192, 10, 1
52
53 <emphasis role="opc">pyruni</emphasis> {{
54 import random
55
56 pool = [(1 + i/10.0) ** 1.2 for i in range(100)]
57
58 def get_number_from_pool(n, p):
59 if random.random() &lt; p:
60 i = int(random.random() * len(pool))
61 pool[i] = n
62 return random.choice(pool)
63 }}
64
65 <emphasis role="oblock">instr</emphasis> 1
66 k1 <emphasis role="opc">oscil</emphasis> 1, 3, giSinusoid
67 k2 <emphasis role="opc">pycall1</emphasis> "get_number_from_pool", k1 <emphasis role="op">+</emphasis> 2, p4
68 <emphasis role="opc">printk</emphasis> 0.01, k2
69 <emphasis role="oblock">endin</emphasis></programlisting>
70 <bridgehead>Credits</bridgehead>
71 <para>Copyright (c) 2002 by Maurizio Umberto Puxeddu. All rights
72 reserved.</para>
73 <para>Portions copyright (c) 2004 and 2005 by Michael Gogins.</para>
74
75 </section>
76 </chapter>