Debian Codegraph @

examples/special/LiSa-munger3.ck

raw
1 //--------------------------------------------------------------------
2 // name: LiSa-munger3.ck
3 // desc: Live sampling utilities for ChucK
4 //
5 // author: Dan Trueman, 2007
6 //
7 // These three example files demonstrate a couple ways to approach
8 // granular sampling with ChucK and LiSa. All are modeled after the
9 // munger~ from PeRColate. One of the cool things about doing this
10 // in ChucK is that there is a lot more ready flexibility in
11 // designing grain playback patterns; rolling one's own idiosyncratic
12 // munger is a lot easier.
13 //
14 // Example 3 (below) uses the same structure as Example 2, but
15 // replicates the groovy tune munging from the original munger~
16 // helppatch (with pitch transposition filtering and all!).
17 //--------------------------------------------------------------------
18
19 // oscillator as source
20 SinOsc s => dac;
21 s.freq( 440 );
22 s.gain( 0.25 );
23
24 // play the munger song!
25 Envelope fsmooth => blackhole;
26 spork ~ playtune(250::ms);
27 spork ~ smoothtune(20::ms);
28
29 // transposition table
30 [0, 4, 7, -2, 12, 15] @=> int pitchtable[];
31
32 // use three buffers to avoid clicks
33 LiSa lisa[3];
34 // allocated buffer size -- remains static
35 1::second => dur bufferlen;
36 // portion of the buffer size to use -- can vary
37 0.1::second => dur reclooplen;
38 // indices for rec and play buffers
39 0 => int recbuf;
40 2 => int playbuf;
41
42 // go through each LiSa
43 for( 0 => int i; i < lisa.size(); i++ )
44 {
45 // set LiSa params
46 lisa[i].duration(bufferlen);
47 lisa[i].loopEndRec(reclooplen);
48 lisa[i].maxVoices(30);
49 lisa[i].clear();
50 lisa[i].gain(0.2);
51 // if you want to retain earlier passes through the recording buff when loop recording:
52 // lisa[i].feedback(0.5);
53 // ramp at extremes of record buffer while recording
54 lisa[i].recRamp(20::ms);
55 lisa[i].record( false );
56 // connect s to each LiSa
57 s => lisa[i] => dac;
58 }
59
60 // start recording in the recbuf LiSa
61 lisa[recbuf].record( true );
62
63 // create grains, rotate record and play bufs as needed
64 // shouldn't click as long as the grainlen < bufferlen
65 while( true )
66 {
67 // will update record and playbufs to use every reclooplen
68 now + reclooplen => time later;
69
70 // toss some grains
71 while( now < later )
72 {
73 Math.random2f(0, 6) $ int => int newpitch; // choose a transposition from the table
74 Std.mtof(pitchtable[newpitch] + 60)/Std.mtof(60) => float newrate;
75 Math.random2f(50, 100) * 1::ms => dur newdur; // create a random duration for the grain
76
77 // spork off a grain!
78 spork ~ getgrain( playbuf, newdur, 20::ms, 20::ms, newrate );
79
80 // wait a bit.... then do it again, until we reach reclooplen
81 5::ms => now;
82 }
83
84 // rotate the record LiSa
85 lisa[recbuf++].record( false );
86 if( recbuf == lisa.size() ) 0 => recbuf;
87 lisa[recbuf].record( true );
88
89 // rotate the play LiSa
90 playbuf++;
91 if( playbuf == lisa.size() ) 0 => playbuf;
92 }
93
94 // for sporking grains; can do lots of different stuff here -- just one example here
95 fun void getgrain(int which, dur grainlen, dur rampup, dur rampdown, float rate)
96 {
97 // get an available voice
98 lisa[which].getVoice() => int newvoice;
99
100 // make sure we got a valid voice
101 if( newvoice > -1 )
102 {
103 // set play rate
104 lisa[which].rate(newvoice, rate);
105 // set play position
106 lisa[which].playPos(newvoice, Math.random2f(0,1) * reclooplen);
107 // set ramp up duration
108 lisa[which].rampUp(newvoice, rampup);
109 // wait for grain length (minus the ramp-up and -down)
110 (grainlen - (rampup + rampdown)) => now;
111 // set ramp down duration
112 lisa[which].rampDown(newvoice, rampdown);
113 // for ramp down duration
114 rampdown => now;
115 }
116 }
117
118 // the munger song lives! thanks to luke dubois....
119 fun void playtune( dur notelen )
120 {
121 // notes
122 [45, 45, 57, 57, 45, 57, 57, 47, 55, 47, 59, 60, 60, 57, 57, 57] @=> int notes[];
123 // note counter
124 0 => int notectr;
125
126 // set frequency to first note
127 Std.mtof(notes[0]) => fsmooth.value => s.freq;
128
129 // infinite time-loop
130 while( true )
131 {
132 // set frequency target
133 Std.mtof(notes[notectr++] + 12) => fsmooth.target;
134 // wrap note counter if reach end
135 if(notectr == notes.size()) 0 => notectr;
136 // wait for note length
137 notelen => now;
138 }
139 }
140
141 // smooth note to note
142 fun void smoothtune( dur smoothtime )
143 {
144 // set smooth duration
145 fsmooth.duration( smoothtime );
146 while( true )
147 {
148 fsmooth.value() => s.freq;
149 1::ms => now;
150 }
151 }