Sophie

Sophie

distrib > Mageia > 5 > x86_64 > media > core-release > by-pkgid > b707d9a4ee443103660a75ccb6e51334 > files > 1808

csound-doc-5.19.01-10.mga5.noarch.rpm

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>if</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.76.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="opi.html" title="i" />
    <link rel="next" href="igoto.html" title="igoto" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">if</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="opi.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="igoto.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry" title="if">
      <a id="if"></a>
      <div class="titlepage"></div>
      <a id="IndexIf" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">if</span>
        </h2>
        <p>if — 
      Branches conditionally at initialization or during performance time.
    </p>
      </div>
      <div class="refsect1" title="Description">
        <a id="idp26473696"></a>
        <h2>Description</h2>
        <p>
      <span class="emphasis"><em>if...igoto</em></span> -- conditional branch at initialization time, depending on the truth value of the logical expression <span class="emphasis"><em>ia</em></span> <span class="emphasis"><em>R</em></span> <span class="emphasis"><em>ib</em></span>. The branch is taken only if the result is true.
    </p>
        <p>
      <span class="emphasis"><em>if...kgoto</em></span> -- conditional branch during performance time, depending on the truth value of the logical expression <span class="emphasis"><em>ka</em></span> <span class="emphasis"><em>R</em></span> <span class="emphasis"><em>kb</em></span>. The branch is taken only if the result is true.
    </p>
        <p>
      <span class="emphasis"><em>if...goto</em></span> -- combination of the above. Condition tested on every pass.
    </p>
        <p>
      <span class="emphasis"><em>if...then</em></span> -- allows the ability to specify conditional <span class="emphasis"><em>if</em></span>/<span class="emphasis"><em>else</em></span>/<span class="emphasis"><em>endif</em></span> blocks.  All <span class="emphasis"><em>if...then</em></span> blocks must end with an <span class="emphasis"><em>endif</em></span> statement. <span class="emphasis"><em>elseif</em></span> and <span class="emphasis"><em>else</em></span> statements are optional. Any number of <span class="emphasis"><em>elseif</em></span> statements are allowed. Only one <span class="emphasis"><em>else</em></span> statement may occur and it must be the last conditional statement before the <span class="emphasis"><em>endif</em></span> statement. Nested <span class="emphasis"><em>if...then</em></span> blocks are allowed.
    </p>
        <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note: Note">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">Note</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
        Note that if the condition uses a k-rate variable (for instance, <span class="quote">“<span class="quote">if kval &gt; 0</span>”</span>), the <span class="emphasis"><em>if...goto</em></span> or <span class="emphasis"><em>if...then</em></span> statement will be ignored during the i-time pass. This allows for opcode initialization, even if the k-rate variable has already been assigned an appropriate value by an earlier init statement.
      </p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="refsect1" title="Syntax">
        <a id="idp26524936"></a>
        <h2>Syntax</h2>
        <pre class="synopsis"><span class="command"><strong>if</strong></span> ia R ib <span class="command"><strong>igoto</strong></span> label</pre>
        <pre class="synopsis"><span class="command"><strong>if</strong></span> ka R kb <span class="command"><strong>kgoto</strong></span> label</pre>
        <pre class="synopsis"><span class="command"><strong>if</strong></span> xa R xb <span class="command"><strong>goto</strong></span> label</pre>
        <pre class="synopsis"><span class="command"><strong>if</strong></span> xa R xb <span class="command"><strong>then</strong></span></pre>
        <p>
      where <span class="emphasis"><em>label</em></span> is in the same instrument block and is not an expression, and where <span class="emphasis"><em>R</em></span> is one of the Relational operators (<span class="emphasis"><em>&lt;</em></span>, <span class="emphasis"><em>=</em></span>, <span class="emphasis"><em>&lt;=</em></span>, <span class="emphasis"><em>==</em></span>, <span class="emphasis"><em>!=</em></span>) (and <span class="emphasis"><em>=</em></span> for convenience, see also under <a class="link" href="ControlConditional.html" title="Conditional Values"><em class="citetitle">Conditional Values</em></a>).
    </p>
        <p>
      If <span class="emphasis"><em>goto</em></span> or <span class="emphasis"><em>then</em></span> is used instead of <span class="emphasis"><em>kgoto</em></span> or <span class="emphasis"><em>igoto</em></span>, the behavior is determined by the type being compared. If the comparison used k-type variables, kgoto is used and viceversa.
    </p>
        <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Note">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Note]" src="images/note.png" />
              </td>
              <th align="left">Note</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
        <span class="emphasis"><em>If/then/goto</em></span> statements cannot do audio-type comparisons. You can't put a-type variables in the comparison expressions for these opcodes. The reason for this is that audio variables are actually vectors, which can't be compared in the same way as scalars. If you need to compare individua audio samples, use  <a class="link" href="kr.html" title="kr"><em class="citetitle">kr = 1</em></a> or <a class="link" href="SigmodCompaccum.html" title="Comparators and Accumulators"><em class="citetitle">Comparators</em></a>
      </p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="refsect1" title="Examples">
        <a id="idp26534792"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the if...igoto combination. It uses the file <a class="ulink" href="examples/igoto.csd" target="_top"><em class="citetitle">igoto.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp26535744"></a>
          <p class="title">
            <strong>Example 350. Example of the if...igoto combination.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o igoto.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Get the value of the 4th p-field from the score.</span>
  iparam <span class="op">=</span> p4

  <span class="comment">; If iparam is 1 then play the high note.</span>
  <span class="comment">; If not then play the low note.</span>
  <span class="octrl">if</span> (iparam <span class="op">==</span> 1) <span class="octrl">igoto</span> <span class="olabel">highnote</span>
    <span class="octrl">igoto</span> <span class="olabel">lownote</span>

