The Alternative Csound Reference Manual
Previous |  | Next

# instr

instr -- Starts an instrument block.

## Description

Starts an instrument block.

## Syntax

instr i, j, ...

## Initialization

Starts an instrument block defining instruments i, j, ...

i, j, ... must be numbers, not expressions. Any positive integer is legal, and in any order, but excessively high numbers are best avoided.

| Note
| There may be any number of instrument blocks in an orchestra.

Instruments can be defined in any order (but they will always be both initialized and performed in ascending instrument number order). Instrument blocks cannot be nested (i.e. one block cannot contain another).

## Performance

### Calling an Instrument within an Instrument

You can call an instrument within an instrument as if it were an opcode either with the subinstr opcode or by specifying an instrument with a text name:

    instr MyOscil
    ...
    endin

If an instrument is defined with a name, you simply call it directly like an opcode:

    asig MyOscil iamp, ipitch, iftable

By default, all output parameters correspond to the called instrument's output with the signal output opcodes. All input parameters are mapped to the called instrument's p-fields starting with the fourth one, p4. The values of the called instrument's second and third p-fields, p2 and p3, are automatically set to those of the calling instrument's.

A named instrument must be defined before any instrument that calls it.

#### Advanced Options

You can optionally define an instrument's interface if you need to pass k-rate values, audio input, or greater than 8 audio output channels. The output and input types are specified after the instrument name, as a list of characters (0, a, i, k, or p):

    instr name, outputlist, inputlist

For example, this instrument:

    instr MyFilter, aak, aaki
    ...
    endin

Specifies an instrument that outputs 2 a-rate signals and 1 k-rate signal. It takes 2 a-rate signals as input followed by a k-rate signal and an i-rate signal.

A call to this instrument would something like like:

    asig1, asig2, k1 MyFilter aleft, aright, kfreq, ibw

The allowable character types are:

Table 1. Allowable Character Types

Character | Signal | Context
--- | --- | ---
0 | Specifies no signal passed. | Allowed with input and output. Cannot occur with other types.
a | a-rate signal. | Allowed with input and output. Accessed with the signal input and signal output opcodes.
i | i-rate signal mapped to a p-field. | Input only.
k | k-rate signal. | Allowed with input and output. Accessed with the ink and outk opcodes.
p | k-rate signal mapped to a p-field. | Input only.

The i and p character types are mapped to p-fields as they occur in order starting with the fourth p-field, p4. The a and k character types are mapped in order of channels to be accessed with the signal input and signal output opcodes.

For example:

    instr MyProcess 0, apaki

Defines an instrument with no output, 2 a-rate signal inputs (the first and third input parameters). The second input ("p") is mapped to p4, which potentially changes every k-pass. The fourth input parameter is mapped to be retrieved with the ink opcode. The last input signal is mapped to p5, and will stay the same value during the instrument instance's performance.

A call to this instrument would look something like:

    MyProcess asig, kfreq, arefsig, kamp, imode

Accessing the data passed to the instrument would look something like:

    instr MyProcess 0, apaki
        imode = p5

        asig, arefsig ins
        kamp ink

        ; p4 can change during performance
        printk .1, p4
    endin

See ink/outk documentation for another example.

| Hint
| If you use the inch, outc, outch, etc. opcodes, you can create an instrument that will compile and function in any orchestra of any number of channels. A nice feature to use with named instruments is the #include feature. You can then define your named instruments in separate files, using #include when you need to use one.

## Examples

Here is an example of the instr opcode. It uses the files instr.orc and instr.sco.

Example 1. Example of the instr opcode.

    /* instr.orc */
    ; Initialize the global variables.
    sr = 44100
    kr = 4410
    ksmps = 10
    nchnls = 1

    ; Instrument #1.
    instr 1
      iamp = 10000
      icps = 440
      iphs = 0

      a1 oscils iamp, icps, iphs
      out a1
    endin
    /* instr.orc */

    /* instr.sco */
    ; Play Instrument #1 for 2 seconds.
    i 1 0 2
    e
    /* instr.sco */

## See Also

endin, ink, in, outk, out, subinstr

## Credits

Example written by Kevin Conder.

Previous | Home | Next
instimes | Up | int