188 lines
11 KiB
HTML
188 lines
11 KiB
HTML
|
<html><head><title>Understanding Streams, Patterns and Events - Part 1</title>
|
||
|
<link rel='stylesheet' href='./../scdoc.css' type='text/css' />
|
||
|
<link rel='stylesheet' href='./../frontend.css' type='text/css' />
|
||
|
<link rel='stylesheet' href='./../custom.css' type='text/css' />
|
||
|
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
|
||
|
<script src='./../scdoc.js' type='text/javascript'></script>
|
||
|
<script src='./../docmap.js' type='text/javascript'></script>
|
||
|
<script src='./../prettify.js' type='text/javascript'></script>
|
||
|
<script src='./../lang-sc.js' type='text/javascript'></script>
|
||
|
<script type='text/javascript'>var helpRoot='./..';</script>
|
||
|
</head>
|
||
|
<ul id='menubar'></ul>
|
||
|
<body onload='fixTOC();prettyPrint()'>
|
||
|
<div class='contents'>
|
||
|
<div class='header'>
|
||
|
<div id='label'>SuperCollider TUTORIALS</div>
|
||
|
<div id='categories'><a href='./../Browse.html#Tutorials>Streams-Patterns-Events'>Tutorials>Streams-Patterns-Events</a></div>
|
||
|
<h1>Understanding Streams, Patterns and Events - Part 1</h1>
|
||
|
<div id='summary'>Streams & Routines</div>
|
||
|
</div>
|
||
|
<div class='subheader'>
|
||
|
<div id='related'>See also: <a href="./../Tutorials/Streams-Patterns-Events2.html">Understanding Streams, Patterns and Events - Part 2</a>, <a href="./../Tutorials/Streams-Patterns-Events3.html">Understanding Streams, Patterns and Events - Part 3</a>, <a href="./../Tutorials/Streams-Patterns-Events4.html">Understanding Streams, Patterns and Events - Part 4</a>, <a href="./../Tutorials/Streams-Patterns-Events5.html">Understanding Streams, Patterns and Events - Part 5</a>, <a href="./../Tutorials/Streams-Patterns-Events6.html">Understanding Streams, Patterns and Events - Part 6</a>, <a href="./../Tutorials/Streams-Patterns-Events7.html">Understanding Streams, Patterns and Events - Part 7</a></div>
|
||
|
</div>
|
||
|
<div id='toc'>
|
||
|
<ul class='toc'><li class='toc1'><a href='#Streams'>Streams</a></li>
|
||
|
<ul class='toc'></ul><li class='toc1'><a href='#Stream%20and%20its%20subclasses'>Stream and its subclasses</a></li>
|
||
|
<ul class='toc'></ul><li class='toc1'><a href='#Math%20operations%20on%20Streams'>Math operations on Streams</a></li>
|
||
|
<ul class='toc'></ul><li class='toc1'><a href='#Filtering%20operations%20on%20streams'>Filtering operations on streams</a></li>
|
||
|
<ul class='toc'></ul><li class='toc1'><a href='#Making%20Music%20with%20Streams'>Making Music with Streams</a></li>
|
||
|
<ul class='toc'></ul><li class='toc1'><a href='#Optional:'>Optional:</a></li>
|
||
|
<ul class='toc'></ul></ul></div>
|
||
|
<p>The SuperCollider Pattern library provides a means of specifying dynamic structural transformations of musical processes. It provides similar capabilities as one finds in Nyquist, Elody, Siren, Kyma, HMSL, DMix, and Patchwork.
|
||
|
<p>By using coroutines and streams rather than eager functional methods it is able to work in a lazy event by event method instead of the all-at-once method of Elody and Siren. It provides the kind of dynamic live control found in HMSL but with the more general event models of the others. In Nyquist and Siren certain transformation like Stretch and Transpose are specially coded into the framework. In SuperCollider Patterns, any parameter may have transformations applied to it. The only one treated specially is time, so that parallel streams can be merged.
|
||
|
<p>In order to understand the framework, a number of concepts must be covered. These concepts are embodied in the classes for Streams, Patterns, and Events. You should learn these concepts in the order presented. The framework is built up in layers. If you skip ahead to get to the cool stuff first, you will have missed some important points.<h2><a class='anchor' name='Streams'>Streams</a></h2>
|
||
|
|
||
|
<p>A stream represents a lazy sequence of values. The next value in the sequence is obtained by sending the message next to the stream object. The sequence can be restarted from the beginning by sending the message reset to the stream object. A stream can be of finite or infinite length. When a finite length stream has reached the end, it returns nil.
|
||
|
<p>A stream can be any object that responds to the next and reset messages. Any object that responds to these messages can act as a stream. It happens that the class <a href="./../Classes/Object.html">Object</a> defines next and reset for all objects. In Object, both next and reset are defined to return <code class='code prettyprint lang-sc'>this</code>. Thus any object is by default a stream that represents an infinite sequence of itself.<pre class='code prettyprint lang-sc'>7.next.postln; // 7 responds to next by returning itself</pre>
|
||
|
<h2><a class='anchor' name='Stream%20and%20its%20subclasses'>Stream and its subclasses</a></h2>
|
||
|
|
||
|
<p>In addition to the default streams implemented by <a href="./../Classes/Object.html">Object</a>, there is a class <a href="./../Classes/Stream.html">Stream</a> that provides more functionality such as math operations on streams and filtering of streams.
|
||
|
<p>A generally useful subclass of Stream is the class <a href="./../Classes/FuncStream.html">FuncStream</a> which allows the user to provide functions to execute in response to next and reset. Here is a FuncStream that represents an infinite random sequence:<pre class='code prettyprint lang-sc'>(
|
||
|
var a;
|
||
|
a = FuncStream.new({ #[1, 2, 3, 4].choose });
|
||
|
5.do({ a.next.postln; }); // print 5 values from the stream
|
||
|
)</pre>
|
||
|
|
||
|
<p>Another useful subclass of Stream is <a href="./../Classes/Routine.html">Routine</a> which is a special kind of function that can act like a Stream. Routines are functions that can return a value from the middle and then be resumed from that point when called again. The yield message returns a value from the Routine. The next time the Routine is called it begins by returning from the yield and continues from that point. See the <a href="./../Classes/Routine.html">Routine</a> help file.
|
||
|
<p>Here is a Routine that represents a finite sequence of values:<pre class='code prettyprint lang-sc'>(
|
||
|
var a;
|
||
|
a = Routine.new({
|
||
|
3.do({ arg i; i.yield; })
|
||
|
});
|
||
|
4.do({ a.next.postln; }); // print 4 values from stream
|
||
|
)</pre>
|
||
|
|
||
|
<p>and another:<pre class='code prettyprint lang-sc'>(
|
||
|
var a;
|
||
|
a = Routine.new({
|
||
|
3.do({ arg i;
|
||
|
(i+1).do({ arg j; j.yield; })
|
||
|
})
|
||
|
});
|
||
|
8.do({ a.next.postln; }); // print 8 values from stream
|
||
|
)</pre>
|
||
|
<h2><a class='anchor' name='Math%20operations%20on%20Streams'>Math operations on Streams</a></h2>
|
||
|
|
||
|
<p>Stream is a subclass of <a href="./../Classes/AbstractFunction.html">AbstractFunction</a> which means that one can do math operations on streams to produce other streams.
|
||
|
<p>Applying a unary operator to a stream:<pre class='code prettyprint lang-sc'>(
|
||
|
var a, b;
|
||
|
// a is a stream that counts from 0 to 9
|
||
|
a = Routine.new({
|
||
|
10.do({ arg i; i.yield; })
|
||
|
});
|
||
|
b = a.squared; // stream b is a square of the stream a
|
||
|
12.do({ b.next.postln; });
|
||
|
)</pre>
|
||
|
|
||
|
<p>Using a binary operator on a stream:<pre class='code prettyprint lang-sc'>(
|
||
|
var a, b;
|
||
|
// a is a stream that counts from 0 to 9
|
||
|
a = Routine.new({
|
||
|
10.do({ arg i; i.yield; })
|
||
|
});
|
||
|
b = a + 100; // add a constant value to stream a
|
||
|
12.do({ b.next.postln; });
|
||
|
)</pre>
|
||
|
|
||
|
<p>Using a binary operator on two streams:<pre class='code prettyprint lang-sc'>(
|
||
|
var a, b, c;
|
||
|
// a is a stream that counts from 0 to 9
|
||
|
a = Routine.new({
|
||
|
10.do({ arg i; i.yield; })
|
||
|
});
|
||
|
// b is a stream that counts from 100 to 280 by 20
|
||
|
b = Routine.new({
|
||
|
forBy (100,280,20, { arg i; i.yield })
|
||
|
});
|
||
|
c = a + b; // add streams a and b
|
||
|
12.do({ c.next.postln; });
|
||
|
)</pre>
|
||
|
<h2><a class='anchor' name='Filtering%20operations%20on%20streams'>Filtering operations on streams</a></h2>
|
||
|
|
||
|
<p>Streams respond to the messages <code class='code prettyprint lang-sc'>collect</code>, <code class='code prettyprint lang-sc'>select</code>, and <code class='code prettyprint lang-sc'>reject</code> by returning a new <a href="./../Classes/Stream.html">Stream</a>.
|
||
|
<p>The <code class='code prettyprint lang-sc'>collect</code> message returns a stream that is modified by a function in the same way as the collect message sent to a <a href="./../Classes/Collection.html">Collection</a> returns a modified Collection.<pre class='code prettyprint lang-sc'>(
|
||
|
var a, b;
|
||
|
// a is a stream that counts from 0 to 9
|
||
|
a = Routine.new({
|
||
|
10.do({ arg i; i.yield; })
|
||
|
});
|
||
|
// b is a stream that adds 100 to even values
|
||
|
b = a.collect({ arg item; if (item.even, { item + 100 },{ item }); });
|
||
|
6.do({ b.next.postln; });
|
||
|
)</pre>
|
||
|
|
||
|
<p>The <code class='code prettyprint lang-sc'>select</code> message creates a stream that passes only items that return true from a user supplied function.<pre class='code prettyprint lang-sc'>(
|
||
|
var a, b;
|
||
|
// a is a stream that counts from 0 to 9
|
||
|
a = Routine.new({
|
||
|
10.do({ arg i; i.yield; })
|
||
|
});
|
||
|
// b is a stream that only returns the odd values from stream a
|
||
|
b = a.select({ arg item; item.odd; });
|
||
|
6.do({ b.next.postln; });
|
||
|
)</pre>
|
||
|
|
||
|
<p>The <code class='code prettyprint lang-sc'>reject</code> message creates a stream that passes only items that return false from a user supplied function.<pre class='code prettyprint lang-sc'>(
|
||
|
var a, b;
|
||
|
// a is a stream that counts from 0 to 9
|
||
|
a = Routine.new({
|
||
|
10.do({ arg i; i.yield; })
|
||
|
});
|
||
|
// b is a stream that only returns the non-odd values from stream a
|
||
|
b = a.reject({ arg item; item.odd; });
|
||
|
6.do({ b.next.postln; });
|
||
|
)</pre>
|
||
|
<h2><a class='anchor' name='Making%20Music%20with%20Streams'>Making Music with Streams</a></h2>
|
||
|
|
||
|
<p>Here is a sound example to show how you might use Streams to generate musical material.<pre class='code prettyprint lang-sc'>(
|
||
|
s = Server.local;
|
||
|
SynthDef(\help_SPE1, { arg i_out=0, freq;
|
||
|
var out;
|
||
|
out = RLPF.ar(
|
||
|
LFSaw.ar( freq, mul: EnvGen.kr( Env.perc, levelScale: 0.3, doneAction: 2 )),
|
||
|
LFNoise1.kr(1, 36, 110).midicps,
|
||
|
0.1
|
||
|
);
|
||
|
// out = [out, DelayN.ar(out, 0.04, 0.04) ];
|
||
|
4.do({ out = AllpassN.ar(out, 0.05, [0.05.rand, 0.05.rand], 4) });
|
||
|
Out.ar( i_out, out );
|
||
|
}).send(s);
|
||
|
)
|
||
|
(
|
||
|
// streams as a sequence of pitches
|
||
|
var stream, dur;
|
||
|
dur = 1/8;
|
||
|
stream = Routine.new({
|
||
|
loop({
|
||
|
if (0.5.coin, {
|
||
|
// run of fifths:
|
||
|
24.yield;
|
||
|
31.yield;
|
||
|
36.yield;
|
||
|
43.yield;
|
||
|
48.yield;
|
||
|
55.yield;
|
||
|
});
|
||
|
rrand(2,5).do({
|
||
|
// varying arpeggio
|
||
|
60.yield;
|
||
|
#[63,65].choose.yield;
|
||
|
67.yield;
|
||
|
#[70,72,74].choose.yield;
|
||
|
});
|
||
|
// random high melody
|
||
|
rrand(3,9).do({ #[74,75,77,79,81].choose.yield });
|
||
|
});
|
||
|
});
|
||
|
Routine({
|
||
|
loop({
|
||
|
Synth(\help_SPE1, [ \freq, stream.next.midicps ] );
|
||
|
dur.wait; // synonym for yield, used by .play to schedule next occurence
|
||
|
})
|
||
|
}).play
|
||
|
)</pre>
|
||
|
<h2><a class='anchor' name='Optional:'>Optional:</a></h2>
|
||
|
|
||
|
<p>More about Streams can be learned from the book A Little Smalltalk by Timothy Budd. He calls them Generators and shows how they can be used to solve problems like the "eight queens" problem etc.
|
||
|
<p>To go to the next file: <a href="./../Tutorials/Streams-Patterns-Events2.html">Understanding Streams, Patterns and Events - Part 2</a><div class='doclink'>helpfile source: <a href='file:///Applications/SuperCollider.app/Contents/Resources/HelpSource/Tutorials/Streams-Patterns-Events1.schelp'>/Applications/SuperCollider.app/Contents/Resources/HelpSource/Tutorials/Streams-Patterns-Events1.schelp</a><br>link::Tutorials/Streams-Patterns-Events1::<br>sc version: 3.8.0</div></div></body></html>
|