# How to delay a note off ?



## mbncp (Jul 2, 2007)

This one drives me nut. I try to delay a note off in on resume, but ..

```
on release
  ignore_event($EVENT_ID)
  wait(10000)
  note_off($EVENT_ID)
  end on
```
Seems to delete $EVENT_ID and then ignores note_off (hanging note)

and:

```
on release
  wait(10000)
  note_off($EVENT_ID)
  end on
```
Does nothing


Hmm wait ..

```
on init
  declare $delay := 10 {msec}
  declare polyphonic $newevid
  $delay := $delay*1000
end on
on note
  ignore_event($EVENT_ID)
  $newevid := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
  while($NOTE_HELD = 1)
    wait(100)
  end while
  wait($delay)
  note_off($newevid)
end on

on release
  if($EVENT_ID # $newevid)
    ignore_event($EVENT_ID)
  end if
end on
```
Seems to work, but I'm probably missing something obvious :?


----------



## kotori (Jul 2, 2007)

Hi mbncp,
In some cases child notes which are generated with a -1 duration will stop the first time when 'on release' is triggered. Even if the release is delayed. For that reason some of us has moved away from using it towards using a duration of 0 and then stopping the child note explicitly.

Btw. is there any reason why you ignore the incoming note and regenerate a child note? Why not use the incoming note?

I'm not sitting at my DAW now so I can't test this but maybe you could try:

```
on init
  declare polyphonic $child_id
end on

on note
  ignore_event($EVENT_ID)
  $child_id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0)
end on

on release
  ignore_event($EVENT_ID)
  wait(10000)
  note_off($EVENT_ID)
  note_off($child_id)
end on
```

Cheers,
Nils


----------



## mbncp (Jul 3, 2007)

Thanks Nils, now that looks nicer.

In the release, if we call ignore_event($EVENT_ID) do we still need to call note_off($EVENT_ID) ?




> Btw. is there any reason why you ignore the incoming note and regenerate a child note? Why not use the incoming note?



That was my initial idea, but then I was unable to delay the note off, either the note would hang or would terminate immediatly (see my first 2 examples).

But I'm probably missing something, again 

Cheers,
Marc


----------



## Big Bob (Jul 3, 2007)

Hi mbncp,

Welcome to the wacky world of RCB triggering :lol: . This area is perhaps the most arcane (and difficult to come up with a set of rules for) in the entire KSP implementation.

I have tried 6 times to distill this behavior to a set of logical rules that we could follow to get predictable behavior. However, the rules are not only non-uniform but, they keep changing slightly from one KSP version to another :cry: .

