Hi,
I have this nice and juicy opcode that simply has the function of reading some values in an array and get them according to a phasor (embodying the tempo of livecoding).
Here it is:
opcode pump, k, kk[]
kdiv, kvals[] xin
kdiv init i(kdiv)
klen lenarray kvals
kph chnget "PHASOR_IN_ANOTHER_INSTR"
kph = int(((kph * kdiv) % 1) * klen)
ktrig changed2 kph
if ktrig == 1 then
kout = kvals[kph]
endif
xout kout
endop
The very big problem is that sometimes kout doesn't get the values I need and csound do not appreciate it. Typically I use this for iterating some GEN tables (tuning system or envelope) and here if there's not a GEN value csound is stuck.
I think it's due to a lack of initialisation, but I tried everything: ktrig init -1, kph init 0, kout init kvals[0] (in this last one Csound gets super nervous). I need to tell csound to get the first value in the init state. How can I do it?
Thanks 
Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here
Maybe the issue is that no matter how you initialiase kph, you are setting it immediately before you use it, so any initialisation is moot.
Maybe reordering the operations, that is setting kph after you use it, might help.
Prof. Victor Lazzarini
Maynooth University
Ireland
Mh.. Thank you Victor, but TBH i’m not sure I understood what u were telling.
Anyway, this is the solution to add inside the code in order to “simulate” a first step init to get the first value of the array:
kinit init 1
if kinit == 1 then
kinit = 0
kout = kvals[0]
endif
Thanks to Johann Philippe,
best.
Here’s what I meant with the other code showing just the relevant lines
kph = int(((kph * kdiv) % 1) * klen) // <—— this assigns to kph
kout = kvals[kph] // <— this reads kph
No matter where you put a line
kph init 0
initialising kph, it won’t make a difference. This is because the first lines above
run *only* at perf time and the initialisaiton *only* at init time.
So the initial value of kph will always be overwritten before it is used. If you
reverse the lines, then you have a chance to use the initial value before
it gets changed.
In any case, it is normal practice to update the state of a phase integrator after
you use it. So you start with an initial value, use it, then increment it.
yap, I understood now.
Thank you. It works.