<span class="olabel">highnote</span><span class="op">:</span>
  ifreq <span class="op">=</span> 880
  <span class="octrl">goto</span> <span class="olabel">playit</span>

<span class="olabel">lownote</span><span class="op">:</span>
  ifreq <span class="op">=</span> 440
  <span class="octrl">goto</span> <span class="olabel">playit</span>

<span class="olabel">playit</span><span class="op">:</span>
  <span class="comment">; Print the values of iparam and ifreq.</span>
  <span class="opc">print</span> iparam
  <span class="opc">print</span> ifreq

  a1 <span class="opc">oscil</span> 10000, ifreq, 1
  <span class="opc">out</span> a1
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Table #1: a simple sine wave.</span>
<span class="stamnt">f</span> 1 0 32768 10 1

<span class="comment">; p4: 1 = high note, anything else = low note</span>
<span class="comment">; Play Instrument #1 for one second, a low note.</span>
<span class="stamnt">i</span> 1 0 1 0
<span class="comment">; Play a Instrument #1 for one second, a high note.</span>
<span class="stamnt">i</span> 1 1 1 1
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />

      Its output should include lines like this:
      </p>
        <pre class="screen">
instr 1:  iparam = 0.000
instr 1:  ifreq = 440.000
instr 1:  iparam = 1.000
instr 1:  ifreq = 880.000</pre>
        <p>
    </p>
        <p>
      Here is an example of the if...kgoto combination. It uses the file <a class="ulink" href="examples/kgoto.csd" target="_top"><em class="citetitle">kgoto.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp26539072"></a>
          <p class="title">
            <strong>Example 351. Example of the if...kgoto combination.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o kgoto.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Change kval linearly from 0 to 2 over</span>
  <span class="comment">; the period set by the third p-field.</span>
  kval <span class="opc">line</span> 0, p3, 2

  <span class="comment">; If kval is greater than or equal to 1 then play the high note.</span>
  <span class="comment">; If not then play the low note.</span>
  <span class="octrl">if</span> (kval <span class="op">&gt;=</span> 1) <span class="octrl">kgoto</span> <span class="olabel">highnote</span>
    <span class="octrl">kgoto</span> <span class="olabel">lownote</span>

<span class="olabel">highnote</span><span class="op">:</span>
  kfreq <span class="op">=</span> 880
  <span class="octrl">goto</span> <span class="olabel">playit</span>

