if
Prev | Orchestra Opcodes and Operators | Next

## if

if — Branches conditionally at initialization or during performance time.

## Description

if...igoto -- conditional branch at initialization time, depending on the truth value of the logical expression ia R ib. The branch is taken only if the result is true.

if...kgoto -- conditional branch during performance time, depending on the truth value of the logical expression ka R kb. The branch is taken only if the result is true.

if...goto -- combination of the above. Condition tested on every pass.

if...then -- allows the ability to specify conditional if/else/endif blocks. All if...then blocks must end with an endif statement. elseif and else statements are optional. Any number of elseif statements are allowed. Only one else statement may occur and it must be the last conditional statement before the endif statement. Nested if...then blocks are allowed.

| Note
Note that if the condition uses a k-rate variable (for instance, “if kval > 0”), the if...goto or if...then 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.

## Syntax

    if ia R ib igoto label

    if ka R kb kgoto label

    if xa R xb goto label

    if xa R xb then

where label is in the same instrument block and is not an expression, and where R is one of the Relational operators (<, =, <=, ==, !=) (and = for convenience, see also under Conditional Values).

If goto or then is used instead of kgoto or igoto, the behavior is determined by the type being compared. If the comparison used k-type variables, kgoto is used and viceversa.

| Note
If/then/goto 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 kr = 1 or Comparators

## Examples

Here is an example of the if...igoto combination. It uses the file igoto.csd.

Example 380. Example of the if...igoto combination.

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

    ; Initialize the global variables.
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      ; Get the value of the 4th p-field from the score.
      iparam = p4

      ; If iparam is 1 then play the high note.
      ; If not then play the low note.
      if (iparam == 1) igoto highnote
        igoto lownote

    highnote:
      ifreq = 880
      goto playit

    lownote:
      ifreq = 440
      goto playit

    playit:
      ; Print the values of iparam and ifreq.
      print iparam
      print ifreq

      a1 oscil 10000, ifreq, 1
      out a1
    endin

    </CsInstruments>
    <CsScore>

    ; Table #1: a simple sine wave.
    f 1 0 32768 10 1

    ; p4: 1 = high note, anything else = low note
    ; Play Instrument #1 for one second, a low note.
    i 1 0 1 0
    ; Play a Instrument #1 for one second, a high note.
    i 1 1 1 1
    e

    </CsScore>
    </CsoundSynthesizer>

Its output should include lines like this:

    instr 1:  iparam = 0.000
    instr 1:  ifreq = 440.000
    instr 1:  iparam = 1.000
    instr 1:  ifreq = 880.000

Here is an example of the if...kgoto combination. It uses the file kgoto.csd.

Example 381. Example of the if...kgoto combination.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ; Audio out   Audio in
    -odac           -iadc    ;;;RT audio I/O
    ; For Non-realtime ouput leave only the line below:
    ; -o kgoto.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    ; Initialize the global variables.
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      ; Change kval linearly from 0 to 2 over
      ; the period set by the third p-field.
      kval line 0, p3, 2

      ; If kval is greater than or equal to 1 then play the high note.
      ; If not then play the low note.
      if (kval >= 1) kgoto highnote
        kgoto lownote

    highnote:
      kfreq = 880
      goto playit

    lownote:
      kfreq = 440
      goto playit

    playit:
      ; Print the values of kval and kfreq.
      printks "kval = %f, kfreq = %f\\n", 1, kval, kfreq

      a1 oscil 10000, kfreq, 1
      out a1
    endin

    </CsInstruments>
    <CsScore>

    ; Table #1: a simple sine wave.
    f 1 0 32768 10 1

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

    </CsScore>
    </CsoundSynthesizer>

Its output should include lines like this:

    kval = 0.000000, kfreq = 440.000000
    kval = 0.999732, kfreq = 440.000000
    kval = 1.999639, kfreq = 880.000000

## Examples

Here is an example of the if...then combo. It uses the file ifthen.csd.

Example 382. Example of the if...then combo.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    ; Audio out   Audio in
    -odac           -iadc    ;;;RT audio I/O
    ; For Non-realtime ouput leave only the line below:
    ; -o ifthen.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      ; Get the note value from the fourth p-field.
      knote = p4

      ; Does the user want a low note?
      if (knote == 0) then
        kcps = 220
      ; Does the user want a middle note?
      elseif (knote == 1) then
        kcps = 440
      ; Does the user want a high note?
      elseif (knote == 2) then
        kcps = 880
      endif

      ; Create the note.
      kamp init 25000
      ifn = 1
      a1 oscili kamp, kcps, ifn

      out a1
    endin

    </CsInstruments>
    <CsScore>

    ; Table #1, a sine wave.
    f 1 0 16384 10 1

    ; p4: 0=low note, 1=middle note, 2=high note.
    ; Play Instrument #1 for one second, low note.
    i 1 0 1 0
    ; Play Instrument #1 for one second, middle note.
    i 1 1 1 1
    ; Play Instrument #1 for one second, high note.
    i 1 2 1 2
    e

    </CsScore>
    </CsoundSynthesizer>

## See Also

elseif, else, endif, goto, igoto, kgoto, tigoto, timout

## Credits

Examples written by Kevin Conder.

Added a note by Jim Aikin.

February 2004. Added a note by Matt Ingalls.

Prev | Up | Next
i | Home | fftinv