active
Prev | Orchestra Opcodes and Operators | Next

## active

active — Returns the number of active instances of an instrument.

## Description

Returns the number of active instances of an instrument with options to ignore releasing instances.

## Syntax

    ir active insnum [,iopt [,inorel]]

    ir active Sinsname [,iopt [,inorel]]

    kres active kinsnum [,iopt [,inorel]]

## Initialization

insnum -- number or string name of the instrument to be reported

Sinsname -- instrument name

iopt -- select currently active (zero, default), or all every active (non zero)

inorel -- if non-zero ignore instruments in release phase (zero, default), only valid if iopts is zero.

## Performance

kinsnum -- number or string name of the instrument to be reported

active returns the number of active instances of instrument number insnum/kinsnum (or named instrument Sinsname). As of Csound 4.17 the output is updated at k-rate (if input arg is k-rate), to allow running count of instr instances.

As of Csound 5.17 if the instrument number is given as zero then all instruments are counted.

## Examples

Here is a simple example of the active opcode. It uses the file active.csd.

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

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

    ; Instrument #1 - a noisy waveform.
    instr 1
      ; Generate a really noisy waveform.
      anoisy rand 44100
      ; Turn down its amplitude.
      aoutput gain anoisy, 2500
      ; Send it to the output.
      out aoutput
    endin

    ; Instrument #2 - counts active instruments.
    instr 2
      ; Count the active instances of Instrument #1.
      icount active 1
      ; Print the number of active instances.
      print icount
    endin

    </CsInstruments>
    <CsScore>

    ; Start the first instance of Instrument #1 at 0:00 seconds.
    i 1 0.0 3.0

    ; Start the second instance of Instrument #1 at 0:015 seconds.
    i 1 1.5 1.5

    ; Play Instrument #2 at 0:01 seconds, when we have only
    ; one active instance of Instrument #1.
    i 2 1.0 0.1

    ; Play Instrument #2 at 0:02 seconds, when we have
    ; two active instances of Instrument #1.
    i 2 2.0 0.1
    e

    </CsScore>
    </CsoundSynthesizer>

Its output should include lines like this:

    instr 2:  icount = 1.000
    instr 2:  icount = 2.000

Here is a more advanced example of the active opcode. It displays the results of the active opcode at k-rate instead of i-rate. It uses the file active_k.csd.

Example 44. Example of the active opcode at k-rate.

    <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 active_k.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

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

    ; Instrument #1 - a noisy waveform.
    instr 1
      ; Generate a really noisy waveform.
      anoisy rand 44100
      ; Turn down its amplitude.
      aoutput gain anoisy, 2500
      ; Send it to the output.
      out aoutput
    endin

    ; Instrument #2 - counts active instruments at k-rate.
    instr 2
      ; Count the active instances of Instrument #1.
      kcount active 1
      ; Print the number of active instances.
      printk2 kcount
    endin

    </CsInstruments>
    <CsScore>

    ; Start the first instance of Instrument #1 at 0:00 seconds.
    i 1 0.0 3.0

    ; Start the second instance of Instrument #1 at 0:015 seconds.
    i 1 1.5 1.5

    ; Play Instrument #2 at 0:01 seconds, when we have only
    ; one active instance of Instrument #1.
    i 2 1.0 0.1

    ; Play Instrument #2 at 0:02 seconds, when we have
    ; two active instances of Instrument #1.
    i 2 2.0 0.1
    e

    </CsScore>
    </CsoundSynthesizer>

Its output should include lines like:

     i2     1.00000
     i2     2.00000

Here is another example of the active opcode, using the number of instances to calculate gain. It uses the file active_scale.csd.

Example 45. Example of the active opcode at k-rate.

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

    sr= 44100
    ksmps = 64
    nchnls = 1
    0dbfs = 1

    ;by Victor Lazzarini 2008

    instr 1
    kscal active  1
    kamp port   1/kscal, 0.01
    asig  oscili  kamp, p4, 1
    kenv linseg 0, 0.1,1,p3-0.2,1,0.1, 0

            out asig*kenv
    endin

    </CsInstruments>
    <CsScore>
    f1 0 16384 10 1

    i1 0 10 440
    i1 1 3  220
    i1 2 5  350
    i1 4 3  700
    e
    </CsScore>
    </CsoundSynthesizer>

## Credits

Author: John ffitch
University of Bath/Codemist Ltd.
Bath, UK
July, 1999

Examples written by Kevin Conder.

New in Csound version 3.57; named instruments added version 5.13

Option for all ever active new in 5.13

Count of all instruments new in 5.17

No release option new in 5.19

Prev | Up | Next
abs | Home | adsr