supernova: fix for small audio vector sizes
[supercollider.git] / HelpSource / Tutorials / Mark_Polishook_tutorial / 13_BinaryOp_synthesis.schelp
blob8cb8c626b7f3791395a55dd2841fbb4f4f06f937
1 title:: 13_BinaryOp_synthesis
2 summary:: Mark Polishook tutorial
3 categories:: Tutorials>Mark_Polishook_tutorial
4 related:: Tutorials/Mark_Polishook_tutorial/00_Introductory_tutorial
6 section::Binary messages
8 The pattern for a binary message is
10 table::
11 ## RECEIVER || OPERATOR || OPERAND
14 For example
16 code::
17 2 * 3
20 is a receiver (the object to which a message is sent), a binary operator, and an operand.
22 section::Mixing = addition
24 Use addition (a binary operation) to mix two or more ugens.
26 code::
28         // mix 2 sawtooth waves
29         {
30                 Saw.ar(500, 0.05)       // receiver
31                 +                       // operator
32                 Saw.ar(600, 0.06)       // operand
33         }.scope;
38 // mix 3 unit generators.
39         {
40                 Saw.ar(500, 0.05)       // receiver
41                 +                       // operator
42                 Saw.ar(600, 0.06)       // operand
43                                                 // when evaluated produce
44                                                 // a BinaryOpUGen
45                                                 // this BinaryOpUGen is then a receiver for an
46                 +                               // addition operator followed by
47                 Saw.ar(700, 0.07)               // an operand
48         }.scope;
52 ////////////////////////////////////////////////////////////////////////////////////////////////////
54 Rewrite the previous example with the Mix ugen.
56 code::
59         Mix.ar(
60                 // the ugens that will be mixed go into an array
61                 [
62                         Saw.ar(500, 0.05),
63                         Saw.ar(600, 0.06),
64                         Saw.ar(700, 0.06)
65                 ]
66         )
67 }.scope
71 Or use Mix.arFill to create the same result.
73 code::
74 { Mix.arFill(3, { arg i; Saw.ar(500 + (i * 100), 0.05) }) }.scope;
77 Every time the function is evaluated, the argument i is incremented. So i equals 0 the first time the function is evaluated, i equals 1 the second time, i equals 2, the third time, and so on.
79 section::Scaling = multiplication
81 Apply an envelope, in the form of a low-frequency sine wave, to a WhiteNoise generator.
83 code::
84 { WhiteNoise.ar(0.1) * SinOsc.kr(1, 1) }.scope;
87         // scaling and mixing
88         // ... imitates a train?
89         {
90                 (WhiteNoise.ar(0.1) * SinOsc.kr(1, 1))
91                 +
92                 (BrownNoise.ar(0.1) * SinOsc.kr(2, 1))
94         }.scope;
98 section::Envelopes
100 Dynamically modulate any parameter in a ugen (such as frequency, phase, or amplitude) with an envelope.
102 code::
103 // modulate amplitude
104 { SinOsc.ar(440, 0, 0.1) * EnvGen.kr(Env.sine(1), doneAction: 2) }.scope;
107 Setting the doneAction argument (control) to 2 insures that after the envelope reaches its endpoint, SuperCollider will release the memory it used for the instances of the SinOsc and the EnvGen.
109 section::Keyword arguments
111 Keywords make code easier to read and they allow arguments to be presented in any order. Here, the doneAction and the timeScale arguments are expressed in keyword style.
113 code::
115 SynthDef("timeScale", { arg ts = 1;
116         Out.ar(
117                 0,
118                 SinOsc.ar(440, 0, 0.4)
119                 *
120                 EnvGen.kr(
121                         Env.sine(1),
122                         doneAction: 2,
123                         timeScale: ts   // scale the duration of an envelope
124                 )
125         )
126 }).add;
129 Synth("timeScale", [\ts, 0.1]); // timeScale controls the duration of the envelope
132 ////////////////////////////////////////////////////////////////////////////////////////////////////
134 code::
135 // scale the duration of the envelope for every new synth
137 r = Routine({
138         loop({
139                 Synth("timeScale", [\ts, 0.01.rrand(0.3)]);
140                 0.5.wait;
141         })
144 r.play
147 section::Additive Synthesis
149 Additive synthesis is as its name says. Components are added (mixed) together.
151 code::
153 {       // evaluate the function 12 times
154         var n = 12;
155         Mix.arFill(
156                 n,
157                 {
158                         SinOsc.ar(
159                                 [67.0.rrand(2000), 67.0.rrand(2000)],
160                                 0,
161                                 n.reciprocal * 0.75
162                         )
163                 }
164         )
165         *
166         EnvGen.kr(Env.perc(11, 6), doneAction: 2)
167 }.scope
171 section::Envelopes
173 The promise of additive synthesis is that one can add sine waves to create any sound that can be imagined.
175 The problem of additive synthesis is that each and every sine wave and their envelopes have to be specified explicitly.
177 Create nuanced textures by scaling sine waves with envelopes and then mixing the result.
179 code::
181 {       var n = 12;
183         Mix.arFill(
184                         n,                                              // generate n sine waves
185                         {
186                         SinOsc.ar(                                      // each with a possible frequency between
187                                 [67.0.rrand(2000), 67.0.rrand(2000)],   // low.rrand(high) ... floating point values
188                                 0,
189                                 n.reciprocal                            // scale the amplitude of each sine wave
190                                                                         // according to the value of n
191                         )
192                         *
193                         EnvGen.kr(                                      // put an envelope on each of the sine waves
194                                 Env.sine(2.0.rrand(17)),
195                                 doneAction: 0                           // deallocate envelopes only when the
196                                                                         // entire sound is complete (why?)
197                         )
198                 }
199         )
200         *                                                               // put an envelope over the whole patch
201         EnvGen.kr(
202                 Env.perc(11, 6),
203                 doneAction: 2,
204                 levelScale: 0.75
205         )
207 }.scope
211 (Or use the link::Classes/Klang:: ugen to produce a similar effect).
213 section::Ring modulation
215 Multiply two UGens.
217 code::
218 { SinOsc.ar(440, 0, 0.571) * SinOsc.kr(880) }.scope
220 // use an lfo to modulate the amplitude of the modulator
222         {
223                 SinOsc.ar(440, 0, 0.571)
224                 *
225                 (SinOsc.kr(880)                         // wrap the modulator and the lfo in parenthese
226                 *                                       // why ... ?
227                 SinOsc.kr([6.99, 8.01].reciprocal)
228                 )
229         }.scope
233 section::Amplitude modulation
235 Multiply two UGens and restrict the value of the modulator to positive values (use the .abs message to calculate 'absolute' value) to create what Charles Dodge calls "classic" amplitude modulation.
237 code::
238 // use an lfo to modulate the amplitude of the modulator
240         {
241                 SinOsc.ar(440, 0, 0.571)
242                 *
243                 (SinOsc.kr(880).abs                     // wrap the modulator and the lfo in parenthese
244                 *                                       // why ... ?
245                 SinOsc.kr([6.99, 8.01].reciprocal)
246                 )
247         }.scope
251 ////////////////////////////////////////////////////////////////////////////////////////////////////
253 Compare "classic" amplitude modulation and ring modulation
255 code::
256 // "classic"
257 { SinOsc.ar(440, 0, 0.571) * SinOsc.kr(880).abs }.scope
259 // "ring"
260 // ... what's the difference?
261 { SinOsc.ar(440, 0, 0.571) * SinOsc.kr(880) }.scope
264 ////////////////////////////////////////////////////////////////////////////////////////////////////
266 go to link::Tutorials/Mark_Polishook_tutorial/14_Subtractive_synthesis::