Granular Pulse

supercollider
// 1. Define SynthDef for granular synthesis
SynthDef(\granularPulse, { |freq = 440, amp = 0.5, gate = 1, grainDur = 0.05, grainDensity = 10|
    var env = EnvGen.ar(Env.asr(0.01, 1, 0.3), gate, doneAction: 2);
    var grainEnv = Env.perc(0.001, grainDur, 0.1);
    var grain = GrainBuf.ar(
        2,
        Buffer.read(s, "path/to/your/sample.wav"),
        Impulse.ar(grainDensity),
        0,
        grainDur,
        1,
        freq * (0.9 + 0.2 * LFNoise0.kr(5).range(0, 1)),
        0
    );
    var sig = grain * env * amp;
    Out.ar(0, sig ! 2);
}).writeDefFile;

// 2. Build Score with granular pulse patterns
Score([
    [0.0, [\s_new, \granularPulse, 1001, 0, 0, \freq, 440, \amp, 0.5, \grainDur, 0.03, \grainDensity, 15]],
    [1.0, [\s_new, \granularPulse, 1002, 0, 0, \freq, 330, \amp, 0.4, \grainDur, 0.04, \grainDensity, 12]],
    [2.0, [\s_new, \granularPulse, 1003, 0, 0, \freq, 550, \amp, 0.6, \grainDur, 0.02, \grainDensity, 18]],
    [4.0, [\n_set, 1001, \gate, 0]],
    [4.5, [\n_set, 1002, \gate, 0]],
    [5.0, [\n_set, 1003, \gate, 0]],
    [8.0, [\s_new, \granularPulse, 1004, 0, 0, \freq, 440, \amp, 0.5, \grainDur, 0.03, \grainDensity, 15]],
    [9.0, [\s_new, \granularPulse, 1005, 0, 0, \freq, 330, \amp, 0.4, \grainDur, 0.04, \grainDensity, 12]],
    [10.0, [\s_new, \granularPulse, 1006, 0, 0, \freq, 550, \amp, 0.6, \grainDur, 0.02, \grainDensity, 18]],
    [12.0, [\n_set, 1004, \gate, 0]],
    [12.5, [\n_set, 1005, \gate, 0]],
    [13.0, [\n_set, 1006, \gate, 0]],
    [16.0, [\s_new, \granularPulse, 1007, 0, 0, \freq, 440, \amp, 0.5, \grainDur, 0.03, \grainDensity, 15]],
    [17.0, [\s_new, \granularPulse, 1008, 0, 0, \freq, 330, \amp, 0.4, \grainDur, 0.04, \grainDensity, 12]],
    [18.0, [\s_new, \granularPulse, 1009, 0, 0, \freq, 550, \amp, 0.6, \grainDur, 0.02, \grainDensity, 18]],
    [20.0, [\n_set, 1007, \gate, 0]],
    [20.5, [\n_set, 1008, \gate, 0]],
    [21.0, [\n_set, 1009, \gate, 0]],
    [24.0, [\c_set, 0, 0]]
]).recordNRT(
    outputFilePath: "~/output.wav".standardizePath,
    headerFormat: "WAV",
    sampleFormat: "int16",
    sampleRate: 44100,
    options: ServerOptions.new.numOutputBusChannels_(2),
    duration: 24,
    action: { 0.exit }
);
toward rhythmic granular synthesis for layered detuned grains creating percussive elements for varying grain density and pitch distribution from initial grain density to more complex patterns about the optimal grain duration for rhythmic clarity toward exploring phase modulation in granular synthesis