mxadsr
Prev | Orchestra Opcodes and Operators | Next

## mxadsr

mxadsr — Calculates the classical ADSR envelope using the expsegr mechanism.

## Description

Calculates the classical ADSR envelope using the expsegr mechanism.

## Syntax

    ares mxadsr iatt, idec, islev, irel [, idel] [, ireltim]

    kres mxadsr iatt, idec, islev, irel [, idel] [, ireltim]

## Initialization

iatt -- duration of attack phase

idec -- duration of decay

islev -- level for sustain phase

irel -- duration of release phase

idel (optional, default=0) -- period of zero before the envelope starts

ireltim (optional, default=-1) -- Control release time after receiving a MIDI noteoff event. If less than zero, the longest release time given in the current instrument is used. If zero or more, the given value will be used for release time. Its default value is -1. (New in Csound 3.59 - not yet properly tested)

## Performance

The envelope is in the range 0 to 1 and may need to be scaled further. The envelope may be described as:

Picture of an exponential ADSR envelope.

The length of the sustain is calculated from the length of the note. This means adsr is not suitable for use with MIDI events. The opcode madsr uses the linsegr mechanism, and so can be used in MIDI applications. The opcode mxadsr is identical to madsr except it uses exponential, rather than linear, line segments.

You can use other pre-made envelopes which start a release segment upon receiving a note off message, like linsegr and expsegr, or you can construct more complex envelopes using xtratim and release. Note that you do not need to use xtratim if you are using mxadsr, since the time is extended automatically.

mxadsr is new in Csound version 3.51.

## Examples

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

Example 648. Example of the mxadsr 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  -m0  --midi-key=4 --midi-velocity-amp=5 ; treat p4 and p5 as midi data
    ; For Non-realtime ouput leave only the line below:
    ; -o mxadsr-2.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

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

    ; by Menno Knevel - 2021

    instr 1	                              ; use score and treat p4 and p5 as midi data
    icps    = cpsmidinn(p4)               ; take midi note (p4) from score
    iveloc  ampmidid p5, 92               ; take velocity (p5) from score
    ;               att, dec, lvl, release
    kenv    mxadsr  .001, .2,   1,    p6
    prints  "duration of note (%ds) + release (%2.1fs)\\n", p3, p6                ;
    asig    vco2	iveloc, icps
    asig    butlp	asig, 2000
    		outs    asig*kenv, asig*kenv

    endin
    </CsInstruments>
    <CsScore>
    ;      note vel release
    s
    i 1 0 1 62  60	0
    i 1 2 1 62  80	0
    i 1 4 1 62  100	0
    i 1 6 1 58  50	0
    s
    i 1 1 1 62  60	0
    i 1 3 1 62  80	1
    i 1 5 1 62  100	3
    i 1 7 1 58  50	5
    e
    </CsScore>
    </CsoundSynthesizer>

Here is an example for the adsr-group, comparing the different adsr opcodes. It uses the file adsr-group.csd.

Example 649. Example of the adsr group.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    -odac    ;;;realtime audio out
    ;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
    ; For Non-realtime ouput leave only the line below:
    ; -o adsr-group.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    ; by Menno Knevel - 2021

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

    ; both amplitude and filter use same ADSR curves
    instr 1
    kenv	adsr	.01, .5, .5, p4    	; linear envelope
    asig	vco2	kenv, 110			; A+D+S+R = p3
    asig	rezzy	asig, 500+(kenv*1000), 10	; same curve but scaled
    		outs	asig, asig
    endin

    instr 2	; midi behavior
    kenv	madsr	.01, .5, .5, p4		; linear envelope
    asig	vco2	kenv, 110			; A+D+S = p3, then go into Release stage
    asig	rezzy	asig, 500+(kenv*1000), 10	; same curve but scaled
    		outs	asig, asig
    endin

    instr 3
    kenv	xadsr	.01, .5 , .5, p4    ; exponential envelope
    asig	vco2	kenv, 110			; A+D+S+R = p3
    asig	rezzy	asig, 500+(kenv*1000), 10	; same curve but scaled
    		outs	asig, asig
    endin

    instr 4	; midi behavior
    kenv	mxadsr	.01, .5 , .5, p4	; exponential envelope
    asig	vco2	kenv, 110			; A+D+S = p3, then go into Release stage
    asig	rezzy	asig, 500+(kenv*1000), 10	; same curve but scaled
    		outs	asig, asig
    endin

    </CsInstruments>
    <CsScore>
    s
    i1 1 2 .01	; same notes for everyone!
    i1 5 . .5
    i1 9 . 1.5
    s
    i2 1 2 .01
    i2 5 . .5
    i2 9 . 1.5
    s
    i3 1 2 .01
    i3 5 . .5
    i3 9 . 1.5
    s
    i4 1 2 .01
    i4 5 . .5
    i4 9 . 1.5
    e
    </CsScore>
    </CsoundSynthesizer>

## See Also

linsegr, expsegr, envlpxr, mxadsr, madsr, adsr, expon, expseg, expsega line, linseg, xtratim

## Credits

Author: John ffitch

November 2002. Thanks to Rasmus Ekman, added documentation for the ireltim parameter.

November 2003. Thanks to Kanata Motohashi, fixed the link to the linsegr opcode.

Prev | Up | Next
mvmfilter | Home | nchnls