0

According to reference csound manual, one should use xtratim (or opcodes that implicitly use it e.g. madsr) when writing instruments that respond to MIDI since the duration of the key/note is not know in advance (for live performances).

However, I also noted in an external example that one can override p3 (the score note length) from within the instrument code. So, is there any difference between doing this and using xtratim?

Fizz
  • 4,782
  • 1
  • 24
  • 51

2 Answers2

1

Yes, xtratim and -r opcodes that have a release segment will trigger that extra time for release when the note turns off. p3 will only be the duration value for the "on" time.

For example, if you run the following:

instr S1
  p3 = 2
  asig = oscili(0.25, 440)
  asig *= linsegr(1, .1, 1, 4, 0)
  out(asig, asig)
endin
schedule("S1", 0, 0.1)
  1. The schedule call sets p3 to 0.1 seconds
  2. The instrument redefines p3 to 2 seconds
  3. The instrument runs for 2 seconds and has a 4 second release time afterwards due to the linsegr release value

In general, overriding p3 should be used with caution but is good for certain situations like ensuring a minimum amount of time is given to an instrument. Release time from xtratim or -r opcodes is probably more applicable to most use cases.

Steven Yi
  • 121
  • 2
0

Since xtratim gets an i-arg for the extra time, I was hoping that changing p3 would be more flexible, but that's not actually the case...

For starters, p3 is treated as an i-var, i.e. it's only assignable on the initial pass, so something like

instr 1
if p3 < 9 then
  p3 = p3 + 1 ; runs only once
  printk 0.1, p3
endif
endin

will only run for three seconds if called from the score as

i1 0 2

This much was to be expected. On the other hand the following hack that calls reinit to [re]run the initialization phase does update p3 more than once, but this has zero effect on the note play time, i.e. only the first ("real init") changes the actual note duration... the perf-time reinits, while changing p3 as read from the instrument, don't have any effect on the note play duration, i.e.

top:
p3  = p3 + 1
printk 0.1, p3
if p3 < 9 then
  reinit top
endif

still plays for exactly 3 seconds with the same score (even if I make sure Csound runs long enough), i.e. with:

i1 0 2
e 10

although the printk does display p3 as reaching the value 9 with the latter instrument...

As for xtratim it doesn't change p3 at all, the "extra time" is implemented outside this as Steven' Yi's answer correctly points out.

Looking at the source code for xtratim, it is actually possible to extend the "extra time", by calling xtratim several times, with ever increasing values, but not shorten it...

int32_t xtratim(CSOUND *csound, XTRADUR *p)
{
    IGN(csound);
    int32_t *xtra = &(p->h.insdshead->xtratim);
    int32_t tim = (int32_t)(*p->extradur * p->h.insdshead->ekr);
    if (*xtra < tim)  /* gab-a5 revised */
      *xtra = tim;
    return OK;
}

So armed with this info, I tried

ixtr = 0
top:
ixtr += 1
xtratim ixtr
print ixtr
if ixtr < 8 then
  reinit top
endif

And lo and behold, the latter hack does work, i.e. this latter code actually extends the instrument/note play time to 10 seconds using the previous score. So it's possible to extend the note programmatically this way from inside the instrument at perf time, for as much as you want, unlike hacking p3.

For those curious, xtratim even takes effect (properly) on play (actually release) time even if called after release has been asserted, i.e. even from the "release segment", e.g.

xtratim 1
kflag release
kdone init 0
if (kflag == 1) && (kdone == 0)  then
  kdone = 1
  reinit more
  more:
  xtratim 4
endif

will give 4 extra seconds of play. Given this, I'm not sure if p3 updates being ignored during perf time is a bug or "by design".

Fizz
  • 4,782
  • 1
  • 24
  • 51
  • Generally if you want an extended duration, you'd probably want to use negative p3. This puts an instrument instance into held state until you do something to turn off the instrument. Is this more of what you're trying to do, hold a note until you signal it to turn off? – Steven Yi Feb 16 '20 at 21:30