Csound Csound-dev Csound-tekno Search About

Re: [Csnd] [EXTERNAL] Re: [Csnd] New UDO syntax question

Date2026-04-26 08:50
FromJean Basile Sosa
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] New UDO syntax question
Hi Victor,

I think I now understand the main difference between the two style approaches, and more generally advantages and limits of the pass by reference concept. Thank you.

The following code works well, but I'm never completely sure of the relevance and the good way in Csound...   
After a quick look nothing lets consider future bugs?
 


-odac 



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

gisquare ftgen 2, 0, 16384, 10, 1, 0 , .33, 0, .2 , 0, .14, 0 , .11, 0, .09

opcode ctrl(pfield:i):(k)
chnidx:S = sprintf("%.02f_p%d", p1, pfield)
fieldvalue:i = pindex(pfield)
print(fieldvalue)
chnset(fieldvalue,chnidx)
value:k init fieldvalue
value:k = chnget(chnidx)
puts(chnidx, 1)
xout(value)
endop

// ctrl() arguments > 3, last argument give number of pfield
// score instructions, schedule etc. must take into account it
 
instr 1
k4 = ctrl(4)
k5 = ctrl(5)
endin 
schedule(1,0,0,0,0)

instr 2
asig = oscil(ctrl(5), ctrl(4), 2)
outs(asig, asig) 
endin



i 2.01 0 1 60 0.1 
e

 

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

Date2026-04-26 10:33
FromVictor Lazzarini <000010b17ddd988e-dmarc-request@LISTSERV.HEANET.IE>
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] New UDO syntax question
Pass by reference should be preferred in general as it is more efficient. So if this code is working now for you, it's better than old-style.
We just need to treat input and output with more care

1- use input arguments as read-only unless you are explicitly looking to do in-place computation or some other particular side-effect.

2- only pass to output local variables created inside the UDO. There should not be a reason to do any different.

So, for rule 1, avoid

opcode op(var:i):i
var += 1
...


unless you want the side effect of incrementing the input.

For rule 2, this

opcode op(var:i):i
...
xout(var)
endop

should be avoided as it may lead to confusion.
In your earlier example, the global var was also causing an issue I think. Instead, this is the correct way

opcode op(var:i):i
lvar:i = var
...
xout(lvar)
endop

With a little bit of care, it should all work well.

Prof. Victor Lazzarini
Maynooth University
Ireland

> On 26 Apr 2026, at 08:50, Jean Basile Sosa  wrote:
>
> Hi Victor,
>
> I think I now understand the main difference between the two style approaches, and more generally advantages and limits of the pass by reference concept. Thank you.
>
> The following code works well, but I'm never completely sure of the relevance and the good way in Csound...
> After a quick look nothing lets consider future bugs?
>
> 
> 
> -odac
> 
> 
>
> sr = 44100
> ksmps = 128
> nchnls = 2
> 0dbfs  = 1
>
> gisquare ftgen 2, 0, 16384, 10, 1, 0 , .33, 0, .2 , 0, .14, 0 , .11, 0, .09
>
> opcode ctrl(pfield:i):(k)
> chnidx:S = sprintf("%.02f_p%d", p1, pfield)
> fieldvalue:i = pindex(pfield)
> print(fieldvalue)
> chnset(fieldvalue,chnidx)
> value:k init fieldvalue
> value:k = chnget(chnidx)
> puts(chnidx, 1)
> xout(value)
> endop
>
> // ctrl() arguments > 3, last argument give number of pfield
> // score instructions, schedule etc. must take into account it
>
> instr 1
> k4 = ctrl(4)
> k5 = ctrl(5)
> endin
> schedule(1,0,0,0,0)
>
> instr 2
> asig = oscil(ctrl(5), ctrl(4), 2)
> outs(asig, asig)
> endin
>
> 
> 
> i 2.01 0 1 60 0.1
> e
> 
> 
>
> 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
The information contained in this email may be confidential and privileged. It is intended only for the addressee(s) stated above. If you are not an addressee any use, dissemination, distribution, publication or copying of the information contained in this email is strictly prohibited. If you have received this email in error, please immediately notify us by email at dataprotection@mu.ie and delete this email from your system.

Please note that Maynooth University is subject to Freedom of Information and Data Protection laws. We may be required to disclose the content of emails under the FOI Act 2014, the Data Protection Act 2018 or GDPR.

Is don seolaí / do na seolaithe thuasluaite amháin an ríomhphost seo. D’fhéadfadh an t-eolas atá ann a bheith rúnda agus faoi phribhléid. Mura seolaí tú, tá cosc iomlán ar aon eolas atá sa ríomhphost seo a úsáid, a scaipeadh, a dháileadh, a fhoilsiú nó a chóipeáil. Má fuair tú an ríomhphost seo trí thimpiste, cuir sin in iúl dúinn láithreach trí ríomhphost a chur chuig  dataprotection@mu.ie  agus scrios an ríomhphost seo ó do chóras.

Tabhair faoi deara go bhfuil Ollscoil Mhá Nuad faoi réir dhlíthe um Shaoráil Faisnéise agus um Chosaint Sonraí. D’fhéadfadh ceangal a bheith orainn ábhar ríomhphoist a nochtadh faoin Acht um Shaoráil Faisnéise 2014, faoin Acht um Chosaint Sonraí 2018 nó faoi GDPR.

Registered charity number 20037130



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