Whenever a Csound instrument is called, all variables are set to initial values. This is called the initialization pass.
but a few paragraphs later is write:
k-Values and Initialization in Multiple Triggered Instruments
What happens on a k-variable if an instrument is called multiple times? What is the initialization value of this variable on the first call, and on the subsequent calls?
If this variable is not set explicitely, the init value in the first call of an instrument is zero, as usual. But, for the next calls, the k-variable is initialized to the value which was left when the previous instance of the same instrument turned off.
… I wondering if is correct what I have understand:
The Init Pass
Whenever a Csound instrument is called, the a and i variables are set to initial values (for the k variables see later). This is called the initialization pass.
@Karamel1 .. you are one most active in this forum… I’m not a guru in Csound but in this forum I have had a lot of support from users. I downloaded your sample player and I try it in this week end. thanks for sharing it.
Every time an instrument is triggered to perform it will run an initial init-pass, followed by a succession of k-passes. There will be sr/ksmps k-cycles each second.
All variables get set to an initial value during the init pass, even k-rate variables.
After the init pass only k and a variables can be updated.
To find the initial value of a k-rate variable during the init pass cast the variable to i(), i.e, print i(Kvar)
Testing the values of variables:
If you wish to test variables during the init pass, test i-rate variables or cast your k variable to i-rate (see above). if-statements testing k-rate variables are only effective during k-cycles, i.e, all cycles after the init pass. For example, this instrument will print hello the first time it is run:
instr 1
k1 init 10
if k1 == 0 then
prints "Hello"
endif
endin
It looks like the value of k1 is set to 10, yet the test returns true when asked if k1 is equal to 0. k1 is indeed set to 10, but the k-rate test will fail during the init pass and result in fall-through. However, if we test i(k1), it won’t print “Hello”, because the initial value of k1 is set to 10.
Sorry I don’t have time to go into much more details. Feel free to query any of this. It’s really a subject of much confusion for anyone starting Csound!
Just one thing I don’t pick up. If i variables are set and are not update, how is it possible to have a loop using a i variable a a counter ? Or does it mean that the loop is done at init time only ?
This drives me to a difficulty I encounter :
Here is a instrument aiming at setting up random values to sliders which works great when called by
event “i”,3,0,0,10 ; P4 correspond to the set of sliders
> instr 3
> reinit SET1
> SET1:
> iCnt init 1
> iRnd init 0
> iCnt =1
> rireturn
> iCnt = 1
> while iCnt <=32 do ;pour chaque pas
> iRnd random 0,5 ; random between 0 and 4
> if int(iRnd) >=4 then ; consider only when rand is >= 3 to allow more silences
> iRnd random 0,127 ; une velocite aleatoire
> Schan sprintf "S%d_%d",p4,iCnt
> chnset int(iRnd), Schan
> Schan = ""
> endif
> iCnt+=1
> od
> endin
Now when the same code is turned into an UDO… it does not work at all
Yeah. You can loop during the init pass. There is no problem there. And your i-variable values can be changed during the init pass, but once the init pass is complete, their values are fixed for the duration of that event.
Why are you using reinit here? I don’t think it is necessary considering all your code is running in the init pass.
reinit SET1
SET1:
iCnt init 1
iRnd init 0
iCnt =1
rireturn
;ResetInst iInstNum
iCnt = 1
while iCnt <=32 do ;pour chaque pas
iRnd random 0,5 ; random between 0 and 4
if int(iRnd) >=4 then ; consider only when rand is >= 3 to allow more silences
iRnd random 0,127 ; une velocite aleatoire
Schan sprintf "S%d_%d", iInstNum ,iCnt
chnset int(iRnd), Schan
Schan = ""
endif
iCnt+=1
od
endop
opcode RandomJM,0,i
iInstNum xin
iCnt init 1
iRnd init 0
iCnt =1
iCnt = 1
while iCnt <=32 do ;pour chaque pas
iRnd random 0,5 ; random between 0 and 4
if int(iRnd) >=4 then ; consider only when rand is >= 3 to allow more silences
iRnd random 0,127 ; une velocite aleatoire
Schan sprintf "S%d_%d", iInstNum ,iCnt
prints Schan
;chnset int(iRnd), Schan
Schan = ""
endif
iCnt+=1
od
endop
Note that I removed the chnset and replaced it with an i-rate prints opcode. It prints out everything I would expect during the initial init pass.