[Csnd] puzzled, array in UDO
| Date | 2023-02-15 21:03 |
| From | Oeyvind Brandtsegg |
| Subject | [Csnd] puzzled, array in UDO |
Hi
Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
I am puzzled by how this (does not) work. There is probably something I misunderstand about internal Csound processes, so I thought I might ask here to get help understanding it. I am trying to make a UDO to reverse the order of all elements in an array. It is easy enough at i-rate, and I can make the code work at k-rate if I do it inline (not in a UDO), but I fail to make it work with k-rate arrays in a UDO. My code to reverse the order of elements is: kindex = 0 while kindex < lenarray(kInput) do kOutput[kindex] = kInput[lenarray(kInput)-1-kindex] kindex += 1 od A full csd showing both the i-rate UDO, k-rate UDO, and regular Csound code (without UDO) is pasted below. The problem shows in instr 1 (k-rate UDO). Any help is greatly appreciated. all best Øyvind <CsoundSynthesizer> <CsOptions> </CsOptions> <CsInstruments> ; UDO to reverse the order of an array at i-rate opcode Reverse, i[], i[] iInput[] xin iOutput[] init lenarray(iInput) index = 0 while index < lenarray(iInput) do iOutput[index] = iInput[lenarray(iInput)-1-index] index += 1 od xout iOutput endop ; UDO to reverse the order of an array at k-rate opcode Reverse, k[], k[]k kInput[], kindex xin kOutput[] init lenarray(kInput) while kindex < lenarray(kInput) do kOutput[kindex] = kInput[lenarray(kInput)-1-kindex] kindex += 1 od xout kOutput endop instr 1 ; it does not work when the reversal is inside a UDO p3 = 4/kr kInput[] fillarray 1,2,3,4 ktrig metro 1 if ktrig > 0 then kindex = 0 kOutput[] Reverse kInput, kindex endif printarray kOutput endin instr 2 ; this one works as expected, when the reversal is not in a UDO p3 = 4/kr kInput[] fillarray 1,2,3,4 kOutput[] init lenarray(kInput) ktrig metro 1 if ktrig > 0 then kindex = 0 while kindex < lenarray(kInput) do kOutput[kindex] = kInput[lenarray(kInput)-1-kindex] kindex += 1 od endif printarray kOutput endin instr 3 ; it also works fine in a UDO with i-rate arrays iInput[] fillarray 1,2,3,4 iOutput[] Reverse iInput printarray iOutput endin </CsInstruments> <CsScore> i1 0 .1 i2 1 .1 i3 2 .1 e </CsScore> </CsoundSynthesizer> |
| Date | 2023-02-15 21:25 |
| From | "Jeanette C." |
| Subject | Re: [Csnd] puzzled, array in UDO |
Hey hey Oeyvind, why do you pass the index to your k-rate opcode? This ight be an issue. Have you tried setting up the kIndex inside the opcode and perhaps passing the trigger and also have the if (kTrig == 1) statement inside the opcode. I didn't test it, but it feels odd reading it like this. HTH. Best wishes, Jeanette Feb 15 2023, Oeyvind Brandtsegg has written: > Hi > > I am puzzled by how this (does not) work. There is probably something I > misunderstand about internal Csound processes, so I thought I might ask > here to get help understanding it. > > I am trying to make a UDO to reverse the order of all elements in an array. > It is easy enough at i-rate, and I can make the code work at k-rate if I do > it inline (not in a UDO), but I fail to make it work with k-rate arrays in > a UDO. > My code to reverse the order of elements is: > kindex = 0 > while kindex < lenarray(kInput) do > kOutput[kindex] = kInput[lenarray(kInput)-1-kindex] > kindex += 1 > od > > A full csd showing both the i-rate UDO, k-rate UDO, and regular Csound code > (without UDO) is pasted below. The problem shows in instr 1 (k-rate UDO). > Any help is greatly appreciated. > > all best > Øyvind > > |
| Date | 2023-02-15 22:59 |
| From | Guillermo Senna |
| Subject | Re: [Csnd] puzzled, array in UDO |
Cool bug! I'm testing with Csound v6.17, but by any chance does adding the line "kindex = kindex" before the loop inside the UDO also fix the issue for you? On 15/2/23 18:03, Oeyvind Brandtsegg wrote: > Hi > > I am puzzled by how this (does not) work. There is probably something > I misunderstand about internal Csound processes, so I thought I might > ask here to get help understanding it. > > I am trying to make a UDO to reverse the order of all elements in an > array. It is easy enough at i-rate, and I can make the code work at > k-rate if I do it inline (not in a UDO), but I fail to make it work > with k-rate arrays in a UDO. > My code to reverse the order of elements is: > kindex = 0 > while kindex < lenarray(kInput) do > kOutput[kindex] = kInput[lenarray(kInput)-1-kindex] > kindex += 1 > od > > A full csd showing both the i-rate UDO, k-rate UDO, and regular Csound > code (without UDO) is pasted below. The problem shows in instr 1 > (k-rate UDO). > Any help is greatly appreciated. > > all best > Øyvind > > |
| Date | 2023-02-15 23:03 |
| From | Oeyvind Brandtsegg |
| Subject | Re: [Csnd] puzzled, array in UDO |
Hi, yes i did have the index inside at first, and it worked the same. But your suggestion of putting the ktrig inside the UDO makes it work. Thanks ons. 15. feb. 2023, 22:27 skrev Jeanette C. <julien@mail.upb.de>: Hey hey Oeyvind, |
| Date | 2023-02-15 23:07 |
| From | Oeyvind Brandtsegg |
| Subject | Re: [Csnd] puzzled, array in UDO |
Whoa, yes, guillermo, it does fix it. Like this. That is indeed strange(?) ; UDO to reverse the order of an array at k-rate opcode Reverse, k[], k[]k kInput[], kindex xin kOutput[] init lenarray(kInput) kindex = kindex while kindex < lenarray(kInput) do kOutput[kindex] = kInput[lenarray(kInput)-1-kindex] kindex += 1 od xout kOutput endop tor. 16. feb. 2023 kl. 00:03 skrev Oeyvind Brandtsegg <obrandts@gmail.com>:
|
| Date | 2023-02-16 01:39 |
| From | Aaron Krister Johnson |
| Subject | Re: [Csnd] puzzled, array in UDO |
Ah, it must be that the assignment to a local scope needs to happen. To be filed away under "things to remember when you need to loop in an UDO" On Wed, Feb 15, 2023 at 4:08 PM Oeyvind Brandtsegg <obrandts@gmail.com> wrote:
|
| Date | 2023-02-16 10:40 |
| From | Victor Lazzarini |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
yes, that’s a bug alright. I have looked at this and what happens is that if there are no perf statements outside the loop,
the opcode behaves in such a way that the loop is ignored. However if you insert any statement (like kindex = kindex) then
it works correctly. For example,
opcode Test0, k, k
kindex xin
while kindex < 3 do
kindex += 1
od
xout kindex
endop
doesn’t work correctly, but this one does
opcode Test1, k, k
kindex xin
k1 = 0
while kindex < 3 do
kindex += 1
od
xout kindex
endop
I know that if there are no perf statements in a UDO there’s no perf cycle, but here we are ignoring perf statements inside
the loop. We’ll fix this.
========================
Prof. Victor Lazzarini
Maynooth University
Ireland
> On 16 Feb 2023, at 01:39, Aaron Krister Johnson |
| Date | 2023-02-16 13:17 |
| From | Victor Lazzarini |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
I believe this is now fix in git (both csound6 and develop branches). ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 16 Feb 2023, at 10:40, Victor Lazzarini |
| Date | 2023-02-16 15:43 |
| From | joachim heintz |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
hi oeyvind -
interesting to have a look at this ...
it does not work for me either, but is it really true to say it works in
instrument 1?
when i do the same here (in my understanding), it would be:
if ktrig > 0 then
kindex = 0
while kindex < lenarray(kInput) do
kOutput[kindex] = kInput[lenarray(kInput)-1-kindex]
kindex += 1
od
kInput = kOutput
printarray kOutput
printarray kInput
endif
and then it does not work at all.
so it seems to me that this copying arrays with the equal sign is not
reliable. what works for me is to copy "manually" in instr 2:
;copy kOOutput to gkArr
kindx = 0
while kindx < lenarray(kInput) do
gkArr[kindx] = kOutput[kindx]
kindx += 1
od
best -
joachim
On 16/02/2023 15:30, Oeyvind Brandtsegg wrote:
> Thanks so much. Happy to hear that it was not my own fault this time ;-)
>
> I have a related issue, this time it is not related to processing it
> with a UDO, but it occurs when I try to reverse the contents of a global
> array.
> My use case is that I would like to k-rate trigger the reversal of the
> contents of a global array in place.
>
> My thinking was that I could do the reversal as before, and since I am
> inside a conditional, I would only copy the reversed array back to the
> global array once the reversal had been processed.
>
> Example below
>
> all best
> Øyvind
>
> |
| Date | 2023-02-16 15:57 |
| From | Victor Lazzarini |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
It looks like the problem is that the copy happens at both i-time and perf-time. Since at i-time kOutput is all zeros, that gets copy to the global array and then to kInput and so it’s zeros all the way. I have added a special k-rate copying opcode that gets called at i-time only to allocate memory if needed and then copies only at k-rate. This is for csound 6 and it’s in the csound6 branch now. I tested your code and it passes. In Csound 7, the code is parsed differently and the copy only happens at i-time (which is also wrong and needs fixing, but the problem is one that we know of, code being parsed to use a =.generic opcode that is causing trouble also elsewhere).That’ll probably need to be addressed differently. ======================== Prof. Victor Lazzarini Maynooth University Ireland > On 16 Feb 2023, at 14:30, Oeyvind Brandtsegg |
| Date | 2023-02-16 16:12 |
| From | Guillermo Senna |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
The use of gotos is discouraged, but -based on what Victor said- if you don't want to update yet this should fix it: igoto jump gkArr = kOutput ; removing this line makes it work, ...but I would like to copy the result back to the same global array jump: printarray gkArr On 16/2/23 12:57, Victor Lazzarini wrote: > It looks like the problem is that the copy happens at both i-time and perf-time. Since at i-time kOutput is all zeros, that gets copy to the global array and > then to kInput and so it’s zeros all the way. I have added a special k-rate copying opcode that gets called at i-time only to allocate memory if needed and > then copies only at k-rate. This is for csound 6 and it’s in the csound6 branch now. I tested your code and it passes. > > In Csound 7, the code is parsed differently and the copy only happens at i-time (which is also wrong and needs fixing, but the problem is one that we > know of, code being parsed to use a =.generic opcode that is causing trouble also elsewhere).That’ll probably need to be addressed differently. > > ======================== > Prof. Victor Lazzarini > Maynooth University > Ireland > >> On 16 Feb 2023, at 14:30, Oeyvind Brandtsegg |
| Date | 2023-02-17 12:49 |
| From | Oeyvind Brandtsegg |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
Ah, thanks, yes now I understand the cause of the problem. Thanks for the fix, and good to know it is being addressed for future improvements too. tor. 16. feb. 2023 kl. 16:57 skrev Victor Lazzarini <Victor.Lazzarini@mu.ie>: It looks like the problem is that the copy happens at both i-time and perf-time. Since at i-time kOutput is all zeros, that gets copy to the global array and |
| Date | 2023-02-17 12:50 |
| From | Oeyvind Brandtsegg |
| Subject | Re: [Csnd] [EXTERNAL] [Csnd] puzzled, array in UDO |
Thanks Guillermo, that is a good hack to mitigate the problem in the current version. Øyvind tor. 16. feb. 2023 kl. 17:14 skrev Guillermo Senna <gsenna@gmail.com>: The use of gotos is discouraged, but -based on what Victor said- if you |