<!-- Converted to DocBook/SGML by Kevin Conder -->
<refentry id="madsr">
  <refmeta>
    <refentrytitle>madsr</refentrytitle>
  </refmeta>
 
  <refnamediv>
    <refname>madsr</refname>
    <refpurpose>
      Calculates the classical ADSR envelope using the linsegr mechanism.
      <indexterm id="IndexMadsr"><primary>madsr</primary></indexterm>
    </refpurpose>
  </refnamediv>
 
  <refsect1>
    <title>Description</title>
    <para>
      Calculates the classical ADSR envelope using the linsegr mechanism.
   </para>
  </refsect1>
 
  <refsect1>
    <title>Syntax</title>
    <synopsis>ar <command>madsr</command> iatt, idec, islev, irel [, idel] [, ireltim]</synopsis>

    <synopsis>kr <command>madsr</command> iatt, idec, islev, irel [, idel] [, ireltim]</synopsis>
  </refsect1>
 
  <refsect1>
    <title>Initialization</title>
    <para>
      <emphasis>iatt</emphasis> -- duration of attack phase
    </para>

    <para>
      <emphasis>idec</emphasis> -- duration of decay
    </para>

    <para>
      <emphasis>islev</emphasis> -- level for sustain phase
    </para>

    <para>
      <emphasis>irel</emphasis> -- duration of release phase. 
    </para>

    <para>
      <emphasis>idel</emphasis> -- period of zero before the envelope starts
    </para>

    <para>
      <emphasis>ireltim</emphasis> (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)
    </para>

    <para>
      Please note that the release time cannot be longer than 32767/<link linkend="kr"><citetitle>kr</citetitle></link> seconds.
    </para>
  </refsect1>
 
  <refsect1>
    <title>Performance</title>
    <para>
      The envelope is the range 0 to 1 and may need to be scaled further. The envelope may be described as:

      <mediaobject>
        <imageobject>
          <imagedata fileref="images/adsr.png" format="PNG">
        </imageobject>

        <textobject>
          <phrase>Picture of an ADSR envelope.</phrase>
        </textobject>
        
        <caption>
          <para>Picture of an ADSR envelope.</para>
        </caption>
      </mediaobject>
    </para>

    <para>
       The length of the sustain is calculated from the length of the note. This means <link linkend="adsr"><citetitle>adsr</citetitle></link> is not suitable for use with MIDI events. The opcode <emphasis>madsr</emphasis> uses the <link linkend="linsegr"><citetitle>linsegr</citetitle></link> mechanism, and so can be used in MIDI applications.
    </para>

  </refsect1>

  <refsect1>
    <title>Examples</title>
    <para>
      Here is an example of the madsr opcode. It uses the files <ulink url="examples/madsr.orc"><citetitle>madsr.orc</citetitle></ulink> and <ulink url="examples/madsr.sco"><citetitle>madsr.sco</citetitle></ulink>.

      <example>
        <title>Example of the madsr opcode.</title>

        <programlisting>
<inlinemediaobject>
            <imageobject>
              <imagedata fileref="examples/madsr.orc" format="linespecific">
            </imageobject>
          </inlinemediaobject>
        </programlisting>

        <programlisting>
<inlinemediaobject>
            <imageobject>
              <imagedata fileref="examples/madsr.sco" format="linespecific">
            </imageobject>
          </inlinemediaobject>
        </programlisting>
      </example>
    </para>
  </refsect1>

  <refsect1>
    <title>See Also</title>
    <para>
      <link linkend="adsr"><citetitle>adsr</citetitle></link>,
      <link linkend="mxadsr"><citetitle>mxadsr</citetitle></link>,
      <link linkend="xadsr"><citetitle>xadsr</citetitle></link>
    </para>
  </refsect1>

  <refsect1>
    <title>Credits</title>
    <para>November 2002. Thanks to &namerasmus;, added documentation for the <emphasis>ireltim</emphasis> parameter.</para>
    <para>December 2002. Thanks to Iain McCurdy, added an example.</para>
    <para>December 2002. Thanks to &nameistvan;, added documentation about the maximum release time.</para>
    <para>New in Csound version 3.49.</para>
  </refsect1>
</refentry>
