pindex
Prev | Orchestra Opcodes and Operators | Next

## pindex

pindex — Returns the value of a specified pfield.

## Description

pindex returns the value of a specified pfield.

## Syntax

    ivalue pindex ipfieldIndex

## Initialization

ipfieldIndex - pfield number to query.

ivalue - value of the pfield.

## Examples

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

Example 590. Example of the pindex 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   No messages  MIDI in
    -odac           -iadc    ; -d         -M0  ;;;RT audio I/O with MIDI in
    ; For Non-realtime ouput leave only the line below:
    ;-o pindex.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>
    ;Example by Anthony Kozar Dec 2006

    instr 1
        inum    pcount
        index   init 1
        loop1:
            ivalue pindex index
            printf_i "p%d = %f\n", 1, index, ivalue
            index   = index + 1
        if  (index <= inum) igoto loop1
        print inum
    endin

    </CsInstruments>
    <CsScore>
    i1  0 3 40 50         ; has 5 pfields
    i1  1 2 80            ; has 5 due to carry
    i1  2 1 40 50 60 70   ; has 7
    e
    </CsScore>
    </CsoundSynthesizer>

The example will produce the following output:

    new alloc for instr 1:
    WARNING: instr 1 uses 3 p-fields but is given 5
    p1 = 1.000000
    p2 = 0.000000
    p3 = 3.000000
    p4 = 40.000000
    p5 = 50.000000
    instr 1:  inum = 5.000
    B  0.000 ..  1.000 T  1.000 TT  1.000 M:      0.0
    new alloc for instr 1:
    WARNING: instr 1 uses 3 p-fields but is given 5
    p1 = 1.000000
    p2 = 1.000000
    p3 = 2.000000
    p4 = 80.000000
    p5 = 50.000000
    instr 1:  inum = 5.000
    B  1.000 ..  2.000 T  2.000 TT  2.000 M:      0.0
    new alloc for instr 1:
    WARNING: instr 1 uses 3 p-fields but is given 7
    p1 = 1.000000
    p2 = 2.000000
    p3 = 1.000000
    p4 = 40.000000
    p5 = 50.000000
    p6 = 60.000000
    p7 = 70.000000
    instr 1:  inum = 7.000

The warnings can be ignored, because the pfields are used indirectly through pindex instead of explicitly through p4, p5, etc.

Here is another example of the pindex opcode. It uses the file pindex-2.csd.

Example 591. Second example of the pindex opcode.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    -odac  ;;;realtime audio
    ;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
    ; For Non-realtime ouput leave only the line below:
    ; -o pindex-2.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

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

    ; This UDO returns a pfield value but returns 0 if it does not exist.
    opcode  mypvalue, i, i
    	index  xin
    	inum   pcount
    	if	  (index > inum)  then
    		iout = 0.0
    	else
    		iout pindex index
    	endif

    	xout	iout
    endop

    ; Envelope UDO that reads parameters from a flexible number of pfields
    ; Syntax:   kenv  flexlinseg  ipstart
    ;           ipstart is the first pfield of the envelope
    ;               parameters.  Reads remaining pfields (up to 21 of them).
    ;           kenv is the output envelope.

    opcode  flexlinseg, k, i
    	ipstart xin

    	iep1   mypvalue	ipstart
    	iep2   mypvalue	ipstart + 1
    	iep3   mypvalue	ipstart + 2
    	iep4   mypvalue	ipstart + 3
    	iep5   mypvalue	ipstart + 4
    	iep6   mypvalue	ipstart + 5
    	iep7   mypvalue	ipstart + 6
    	iep8   mypvalue	ipstart + 7
    	iep9   mypvalue	ipstart + 8
    	iepa   mypvalue	ipstart + 9
    	iepb   mypvalue	ipstart + 10
    	iepc   mypvalue	ipstart + 11
    	iepd   mypvalue	ipstart + 12
    	iepe   mypvalue	ipstart + 13
    	iepf   mypvalue	ipstart + 14
    	iepg   mypvalue	ipstart + 15
    	ieph   mypvalue	ipstart + 16
    	iepi   mypvalue	ipstart + 17
    	iepj   mypvalue	ipstart + 18
    	iepk   mypvalue	ipstart + 19
    	iepl   mypvalue	ipstart + 20

    	kenv   linseg	 iep1, iep2, iep3, iep4, iep5, iep6, iep7, iep8, \
    	                   iep9, iepa, iepb, iepc, iepd, iepe, iepf, iepg, \
    	                   ieph, iepi, iepj, iepk, iepl
    	xout   kenv
    endop

    instr 1
    ; This instrument only requires 3 pfields but can accept up to 24.
    ; (You will still get warnings about more than 3).
    kenv flexlinseg  4		; envelope params start at p4
    aout oscili kenv*.5, 256, 1
         outs aout, aout

    endin
    </CsInstruments>
    <CsScore>
    f1  0 8192 10 1

    i1  0 2  0.0 1.0 1.0 1.0 0.0
    i1  2 2  0.0 0.1 1.0 1.0 1.0 0.1 0.0
    i1  4 2  0.0 0.5 0.0	  			; one problem is that "missing" pfields carry
    i1  6 2  0.0 0.5 0.0 !	  			; now we can fix this problem with !
    i1  8 10  0.0  3.0 1.0  0.3 0.1  0.3 1.0  0.3 0.1  0.3 1.0  0.3 0.1  0.8 0.9  5.0 0.0

    e
    </CsScore>
    </CsoundSynthesizer>

The example will produce the following output:

    WARNING: instr 1 uses 3 p-fields but is given 8
    B  0.000 ..  2.000 T  2.000 TT  2.000 M:  0.49966  0.49966
    WARNING: instr 1 uses 3 p-fields but is given 10
    B  2.000 ..  4.000 T  4.000 TT  4.000 M:  0.50000  0.50000
    WARNING: instr 1 uses 3 p-fields but is given 10
    B  4.000 ..  6.000 T  6.000 TT  6.000 M:  0.49943  0.49943
    WARNING: instr 1 uses 3 p-fields but is given 6
    B  6.000 ..  8.000 T  8.000 TT  8.000 M:  0.00000  0.00000
    WARNING: instr 1 uses 3 p-fields but is given 20
    B  8.000 .. 18.000 T 18.000 TT 18.000 M:  0.49994  0.49994

## See Also

pcount

## Credits

Example by: Anthony Kozar

Dec. 2006

Prev | Up | Next
phasorbnk | Home | pinkish