If you haven't already, you might want to glance at the attached .pdf. The data was obtained with K2.1, and shortly after completing this document, I discovered a few twists and turns I hadn't noticed before. Then, before I could run those down, K2.2 was released and a whole bunch of things changed again. After that, I just sort of threw my hands up! :( 

One thing I can tell you is that once a note produces an RCB (due to key lift), another RCB cannot be generated with a note_off call. Secondly, under most situations, a note_off call in the RCB itself cannot affect the event that caused the RCB in the first place. Finally, if you generate the note internally (ie make a child note), the rules for RCB triggering are quite different than if the note originates from the 'outside' MIDI world.

If NI finally settles this area down a little, I may get the energy to once more try to fully characterize its behavior. In the meantime, we're mostly stuck with testing specific combinations as the need arises. Wouldn't it be nice if NI would actually publish the details of how we can expect these things to work and not work :roll: Of course the way they keep changing the rules, I guess it's not too surprising that they don't.

I know this post is probably not too helpful, but, at least you have my sympathy.

God Bless,

Bob


----------



## Moonchilde (Jul 3, 2007)

Is this just something you could do with AHDSR?


----------



## mbncp (Jul 3, 2007)

> [..]at least you have my sympathy


 Could I have an aspirin instead :lol: 

Very usefull info Bob, I just hope NI has a copy. :wink: 
If I understand this correctly it would finally be better if slot0 receives an nrpn or a cc instead of a note event and slot1 would generate the events, or should I expect some more surprises ?




> Is this just something you could do with AHDSR?


The idea is to force a legato under certain situations, so the next note on is received before the note off. AHDSR won't help in this case.


Cheers,
marc


----------



## Moonchilde (Jul 3, 2007)

Sure it can. Load up AHDSR and change it to just AHDS. Add a bit of hold and a little decay. This will force the samples to play for however long the Hold setting is. The next note starts as soon as you press the next key while the previous is held automatically for as long as the Hold variation is set to. Its pretty simple and effective.


----------



## Big Bob (Jul 3, 2007)

Hi Marc,



> If I understand this correctly it would finally be better if slot0 receives an nrpn or a cc instead of a note event and slot1 would generate the events, or should I expect some more surprises ?



Not necessarily, you can generate notes in Slot 1 just as well as slot 2 for some situations (or did you actually mean slot 0 and slot 1? I assume you mean 1 & 2 since slot 0 is my name the 'pseudo-slot' ahead of the first script slot). It always comes down to the specifics of a given situation and which version of K2 you want it to run with. I've gotten so that I try to code for the least common denominator so the script will run on K2.1 or K2.2 but that 'ain't easy' and isn't always possible. I'm afraid I would need to study the complete specifics of what you are trying to do before I could make any useful suggestions. 

The whole business with the slot N + 2 discovery resulted from an Observation that Nickie made one day when she discovered that scripts would behave differently in slot 1 than they would in the remaining slots. After more careful analysis, I determined that the nature of this problem had to do with being one or two slots beyond where a child note was created. Once you got beyond 2 or more, things got more sensible but one slot later things were crazy. Thus, I put together the .pdf in an attempt to codify all these strange and inconsistent observations so we might be able to design scripts more intelligently. But, unfortunately, the N + 2 thing again changed character with the release of K2.2 (it's still there but it behaves quite differently).

BTW I'm sorry Marc but I'm all out of aspirin (been using a lot myself lately :lol: ).

God Bless,

Bob


----------



## mbncp (Jul 3, 2007)

Moonchilde @ Tue Jul 03 said:


> Sure it can. Load up AHDSR and change it to just AHDS. Add a bit of hold and a little decay. This will force the samples to play for however long the Hold setting is. The next note starts as soon as you press the next key while the previous is held automatically for as long as the Hold variation is set to. Its pretty simple and effective.



I'm a bit confused here.
What I mean is to delay the note off by a certain amount of time, not setting a given note duration.
Example if I get a note that plays for 2 seconds, I want it to play 2.1 seconds and if one plays for 10 sec I want it to play for 10.1 sec.
Now I don't see how this could be done with an envelope. 

Anyway, my initial idea was to change the Solo Mode script to force it to play legato as long as one or more notes where held, as I wanted to be able to use retriggers with SIPS, as it's pretty natural for me on the keyboard.
And then I though that while I was there, let's take 5 minutes to make a simple script that would just delay the end of the notes by a given amount.
It finally took a little more time :mrgreen:


----------



## mbncp (Jul 3, 2007)

Hi Bob,



> or did you actually mean slot 0 and slot 1?



Yes that's what I mean. The idea is to convert every MIDI event to a NRPN before reaching K2, so slot0 (K2) receives only NRPN, nothing else. Then the script in slot1 would decode these NRPN and create the necessary events (note on, off , cc,..)

Now I will see, as it may be a little tricky to use, but this way ksp could receive any event type, even sysex and channel info as well, as I would encode port and channel information. Channel info could be used to trigger different groups or to deal with a 6 channel MIDI guitar and this in a single instrument using a specific port.

I was thinking that one of the problem with ksp was when mixing real notes and child notes created by the script, but it looks even more complicated than that. :( 

Maybe 2.2.3 will take care of these issues. 

Cheers,
Marc


----------



## Moonchilde (Jul 3, 2007)

Marc, sorry, I had my envelopes mixed up. You can do this in Flexible envelope. I'm attaching a patch and preset for you, its really easy to use and set up.

Take a look at the picture. The brown brackets on the envelope are the "loop" area. Basically, you hold down the note and it plays and plays and plays, unless the original sample does not sustain infinitely. The curve afterwards is the length of the note off. You can drag the bottom square left or right to shorten or lengthen the delay of the note off. Its really simple and easy to use. 

I made a preset you could add to your Kontakt 2->presets->modulators->flex envelope folder. Then you could just add the flex envelope to any instrument and load it up via the preset button.


----------



## mbncp (Jul 4, 2007)

Thanks Moonchilde, I learned something :D 

The only problem with K2 is that you can't change a value once a note started.
If you set CC1 to change Time3 in your example it will only be effective on the next note. 
I can admit that you can't change the sustain values once an event started, but on note off K2 should read the current value, or at least make it an option.

Nevertheless these flexible enveloppes can be very handy, it was about time I gave them a closer look. 8) 

Cheers,
Marc


----------

