Debian Codegraph @

examples/special/LiSa-munger2.ck

raw
1 //--------------------------------------------------------------------
2 // name: LiSa-munger2.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 2 (below) demonstrates the rotating buffer approach used
15 // in the munger~; see after the code for an explanation of this
16 // approach. presumably someone smarter than me will dream up a more
17 // elegant technique.
18 //--------------------------------------------------------------------
19
20 // oscillator as source
21 // fun scary sounds!!!
22 SinOsc s => dac;
23 // set frequency
24 s.freq( 440 );
25 // set gain
26 s.gain( 0.25 );
27
28 // modulate
29 SinOsc freqmod => blackhole;
30 // modulation frequency
31 freqmod.freq( 0.1 );
32
33 // use three LiSas
34 LiSa lisa[3];
35 // buffer length
36 1::second => dur bufferlen;
37 0 => int recbuf;
38 2 => int playbuf;
39
40 // LiSa params
41 for( 0 => int i; i < 3; i++ )
42 {
43 lisa[i].duration( bufferlen );
44 lisa[i].maxVoices( 30 );
45 lisa[i].clear();
46 lisa[i].gain( 0.1 );
47 lisa[i].feedback( 0.5 );
48 lisa[i].recRamp( 20::ms );
49 lisa[i].record( false );
50 // connect s to each LiSa
51 s => lisa[i] => dac;
52 }
53
54 // start recording in the recbuf LiSa
55 lisa[recbuf].record( true );
56
57 // create grains, rotate record and play bufs as needed
58 // shouldn't click as long as the grainlen < bufferlen
59 while( true )
60 {
61 // compute later time
62 now + bufferlen => time later;
63
64 // toss some grains
65 while( now < later )
66 {
67 // new rate and duration
68 Math.random2f(0.5, 2.5) => float newrate;
69 Math.random2f(250, 600)::ms => dur newdur;
70
71 // spork a grain!
72 spork ~ getgrain(playbuf, newdur, 20::ms, 20::ms, newrate);
73 // freq
74 freqmod.last() * 400. + 800. => s.freq;
75 // advance time
76 10::ms => now;
77 }
78
79 // rotate the record LiSa
80 lisa[recbuf++].record( false );
81 if( recbuf == 3 ) 0 => recbuf;
82 lisa[recbuf].record( true );
83
84 // rotate the play LiSa
85 playbuf++;
86 if( playbuf == 3 ) 0 => playbuf;
87 }
88
89 // sporkee: a grain!
90 fun void getgrain( int which, dur grainlen, dur rampup, dur rampdown, float rate )
91 {
92 // get an available voice
93 lisa[which].getVoice() => int newvoice;
94
95 // make sure we got a valid voice
96 if( newvoice > -1 )
97 {
98 // set play rate
99 lisa[which].rate(newvoice, rate);
100 // set play position
101 lisa[which].playPos(newvoice, Math.random2f(0,1) * bufferlen);
102 // set ramp up duration
103 lisa[which].rampUp(newvoice, rampup);
104 // wait for grain length (minus the ramp-up and -down)
105 (grainlen - (rampup + rampdown)) => now;
106 // set ramp down duration
107 lisa[which].rampDown(newvoice, rampdown);
108 // for ramp down duration
109 rampdown => now;
110 }
111 }
112
113 /*--------------------------------------------------------------------
114 Rotating Buffer Explanation (from the munger~ source code)
115
116 the munger~ (and scrubber~) use a rotating three-buffer scheme for
117 creating clickless grains. basically, you don't want to be recording
118 new data anywhere in the buffer where a grain might be playing. so,
119 we divide the buffer into three equal parts (B1, B2, B3). at the
120 outset, let's say:
121
122 B1 = the recording buffer part (RB)
123 B2 = a part where nothing is happening (QB, for Quiet Buffer)
124 B3 = a part where grains are playing from (PB)
125
126 let's say each part is N samples long. after we have recorded N
127 sample into B1, we rotate the functionality, so now:
128
129 B1 = PB
130 B2 = RB
131 B3 = QB
132
133 why?
134
135 as long as the grains are guaranteed to be no longer than N samples
136 (adjusted for playback rate) we are assured that none of them will
137 have clicks due to recording discontinuities. we need the Quiet
138 Buffer because when the rotation happens, there may still be grains
139 playing in what was the PB and is now the QB.
140
141 so, this is a bit cumbersome, but it works. the code here is super
142 duper ugly -- i was learning to code and working out this scheme on
143 the fly, and the code reflects that, but it does work.
144 --------------------------------------------------------------------*/