Cabbage Logo
Back to Cabbage Site

Opcode with optional parameters?

I’m working on an opcode that potentially will have dozens of parameters. Typically, those will mostly take default values. Is there a way to simplify the call to the opcode so that it assumes the default values unless I explicitly assign them? Is there a standard way that folks handle this?

From a programming perspective, I don’t want each call to the opcode to require dozens of values/variables to be in the instrument if most are defaults. That seems like it will be error prone and hard to look at.

Thank you!

EDIT - I’m almost seeing it with the j, O, P, V, J, o, and p inputs, but I’m not sure about the syntax for calling. An example would be really helpful!

As far as I know UDOs can’t have an optional list of inputs, even though native opcodes can. You can overload the UDO, i.e,

opcode MyOpcode, aa, kk
(...)
endop

opcode MyOpcode, aa, kkk
(...)
endop

opcode MyOpcode, aa, kkkk
(...)
endop

I know it’s a little cumbersome, but in the end should help you achieve your goal.

There are special characters you can use (from https://flossmanual.csound.com/csound-language/user-defined-opcodes)

Default Arguments

For i-time arguments, you can use a simple feature to set default values:

  • o (instead of i ) defaults to 0
  • p (instead of i ) defaults to 1
  • j (instead of i ) defaults to -1

For k-time arguments, you can use since Csound 5.18 these default values:

  • O (instead of k ) defaults to 0
  • P (instead of k ) defaults to 1
  • V (instead of k ) defaults to 0.5
  • J (instead of k ) defaults to -1

So you can omit these arguments - in this case the default values will be used. If you give an input argument instead, the default value will be overwritten

This is true, but my my point was you have to have a fixed number if inputs at the UDO level, including any optional inputs. This may have changed in Csound7. Hopefully it has :+1:

1 Like