Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / examples / demonstrations / SC2-examples_2.scd
blob094c6249c341f132223f7a936085e3e75ed8a2e1
3 //////////////////////////////////////////////////////////////////////////
6 // analog bubbles - with mouse control
7 play({
8         var freq;
9         freq = LFSaw.kr(
10                 MouseY.kr(0.1,10,'exponential'),        // lfo 1 rate
11                 24,                                                     // lfo 1 depth in semitones
12                 // lfo 2 in lfo 1's add input :
13                 LFSaw.kr(
14                         MouseX.kr(2,40,'exponential'),  // lfo 2 rate
15                         -3, 80                                  // lfo 2 depth & offset in semitones
16                 )
17         ).midicps; // convert to frequency
18         CombN.ar(SinOsc.ar(freq, 0, 0.04), 0.2, 0.2, 2) // echoing sine wave
20 )  
22 //////////////////////////////////////////////////////////////////////////
23 //////////////////////////////////////////////////////////////////////////
26         // sample and hold liquidities
27         // mouse x controls clock rate, mouse y controls center frequency
29         var clockRate, clockTime, clock, centerFreq, freq, panPos, patch;
31         clockRate = MouseX.kr(1, 200, 'exponential');
32         clockTime = clockRate.reciprocal;
33         clock = Impulse.kr(clockRate, 0.4);
35         centerFreq = MouseY.kr(100, 8000, 'exponential');
36         freq = Latch.kr(WhiteNoise.kr(centerFreq * 0.5, centerFreq), clock);
37         panPos = Latch.kr(WhiteNoise.kr, clock);
38         patch = CombN.ar(
39                         Pan2.ar( 
40                                 SinOsc.ar(
41                                         freq, 
42                                         0, 
43                                         Decay2.kr(clock, 0.1 * clockTime, 0.9 * clockTime)
44                                 ), 
45                                 panPos
46                         ),
47                         0.3, 0.3, 2
48                 );
49         patch
50 }.play
53 //////////////////////////////////////////////////////////////////////////
56 //////////////////////////////////////////////////////////////////////////
59 // distort input
60
61         var gain;
62         gain = MouseX.kr(1,100,'exponential');  // mouse x controls gain into distortion
63         AudioIn.ar([1,2], gain).distort * 0.4
64 }.play))
66 //////////////////////////////////////////////////////////////////////////
69 //////////////////////////////////////////////////////////////////////////
72 // ring modulate input
73
74         var input, modulator;
75         input = AudioIn.ar([1,2]);
76         modulator = SinOsc.ar(
77                                 MouseX.kr(10,4000,'exponential'),       // mouse x controls ring mod freq
78                                 [0,0.5pi]                                               // offset phase ofone osc by 90 degrees
79                         );
80         input * modulator
81 }.play
84 //////////////////////////////////////////////////////////////////////////
87 //////////////////////////////////////////////////////////////////////////
90 // ring modulate input using ring1
91
92         var input, modulator;
93         input = AudioIn.ar([1,2], 0.5);
94         modulator = SinOsc.ar(
95                                 MouseX.kr(10,4000,'exponential'),       // mouse x controls ring mod freq
96                                 [0,0.5pi]                                               // offset phase ofone osc by 90 degrees
97                         );
98         input ring1: modulator
99 }.play
102 //////////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////////
108 // filter the input
110         var rQ;
111         rQ = MouseY.kr(0.01, 1, 'exponential'); // bandwidth ratio = 1/Q
112         RLPF.ar(
113                 AudioIn.ar([1,2], 0.4 * rQ.sqrt),               // attenuate to offset resonance
114                 MouseX.kr(100, 12000, 'exponential'),   // mouse x controls cutoff freq
115                 rQ              
116         )
117 }.play
120 //////////////////////////////////////////////////////////////////////////
125 // input limiter
127         CompanderD.ar(
128                 AudioIn.ar([1,2]), 
129                 MouseX.kr(0.01, 0.5),           // threshold
130                 1,              // below threshold slope
131                 0.1             // above threshold slope
132         )
133 }.play
136 //////////////////////////////////////////////////////////////////////////
137 //////////////////////////////////////////////////////////////////////////
140 // input noise gate
142         var input;
143         input = AudioIn.ar([1,2]);
144         Compander.ar(
145                 input, input,
146                 MouseX.kr(0.01, 0.5),           // threshold
147                 10,             // below threshold slope
148                 1               // above threshold slope
149         )
150 }.play
153 //////////////////////////////////////////////////////////////////////////
156 //////////////////////////////////////////////////////////////////////////
159 // echo input
161         var in;
162         in = AudioIn.ar([1,2], 0.1);
163         CombL.ar(
164                 in, 
165                 0.5,                            // max delay time
166                 MouseX.kr(0,0.5),       // mouse x controls delay time
167                 4,                              // echo 60 dB decay time in seconds
168                 1,                              // scale by unity
169                 in                              // mix with input
170         )
171 }.play
174 //////////////////////////////////////////////////////////////////////////
177 //////////////////////////////////////////////////////////////////////////
180 // ring modulate & echo input
182         var in;
183         in = AudioIn.ar([1,2], 0.4) * SinOsc.ar(MouseX.kr(10,2000,'exponential'), [0,0.5pi]);
184         CombL.ar(
185                 in,
186                 0.5, 
187                 MouseY.kr(0,0.5),       // mouse y controls delay time
188                 4,                              // echo 60 dB decay time in seconds
189                 1,                              // scale by unity
190                 in                              // mix with input
191         )
192 }.play
195 //////////////////////////////////////////////////////////////////////////
196 //////////////////////////////////////////////////////////////////////////
199 // ring modulated and resonant filtered input
201         var input, modulator;
202         input = AudioIn.ar([1,2],0.2);
203         modulator = SinOsc.ar(
204                                 MouseX.kr(10,4000,'exponential'),       // mouse x controls ring mod freq
205                                 [0,0.5pi]                                               // offset phase ofone osc by 90 degrees
206                         );
207         RLPF.ar(
208                 input * modulator,                                      // do ring modulation
209                 MouseY.kr(100, 12000, 'exponential'),   // mouse y controls cutoff freq
210                 0.1)                                                            // bandwidth ratio = 1/Q
211 }.play
214 //////////////////////////////////////////////////////////////////////////
215 //////////////////////////////////////////////////////////////////////////
218 // distort, ring modulate & echo input - a real noise fest
220         var in;
221         in = AudioIn.ar([1,2], 20).distort.ring1(
222                         SinOsc.ar(MouseX.kr(10,2000,'exponential'), [0,0.5pi])
223                 ) * 0.02; 
224         CombL.ar(
225                 in,
226                 0.5, 
227                 MouseY.kr(0,0.5), // mouse y controls delay time
228                 4,
229                 1,
230                 in
231         )
232 }.play
235 //////////////////////////////////////////////////////////////////////////
237 //////////////////////////////////////////////////////////////////////////
240 // sweep verb
242         var s, z, y;
244         s = AudioIn.ar([1,2], 0.01) ;
245         
246                 // reverb predelay time :
247         z = DelayN.ar(Mix.ar(s), 0.048);
248         
249                 // 6 modulated comb delays in parallel :
250         y = Mix.ar(CombL.ar(z, 0.1, LFNoise1.kr(Array.fill(6,{0.1.rand}), 0.04, 0.05), 15)); 
251         
252                 // chain of 4 allpass delays on each of two channels (8 total) :
253         4.do({ y = AllpassN.ar(y, 0.050, [0.050.rand,0.050.rand], 1) });
254         
255         // eliminate DC
256         LeakDC.ar(y)
257 }.play
261 //////////////////////////////////////////////////////////////////////////
264 // monastic resonance
265 // mouse controls size and reverb time
267         var s, z, y, decayTime, delayScale;
268         
269         decayTime = MouseX.kr(0,16);
270         delayScale = MouseY.kr(0.01, 1);
271         
272         s = AudioIn.ar([1,2], 0.005) ;
273         
274                 // reverb predelay time :
275         z = DelayN.ar(Mix.ar(s), 0.048);
276         
277                 // 8 comb delays in parallel :
278         y = Mix.ar(CombL.ar(z, 0.1, Array.fill(8,{0.04.rand2 + 0.05}) * delayScale, decayTime)); 
279         
280                 // chain of 5 allpass delays on each of two channels (10 total) :
281         5.do({ y = AllpassN.ar(y, 0.050, [0.050.rand,0.050.rand], 1) });
282         
283         // eliminate DC
284         LeakDC.ar(y)
285 }.play
289 //////////////////////////////////////////////////////////////////////////
292 // noise burst sweep
294         var lfoRate, amp, cfreq, filt;
296         lfoRate = MouseX.kr(10, 60, 'exponential');
297         amp = max(0, LFSaw.kr(lfoRate, -1));
298         cfreq = MouseY.kr(400, 8000, 'exponential');
299         cfreq = SinOsc.kr(0.2, 0, cfreq, 1.05 * cfreq);
300         Resonz.ar(WhiteNoise.ar(amp), cfreq, 0.1) ! 2;
301 }.play
304 //////////////////////////////////////////////////////////////////////////
309 // aleatoric quartet
310 // mouse x controls density
312         var amp, density, dmul, dadd, signal;
313         amp = 0.07;
314         density = MouseX.kr(0.01,1); // mouse determines density of excitation
315         
316         // calculate multiply and add for excitation probability
317         dmul = density.reciprocal * 0.5 * amp;
318         dadd = dmul.neg + amp;
319         
320         signal = Mix.ar(        // mix an array of 4 instruments
321                 Array.fill(4, {
322                         var excitation, freq;
323                         
324                         excitation = PinkNoise.ar(
325                                 // if amplitude is below zero it is clipped
326                                 // density determines the probability of being above zero
327                                 max(0, LFNoise1.kr(8, dmul, dadd)) 
328                         );
329                         
330                         freq = Lag.kr(                  // lag the pitch so it glissandos between pitches
331                                 LFNoise0.kr(                            // use low freq step noise as a pitch control
332                                         [1, 0.5, 0.25].choose,  // choose a frequency of pitch change
333                                         7,                                      // +/- 7 semitones
334                                         66 + 30.rand2                   // random center note
335                                 ).round(1),             // round to nearest semitone
336                                 0.2                             // gliss time
337                         ).midicps;                      // convert to hertz
338                         
339                         Pan2.ar(        // pan each intrument
340                                 CombL.ar(excitation, 0.02, freq.reciprocal, 3), // comb delay simulates string
341                                 1.0.rand2               // random pan position
342                         );
343         }));
344         
345         // add some reverb via allpass delays
346         5.do({ signal = AllpassN.ar(signal, 0.05, [0.05.rand,0.05.rand], 1) });
347         LeakDC.ar( signal, 0.995);              // delays build up a lot of DC, so leak it out here.
348 }.play;
355 //////////////////////////////////////////////////////////////////////
358     // strummable guitar
359     // use mouse to strum strings
361         var pitch, mousex, out;
362         // e a d g b e
363         pitch = [ 52, 57, 62, 67, 71, 76 ];
364         mousex = MouseX.kr;
365         out = Mix.arFill(pitch.size, { arg i;
366                 var trigger, pluck, period, string;
367                 // place trigger points from 0.25 to 0.75
368                 trigger = HPZ1.kr(mousex > (0.25 + (i * 0.1))).abs;
369                 pluck = PinkNoise.ar(Decay.kr(trigger, 0.05));
370                 period = pitch.at(i).midicps.reciprocal;
371                 string = CombL.ar(pluck, period, period, 4);
372                 Pan2.ar(string, i * 0.2 - 0.5);
373         });
374         LPF.ar(out, 12000);
375         LeakDC.ar(out);
376 }.play;
382 //////////////////////////////////////////////////////////////////////
385     // strummable 12 string guitar
386     // use mouse to strum strings
388         var pitch, mousex, out;
389         // e a d g b e
390         pitch = [ 52, 57, 62, 67, 71, 76 ];
391         mousex = MouseX.kr;
392         out = Mix.arFill(pitch.size, { arg i;
393                 var pos, trigger1, trigger2, pluck1, pluck2, period, string1, string2;
394                 // place trigger points from 0.25 to 0.75
395                 pos = 0.25 + (i * 0.1);
396                 period = pitch.at(i).midicps.reciprocal;
397                 
398                 trigger1 = HPZ1.kr(mousex > pos).abs;
399                 pluck1 = PinkNoise.ar(Decay.kr(trigger1, 0.05));
400                 string1 = CombL.ar(pluck1, period, period, 4);
401                 
402                 trigger2 = HPZ1.kr(mousex > (pos + 0.015)).abs;
403                 pluck2 = PinkNoise.ar(Decay.kr(trigger2, 0.05));
404                 string2 = CombL.ar(pluck2, period/2, period/2, 4);
405                 
406                 Pan2.ar(string1 + string2, i * 0.2 - 0.5);
407         });
408         LPF.ar(out, 12000);
409         LeakDC.ar(out);
410 }.play;
413 //////////////////////////////////////////////////////////////////////
416 //////////////////////////////////////////////////////////////////////
419     // bidirectional strummable guitar
420     // use mouse to strum strings
422         var pitch1, pitch2, mousex, out;
423         // e a d g b e
424         pitch1 = [ 52, 57, 62, 67, 71, 76 ];
425         pitch2 = pitch1 + 7;
426         mousex = MouseX.kr;
427         out = Mix.arFill(pitch1.size, { arg i;
428                 var trigger, pluck1, pluck2, period1, period2, string1, string2;
429                 // place trigger points from 0.25 to 0.75
430                 trigger = HPZ1.kr(mousex > (0.25 + (i * 0.1)));
431                 
432                 pluck1 = PinkNoise.ar(Decay.kr(trigger.max(0), 0.05));
433                 period1 = pitch1.at(i).midicps.reciprocal;
434                 string1 = CombL.ar(pluck1, period1, period1, 4);
435                 
436                 pluck2 = BrownNoise.ar(Decay.kr(trigger.neg.max(0), 0.05));
437                 period2 = pitch2.at(i).midicps.reciprocal;
438                 string2 = CombL.ar(pluck2, period2, period2, -4);
439                 
440                 Pan2.ar(string1 + string2, i * 0.2 - 0.5);
441         });
442         LPF.ar(out, 12000);
443         LeakDC.ar(out);
444 }.play;
447 //////////////////////////////////////////////////////////////////////
450     // harmonic zither
451     // use mouse to strum strings
453         var pitch, mousex, mousey, out, triggerSpacing, panSpacing;
454         // harmonic series
455         pitch = [ 50, 53.86, 57.02, 59.69, 62, 64.04, 65.86, 67.51, 69.02, 71.69, 72.88, 74 ];
456         mousex = MouseX.kr;
457         mousey = MouseY.kr;
458         triggerSpacing = 0.5 / (pitch.size - 1);
459         panSpacing = 1.5 / (pitch.size - 1);
460         out = Mix.arFill(pitch.size, { arg i;
461                 var trigger, pluck, period, string;
462                 // place trigger points from 0.25 to 0.75
463                 trigger = HPZ1.kr(mousex > (0.25 + (i * triggerSpacing))).abs;
464                 pluck = PinkNoise.ar(Decay.kr(trigger, 0.05));
465                 period = pitch.at(i).midicps.reciprocal;
466                 string = CombL.ar(pluck, period, period, 8);
467                 Pan2.ar(string, i * panSpacing - 0.75);
468         });
469         LPF.ar(out, 12000);
470         LeakDC.ar(out);
471 }.play;
474 //////////////////////////////////////////////////////////////////////
477     // strummable metals
478     // use mouse to strum strings
480         var mousex, out;
481         mousex = MouseX.kr;
482         out = Mix.arFill(8, { arg i;
483                 var trigger, pluck, period, metal, z, n=15;
484                 // place trigger points from 0.25 to 0.75
485                 trigger = HPZ1.kr(mousex > (0.25 + (i * 0.07))).abs;
486                 pluck = PinkNoise.ar(Decay.kr(trigger, 0.05, 0.04));
487                 
488                 z = `[  // filter bank specification :
489                                 y = Array.fill(n, { (300 * i) + 8000.0.linrand} ), // frequencies
490                                 nil,                                                    // amplitudes default to 1.0
491                                 Array.fill(n, { 1.0 + 4.0.rand })       // ring times
492                         ];
493                 metal = Klank.ar(z, pluck);
494                 //metal = SinOsc.ar(800, 0, 0.1);
495                 Pan2.ar(metal, i * 0.2 - 0.5);
496         });
497         LPF.ar(out, 12000);
498         LeakDC.ar(out);
499 }.play;
502 //////////////////////////////////////////////////////////////////////
505     // strummable pipes
507         var mousex, out;
508         mousex = MouseX.kr;
509         out = Mix.arFill(8, { arg i;
510                 var trigger, pluck, period, metal, z, n=15, freq;
511                 // place trigger points from 0.25 to 0.75
512                 trigger = HPZ1.kr(mousex > (0.25 + (i * 0.07))).abs;
513                 pluck = PinkNoise.ar(Lag.kr(Trig.kr(trigger, 1), 0.2, 0.01));
514                 freq = (#[0, 3, 5, 7, 10, 12, 15, 17].at(i) + 60).midicps;
515                 z = `[  // filter bank specification :
516                                 y = Array.fill(n, { arg j; (j+1) * freq } ), // frequencies
517                                 nil,                                                    // amplitudes default to 1.0
518                                 Array.fill(n, { 0.2.rand })     // ring times
519                         ];
520                 metal = Klank.ar(z, pluck);
521                 //metal = SinOsc.ar(800, 0, 0.1);
522                 Pan2.ar(metal, i * 0.2 - 0.5);
523         });
524         LPF.ar(out, 12000);
525         out = LeakDC.ar(out);
526         6.do({ out = AllpassN.ar(out, 0.1, [0.05.rand,0.05.rand], 4) });
527         out
528 }.play;
531 //////////////////////////////////////////////////////////////////////