polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Libraries / JITLib / Patterns / Tdef.html
blob011deeb908f91037e1bee3e4e85c820d418cbb16
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="1038.25">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000ee}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
16 p.p7 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 14.0px Helvetica}
17 p.p8 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 12.0px Helvetica; min-height: 14.0px}
18 p.p9 {margin: 0.0px 0.0px 0.0px 57.0px; text-indent: -57.0px; font: 12.0px Helvetica}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #cd1612}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #00831b}
23 p.p14 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
24 p.p15 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
25 p.p16 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
26 span.s1 {font: 18.0px Helvetica}
27 span.s2 {font: 9.0px Monaco}
28 span.s3 {color: #000000}
29 span.s4 {text-decoration: underline}
30 span.s5 {color: #1135f9}
31 span.s6 {color: #0000bf}
32 span.s7 {color: #007300}
33 span.s8 {color: #737373}
34 span.s9 {color: #606060}
35 span.s10 {color: #0b28c6}
36 span.s11 {color: #bf0000}
37 span.Apple-tab-span {white-space:pre}
38 </style>
39 </head>
40 <body>
41 <p class="p1"><span class="s1"><b>Tdef<span class="Apple-tab-span"> </span></b></span><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><b>task reference definition</b></p>
42 <p class="p2"><br></p>
43 <p class="p3"><span class="s3"><b>Inherits from:<span class="Apple-converted-space"> </span></b> <a href="../../../Core/Object.html"><span class="s4">Object</span></a> : <a href="../../../Core/Kernel/AbstractFunction.html"><span class="s4">AbstractFunction</span></a> : <a href="../../../Streams-Patterns-Events/Patterns/Pattern.html"><span class="s4">Pattern</span></a> : <a href="PatternProxy.html"><span class="s4">PatternProxy</span></a> : <a href="TaskProxy.html"><span class="s5">TaskProxy</span></a></span></p>
44 <p class="p4"><br></p>
45 <p class="p4"><br></p>
46 <p class="p1">Pdefn provides an interface to its superclass <a href="TaskProxy.html"><span class="s5">TaskProxy</span></a><span class="s5">.</span>Tdef keeps a reference to a task (<b>time pattern</b>) that can be replaced while playing. It continues playing when the old stream ended and a new stream is set and schedules the changes to the beat. One Tdef may be used in many tasks in different places. A change in the task definition Tdef propagates through all tasks.</p>
47 <p class="p4"><br></p>
48 <p class="p1"><b>Tdef(key) </b>returns the instance</p>
49 <p class="p1"><b>Tdef(key, func) </b>defines the task and returns the instance, like <a href="Pdef.html"><span class="s6">Pdef</span></a> and <a href="../nodeproxy/Ndef.html"><span class="s6">Ndef</span></a>.</p>
50 <p class="p4"><br></p>
51 <p class="p3"><span class="s3">See also: <a href="TaskProxy.html"><span class="s4">TaskProxy</span></a>, <a href="../../../Scheduling/Task.html"><span class="s4">Task</span></a>, <a href="../../../Core/Kernel/Routine.html"><span class="s4">Routine</span></a></span></p>
52 <p class="p1">Graphical overview over all current Tdefs: <a href="../extras/TdefAllGui.html"><span class="s6">TdefAllGui</span></a><span class="s6">.</span></p>
53 <p class="p1">Overview: <a href="../JITLib.html"><span class="s6">JITLib</span></a></p>
54 <p class="p4"><br></p>
55 <p class="p4"><br></p>
56 <p class="p5"><b>First Example</b></p>
57 <p class="p4"><br></p>
58 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop { 0.5.wait; <span class="s8">"aaaaaaaaaaaaaazz"</span>.scramble.postln } }).play;</p>
59 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop { 0.125.wait; <span class="s8">"aazz"</span>.scramble.postln } });</p>
60 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop { 0.5.wait; (note: 14.rand).play } });</p>
61 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop { 0.5.wait; (note: 14.rand + [0, 3, 6, 7].keep(4.rand)).play } });</p>
62 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>).stop;</p>
63 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>).play;</p>
64 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\x</span>).clear;</p>
65 <p class="p2"><span class="Apple-tab-span"> </span></p>
66 <p class="p4"><br></p>
67 <p class="p4"><span class="Apple-tab-span"> </span></p>
68 <p class="p7"><b>Creation / Class Methods</b></p>
69 <p class="p8"><br></p>
70 <p class="p9"><span class="Apple-tab-span"> </span><b>*new(key, func)</b></p>
71 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Store the task in a global dictionary under key, replacing its routine function with the new one.</p>
72 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
73 <p class="p9"><span class="Apple-tab-span"> </span><b>*new(key)</b></p>
74 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Access the pattern at that key (if none is given, a default task is created)</p>
75 <p class="p8"><span class="Apple-tab-span"> </span></p>
76 <p class="p9"><span class="Apple-tab-span"> </span><b>*default</b></p>
77 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Default source, if none is given. The default task has a function that waits in 1.0 beat steps and does nothing.</p>
78 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
79 <p class="p9"><span class="Apple-tab-span"> </span><b>*removeAll</b></p>
80 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Remove all proxies from the global dictionary (*<b>all</b>)</p>
81 <p class="p8"><span class="Apple-tab-span"> </span></p>
82 <p class="p9"><span class="Apple-tab-span"> </span><b>*clear</b></p>
83 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Clear all proxies, setting their source to silence.</p>
84 <p class="p8"><span class="Apple-tab-span"> </span></p>
85 <p class="p9"><span class="Apple-tab-span"> </span><b>*all</b></p>
86 <p class="p9"><b><span class="Apple-tab-span"> </span>*all_(envir)</b></p>
87 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Set or return the environment (<a href="../../../Collections/IdentityDictionary.html"><span class="s6">IdentityDictionary</span></a>) that stores all instances.</p>
88 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
89 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
90 <p class="p5"><b>Changing the definition / setting the source</b></p>
91 <p class="p1">One Tdef may have many tasks in different places. A change in the task definition Tdef propagates through all tasks. The change does not have to be immediate - there is a scheme to schedule when the change becomes effective: a <b>quant</b> and <b>clock</b> (like elsewhere) and a <b>condition</b>.</p>
92 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
93 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
94 <p class="p9"><b>quant_(beats)</b></p>
95 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Set the quantisation time for beat accurate scheduling.<span class="Apple-converted-space"> </span></p>
96 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>quant can be an array <b>[quant, phase, timingOffset, outset]</b>, or just <b>[quant, phase] </b>etc.</p>
97 <p class="p8"><span class="Apple-tab-span"> </span></p>
98 <p class="p9"><span class="Apple-tab-span"> </span>*<b>defaultQuant_(beats)</b></p>
99 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Set the default quantisation for new instances (default: 1.0)</p>
100 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>This can be an array<span class="Apple-converted-space">  </span>[quant, phase, timingOffset, outset]</p>
101 <p class="p8"><span class="Apple-tab-span"> </span></p>
102 <p class="p9"><span class="Apple-tab-span"> </span><b>condition_(func)</b></p>
103 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Provide a condition under which the pattern is switched when a new one is inserted.</p>
104 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>The stream value and a count value is passed into the function.</p>
105 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
106 <p class="p9"><span class="Apple-tab-span"> </span><b>count(n)</b></p>
107 <p class="p9"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>Create and update condition that simply counts up to n and switches the pattern then</p>
108 <p class="p8"><br></p>
109 <p class="p9"><span class="Apple-tab-span"> </span><b>reset</b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
110 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Switch the task immediately (stuck conditions can be subverted by this).</p>
111 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
112 <p class="p9"><span class="Apple-tab-span"> </span><b>envir_(event)</b></p>
113 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Set the environment (an event) for the Tdef. <b>It is passed as first argument into the Task function.</b></p>
114 <p class="p8"><span class="Apple-tab-span"> </span></p>
115 <p class="p9"><span class="Apple-tab-span"> </span><b>set(key, val, key2, val2, ...)</b></p>
116 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Set arguments in the default event. If there is none, it is created and the task routine is rebuilt.</p>
117 <p class="p8"><span class="Apple-tab-span"> </span></p>
118 <p class="p9"><span class="Apple-tab-span"> </span><b>clear</b></p>
119 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Set the source to nil</p>
120 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
121 <p class="p9"><span class="Apple-tab-span"> </span><b>endless</b></p>
122 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Returns a Prout that plays the task endlessly, replacing <b>nil</b> with a <b>default</b> value 1. This allows to create streams that idle on until a new pattern is inserted.</p>
123 <p class="p8"><br></p>
124 <p class="p4"><br></p>
125 <p class="p4"><br></p>
126 <p class="p4"><br></p>
127 <p class="p7"><b>Tdef as stream reference</b></p>
128 <p class="p1">A single Tdef may serve as a definition for multiple tasks. These methods show how to fork off separate routines from one instance. Even if they run in different contexts, their definition may still be changed.</p>
129 <p class="p4"><span class="Apple-tab-span"> </span></p>
130 <p class="p4"><span class="Apple-tab-span"> </span></p>
131 <p class="p9"><span class="Apple-tab-span"> </span><b>fork(clock, quant, event)</b></p>
132 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Play an independent task in parallel.</p>
133 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>quant </b>can be an array of [quant, phase, offset], or a <a href="../../../Scheduling/Quant.html"><span class="s6">Quant</span></a> value.</p>
134 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
135 <p class="p9"><span class="Apple-tab-span"> </span><b>embed(val)</b><span class="Apple-tab-span"> </span></p>
136 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Pass a value (typically an <b>event</b>) into the task function, and embed the Tdef in the stream.</p>
137 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
138 <p class="p9"><span class="Apple-tab-span"> </span><b>embedInStream(inval)</b></p>
139 <p class="p9"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>just like any pattern, embeds itself in stream</p>
140 <p class="p4"><span class="Apple-tab-span"> </span></p>
141 <p class="p4"><br></p>
142 <p class="p4"><span class="Apple-tab-span"> </span></p>
143 <p class="p7"><b>Tdef as EventStreamPlayer</b></p>
144 <p class="p1">For live coding, each Tdef also may control one instance that plays one task. This is a PauseStream, accessible in the instance variable <b>player</b>.</p>
145 <p class="p4"><br></p>
146 <p class="p4"><span class="Apple-tab-span"> </span></p>
147 <p class="p9"><b><span class="Apple-tab-span"> </span>play(clock, protoEvent, quant)</b></p>
148 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Starts the Tdef and creates a player.<span class="Apple-converted-space"> </span></p>
149 <p class="p9"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>quant </b>can be an array of [quant, phase, offset] or a <a href="../../../Scheduling/Quant.html"><span class="s6">Quant</span></a> value.</p>
150 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
151 <p class="p9"><span class="Apple-tab-span"> </span><b>stop</b></p>
152 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Stops the player</p>
153 <p class="p8"><span class="Apple-tab-span"> </span></p>
154 <p class="p9"><span class="Apple-tab-span"> </span><b>player</b><span class="Apple-tab-span"> </span></p>
155 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Return the current player (if the Tdef is simply used in other streams this is nil)</p>
156 <p class="p8"><br></p>
157 <p class="p9"><span class="Apple-tab-span"> </span><b>pause / resume / reset / mute / unmute</b></p>
158 <p class="p9"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>Perform this method on the player.</p>
159 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
160 <p class="p9"><span class="Apple-tab-span"> </span><b>isPlaying</b><span class="Apple-converted-space"> </span></p>
161 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Returns true if player is running. If a Tdef is playing and its stream ends, it will schedule a stream for playing<b> as soon as a new one is assigned to it</b>. If it is stopped by <b>stop</b>, it won't.</p>
162 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
163 <p class="p2"><br></p>
164 <p class="p2"><br></p>
165 <p class="p2"><br></p>
166 <p class="p2"><br></p>
167 <p class="p7"><b>Example: Tdef as a Task player</b></p>
168 <p class="p2"><br></p>
169 <p class="p2"><br></p>
170 <p class="p10"><span class="s6">Tdef</span><span class="s3">(</span><span class="s7">\x</span><span class="s3">).play; </span>// create an empty Tdef and play it.</p>
171 <p class="p2"><br></p>
172 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop({ <span class="s9">"ggggggggggggggggg9999ggg999ggg999gg"</span>.scramble.postln; 0.5.wait; }) });</p>
173 <p class="p2"><br></p>
174 <p class="p2"><br></p>
175 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop({ <span class="s9">"---------////----------------------"</span>.scramble.postln; 0.25.wait; }) });</p>
176 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop({ <span class="s6">thisThread</span>.seconds.postln; 1.wait; }) });</p>
177 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop({ <span class="s6">thisThread</span>.seconds.postln; 1.01.wait; }) });</p>
178 <p class="p2"><br></p>
179 <p class="p6"><span class="s6">TempoClock</span>.default.tempo = 2;</p>
180 <p class="p2"><br></p>
181 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { <span class="s9">"the end"</span>.postln });</p>
182 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { <span class="s9">"one more"</span>.postln });</p>
183 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { 10.do({ <span class="s9">"ten more"</span>.scramble.postln; 0.25.wait; }) });</p>
184 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, { loop({ <span class="s9">"lots more"</span>.scramble.postln; 0.25.wait; }) });</p>
185 <p class="p2"><br></p>
186 <p class="p6"><span class="s6">TempoClock</span>.default.tempo = 1;</p>
187 <p class="p2"><br></p>
188 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>).stop;</p>
189 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>).play;</p>
190 <p class="p2"><br></p>
191 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>).clear;</p>
192 <p class="p2"><br></p>
193 <p class="p2"><br></p>
194 <p class="p10">// sound example</p>
195 <p class="p2"><br></p>
196 <p class="p6">(</p>
197 <p class="p11">// load a synthdef</p>
198 <p class="p12">s.boot;</p>
199 <p class="p13"><span class="s10">SynthDef</span><span class="s3">(</span>\pdef_grainlet<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
200 <p class="p12"><span class="Apple-tab-span"> </span>{ <span class="s10">arg</span> out=0, freq=440, sustain=0.05;</p>
201 <p class="p12"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s10">var</span> env;</p>
202 <p class="p12"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>env = <span class="s10">EnvGen</span>.kr(<span class="s10">Env</span>.perc(0.01, sustain, 0.3), doneAction:2);</p>
203 <p class="p12"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s10">Out</span>.ar(out, <span class="s10">SinOsc</span>.ar(freq, 0, env))</p>
204 <p class="p12"><span class="Apple-tab-span"> </span>}).add;</p>
205 <p class="p6">)</p>
206 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>).play;</p>
207 <p class="p2"><br></p>
208 <p class="p6">(</p>
209 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, {<span class="Apple-converted-space"> </span></p>
210 <p class="p6"><span class="Apple-tab-span"> </span>loop({</p>
211 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>s.sendMsg(<span class="s9">"/s_new"</span>, <span class="s9">"pdef_grainlet"</span>, -1,0,0, <span class="s7">\freq</span>, rrand(600, 640));</p>
212 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.1.wait;</p>
213 <p class="p6"><span class="Apple-tab-span"> </span>})</p>
214 <p class="p6">})</p>
215 <p class="p6">)</p>
216 <p class="p2"><br></p>
217 <p class="p6">(</p>
218 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, {<span class="Apple-converted-space"> </span></p>
219 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">var</span> x;</p>
220 <p class="p6"><span class="Apple-tab-span"> </span>x = <span class="s6">Pseries</span>(300, 20, 100).loop.asStream;</p>
221 <p class="p6"><span class="Apple-tab-span"> </span>loop({</p>
222 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>s.sendMsg(<span class="s9">"/s_new"</span>, <span class="s9">"pdef_grainlet"</span>, -1,0,0, <span class="s7">\freq</span>, x.next);</p>
223 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.05.wait;</p>
224 <p class="p6"><span class="Apple-tab-span"> </span>})</p>
225 <p class="p6">})</p>
226 <p class="p6">)</p>
227 <p class="p2"><br></p>
228 <p class="p6">(</p>
229 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, {<span class="Apple-converted-space"> </span></p>
230 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">var</span> x;</p>
231 <p class="p6"><span class="Apple-tab-span"> </span>x = <span class="s6">Plazy</span>({ <span class="s6">Pseries</span>(300 + 300.rand, 10 + 30.rand, 10 + 30.rand) }).loop.asStream;</p>
232 <p class="p6"><span class="Apple-tab-span"> </span>loop({</p>
233 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>s.sendMsg(<span class="s9">"/s_new"</span>, <span class="s9">"pdef_grainlet"</span>, -1,0,0, <span class="s7">\freq</span>, x.next);</p>
234 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.05.wait;</p>
235 <p class="p6"><span class="Apple-tab-span"> </span>})</p>
236 <p class="p6">})</p>
237 <p class="p6">)</p>
238 <p class="p2"><br></p>
239 <p class="p10">// metronome</p>
240 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\y</span>, { loop({ s.sendMsg(<span class="s9">"/s_new"</span>, <span class="s9">"pdef_grainlet"</span>, -1,0,0, <span class="s7">\freq</span>, 1500); 1.wait; }) }).play;</p>
241 <p class="p2"><br></p>
242 <p class="p10">// play ending stream once</p>
243 <p class="p6">(</p>
244 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>, {<span class="Apple-converted-space"> </span></p>
245 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">var</span> x, dt;</p>
246 <p class="p6"><span class="Apple-tab-span"> </span>dt = [0.1, 0.125, 0.05].choose;</p>
247 <p class="p6"><span class="Apple-tab-span"> </span>x = <span class="s6">Plazy</span>({ <span class="s6">Pseries</span>(1300 + 300.rand, 110 + 130.rand, 16) }).asStream;</p>
248 <p class="p6"><span class="Apple-tab-span"> </span>x.do({ <span class="s6">arg</span> item;</p>
249 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>s.sendMsg(<span class="s9">"/s_new"</span>, <span class="s9">"pdef_grainlet"</span>, -1,0,0, <span class="s7">\freq</span>, item);</p>
250 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>dt.wait;</p>
251 <p class="p6"><span class="Apple-tab-span"> </span>})</p>
252 <p class="p6">})</p>
253 <p class="p6">)</p>
254 <p class="p2"><br></p>
255 <p class="p10">// ... and so on ...</p>
256 <p class="p2"><br></p>
257 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\x</span>).stop;</p>
258 <p class="p6"><span class="s6">Tdef</span>.removeAll;</p>
259 <p class="p2"><br></p>
260 <p class="p2"><br></p>
261 <p class="p2"><br></p>
262 <p class="p5"><b>Embed and fork: Tdef within other Tasks / Routines.</b></p>
263 <p class="p2"><br></p>
264 <p class="p2"><br></p>
265 <p class="p10">// embed plays tdefs in sequence within a task.</p>
266 <p class="p6">(</p>
267 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\a</span>, { <span class="s9">"one"</span>.postln; 1.wait; <span class="s9">"two"</span>.postln });</p>
268 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\c</span>, { <span class="s6">var</span> z; z = <span class="s6">Synth</span>(<span class="s7">\default</span>); 0.5.wait; z.release });</p>
269 <p class="p6">r = <span class="s6">Task</span>({</p>
270 <p class="p14"><span class="s3"><span class="Apple-tab-span"> </span></span>"counting..."<span class="s3">.postln;</span></p>
271 <p class="p6"><span class="Apple-tab-span"> </span>2.wait;</p>
272 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\a</span>).embed;</p>
273 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
274 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\c</span>).embed;</p>
275 <p class="p6"><span class="Apple-tab-span"> </span><span class="s9">"done."</span>.postln;</p>
276 <p class="p6">});</p>
277 <p class="p6">)</p>
278 <p class="p2"><br></p>
279 <p class="p10"><span class="s3">r.play; </span>// play a stream</p>
280 <p class="p2"><br></p>
281 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\c</span>, { <span class="s6">var</span> z; z = <span class="s6">Synth</span>(<span class="s7">\default</span>, [<span class="s7">\freq</span>, 300]); 1.5.wait; z.release }); <span class="s11">// change the def</span></p>
282 <p class="p2"><br></p>
283 <p class="p6">r.reset;</p>
284 <p class="p6">r.play;</p>
285 <p class="p2"><br></p>
286 <p class="p10">// of course Tdefs can be used in other Tdefs:</p>
287 <p class="p6">(</p>
288 <p class="p6">Tdef(\a, { 10.do { |i| (" a: " + i).postln; 0.3.wait; } });</p>
289 <p class="p6">Tdef(\b, { 15.do { |i| ("\t\t b: " + i).postln; 0.2.wait; } });</p>
290 <p class="p6">Tdef(\c, { 5.do { |i| ("\t\t\t\t c: " + i).postln; 0.5.wait; } });</p>
291 <p class="p2"><br></p>
292 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\d</span>, {</p>
293 <p class="p14"><span class="s3"><span class="Apple-tab-span"> </span></span>"embed - sequence."<span class="s3">.postln;</span></p>
294 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
295 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\a</span>).embed;</p>
296 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
297 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\b</span>).embed;</p>
298 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;<span class="Apple-converted-space"> </span></p>
299 <p class="p6"><span class="Apple-tab-span"> </span>Tdef(\c).embed;</p>
300 <p class="p2"><span class="Apple-tab-span"> </span></p>
301 <p class="p6"><span class="Apple-tab-span"> </span><span class="s9">"done."</span>.postln;</p>
302 <p class="p6">});</p>
303 <p class="p6">)</p>
304 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\d</span>).play;</p>
305 <p class="p2"><br></p>
306 <p class="p10">// to start a tdef in its own separate thread, thus branching into parallel threads,<span class="Apple-converted-space"> </span></p>
307 <p class="p10">// one can use .fork, or .playOnce</p>
308 <p class="p6">(</p>
309 <p class="p6">Tdef(\a, { 10.do { |i| (" a: " + i).postln; 0.3.wait; } });</p>
310 <p class="p6">Tdef(\b, { 15.do { |i| ("\t\t b: " + i).postln; 0.2.wait; } });</p>
311 <p class="p6">Tdef(\c, { 5.do { |i| ("\t\t\t\t c: " + i).postln; 0.5.wait; } });</p>
312 <p class="p2"><br></p>
313 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\d</span>, {</p>
314 <p class="p14"><span class="s3"><span class="Apple-tab-span"> </span></span>"fork - parallel."<span class="s3">.postln;</span></p>
315 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
316 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\a</span>).fork;</p>
317 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;</p>
318 <p class="p6"><span class="Apple-tab-span"> </span><span class="s6">Tdef</span>(<span class="s7">\b</span>).fork;</p>
319 <p class="p6"><span class="Apple-tab-span"> </span>1.wait;<span class="Apple-converted-space"> </span></p>
320 <p class="p6"><span class="Apple-tab-span"> </span>Tdef(\c).fork;</p>
321 <p class="p2"><span class="Apple-tab-span"> </span></p>
322 <p class="p6"><span class="Apple-tab-span"> </span><span class="s9">"done."</span>.postln;</p>
323 <p class="p6">});</p>
324 <p class="p6">)</p>
325 <p class="p2"><br></p>
326 <p class="p2"><br></p>
327 <p class="p2"><br></p>
328 <p class="p2"><br></p>
329 <p class="p2"><br></p>
330 <p class="p2"><br></p>
331 <p class="p5"><b>Tdef as a time pattern</b></p>
332 <p class="p1">Instead of using a Pdefn for time values, it can be useful to use a Tdef. When changing its source, it keeps the stream of values synchronized to its clock.</p>
333 <p class="p4"><br></p>
334 <p class="p2"><br></p>
335 <p class="p6">(</p>
336 <p class="p10">// load a synthdef</p>
337 <p class="p6">s.boot;</p>
338 <p class="p14"><span class="s6">SynthDef</span><span class="s3">(</span>"pdef_grainlet"<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
339 <p class="p6"><span class="Apple-tab-span"> </span>{ <span class="s6">arg</span> out=0, freq=440, sustain=0.05;</p>
340 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">var</span> env;</p>
341 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>env = <span class="s6">EnvGen</span>.kr(<span class="s6">Env</span>.perc(0.01, sustain, 0.3), doneAction:2);</p>
342 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">Out</span>.ar(out, <span class="s6">SinOsc</span>.ar(freq, 0, env))</p>
343 <p class="p6"><span class="Apple-tab-span"> </span>}).store;</p>
344 <p class="p6">)</p>
345 <p class="p2"><br></p>
346 <p class="p2"><br></p>
347 <p class="p2"><br></p>
348 <p class="p2"><br></p>
349 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\z</span>, <span class="s6">Pseq</span>([1, 1, 1, 0.5, 0.5], <span class="s6">inf</span>));</p>
350 <p class="p2"><br></p>
351 <p class="p6">(</p>
352 <p class="p15"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s6">Pset</span><span class="s3">(</span>\instrument<span class="s3">, </span>\pdef_grainlet<span class="s3">,<span class="Apple-converted-space"> </span></span></p>
353 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">Ppar</span>([</p>
354 <p class="p16"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>Pbind<span class="s3">(</span></p>
355 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\dur</span>, <span class="s6">Tdef</span>(<span class="s7">\z</span>),</p>
356 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\note</span>, <span class="s6">Pseq</span>([1, 3, 2, 1, 0], <span class="s6">inf</span>),</p>
357 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\x</span>, <span class="s6">Pfunc</span><span class="s6">TempoClock</span>.default.elapsedBeats.postln } <span class="s11">// posts the onset times</span></p>
358 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>),</p>
359 <p class="p16"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>Pbind<span class="s3">(</span></p>
360 <p class="p10"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s7">\dur</span><span class="s3">, 4, </span>// reference beat</p>
361 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\sustain</span>, 0.1,</p>
362 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s7">\note</span>, 8</p>
363 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>)</p>
364 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>])</p>
365 <p class="p6"><span class="Apple-tab-span"> </span>).play(quant:1);</p>
366 <p class="p6">)</p>
367 <p class="p2"><br></p>
368 <p class="p2"><br></p>
369 <p class="p6"><span class="s6">Tdef</span>(<span class="s7">\z</span>, <span class="s6">Prand</span>([1, 1, 0.23, 0.5, 0.5], <span class="s6">inf</span>)); <span class="s11">// exchange time pattern</span></p>
370 <p class="p10"><span class="s6">Tdef</span><span class="s3">(</span><span class="s7">\z</span><span class="s3">, </span><span class="s6">Pseq</span><span class="s3">([1, 1, 1, 1], </span><span class="s6">inf</span><span class="s3">)); </span>// pattern stays in sync.</p>
371 <p class="p10"><span class="s6">Tdef</span><span class="s3">(</span><span class="s7">\z</span><span class="s3">, </span><span class="s6">Pseq</span><span class="s3">([1, 1, 1, 0.5, 0.5], </span><span class="s6">inf</span><span class="s3">)); </span>// but might be in different order.</p>
372 <p class="p10"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// to avoid this, set quant to an appropriate value.</p>
373 <p class="p2"><br></p>
374 <p class="p2"><br></p>
375 </body>
376 </html>