| 1 |
//-------------------------------------------------------------------- |
| 2 |
// name: twilight-granular-kb.ck |
| 3 |
// desc: Twilight (2013) granular synthesis instrument redux, |
| 4 |
// keyboard (non-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 randomization |
| 45 |
.001 => float GRAIN_POSITION_RANDOM; |
| 46 |
// grain jitter (0 == periodic fire rate) |
| 47 |
1 => float GRAIN_FIRE_RANDOM; |
| 48 |
|
| 49 |
// max lisa voices |
| 50 |
30 => int LISA_MAX_VOICES; |
| 51 |
// load file into a LiSa (use one LiSa per sound) |
| 52 |
load( FILENAME ) @=> LiSa @ lisa; |
| 53 |
|
| 54 |
// patch it |
| 55 |
PoleZero blocker => NRev reverb => dac; |
| 56 |
// connect |
| 57 |
lisa.chan(0) => blocker; |
| 58 |
|
| 59 |
// reverb mix |
| 60 |
.05 => reverb.mix; |
| 61 |
// pole location to block DC and ultra low frequencies |
| 62 |
.99 => blocker.blockZero; |
| 63 |
|
| 64 |
|
| 65 |
// HID objects |
| 66 |
Hid hi; |
| 67 |
HidMsg msg; |
| 68 |
|
| 69 |
// which joystick |
| 70 |
0 => int device; |
| 71 |
// get from command line |
| 72 |
if( me.args() ) me.arg(0) => Std.atoi => device; |
| 73 |
|
| 74 |
// open joystick 0, exit on fail |
| 75 |
if( !hi.openKeyboard( device ) ) me.exit(); |
| 76 |
// log |
| 77 |
<<< "keyboard '" + hi.name() + "' ready", "" >>>; |
| 78 |
|
| 79 |
// keycodes (for MacOS; may need to change for other systems) |
| 80 |
45 => int KEY_DASH; |
| 81 |
46 => int KEY_EQUAL; |
| 82 |
54 => int KEY_COMMA; |
| 83 |
55 => int KEY_PERIOD; |
| 84 |
79 => int KEY_RIGHT; |
| 85 |
80 => int KEY_LEFT; |
| 86 |
81 => int KEY_DOWN; |
| 87 |
82 => int KEY_UP; |
| 88 |
|
| 89 |
// spork it |
| 90 |
spork ~ print(); |
| 91 |
spork ~ kb(); |
| 92 |
|
| 93 |
// main loop |
| 94 |
while( true ) |
| 95 |
{ |
| 96 |
// fire a grain |
| 97 |
fireGrain(); |
| 98 |
// amount here naturally controls amount of overlap between grains |
| 99 |
(GRAIN_LENGTH / 2 + Math.random2f(0,GRAIN_FIRE_RANDOM)::ms)/2 => now; |
| 100 |
} |
| 101 |
|
| 102 |
// fire! |
| 103 |
fun void fireGrain() |
| 104 |
{ |
| 105 |
// grain length |
| 106 |
GRAIN_LENGTH => dur grainLen; |
| 107 |
// ramp time |
| 108 |
GRAIN_LENGTH * GRAIN_RAMP_FACTOR => dur rampTime; |
| 109 |
// play pos |
| 110 |
GRAIN_POSITION + Math.random2f(0,GRAIN_POSITION_RANDOM) => float pos; |
| 111 |
// a grain |
| 112 |
if( lisa != null && pos >= 0 ) |
| 113 |
spork ~ grain( lisa, pos * lisa.duration(), grainLen, rampTime, rampTime, |
| 114 |
GRAIN_PLAY_RATE ); |
| 115 |
} |
| 116 |
|
| 117 |
// grain sporkee |
| 118 |
fun void grain( LiSa @ lisa, dur pos, dur grainLen, dur rampUp, dur rampDown, float rate ) |
| 119 |
{ |
| 120 |
// get a voice to use |
| 121 |
lisa.getVoice() => int voice; |
| 122 |
|
| 123 |
// if available |
| 124 |
if( voice > -1 ) |
| 125 |
{ |
| 126 |
// set rate |
| 127 |
lisa.rate( voice, rate ); |
| 128 |
// set playhead |
| 129 |
lisa.playPos( voice, pos ); |
| 130 |
// ramp up |
| 131 |
lisa.rampUp( voice, rampUp ); |
| 132 |
// wait |
| 133 |
(grainLen - rampUp) => now; |
| 134 |
// ramp down |
| 135 |
lisa.rampDown( voice, rampDown ); |
| 136 |
// wait |
| 137 |
rampDown => now; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// print |
| 142 |
fun void print() |
| 143 |
{ |
| 144 |
// time loop |
| 145 |
while( true ) |
| 146 |
{ |
| 147 |
// values |
| 148 |
<<< "pos:", GRAIN_POSITION, "random:", GRAIN_POSITION_RANDOM, |
| 149 |
"rate:", GRAIN_PLAY_RATE, "size:", GRAIN_LENGTH/second >>>; |
| 150 |
// advance time |
| 151 |
100::ms => now; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// keyboard |
| 156 |
fun void kb() |
| 157 |
{ |
| 158 |
// infinite event loop |
| 159 |
while( true ) |
| 160 |
{ |
| 161 |
// wait on HidIn as event |
| 162 |
hi => now; |
| 163 |
|
| 164 |
// messages received |
| 165 |
while( hi.recv( msg ) ) |
| 166 |
{ |
| 167 |
// button donw |
| 168 |
if( msg.isButtonDown() ) |
| 169 |
{ |
| 170 |
if( msg.which == KEY_LEFT ) |
| 171 |
{ |
| 172 |
.005 -=> GRAIN_PLAY_RATE; |
| 173 |
if( GRAIN_PLAY_RATE < 0 ) 0 => GRAIN_PLAY_RATE; |
| 174 |
} |
| 175 |
else if( msg.which == KEY_RIGHT ) |
| 176 |
{ |
| 177 |
.005 +=> GRAIN_PLAY_RATE; |
| 178 |
if( GRAIN_PLAY_RATE > 2 ) 2 => GRAIN_PLAY_RATE; |
| 179 |
} |
| 180 |
else if( msg.which == KEY_DOWN ) |
| 181 |
{ |
| 182 |
.01 -=> GRAIN_POSITION; |
| 183 |
if( GRAIN_POSITION < 0 ) 0 => GRAIN_POSITION; |
| 184 |
} |
| 185 |
else if( msg.which == KEY_UP ) |
| 186 |
{ |
| 187 |
.01 +=> GRAIN_POSITION; |
| 188 |
if( GRAIN_POSITION > 1 ) 1 => GRAIN_POSITION; |
| 189 |
} |
| 190 |
else if( msg.which == KEY_COMMA ) |
| 191 |
{ |
| 192 |
.95 *=> GRAIN_LENGTH; |
| 193 |
if( GRAIN_LENGTH < 1::ms ) 1::ms => GRAIN_LENGTH; |
| 194 |
} |
| 195 |
else if( msg.which == KEY_PERIOD ) |
| 196 |
{ |
| 197 |
1.05 *=> GRAIN_LENGTH; |
| 198 |
if( GRAIN_LENGTH > 1::second ) 1::second => GRAIN_LENGTH; |
| 199 |
} |
| 200 |
else if( msg.which == KEY_DASH ) |
| 201 |
{ |
| 202 |
.9 *=> GRAIN_POSITION_RANDOM; |
| 203 |
if( GRAIN_POSITION_RANDOM < .000001 ) .000001 => GRAIN_POSITION_RANDOM; |
| 204 |
} |
| 205 |
else if( msg.which == KEY_EQUAL ) |
| 206 |
{ |
| 207 |
1.1 *=> GRAIN_POSITION_RANDOM; |
| 208 |
if( GRAIN_POSITION_RANDOM > 1 ) 1 => GRAIN_POSITION_RANDOM; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// load file into a LiSa |
| 216 |
fun LiSa load( string filename ) |
| 217 |
{ |
| 218 |
// sound buffer |
| 219 |
SndBuf buffy; |
| 220 |
// load it |
| 221 |
filename => buffy.read; |
| 222 |
|
| 223 |
// new LiSa |
| 224 |
LiSa lisa; |
| 225 |
// set duration |
| 226 |
buffy.samples()::samp => lisa.duration; |
| 227 |
|
| 228 |
// transfer values from SndBuf to LiSa |
| 229 |
for( 0 => int i; i < buffy.samples(); i++ ) |
| 230 |
{ |
| 231 |
// args are sample value and sample index |
| 232 |
// (dur must be integral in samples) |
| 233 |
lisa.valueAt( buffy.valueAt(i), i::samp ); |
| 234 |
} |
| 235 |
|
| 236 |
// set LiSa parameters |
| 237 |
lisa.play( false ); |
| 238 |
lisa.loop( false ); |
| 239 |
lisa.maxVoices( LISA_MAX_VOICES ); |
| 240 |
|
| 241 |
return lisa; |
| 242 |
} |