| 1 | <!doctype html> |
| 2 | <html lang="en-us"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 | <title>ChucK + JS => ChucKScript</title> |
| 7 | <link rel="stylesheet" href="../../css/editor.css"> |
| 8 | </head> |
| 9 | <body onresize="chuckEditor.resize();"> |
| 10 | <div> |
| 11 | <div id="program">// LiSa 1: pre-built files; this is an example that forces memory to grow |
| 12 | //----------------------------------------------------------------------------- |
| 13 | // name: aah.ck |
| 14 | // desc: Granular synthesis on "ahh" |
| 15 | // |
| 16 | // author: Jack Atherton |
| 17 | // |
| 18 | // Combining Dan Trueman's various helper scripts for Lisa |
| 19 | // https://github.com/ccrma/music220a/blob/master/chuck-examples/special/LiSa-SndBuf.ck |
| 20 | // http://chuck.cs.princeton.edu/doc/examples/special/LiSa-munger2.ck |
| 21 | // and adding several top level control variables |
| 22 | //----------------------------------------------------------------------------- |
| 23 | |
| 24 | // PARAMS |
| 25 | class AhhSynth extends Chubgraph |
| 26 | { |
| 27 | LiSa lisa => outlet; |
| 28 | |
| 29 | // spawn rate: how often a new grain is spawned (ms) |
| 30 | 25 => float grainSpawnRateMS; |
| 31 | 0 => float grainSpawnRateVariationMS; |
| 32 | 0.0 => float grainSpawnRateVariationRateMS; |
| 33 | |
| 34 | // position: where in the file is a grain (0 to 1) |
| 35 | 0.61 => float grainPosition; |
| 36 | 0.2 => float grainPositionRandomness; |
| 37 | |
| 38 | // grain length: how long is a grain (ms) |
| 39 | 300 => float grainLengthMS; |
| 40 | 10 => float grainLengthRandomnessMS; |
| 41 | |
| 42 | // grain rate: how quickly is the grain scanning through the file |
| 43 | 1.004 => float grainRate; // 1.002 == in-tune Ab |
| 44 | 0.015 => float grainRateRandomness; |
| 45 | |
| 46 | // ramp up/down: how quickly we ramp up / down |
| 47 | 50 => float rampUpMS; |
| 48 | 200 => float rampDownMS; |
| 49 | |
| 50 | // gain: how loud is everything overall |
| 51 | 1 => float gainMultiplier; |
| 52 | |
| 53 | float myFreq; |
| 54 | fun float freq( float f ) |
| 55 | { |
| 56 | f => myFreq; |
| 57 | 61 => Std.mtof => float baseFreq; |
| 58 | // 1.002 == in tune for 56 for aah4.wav |
| 59 | // 1.004 == in tune for 60 for aah5.wav |
| 60 | myFreq / baseFreq * 0.98 => grainRate; |
| 61 | |
| 62 | return myFreq; |
| 63 | } |
| 64 | |
| 65 | fun float freq() |
| 66 | { |
| 67 | return myFreq; |
| 68 | } |
| 69 | |
| 70 | fun float gain( float g ) |
| 71 | { |
| 72 | g => lisa.gain; |
| 73 | return g; |
| 74 | } |
| 75 | |
| 76 | fun float gain() |
| 77 | { |
| 78 | return lisa.gain(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | SndBuf buf; |
| 84 | me.dir() + "aah5.wav" => buf.read; |
| 85 | buf.length() => lisa.duration; |
| 86 | // copy samples in |
| 87 | for( int i; i < buf.samples(); i++ ) |
| 88 | { |
| 89 | lisa.valueAt( buf.valueAt( i ), i::samp ); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | buf.length() => dur bufferlen; |
| 94 | |
| 95 | // LiSa params |
| 96 | 100 => lisa.maxVoices; |
| 97 | 0.1 => lisa.gain; |
| 98 | true => lisa.loop; |
| 99 | false => lisa.record; |
| 100 | |
| 101 | |
| 102 | // modulate |
| 103 | SinOsc freqmod => blackhole; |
| 104 | 0.1 => freqmod.freq; |
| 105 | |
| 106 | |
| 107 | |
| 108 | 0.1 => float maxGain; |
| 109 | |
| 110 | fun void SetGain() |
| 111 | { |
| 112 | while( true ) |
| 113 | { |
| 114 | maxGain * gainMultiplier => lisa.gain; |
| 115 | 1::ms => now; |
| 116 | } |
| 117 | } |
| 118 | spork ~ SetGain(); |
| 119 | |
| 120 | |
| 121 | fun void SpawnGrains() |
| 122 | { |
| 123 | // create grains |
| 124 | while( true ) |
| 125 | { |
| 126 | // grain length |
| 127 | ( grainLengthMS + Math.random2f( -grainLengthRandomnessMS / 2, grainLengthRandomnessMS / 2 ) ) |
| 128 | * 1::ms => dur grainLength; |
| 129 | |
| 130 | // grain rate |
| 131 | grainRate + Math.random2f( -grainRateRandomness / 2, grainRateRandomness / 2 ) => float grainRate; |
| 132 | |
| 133 | // grain position |
| 134 | ( grainPosition + Math.random2f( -grainPositionRandomness / 2, grainPositionRandomness / 2 ) ) |
| 135 | * bufferlen => dur playPos; |
| 136 | |
| 137 | // grain: grainlen, rampup, rampdown, rate, playPos |
| 138 | spork ~ PlayGrain( grainLength, rampUpMS::ms, rampDownMS::ms, grainRate, playPos); |
| 139 | |
| 140 | // advance time (time per grain) |
| 141 | // PARAM: GRAIN SPAWN RATE |
| 142 | grainSpawnRateMS::ms + freqmod.last() * grainSpawnRateVariationMS::ms => now; |
| 143 | grainSpawnRateVariationRateMS => freqmod.freq; |
| 144 | } |
| 145 | } |
| 146 | spork ~ SpawnGrains(); |
| 147 | |
| 148 | // sporkee |
| 149 | fun void PlayGrain( dur grainlen, dur rampup, dur rampdown, float rate, dur playPos ) |
| 150 | { |
| 151 | lisa.getVoice() => int newvoice; |
| 152 | |
| 153 | if(newvoice > -1) |
| 154 | { |
| 155 | lisa.rate( newvoice, rate ); |
| 156 | lisa.playPos( newvoice, playPos ); |
| 157 | lisa.rampUp( newvoice, rampup ); |
| 158 | ( grainlen - ( rampup + rampdown ) ) => now; |
| 159 | lisa.rampDown( newvoice, rampdown) ; |
| 160 | rampdown => now; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | } |
| 166 | |
| 167 | LPF lpf => NRev rev => dac; |
| 168 | 7000 => lpf.freq; |
| 169 | rev.mix(0.1); |
| 170 | |
| 171 | 57 => int A3; |
| 172 | 59 => int B3; |
| 173 | 61 => int Cs4; |
| 174 | 64 => int E4; |
| 175 | 66 => int Fs4; |
| 176 | 68 => int Gs4; |
| 177 | 71 => int B4; |
| 178 | 73 => int Cs5; |
| 179 | 75 => int Ds5; |
| 180 | 78 => int Fs5; |
| 181 | 80 => int Gs5; |
| 182 | 83 => int B5; |
| 183 | E4 - 12 => int E3; |
| 184 | E4 + 12 => int E5; |
| 185 | Gs4 - 12 => int Gs3; |
| 186 | |
| 187 | |
| 188 | // the notes |
| 189 | [ |
| 190 | [A3, E4, Cs5, Gs5], |
| 191 | [E3, B4, Ds5, Gs5], |
| 192 | [Cs4 - 12, Gs4, E5, B5], |
| 193 | [Gs3, Cs4, B4, Fs5] |
| 194 | ] @=> int notes[][]; |
| 195 | |
| 196 | |
| 197 | AhhSynth ahhs[ notes[0].size() ]; |
| 198 | for( int i; i < ahhs.size(); i++ ) |
| 199 | { |
| 200 | 0.8 / ahhs.size() => ahhs[i].gain; |
| 201 | ahhs[i] => lpf; |
| 202 | } |
| 203 | |
| 204 | // wait 1 second for crappy audio of memory expanding to disappear |
| 205 | 0 => rev.gain; |
| 206 | 1::second => now; |
| 207 | 1 => rev.gain; |
| 208 | |
| 209 | while( true ) |
| 210 | { |
| 211 | for( int i; i < notes.size(); i++ ) |
| 212 | { |
| 213 | for( int j; j < notes[i].size(); j++ ) |
| 214 | { |
| 215 | notes[i][j] - 12 => Std.mtof => ahhs[j].freq; |
| 216 | } |
| 217 | |
| 218 | 2::second => now; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | </div> |
| 223 | </div> |
| 224 | <div class="buttons"> |
| 225 | <button class="chuckButton" id="startChuck" disabled="disabled">Start ChucK</button> |
| 226 | <button class="chuckButton" id="compileButton" disabled="disabled">Compile and Run</button> |
| 227 | <button class="chuckButton" id="replaceButton" disabled="disabled">Replace</button> |
| 228 | <button class="chuckButton" id="removeButton" disabled="disabled">Remove</button> |
| 229 | <button class="chuckButton" id="clearButton" disabled="disabled">Clear VM and Globals</button> |
| 230 | <button class="chuckButton" id="micButton" disabled="disabled">Enable Microphone</button> |
| 231 | <button class="chuckButton" id="fileFormButton">Show File Uploader</button> |
| 232 | </div> |
| 233 | <div id="fileForm" class="hidden"> |
| 234 | <p>File name to use in ChucK: <input type="text" id="uploadFilename"></p> |
| 235 | <p>Local file to upload: <input type="file" id="uploadFile" name="uploadFile"></p> |
| 236 | <p><button class="chuckButton" id="fileButton" disabled="disabled">Upload File to Virtual File System</button></p> |
| 237 | </div> |
| 238 | <div> |
| 239 | <textarea id="output"></textarea> |
| 240 | <div id="shreds"> |
| 241 | <table id="shredstable"> |
| 242 | <col width="60px"> |
| 243 | <col width="1*"> |
| 244 | <col width="50px"> |
| 245 | <col width="60px"> |
| 246 | <tr><th>shred</th><th>code</th><th>time</th><th>remove</th> |
| 247 | </table> |
| 248 | </div> |
| 249 | <div class="clearfix"></div> |
| 250 | </div> |
| 251 | <script type="text/javascript" src="../../js/ace-min-noconflict/ace.js" charset="utf-8"></script> |
| 252 | <script type="text/javascript" src="../../js/editor.js"></script> |
| 253 | <script type="text/javascript" src="../../js/defer.js"></script> |
| 254 | <script> |
| 255 | // override |
| 256 | var whereIsChuck = "../../js"; |
| 257 | |
| 258 | var serverFilesToPreload = [ |
| 259 | { |
| 260 | serverFilename: '../../wav/aah.wav', |
| 261 | virtualFilename: 'aah5.wav' |
| 262 | } |
| 263 | ]; |
| 264 | </script> |
| 265 | <script type="text/javascript" src="../../js/webchuck_host.js"></script> |
| 266 | <script type="text/javascript" src="../../js/host.js"></script> |
| 267 | </body> |
| 268 | </html> |
| 269 | |
| 270 |