[Csnd] Easiest time-controlled switching

Hey hey,
I was wondering if my code could be more elegant.

I have a switch (kSwitch), which can have two states. Now I need to change it over time. At the moment I'm doing this:
kSwitch = linseg(0, iDur1, 0, 0, 1, iDur2, 1, 0, 0, iDur3 0)
Is there a better, nicer, easier way?

Best wishes and TIA,


You should also used a small table with 0 and 1 in it?

Mar 20 2021, Rory Walsh has written:

You should also used a small table with 0 and 1 in it?

Could you perhaps elaborate a little, please?

Best wishes,


You could also use an array and just switch between the two states? Or am I missing something?

you mean the durations are not the same, so it is an aperiodic switch?

i have a udo which is:

opcode Switch, k, k[]
  kDurs[] xin
  kndx init 0
  kOut init 0
  if metro(1/kDurs[kndx])==1 then
   kOut = (kOut==0) ? 1 : 0
   kndx = (kndx+1) % lenarray(kDurs)
  xout kOut

you can feed it with an array of the durations, e.g.

instr 1
  kOut Switch fillarray(1, .5, 1, .5, 2)
  printk2 kOut

it will read the array in a loop, but it could also stop at last value, or whatever behaviour you wish for your case.

best -

Many thanks, Joachim! This is neat! That's why I love this list! <3

Mar 20 2021, joachim heintz has written: