granule
Prev | Orchestra Opcodes and Operators | Next

## granule

granule — A more complex granular synthesis texture generator.

## Description

The granule unit generator is more complex than grain, but does add new possibilities.

granule is a Csound unit generator which employs a wavetable as input to produce granularly synthesized audio output. Wavetable data may be generated by any of the GEN subroutines such as GEN01 which reads an audio data file into a wavetable. This enable a sampled sound to be used as the source for the grains. Up to 128 voices are implemented internally. The maximum number of voices can be increased by redefining the variable MAXVOICE in the grain4.h file. granule has a build-in random number generator to handle all the random offset parameters. Thresholding is also implemented to scan the source function table at initialization stage. This facilitates features such as skipping silence passage between sentences.

The characteristics of the synthesis are controlled by 22 parameters. xamp is the amplitude of the output and it can be either audio rate or control rate variable.

## Syntax

    ares granule xamp, ivoice, iratio, imode, ithd, ifn, ipshift, igskip, igskip_os, ilength, kgap, igap_os, kgsize, igsize_os, iatt, idec \
          [, iseed] [, ipitch1] [, ipitch2] [, ipitch3] [, ipitch4] [, ifnenv]

## Initialization

ivoice -- number of voices.

iratio -- ratio of the speed of the gskip pointer relative to output audio sample rate. eg. 0.5 will be half speed.

imode -- +1 grain pointer move forward (same direction of the gskip pointer), -1 backward (oppose direction to the gskip pointer) or 0 for random.

ithd -- threshold, if the sampled signal in the wavetable is smaller then ithd, it will be skipped.

ifn -- function table number of sound source.

ipshift -- pitch shift control. If ipshift is 0, pitch will be set randomly up and down an octave. If ipshift is 1, 2, 3 or 4, up to four different pitches can be set amount the number of voices defined in ivoice. The optional parameters ipitch1, ipitch2, ipitch3 and ipitch4 are used to quantify the pitch shifts.

igskip -- initial skip from the beginning of the function table in sec.

igskip_os -- gskip pointer random offset in sec, 0 will be no offset.

ilength -- length of the table to be used starting from igskip in sec.

igap_os -- gap random offset in % of the gap size, 0 gives no offset.

igsize_os -- grain size random offset in % of grain size, 0 gives no offset.

iatt -- attack of the grain envelope in % of grain size.

idec -- decade of the grain envelope in % of grain size.

iseed (optional, default=0.5) -- seed for the random number generator.

ipitch1, ipitch2, ipitch3, ipitch4 (optional, default=1) -- pitch shift parameter, used when ipshift is set to 1, 2, 3 or 4. Time scaling technique is used in pitch shift with linear interpolation between data points. Default value is 1, the original pitch.

ifnenv (optional, default=0) -- function table number to be used to generate the shape of the envelope.

## Performance

xamp -- amplitude.

kgap -- gap between grains in sec.

kgsize -- grain size in sec.

## Examples

Here is an example of the granule opcode. It uses the files granule.csd, marimba.aif and fox.wav

Example 434. Example of the granule opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ; Audio out   Audio in    No messages
    -odac    ;;;RT audio out
    ; For Non-realtime ouput leave only the line below:
    ; -o granule.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    ; additions by Menno Knevel 2022

    sr = 44100
    ksmps = 32
    nchnls = 2
    0dbfs  = 1

    instr 1

    kenv linseg 0,0.5,1,(p3-0.5),1      ; envelope with attack time of .5 seconds

    iamp = p4
    ivoice = p5
    iratio = p6
    imode = p7
    ithd = p8
    ifn = p9
    ipshift = p10
    igskip = p11
    igskipos = p12
    ilenght = p13
    kgap = p14
    igapos = p15
    kgsize = p16
    igsize_os = p17
    iatt = p18
    idec = p19
    iseed = p20
    ipitch1 = p21
    ipitch2 = p22
    ipitch3 = p23
    ipitch4 = p24

    a1  granule iamp*kenv, ivoice, iratio, imode, ithd, ifn, ipshift ,igskip ,igskipos , ilenght, kgap ,igapos, kgsize, igsize_os, iatt, idec, iseed,      ipitch1, ipitch2, ipitch3, ipitch4
    a2  granule iamp*kenv, ivoice, iratio, imode, ithd, ifn, ipshift ,igskip ,igskipos , ilenght, kgap ,igapos, kgsize, igsize_os, iatt, idec, iseed+0.17, ipitch1, ipitch2, ipitch3, ipitch4
    outs a1,a2

    endin

    </CsInstruments>
    <CsScore>
    f1      0 0 1 "marimba.aif" 0 0 0
    f2      0 0 1 "fox.wav" 0 0 0

    ; both samples are played back 2 x slower

    ;         p4 p5 p6 p7 p8 p9 p10 p11 p12  p13 p14  p15 p16   p17 p18 p19  [p20   p21 p22  p23  p24]
    i1 0  10 .25 64 1 0   0  1   4  0  0.005  1  0.01  0  0.02   50 25  25    0.39  1  1.42 0.29  2 ;part of marimba
    i1 11 20 .37 9  1 0   0  2   0  0  0.005  1  0.01  0  0.02   50 25  25    0.39  1  1.42 0.29  2 ;part of fox

    e
    </CsScore>
    </CsoundSynthesizer>

The above example reads a sound file called marimba.aif into wavetable number 1, a second reads fox.wav into wavetable number 2. The first note generates 10 seconds of stereo audio output using the wavetable. In the orchestra file, all parameters required to control the synthesis are passed from the score file. A linseg function generator is used to generate an envelope with 0.5 second of linear attack and decay. Stereo effect is generated by using different seeds for the two granule function calls. In the example, 0.17 is added to p20 (= iseed) before passing into the second granule call to ensure that all of the random offset events are different from the first one.

The second note (wavetable number 2) has slightly different parameters.

In the score file for the first note, the parameters are interpreted as:

Parameter | Interpreted As
--- | ---
p5 (ivoice) | the number of voices is set to 64
p6 (iratio) | set to 0.5, it scans the wavetable at half of the speed of the audio output rate
p7 (imode) | set to 0, the grain pointer only move forward
p8 (ithd) | set to 0, skipping the thresholding process
p9 (ifn) | set to 1, function table number 1 is used
p10 (ipshift) | set to 4, four different pitches are going to be generated
p11 (igskip) | set to 0 and p12 (igskip_os) is set to 0.005, no skipping into the wavetable and a 5 mSec random offset is used
p13 (ilength) | set to 1, 1 second of the wavetable is to be used
p14 (kgap) | set to 0.01 and p15 (igap_os) is set to 50, 10 mSec gap with 50% random offset is to be used
p16 (kgsize) | set to 0.02 and p17 (igsize_os) is set to 50, 20 mSec grain with 50% random offset is used
p18 (iatt) and p19 (idec) | set to 25, 25% of linear attack and decade is applied to the grain
p20 (iseed) | seed for the random number generator is set to 0.39
p21 - p24 | pitches set to 1 which is the original pitch, 1.42 which is a 5th up, 0.29 which is a 7th down and finally 2 which is an octave up.

## See Also

Granular Synthesis

## Credits

Author: Allan Lee

Belfast

1996

New in version 3.35

Prev | Up | Next
grain3 | Home | guiro