class library: SynthDef - lazy implementation of removeUGen
[supercollider.git] / HelpSource / Classes / Klank.schelp
blob87e38f21fc4fbacdbe9b616fc2f44228a4d519b0
1 class:: Klank
2 summary:: Bank of resonators
3 related:: Classes/DynKlank, Classes/Klang
4 categories::  UGens>Generators>Deterministic, UGens>Filters>Linear
7 Description::
9 Klank is a bank of fixed frequency resonators which can be used to
10 simulate the resonant modes of an object. Each mode is given a ring time,
11 which is the time for the mode to decay by 60 dB.
14 classmethods::
16 method::ar
18 argument::specificationsArrayRef
19 A link::Classes/Ref:: to an link::Classes/Array:: of three Arrays:
21 definitionlist::
22 ## frequencies: || An Array of filter frequencies.
23 ## amplitudes: || an Array of filter amplitudes, or nil. If nil, then amplitudes default to 1.0.
24 ## ring times: || an Array of 60 dB decay times for the filters.
26 All subarrays, if not nil, should have the same length.
28 argument::input
29 The excitation input to the resonant filter bank.
31 argument::freqscale
32 A scale factor multiplied by all frequencies at initialization time.
34 argument::freqoffset
35 An offset added to all frequencies at initialization time.
37 argument::decayscale
38 A scale factor multiplied by all ring times at initialization time.
40 discussion::
41 The parameters in code::specificationsArrayRef:: can't be changed after it has been started.
42 For a modulatable but less efficient version, see link::Classes/DynKlank::.
45 Examples::
47 code::
48 { Klank.ar(`[[800, 1071, 1153, 1723], nil, [1, 1, 1, 1]], Impulse.ar(2, 0, 0.1)) }.play;
50 { Klank.ar(`[[800, 1071, 1353, 1723], nil, [1, 1, 1, 1]], Dust.ar(8, 0.1)) }.play;
52 { Klank.ar(`[[800, 1071, 1353, 1723], nil, [1, 1, 1, 1]], PinkNoise.ar(0.007)) }.play;
54 { Klank.ar(`[[200, 671, 1153, 1723], nil, [1, 1, 1, 1]], PinkNoise.ar([0.007,0.007])) }.play;
57 play({
58         Klank.ar(`[
59                 Array.rand(12, 800.0, 4000.0),          // frequencies
60                 nil,                                                    // amplitudes (default to 1.0)
61                 Array.rand(12, 0.1, 2)                          // ring times
62                 ], Decay.ar(Impulse.ar(4), 0.03, ClipNoise.ar(0.01)))
68 // a synth def that has 4 partials
70 s = Server.local;
71 SynthDef("help-Control", { arg out=0,i_freq;
72         var klank, n, harm, amp, ring;
74         // harmonics
75         harm = Control.names([\harm]).ir(Array.series(4,1,1).postln);
76         // amplitudes
77         amp = Control.names([\amp]).ir(Array.fill(4,0.05));
78         // ring times
79         ring = Control.names([\ring]).ir(Array.fill(4,1));
81         klank = Klank.ar(`[harm,amp,ring], {ClipNoise.ar(0.003)}.dup, i_freq);
83         Out.ar(out, klank);
84 }).send(s);
87 // nothing special yet, just using the default set of harmonics.
88 a = Synth("help-Control",[\i_freq, 300]);
89 b = Synth("help-Control",[\i_freq, 400]);
90 c = Synth("help-Control",[\i_freq, 533.33]);
91 d = Synth("help-Control",[\i_freq, 711.11]);
93 a.free;
94 b.free;
95 c.free;
96 d.free;
98 // in order to set the harmonics amps and ring times at
99 // initialization time we need to use an OSC bundle.
101 s.sendBundle(nil,
102         ["/s_new", "help-Control", 2000, 1, 0, \i_freq, 500], // start note
103         ["/n_setn", 2000, "harm", 4, 1, 3, 5, 7] // set odd harmonics
104         );
107 s.sendMsg("/n_free", 2000);
110 s.sendBundle(nil,
111         ["/s_new", "help-Control", 2000, 1, 0, \i_freq, 500], // start note
112         // set geometric series harmonics
113         ["/n_setn", 2000, "harm", 4] ++ Array.geom(4,1,1.61)
114         );
117 s.sendMsg("/n_free", 2000);
120 // set harmonics, ring times and amplitudes
121 s.sendBundle(nil,
122         ["/s_new", "help-Control", 2000, 1, 0, \i_freq, 500], // start note
123         ["/n_setn", 2000, "harm", 4, 1, 3, 5, 7], // set odd harmonics
124         ["/n_setn", 2000, "ring", 4] ++ Array.fill(4,0.1), // set shorter ring time
125         ["/n_setn", 2000, "amp", 4] ++ Array.fill(4,0.2) // set louder amps
126         );
129 s.sendMsg(\n_trace, 2000);
130 s.sendMsg("/n_free", 2000);
133 // same effect as above, but packed into one n_setn command
134 s.sendBundle(nil,
135         ["/s_new", "help-Control", 2000, 1, 0, \i_freq, 500], // start note
136         ["/n_setn", 2000, "harm", 4, 1, 3, 5, 7,
137                 "ring", 4] ++ Array.fill(4,0.1)
138                 ++ ["amp", 4] ++ Array.fill(4,0.2)
139         );
142 s.sendMsg("/n_free", 2000);
147 //////////////////////////////////////////////////////////////////////
149 //-- overlap texture
151 SynthDef("help-KlankOverlapTexture",
152 {|out = 0, freqs = #[0,0,0,0,0,0,0,0,0,0,0,0], rings = #[0,0,0,0,0,0,0,0,0,0,0,0], atk = 5, sus = 8, rel = 5, pan = 0|
153         var e = EnvGen.kr(Env.linen(atk, sus, rel, 1, 4), doneAction:2);
154         var i = Decay.ar(Impulse.ar(Rand(0.8, 2.2)), 0.03, ClipNoise.ar(0.01));
155         var z = Klank.ar(
156                 `[freqs, nil, rings],   // specs
157                 i                                       // input
158         );
159         Out.ar(out, Pan2.ar(z*e, pan));
160 }).send(s);
162 r = Routine{
163         var sustain = 8, transition = 3, overlap = 4;
164         var period = transition*2+sustain/overlap;
165         0.5.wait;                       //wait for the synthdef to be sent to the server
166         inf.do{
167                 Synth("help-KlankOverlapTexture", [
168                         \atk, transition,
169                         \sus, sustain,
170                         \rel, transition,
171                         \pan, 1.0.rand2
172                 ]).setn(
173                         \freqs, {200.0.rrand(4000)}.dup(12),
174                         \rings, {0.1.rrand(2)}.dup(12)
175                 );
176                 period.wait;
177         }
179 r.play;
182 r.stop; //stop spawning new synths
186 //-- frequency and decay scaling
188 SynthDef("help-KlankScaling", {|out = 0, freq = 0, rings = #[0,0,0,0,0,0,0,0,0,0,0,0], pan = 0|
189         var e = EnvGen.kr(Env(#[1, 1, 0], #[0.4, 0.01]), doneAction:2);
190         var i = Decay.ar(Impulse.ar(0), 0.03, ClipNoise.ar(0.01));
191         var z = Klank.ar(
192                 `[(1..12), nil, rings],                         // specs (partials, amplitudes, ringtimes)
193                 i,                                                              // input
194                 freq,                                                   // scale to this frequency
195                 0,                                                              // frequency offset
196                 MouseX.kr(0.2, 3)                                       // scale decay times
197         );
198         Out.ar(out, Pan2.ar(z*e, pan));
199 }).send(s);
201 r = Routine{
202         var sustain = 8, transition = 3;
203         var mode = #[0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23, 24];
204         0.5.wait;                       //wait for the synthdef to be sent to the server
205         inf.do{|i|
206                 Synth("help-KlankScaling", [
207                         \freq, (72 + (mode @@ i)).midicps
208                 ]).setn(\rings, {0.1.rrand(2)}.dup(12));
209                 0.2.wait;
210         }
212 r.play;
215 r.stop;
218 //-- overlap texture 2
220 SynthDef("help-KlankOverlapTexture2",
221 {|out = 0, freqs = #[0,0,0,0,0,0,0,0,0,0,0,0], rings = #[0,0,0,0,0,0,0,0,0,0,0,0], atk = 5, sus = 8, rel = 5, pan = 0|
222         var e = EnvGen.kr(Env.linen(atk, sus, rel, 1, 4), doneAction:2);
223         var i = BrownNoise.ar(0.0012);
224         var z = Klank.ar(
225                 `[freqs, nil, rings],   // specs
226                 i                                       // input
227         );
228         Out.ar(out, Pan2.ar(z*e, pan));
229 }).send(s);
231 r = Routine{
232         var sustain = 6, transition = 4, overlap = 5;
233         var period = transition*2+sustain/overlap;
234         0.5.wait;                       //wait for the synthdef to be sent to the server
235         inf.do{
236                 Synth("help-KlankOverlapTexture2", [
237                         \atk, transition,
238                         \sus, sustain,
239                         \rel, transition,
240                         \pan, 1.0.rand2
241                 ]).setn(
242                         \freqs, {6000.0.linrand+80}.dup(12),
243                         \rings, {0.1.rrand(3)}.dup(12)
244                 );
245                 period.wait;
246         }
248 r.play;
251 r.stop;
254 //-- overlap texture 3
256 SynthDef("help-KlankOverlapTexture3",
257 {|out = 0, freqs = #[0,0,0,0,0,0,0,0,0,0,0,0], rings = #[0,0,0,0,0,0,0,0,0,0,0,0], pan = 0|
258         var e = EnvGen.kr(Env(#[1, 1, 0], #[18, 3]), doneAction:2);
259         var i = Decay.ar(Impulse.ar(Rand(0.2, 0.6)), 0.8, ClipNoise.ar(0.001));
260         var z = Klank.ar(
261                 `[freqs, 2, rings],     // specs
262                 i                                       // input
263         );
264         Out.ar(out, Pan2.ar(z*e, pan));
265 }).send(s);
267 r = Routine{
268         0.5.wait;                       //wait for the synthdef to be sent to the server
269         inf.do{
270                 Synth("help-KlankOverlapTexture3", [
271                         \pan, 1.0.rand2
272                 ]).setn(
273                         \freqs, {12000.0.linrand+80}.dup(12),
274                         \rings, {3.rrand(10)}.dup(12)
275                 );
276                 3.wait;
277         }
279 r.play;
282 r.stop;