<span class="olabel">lownote</span><span class="op">:</span>
  kfreq <span class="op">=</span> 440
  <span class="octrl">goto</span> <span class="olabel">playit</span>

<span class="olabel">playit</span><span class="op">:</span>
  <span class="comment">; Print the values of kval and kfreq.</span>
  <span class="opc">printks</span> "kval <span class="op">=</span> <span class="op">%</span>f, kfreq <span class="op">=</span> <span class="op">%</span>f\\n", 1, kval, kfreq

  a1 <span class="opc">oscil</span> 10000, kfreq, 1
  <span class="opc">out</span> a1
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Table #1: a simple sine wave.</span>
<span class="stamnt">f</span> 1 0 32768 10 1

<span class="comment">; Play Instrument #1 for two seconds.</span>
<span class="stamnt">i</span> 1 0 2
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />

      Its output should include lines like this:
      </p>
        <pre class="screen">
kval = 0.000000, kfreq = 440.000000
kval = 0.999732, kfreq = 440.000000
kval = 1.999639, kfreq = 880.000000</pre>
        <p>
    </p>
      </div>
      <div class="refsect1" title="Examples">
        <a id="idp26540584"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the if...then combo. It uses the file <a class="ulink" href="examples/ifthen.csd" target="_top"><em class="citetitle">ifthen.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp26541536"></a>
          <p class="title">
            <strong>Example 352. Example of the if...then combo.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o ifthen.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Get the note value from the fourth p-field.</span>
  knote <span class="op">=</span> p4

  <span class="comment">; Does the user want a low note?</span>
  <span class="octrl">if</span> (knote <span class="op">==</span> 0) then
    kcps <span class="op">=</span> 220
  <span class="comment">; Does the user want a middle note?</span>
  <span class="octrl">elseif</span> (knote <span class="op">==</span> 1) then
    kcps <span class="op">=</span> 440
  <span class="comment">; Does the user want a high note?</span>
  <span class="octrl">elseif</span> (knote <span class="op">==</span> 2) then
    kcps <span class="op">=</span> 880
  <span class="octrl">endif</span>

  <span class="comment">; Create the note.</span>
  kamp <span class="opc">init</span> 25000
  ifn <span class="op">=</span> 1
  a1 <span class="opc">oscili</span> kamp, kcps, ifn

  <span class="opc">out</span> a1
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Table #1, a sine wave.</span>
<span class="stamnt">f</span> 1 0 16384 10 1

<span class="comment">; p4: 0=low note, 1=middle note, 2=high note.</span>
<span class="comment">; Play Instrument #1 for one second, low note.</span>
<span class="stamnt">i</span> 1 0 1 0
<span class="comment">; Play Instrument #1 for one second, middle note.</span>
<span class="stamnt">i</span> 1 1 1 1
<span class="comment">; Play Instrument #1 for one second, high note.</span>
<span class="stamnt">i</span> 1 2 1 2
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1" title="See Also">
        <a id="idp26542496"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="elseif.html" title="elseif"><em class="citetitle">elseif</em></a>,
      <a class="link" href="else.html" title="else"><em class="citetitle">else</em></a>,
      <a class="link" href="endif.html" title="endif"><em class="citetitle">endif</em></a>,
      <a class="link" href="goto.html" title="goto"><em class="citetitle">goto</em></a>,
      <a class="link" href="igoto.html" title="igoto"><em class="citetitle">igoto</em></a>,
      <a class="link" href="kgoto.html" title="kgoto"><em class="citetitle">kgoto</em></a>,
      <a class="link" href="tigoto.html" title="tigoto"><em class="citetitle">tigoto</em></a>,
      <a class="link" href="timout.html" title="timout"><em class="citetitle">timout</em></a>
    </p>
      </div>
      <div class="refsect1" title="Credits">
        <a id="idp26546872"></a>
        <h2>Credits</h2>
        <p>Examples written by Kevin Conder.</p>
        <p>Added a note by Jim Aikin.</p>
        <p>February 2004. Added a note by Matt Ingalls.</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="opi.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="igoto.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">i </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> igoto</td>
        </tr>
      </table>
    </div>
  </body>
</html>