Skip to content

Instantly share code, notes, and snippets.

@DexterShepherd
Created May 25, 2018 22:49
Show Gist options
  • Select an option

  • Save DexterShepherd/16fe6d2d84d3840bfd623fc5f8e583cd to your computer and use it in GitHub Desktop.

Select an option

Save DexterShepherd/16fe6d2d84d3840bfd623fc5f8e583cd to your computer and use it in GitHub Desktop.
40 => int numGrains;
SndBuf grain[numGrains];
ADSR grainEnv[numGrains];
Gain granular => dac;
50000 => int startPos;
0.1 => float readRate;
1000 => int grainLengthMS;
grainLengthMS::ms => dur grainLength;
1000 => int jitter;
1000 => int timeJitter;
0.0 => float pitchJitter;
1.1 => float rate;
200::ms => dur attackTime;
200::ms => dur releaseTime;
for(0 => int i; i < numGrains; i++) {
grain[i] => grainEnv[i] => granular;
grainEnv[i].set(attackTime, 0::ms, 1.0, releaseTime);
"sounds/geosmin.wav" => grain[i].read;
0 => grain[i].rate;
}
spork ~ tick();
for( 0 => int i; i < numGrains; i++ ) {
100::ms => now;
spork ~ play(i);
}
fun void play(int index) {
rate => grain[index].rate;
while(1) {
Math.random2(jitter / -2, jitter / 2) => int jitterShift;
if ( ( startPos + jitterShift ) > 0 ) {
startPos + jitterShift => grain[index].pos;
} else {
0 => grain[index].pos;
}
rate + Math.random2f(pitchJitter * -1, pitchJitter) => grain[index].rate;
if ( Math.random2f(0, 1) > 0.9 ) {
grain[index].rate() * 2 => grain[index].rate;
} else if ( Math.random2f(0, 1) > 0.8 ) {
grain[index].rate() * 0.5 => grain[index].rate;
} else if ( Math.random2f(0, 1) > 0.9 ) {
grain[index].rate() * 0.25 => grain[index].rate;
}
Math.random2(timeJitter / -2, timeJitter / 2) => int timeJitterShift;
grainEnv[index].keyOn();
if ( ( grainLength + timeJitterShift::ms - releaseTime ) > 0::ms ) {
grainLength + timeJitterShift::ms - releaseTime => now;
} else {
grainLength => now;
}
grainEnv[index].keyOff();
releaseTime => now;
}
}
fun void tick() {
SinOsc lfo => blackhole;
SinOsc lfo2 => blackhole;
0.013 => lfo.freq;
0.002 => lfo2.freq;
while(1) {
(( lfo.last() + 1 ) * 0.5) => float theta;
( ( theta * grainLengthMS ) + 20 )::ms => grainLength;
(( lfo2.last() + 1 ) * 0.5) => theta;
( ( theta * 200 ) + 10 ) $ int => jitter;
( ( theta * 100 ) + 10 )$ int => timeJitter;
1::samp => now;
}
}
while(1) {
( startPos + (44.1 * readRate) ) $ int => startPos;
if ( startPos > grain[0].samples() ) {
me.exit();
0 => startPos;
}
1::ms => now;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment