Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 70 additions & 38 deletions granulator.dsp
Original file line number Diff line number Diff line change
@@ -1,56 +1,88 @@
declare name "Granulator";
declare author "Mayank Sanganeria";
declare version "1.0";
declare version "1.1";
declare description "Granular processor based on a single shared delay line";

import("stdfaust.lib");

// =====================================================
// Constants
// =====================================================

// Maximum number of simultaneous grains.
// Lower this (e.g. 16) if you get xruns on a Raspberry Pi.
maxN = 32;

// Size of the shared delay buffer in samples (power of two).
// 524288 samples is about 10.9 s at 48 kHz.
// Faust allocates only ONE buffer, shared by all grains.
maxDelay = 1 << 19;

// =====================================================
// Controls
N = hslider("grain density", 1, 1, maxN, 1);
gLength = hslider("grain length", 0.1, 0.01, 0.5, 0.01);
dLength = hslider("delay length", 10, 0.5, 10, 0.1);
// =====================================================

// Number of active grains (smoothed to avoid zipper noise and clicks)
N = hslider("grain density", 1, 1, maxN, 1) : si.smoo;

// Globals
SR = fconstant(int fSamplingFreq, <math.h>);
counter = +(1)%delayLength~_; // to iterate through the delay line
delayBufferSize = 480000;
maxN = 64;
// Grain length in seconds (smoothed so the grain rate changes without jumps)
gLength = hslider("grain length", 0.1, 0.01, 0.5, 0.01) : si.smoo;

// Maximum random delay in seconds. This only affects newly started grains,
// so changing it never produces clicks.
dLength = hslider("delay length", 10, 0.5, 10, 0.1);

//Granular synth variables
grainLength = int(SR*gLength);
delayLength = int(SR*dLength);
// =====================================================
// Grain phases
// =====================================================

// Master ramp 0..1 whose period equals the grain length
master = os.lf_sawpos(1.0 / gLength);

//Noise Generator
// Phase of grain i. Grains are evenly spaced across the cycle (offset i/N),
// so the overlapping windows sum to a roughly constant level.
phase(i) = ma.frac(master + i / max(1, N));

S(1,F) = F;
S(i,F) = F <: S(i-1,F),_ ;
Divide(n,k) = par(i, n, /(k)) ;
random = +(12345) : *(1103515245) ;
RANDMAX = 2^32 - 1 ;
chain(n) = S(n,random) ~ _;
NoiseN(n) = chain(n) : Divide(n,RANDMAX);
// Trigger pulse at the first sample of each new grain
// (the phase wrapped around from ~1 back to ~0).
trig(i) = phase(i) < phase(i)';

noiser = NoiseN(maxN+1); //multi channel noiser
// Hann window: smoothly goes 0 -> 1 -> 0 over one grain.
// sin^2(pi * phase) is zero at both ends, so there are no discontinuities.
window(i) = sin(ma.PI * phase(i)) ^ 2;

NoiseChan(n,0) = noiser:>_,par( j, n-1 , !);
NoiseChan(n,i) = noiser:>par( j, i , !) , _, par( j, n-i-1,!);
// Soft on/off gate for grain i depending on density.
// Grains fade in/out instead of switching abruptly.
gate(i) = min(1, max(0, N - i));

noise(i) = (NoiseChan(maxN+1,i) + 1) / 2; //get nth channel of multi-channel noiser
//-------------Noise Generator End
// =====================================================
// Randomness
// =====================================================

//Sample & Hold
SH(trig,x) = (*(1 - trig) + x * trig) ~ _;
// Independent noise generator per grain, scaled to the 0..1 range
rnd(i) = no.noises(maxN, i) * 0.5 + 0.5;

// Random delay time (in samples) for grain i.
// Sample & hold: the value changes ONLY when a new grain starts.
// At that moment the window is ~0, so the jump is inaudible.
// The delay stays constant during the grain, so the read head moves at the
// same speed as the write head (no pitch change, no read/write head crossing).
grainDelay(i) = rnd(i) * min(maxDelay - 1, dLength * ma.SR) : ba.sAndH(trig(i));

//Grain Positions
grainOffset(i) = int(SH(1-1',int(delayLength*noise(i)))) ;
grainCounterMaster = +(1)%grainLength~_; // universal counter for all grains
grainCounter(i) = (grainCounterMaster + grainOffset(i) ) % grainLength;
grainRandomStartPos(i) = int(SH(int(grainCounter(i)/(grainLength-1)),int(delayLength*noise(i))));
grainPosition(i) = grainCounter(i) + grainRandomStartPos(i);
// =====================================================
// A single grain
// =====================================================

//Delay Line
buffer(write,read,x) = rwtable(delayBufferSize, 0.0, write%delayLength, x, read%delayLength);
// Read from the shared delay line at the random delay,
// then apply the window and the density gate.
grain(i) = de.delay(maxDelay, int(grainDelay(i))) * window(i) * gate(i);

//sin wave for windowing
window(i) = sin(2*3.14159*grainCounter(i)/(grainLength-1));
// =====================================================
// Output
// =====================================================

process = _<: par(i,maxN,buffer(counter, grainPosition(i))*window(i)*(i<N)/N) :> _,_;
// Mono input -> maxN parallel grains -> sum -> level compensation -> stereo out.
// Dividing by sqrt(N) keeps the loudness roughly constant as density changes.
process = _ <: par(i, maxN, grain(i)) :> _
: *(1.0 / sqrt(max(1, N)))
<: _, _;