madsr

madsr — Calculates the classical ADSR envelope using the linsegr mechanism.

Description

Calculates the classical ADSR envelope using the linsegr mechanism.

Syntax

ares madsr iatt, idec, islev, irel [, idel] [, ireltim]
kres madsr 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 -- 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)

Please note that the release time cannot be longer than 32767/kr seconds.

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 ADSR envelope.

Picture of an 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.

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 don't need to use xtratim if you are using madsr, since the time is extended automatically.

[Note] Note

Times for iatt, idec and irel cannot be 0. If 0 is used, no envelope is generated. Use a very small value like 0.0001 if you need an instantaneous attack, decay or release.

Examples

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

Example 467. Example of the madsr 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           -iadc     -d     ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o madsr.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

/* Written by Iain McCurdy */
; Initialize the global variables.
sr = 44100
kr = 441
ksmps = 100
nchnls = 1

; Instrument #1.
instr 1
  ; Attack time.
  iattack = 0.5
  ; Decay time.
  idecay = 0
  ; Sustain level.
  isustain = 1
  ; Release time.
  irelease = 0.5
  aenv madsr iattack, idecay, isustain, irelease

  a1 oscili 10000, 440, 1
  out a1*aenv
endin


</CsInstruments>
<CsScore>

/* Written by Iain McCurdy */
; Table #1, a sine wave.
f 1 0 1024 10 1

; Leave the score running for 6 seconds.
f 0 6

; Play Instrument #1 for two seconds.
i 1 0 2
e


</CsScore>
</CsoundSynthesizer>


Here is another example of the madsr opcode, using midi input. It uses the file madsr-2.csd.

Example 468. second example of the madsr opcode.

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac  -+rtmidi=virtual -M0   ;;;realtime audio out and realtime midi in
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o madsr-2.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

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

instr 1
 	 
icps	cpsmidi	 	 
iamp	ampmidi	.5	 
 	 	 	 
kenv	madsr	0.5, 0, 1, 0.5
asig	pluck	kenv, icps, icps, 2, 1	 
	outs	asig, asig
	 
endin
</CsInstruments>
<CsScore>
f 2 0 4096 10 1	

f0 30	;runs 30 seconds
e
</CsScore>
</CsoundSynthesizer>


See Also

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

Credits

Author: John ffitch

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

December 2002. Thanks to Iain McCurdy, added an example.

December 2002. Thanks to Istvan Varga, added documentation about the maximum release time.

New in Csound version 3.49.