summaryrefslogtreecommitdiff
path: root/opcodes/pyexec.xml
blob: 93332cfc1ce785d18216c6167a2ace959d6b836d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<refentry id="pyexec">
<indexterm id="IndexPyExec"><primary>pyexec</primary></indexterm>
  <refentryinfo><title>Python Opcodes</title></refentryinfo>
  <refmeta>
    <refentrytitle>pyexec Opcodes</refentrytitle>
  </refmeta>
  
  <refnamediv>
    <refname>pyexec</refname>
    <refpurpose>Execute a script from a file at k-time or i-time (i suffix).
    </refpurpose>
  </refnamediv>
  <refsect1>
    <title>Syntax</title>
    <synopsis><command>pyexec</command> "filename"</synopsis>
    <synopsis><command>pyexeci</command> "filename"</synopsis>
    <synopsis><command>pylexec</command> "filename"</synopsis>
    <synopsis><command>pylexeci</command> "filename"</synopsis>
    <synopsis><command>pyexect</command> ktrigger, "filename"</synopsis>
    <synopsis><command>plyexect</command> ktrigger, "filename"</synopsis>
  </refsect1>
  <refsect1>
    <title>Description</title>
    <para>Execute a script from a file at k-time or i-time (i
      suffix).</para>
    <para>This is not the same as calling the script with the <literal>system()</literal>
      call, since the code is executed by the embedded interpreter.</para>
    <para>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.</para>
    <para>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.</para>
    <para>The "local" version of the <emphasis>pyexec</emphasis> opcodes are useful when the
      code ran by different instances of an instrument should not
      interact.</para>
  </refsect1>
  <refsect1>
    <title>Example of the pyexec Opcode Group</title>
    <example>
      <title>Orchestra (pyexec.orc)</title>
<programlisting>
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
</programlisting>

</example>

<example>

<title>Score (pyexec.sco)</title>

<programlisting>
i1 0 0.01
i1 0 0.01
        </programlisting>

</example>

<example>

<title>The pyexec1.py Script</title>

<programlisting>
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()
</programlisting>
</example>
<example>
<title>The pyexec2.py script</title>
<programlisting>
print 'your answer is "%s"' % answer
</programlisting>
</example>
<example>
<title>The pyexec3.py script</title>
<programlisting>
message = 'a private random number: %f' % random.random()
</programlisting>
</example>
<example>
<title>The pyexec4.py script</title>

<programlisting>
print message
</programlisting>
</example>

<para>If I run this example on my machine I get something like:</para>
<screen>
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?
</screen>
<para>then I answer</para>
<screen>
a sound
</screen>
<para>then Csound continues with the normal performance</para>
<screen>
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"
...
</screen>
<para>In the same instrument a message is created in the private
namespace and printed, appearing different for each instance.</para>

  </refsect1>

  <refsect1>
    <title>Credits</title>
    <para>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
      &namemichael;.</para>
  </refsect1>
</refentry>