Skip to content

nstrnum

Returns an instrument number from a name or definition.

instrnum is another name for this opcode in Csound 7.

Syntax

insno = nstrnum("name")
insno = nstrnum(definition[, iOffset])
insno nstrnum "name"
insno nstrnum definition[, iOffset]

Initialization

insno -- the instrument number of the named instrument.

"name" is the instrument name. definition is an InstrDef, which can refer to a named or unnamed instrument. Both forms run at initialization.

The definition form accepts an optional numeric iOffset, which defaults to 0 and is added to the instrument number. A fractional offset can identify a particular note when scheduling by number.

If an instrument with the specified name does not exist, an init error occurs, and -1 is returned.

Examples

Here is an example of the nstrnum opcode. It uses the file nstrnum_nstrstr.csd.

Example of the nstrnum opcode.
<CsoundSynthesizer>
<CsOptions>
-n
</CsOptions>
<CsInstruments>

sr      = 44100
ksmps = 32
nchnls  = 2
0dbfs   = 1

instr nothing
endin

instr john
 prints "instrument name = "
 puts nstrstr(p1),1
 prints "instrument number = %d\n", nstrnum("john")
endin

instr test
endin

</CsInstruments>
<CsScore>
i "john" 0 0
</CsScore>
</CsoundSynthesizer>

The example will produce the following output:

instrument name = john
instrument number = 2

A musical examples featuring the nstrnum opcode: TivalNstrnum_Yi.csd by Steven Yi.

This Csound 7 example uses the instrnum and instrstr spellings with a named definition. It uses instrument-names.csd.

Find the number of a typed definition
<CsoundSynthesizer>
<CsOptions>
-odac -d -m0
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

instr Tone
  aTone poscil 0.1, p4
  out aTone
endin

instr 1
  definition:InstrDef = Tone
  Sname = str(definition)
  iNumber = instrnum(definition)
  Slookup = instrstr(iNumber)
  printf_i "Definition name %s\n", 1, Sname
  printf_i "Number lookup gives %s\n", 1, Slookup
  ; The typed definition can also go straight to schedule.
  schedule(definition, 0, 0.2, 440)
endin
</CsInstruments>
<CsScore>
i 1 0 0.3
e
</CsScore>
</CsoundSynthesizer>

See also

instrnum, nstrstr, str, Instrument definitions, instances and opcode objects

Credits

Author: Istvan Varga
New in version 4.23
Written in the year 2002.

The InstrDef form was added by Victor Lazzarini for Csound 7.