diskgrain
Prev | Orchestra Opcodes and Operators | Next

## diskgrain

diskgrain — Synchronous granular synthesis, using a soundfile as source.

## Description

diskgrain implements synchronous granular synthesis. The source sound for the grains is obtained by reading a soundfile containing the samples of the source waveform.

## Syntax

    asig diskgrain Sfname, kamp, kfreq, kpitch, kgrsize, kprate, \
          ifun, iolaps [,imaxgrsize , ioffset]

## Initialization

Sfilename -- source soundfile.

ifun -- grain envelope function table.

iolaps -- maximum number of overlaps, max(kfreq)*max(kgrsize). Estimating a large value should not affect performance, but exceeding this value will probably have disastrous consequences.

imaxgrsize -- max grain size in secs (default 1.0).

ioffset -- start offset in secs from beginning of file (default: 0).

## Performance

kamp -- amplitude scaling

kfreq -- frequency of grain generation, or density, in grains/sec.

kpitch -- grain pitch scaling (1=normal pitch, < 1 lower, > 1 higher; negative, backwards)

kgrsize -- grain size in secs.

kprate -- readout pointer rate, in grains. The value of 1 will advance the reading pointer 1 grain ahead in the source table. Larger values will time-compress and smaller values will time-expand the source signal. Negative values will cause the pointer to run backwards and zero will freeze it.

The grain generator has full control of frequency (grains/sec), overall amplitude, grain pitch (a sampling increment) and grain size (in secs), both as fixed or time-varying (signal) parameters. An extra parameter is the grain pointer speed (or rate), which controls which position the generator will start reading samples in the file for each successive grain. It is measured in fractions of grain size, so a value of 1 (the default) will make each successive grain read from where the previous grain should finish. A value of 0.5 will make the next grain start at the midway position from the previous grain start and finish, etc.. A value of 0 will make the generator read always from a fixed position (wherever the pointer was last at). A negative value will decrement pointer positions. This control gives extra flexibility for creating timescale modifications in the resynthesis.

Diskgrain will generate any number of parallel grain streams (which will depend on grain density/frequency), up to the olaps value (default 100). The number of streams (overlapped grains) is determined by grainsize*grain_freq. More grain overlaps will demand more calculations and the synthesis might not run in realtime (depending on processor power).

Diskgrain can simulate FOF-like formant synthesis, provided that a suitable shape is used as grain envelope and a sinewave as the grain wave. For this use, grain sizes of around 0.04 secs can be used. The formant centre frequency is determined by the grain pitch. Since this is a sampling increment, in order to use a frequency in Hz, that value has to be scaled by tablesize/sr. Grain frequency will determine the fundamental.

This opcode is a variation on the syncgrain opcode.

| Note
diskgrain does not do any resanmpling, so if the sample rate of the source filename is not the same as csound's sr value there will be pitch shifts

## Examples

Here is an example of the diskgrain opcode. It uses the file diskgrain.csd.

Example 236. Example of the diskgrain 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
    -odac     ;;;RT audio out
    ;-iadc    ;;;uncomment -iadc if RT audio input is needed too
    ; For Non-realtime ouput leave only the line below:
    ; -o diskgrain.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    ; additions by Menno Knevel 2022

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

    instr 1

    iolaps  = 2
    igrsize = 0.04
    ifreq   = iolaps/igrsize
    ips     = 1/iolaps

    istr = p4  /* timescale */
    ipitch = p5 /* pitchscale */

    a1 diskgrain "marimba.aif", 1, ifreq, ipitch, igrsize, ips*istr, 1, iolaps
    prints "\nmarimba is playing....\n\n"
       outs   a1, a1

    endin

    instr 2

    iolaps  = 5
    igrsize = 0.1
    ifreq   = iolaps/igrsize
    ips     = 1/iolaps

    istr = p4  /* timescale */
    ipitch = p5 /* pitchscale */

    a1 diskgrain "singFemale.aif", .5, ifreq, ipitch, igrsize, ips*istr, 1, iolaps
    prints "\nFemale is singing....\n\n"
       outs   a1, a1

    endin

    </CsInstruments>
    <CsScore>
    f 1 0 8192 20 2 1  ;Hanning function

    ;           timescale   pitchscale
    i 1   0   6     1           1
    i 1   +   6     2           1
    i 1   +   6     1          0.75
    i 1   +   6     1.5        1.5
    i 1   +   6.25  0.5        1.5

    ;           timescale   pitchscale
    i 2   31  4     1           1
    i 2   +   4     2           1
    i 2   +   4     1          0.75
    i 2   +   5     1.5        1.5
    i 2   +   7.9   0.5        1.5
    e
    </CsScore>
    </CsoundSynthesizer>

## See Also

Granular Synthesis

## Credits

Author: Victor Lazzarini
May 2007

New in Csound 5.06

Prev | Up | Next
directory | Home | diskin