An array must be created (via init or fillarray) as kMyName plus_ ending brackets. The brackets determine the dimensions of the array. So,
kArr[]init10
creates a one-dimensional array of length 10, whereas
kArr[][]init10,10
creates a two-dimensional array with 10 rows and 10 columns.
After the initalization of the array, referring to the array as a whole is done without any brackets. Brackets are only used if an element is indexed:
kArr[]init10;with brackets because of initializationkLen=lenarray(kArr);without bracketskFirstEl=kArr[0];indexing with brackets
The same syntax is used for a simple copy via the '=' operator:
kArr1[]fillarray1,2,3,4,5kArr2[]=kArr1;creates kArr2 as copy of kArr1
k-rate
Note that most array operations are currently k-rate only. So like any other k-rate opcode, an operation on arrays will be automatically repeated every k-cycle. For instance, this code will repeat re-writing the array with different random values every k-cycle, as long as the instrument runs:
If you want to avoid this, you must organize it in one of the usual ways, for instance by using a trigger:
kArr[]init10kTrigmetro1ifkTrig==1then;do the following once a secondkIndx=0untilkIndx==lenarray(kArr)dokArr[kIndx]rnd3110,0kIndx+=1odendif
Creation/Initialization
The usual way to create an array is with init:
kArr[]init10;creates one-dimensional array with length 10kArr[][]init10,10;creates two-dimensional array
A one-dimensional array can also be created and filled with distinct values by the opcode fillarray. This line creates a vector with length 4 and puts in the numbers [1, 2, 3, 4]:
kArr[]fillarray1,2,3,4
Length
The function lenarray(kArr) reports the length of an array. See example for function lenarray.
If the four basic math operators are used between an array and a scalar (number), the operation is applied to each element. The safest way to do this is to store the result in a new array:
kArr1[]fillarray1,2,3kArr2[]=kArr1+10;(kArr2 is now [11, 12, 13])
<CsoundSynthesizer><CsOptions>-n -m128
</CsOptions><CsInstruments>instr1;create array and fill with numbers 1..10kArr1[]fillarray1,2,3,4,5,6,7,8,9,10;print contentprintf"%s",1,"\nInitial content:\n"kndx=0untilkndx==lenarray(kArr1)doprintf"kArr[%d] = %f\n",kndx+1,kndx,kArr1[kndx]kndx+=1od;add 10kArr2[]=kArr1+10;print contentprintf"%s",1,"\nAfter adding 10:\n"kndx=0untilkndx==lenarray(kArr2)doprintf"kArr[%d] = %f\n",kndx+1,kndx,kArr2[kndx]kndx+=1od;subtract 5kArr3[]=kArr2-5;print contentprintf"%s",1,"\nAfter subtracting 5:\n"kndx=0untilkndx==lenarray(kArr3)doprintf"kArr[%d] = %f\n",kndx+1,kndx,kArr3[kndx]kndx+=1od;multiply by -1.5kArr4[]=kArr3*-1.5;print contentprintf"%s",1,"\nAfter multiplying by -1.5:\n"kndx=0untilkndx==lenarray(kArr4)doprintf"kArr[%d] = %f\n",kndx+1,kndx,kArr4[kndx]kndx+=1od;divide by -3/2kArr5[]=kArr4/-(3/2);print contentprintf"%s",1,"\nAfter dividing by -3/2:\n"kndx=0untilkndx==lenarray(kArr5)doprintf"kArr[%d] = %f\n",kndx+1,kndx,kArr5[kndx]kndx+=1od;turnoffturnoffendin</CsInstruments><CsScore>i10.1</CsScore></CsoundSynthesizer>
+, -, *, / on a Second Array
If the four basic math operators are used between two arrays, the operation is applied element by element. The result can be straightforward stored in a new array:
kArr1[]fillarray1,2,3kArr2[]fillarray10,20,30kArr3[]=kArr1+kArr2;(kArr3 is now [11, 22, 33])
Use reshapearray to change the shape of an array without changing its capacity (change a 1D to a 2D array and viceversa). See example for opcode reshapearray.
Arrays in UDOs
The dimension of an input array must be declared in two places:
as k[] or k[][] in the type input list
as kName[], kName[][] etc in the xin list.
For instance :
opcodeFirstEl,k,k[];returns the first element of vector kArrkArr[]xinxoutkArr[0]endop
Here is an example of an array in an UDO. It uses the file array_udo-modern.csd.
<CsoundSynthesizer><CsOptions>-nm128
</CsOptions><CsInstruments>opcodeFirstEl,k,k[];returns the first element of vector kArrArr:k[]=xin()xout(Arr[0])endopinstr1Arr:k[]=fillarray(6,3,9,5,1)first:k=FirstEl(Arr)printf("first = %d\n",1,first)turnoff()endin</CsInstruments><CsScore>i100.1</CsScore></CsoundSynthesizer>
Here is an example of an array in an UDO. It uses the file array_udo.csd.
<CsoundSynthesizer><CsOptions>-nm128
</CsOptions><CsInstruments>opcodeFirstEl,k,k[];returns the first element of vector kArrkArr[]xinxoutkArr[0]endopinstr1kArr[]fillarray6,3,9,5,1kFirstFirstElkArrprintf"kFirst = %d\n",1,kFirstturnoffendin</CsInstruments><CsScore>i10.1</CsScore></CsoundSynthesizer>
Note that if an opcode (for example inrg), alters arguments on its right hand argument list, an array index should not be used there. Unlike a normal variable, the array won't changed by the opcode.
Credits
This manual page has been written by Joachim Heintz.
July 2013