Debian Codegraph @

examples/special/twilight/twilight-granular-kb-interp.ck

raw
1 //--------------------------------------------------------------------
2 // name: twilight-granular-kb-interp.ck
3 // desc: Twilight (2013) granular synthesis instrument redux,
4 // keyboard (interpolated) edition --
5 // adapted from the original Gametrak-based instrument
6 //
7 // Twilight (2013) for laptop orchestra:
8 // http://slork.stanford.edu/works/twilight/
9 // https://www.youtube.com/watch?v=chA-4GRCb-I
10 // https://vimeo.com/100624271
11 //
12 // author: Ge Wang ([email protected])
13 // date: initial instrument: Spring 2013
14 // redux version: Summer 2014
15 // keyboard version: Winter 2015
16 //
17 // usage (loads twilight-source.aiff by default):
18 // chuck twilight-granular-kb
19 // usage (with argument):
20 // chuck twilight-granular-kb:SOUND.WAV
21 //
22 // keyboard control (also see kb() below):
23 // UP/DOWN -- grain position
24 // LEFT/DOWN -- grain rate / tuning
25 // ','/'.' -- grain size
26 // '-'/'=' -- grain position random amount
27 //--------------------------------------------------------------------
28
29 // default filename (can be overwritten via input argument)
30 "twilight-granular.aiff" => string FILENAME;
31 // get file name, if one specified as input x0argument
32 if( me.args() > 0 ) me.arg(0) => FILENAME;
33
34 // overall volume
35 1 => float MAIN_VOLUME;
36 // grain duration base
37 50::ms => dur GRAIN_LENGTH;
38 // factor relating grain duration to ramp up/down time
39 .5 => float GRAIN_RAMP_FACTOR;
40 // playback rate
41 1 => float GRAIN_PLAY_RATE;
42 // grain position (0 start; 1 end)
43 0 => float GRAIN_POSITION;
44 // grain position goal (for interp)
45 0 => float GRAIN_POSITION_GOAL;
46 // grain position randomization
47 .001 => float GRAIN_POSITION_RANDOM;
48 // grain jitter (0 == periodic fire rate)
49 1 => float GRAIN_FIRE_RANDOM;
50
51 // max lisa voices
52 30 => int LISA_MAX_VOICES;
53 // load file into a LiSa (use one LiSa per sound)
54 load( FILENAME ) @=> LiSa @ lisa;
55
56 // patch it
57 PoleZero blocker => NRev reverb => dac;
58 // connect
59 lisa.chan(0) => blocker;
60
61 // reverb mix
62 .05 => reverb.mix;
63 // pole location to block DC and ultra low frequencies
64 .99 => blocker.blockZero;
65
66
67 // HID objects
68 Hid hi;
69 HidMsg msg;
70
71 // which joystick
72 0 => int device;
73 // get from command line
74 if( me.args() ) me.arg(0) => Std.atoi => device;
75
76 // open joystick 0, exit on fail
77 if( !hi.openKeyboard( device ) ) me.exit();
78 // log
79 <<< "keyboard '" + hi.name() + "' ready", "" >>>;
80
81 // keycodes (for MacOS; may need to change for other systems)
82 45 => int KEY_DASH;
83 46 => int KEY_EQUAL;
84 54 => int KEY_COMMA;
85 55 => int KEY_PERIOD;
86 79 => int KEY_RIGHT;
87 80 => int KEY_LEFT;
88 81 => int KEY_DOWN;
89 82 => int KEY_UP;
90
91 // spork it
92 spork ~ print();
93 spork ~ kb();
94 spork ~ interp( .025, 5::ms );
95
96 // main loop
97 while( true )
98 {
99 // fire a grain
100 fireGrain();
101 // amount here naturally controls amount of overlap between grains
102 GRAIN_LENGTH / 8 + Math.random2f(0,GRAIN_FIRE_RANDOM)::ms => now;
103 }
104
105 // fire!
106 fun void fireGrain()
107 {
108 // grain length
109 GRAIN_LENGTH => dur grainLen;
110 // ramp time
111 GRAIN_LENGTH * GRAIN_RAMP_FACTOR => dur rampTime;
112 // play pos
113 GRAIN_POSITION + Math.random2f(0,GRAIN_POSITION_RANDOM) => float pos;
114 // a grain
115 if( lisa != null && pos >= 0 )
116 spork ~ grain( lisa, pos * lisa.duration(), grainLen, rampTime, rampTime,
117 GRAIN_PLAY_RATE );
118 }
119
120 // interp
121 fun void interp( float slew, dur RATE )
122 {
123 while( true )
124 {
125 // interp grain position
126 (GRAIN_POSITION_GOAL - GRAIN_POSITION)*slew + GRAIN_POSITION => GRAIN_POSITION;
127 // interp rate
128 RATE => now;
129 }
130 }
131
132 // grain sporkee
133 fun void grain( LiSa @ lisa, dur pos, dur grainLen, dur rampUp, dur rampDown, float rate )
134 {
135 // get a voice to use
136 lisa.getVoice() => int voice;
137
138 // if available
139 if( voice > -1 )
140 {
141 // set rate
142 lisa.rate( voice, rate );
143 // set playhead
144 lisa.playPos( voice, pos );
145 // ramp up
146 lisa.rampUp( voice, rampUp );
147 // wait
148 (grainLen - rampUp) => now;
149 // ramp down
150 lisa.rampDown( voice, rampDown );
151 // wait
152 rampDown => now;
153 }
154 }
155
156 // print
157 fun void print()
158 {
159 // time loop
160 while( true )
161 {
162 // values
163 <<< "pos:", GRAIN_POSITION, "random:", GRAIN_POSITION_RANDOM,
164 "rate:", GRAIN_PLAY_RATE, "size:", GRAIN_LENGTH/second >>>;
165 // advance time
166 100::ms => now;
167 }
168 }
169
170 // keyboard
171 fun void kb()
172 {
173 // infinite event loop
174 while( true )
175 {
176 // wait on HidIn as event
177 hi => now;
178
179 // messages received
180 while( hi.recv( msg ) )
181 {
182 // button donw
183 if( msg.isButtonDown() )
184 {
185 if( msg.which == KEY_LEFT )
186 {
187 .005 -=> GRAIN_PLAY_RATE;
188 if( GRAIN_PLAY_RATE < 0 ) 0 => GRAIN_PLAY_RATE;
189 }
190 else if( msg.which == KEY_RIGHT )
191 {
192 .005 +=> GRAIN_PLAY_RATE;
193 if( GRAIN_PLAY_RATE > 2 ) 2 => GRAIN_PLAY_RATE;
194 }
195 else if( msg.which == KEY_DOWN )
196 {
197 .01 -=> GRAIN_POSITION_GOAL;
198 if( GRAIN_POSITION_GOAL < 0 ) 0 => GRAIN_POSITION_GOAL;
199 }
200 else if( msg.which == KEY_UP )
201 {
202 .01 +=> GRAIN_POSITION_GOAL;
203 if( GRAIN_POSITION_GOAL > 1 ) 1 => GRAIN_POSITION_GOAL;
204 }
205 else if( msg.which == KEY_COMMA )
206 {
207 .95 *=> GRAIN_LENGTH;
208 if( GRAIN_LENGTH < 1::ms ) 1::ms => GRAIN_LENGTH;
209 }
210 else if( msg.which == KEY_PERIOD )
211 {
212 1.05 *=> GRAIN_LENGTH;
213 if( GRAIN_LENGTH > 1::second ) 1::second => GRAIN_LENGTH;
214 }
215 else if( msg.which == KEY_DASH )
216 {
217 .9 *=> GRAIN_POSITION_RANDOM;
218 if( GRAIN_POSITION_RANDOM < .000001 ) .000001 => GRAIN_POSITION_RANDOM;
219 }
220 else if( msg.which == KEY_EQUAL )
221 {
222 1.1 *=> GRAIN_POSITION_RANDOM;
223 if( GRAIN_POSITION_RANDOM > 1 ) 1 => GRAIN_POSITION_RANDOM;
224 }
225 }
226 }
227 }
228 }
229
230 // load file into a LiSa
231 fun LiSa load( string filename )
232 {
233 // sound buffer
234 SndBuf buffy;
235 // load it
236 filename => buffy.read;
237
238 // new LiSa
239 LiSa lisa;
240 // set duration
241 buffy.samples()::samp => lisa.duration;
242
243 // transfer values from SndBuf to LiSa
244 for( 0 => int i; i < buffy.samples(); i++ )
245 {
246 // args are sample value and sample index
247 // (dur must be integral in samples)
248 lisa.valueAt( buffy.valueAt(i), i::samp );
249 }
250
251 // set LiSa parameters
252 lisa.play( false );
253 lisa.loop( false );
254 lisa.maxVoices( LISA_MAX_VOICES );
255
256 return lisa;
257 }