# Restricting note length



## tonewill (Jun 4, 2010)

Although *play_note* allows you to specify how long the note will play for, it carries on playing even if you release the note. Is there a way to have the note play for a maximum length but still stop when you release the note as usual? I haven't been able to figure this out.

Thanks for any help.


----------



## polypx (Jun 4, 2010)

Maybe like this?


```
on note
wait (6000000)
note_off($EVENT_ID) 
end on
```


----------



## EvilDragon (Jun 4, 2010)

Hehe, trying to emulate a Mellotron?


----------



## tonewill (Jun 4, 2010)

polypx @ 4th June 2010 said:


> Maybe like this?
> 
> 
> ```
> ...



It didn't work for me but then I'm using code similar to this (essentially):

```
on note
ignore_event($EVENT_ID)
... (more code)
play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
end on
```

@EvilDragon :lol: spot on!


----------



## EvilDragon (Jun 4, 2010)

Wasn't 8 seconds the limit?


----------



## EvilDragon (Jun 4, 2010)

BTW this should work:


```
on note
	ignore_event($EVENT_ID)
	play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
	wait(8000000)
	note_off($EVENT_ID) 
end on
```


----------



## gregjazz (Jun 4, 2010)

EvilDragon @ Fri Jun 04 said:


> BTW this should work:
> 
> 
> ```
> ...



Wouldn't something like this do essentially the same thing?


```
on note
	ignore_event($EVENT_ID)
	play_note($EVENT_NOTE,$EVENT_VELOCITY,0,8000000)
end on
```


----------



## Big Bob (Jun 4, 2010)

Hi tonewill,

To clarify a bit further, are you perhaps saying: when a key is pressed for a period longer than 8 secs, the note should stop after 8 secs. But, when the key is pressed for less than 8 secs, the note should stop when the key is lifted? If so, something like this may work:


```
on init
declare polyphonic $my_note_id
end on

on note
ignore_event($EVENT_ID)

{ more code ... }
$my_note_id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,8000000)
end on

on release
note_off($my_note_id)
end on
```

I haven't tried this but it should work.

God Bless,

Bob


----------



## kotori (Jun 4, 2010)

gregjazz @ Fri Jun 04 said:


> Wouldn't something like this do essentially the same thing? (...)


No, your code would always play exactly 8 seconds but never less (unless you envisioned a release callback similar to the one that Big Bob used). ED's and Big Bob's code would in some cases give notes shorter than 8 seconds.


----------



## tonewill (Jun 4, 2010)

Thanks for all the help chaps, most appreciated.

@EvilDragon, oops, yes, eight seconds.

@Big Bob, yes, that's exactly what I'm trying to do.

@gregjazz, thank you also.

I've tried all three ideas and found that if the *play_note* function has a duration in it, then the *on_release* has no effect, even when using big bobs polyphonic variable idea. The note plays the full length even when you release the key. Putting *wait()* after the *play_note* function has no effect at all.

Seems more complicated than I first thought!


----------



## kotori (Jun 4, 2010)

tonewill @ Fri Jun 04 said:


> I've tried all three ideas and found that if the *play_note* function has a duration in it, then the *on_release* has no effect, even when using big bobs polyphonic variable idea. The note plays the full length even when you release the key. Putting *wait()* after the *play_note* function has no effect at all.



Yes, you seem to be right. But EvilDragon's script works since he used -1 for the duration. Did you try that?


----------



## EvilDragon (Jun 4, 2010)

I dunno, my script for this, although a bit weird I admit, works in all cases for me. Note is played, then NCB is halted for 8 seconds, and then a note off is sent. No need for release callback in this example.


----------



## Big Bob (Jun 4, 2010)

> I've tried all three ideas and found that if the play_note function has a duration in it, then the on_release has no effect, even when using big bobs polyphonic variable idea. The note plays the full length even when you release the key. Putting wait() after the play_note function has no effect at all.



If that's the case, then I guess that note_off will not kill a note that has been generated with a fixed duration. If that's the cause of the problem then one would have to use play_note with the 4th parameter set to zero (which will cause the note to play until the sample runs out) and use a scripted timer to end the note after 8 secs or on release.

Perhaps something like this?

```
on init 
declare polyphonic $my_note_id 
end on 

on note 
ignore_event($EVENT_ID) 

{ more code ... } 
$my_note_id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0) 
wait(8000000)
note_off($my_note_id)
end on 

on release 
note_off($my_note_id) 
end on
```


----------



## tonewill (Jun 4, 2010)

EvilDragon @ 4th June 2010 said:


> I dunno, my script for this, although a bit weird I admit, works in all cases for me. Note is played, then NCB is halted for 8 seconds, and then a note off is sent. No need for release callback in this example.


I tried again and the note still plays indefinitely when I hold a key. I suppose there's some other difference between our settings. 

@big bob,
I tried this and the sound just carried on when I released the key. I should mention my samples are looped. I'm using Kontakt 3.5 in case that's relevant.

Still, it's not _really_ important that I do this. I wouldn't want anyone wasting any time on it but if someone finds the answer, I'm still interested.

Thanks for the help everyone.


----------



## EvilDragon (Jun 4, 2010)

I'm using K4. Maybe that's the issue here?

But my script really works over here. :/


----------



## Big Bob (Jun 4, 2010)

> @big bob,
> I tried this and the sound just carried on when I released the key. I should mention my samples are looped. I'm using Kontakt 3.5 in case that's relevant.



This is very puzzling! When you say 'I tried this' are you sure you tried the last version I posted? I decided to fire up Kontakt and try it myself and it works properly for K2, K3.5, and K4.1 for me. Here is the actual KScript source code I used for this test.

*on init*
``*declare* polyphonic my_note_id
*end on*

*on note*
``ignore_event(EVENT_ID)
``my_note_id := play_note(EVENT_NOTE,EVENT_VELOCITY,0,0)
``wait(8000000)
``note_off(my_note_id)
*end on*

*on release*
``note_off(my_note_id)
*end on*

For my tests I used the K2 Library Instrument for the 1960's E-Organ which is a looped sample instrument.

If I hold a key down for more than 8 secs, the note stops sounding after 8 secs whereas if I lift the key before the 8 secs expire, the note stops immediately after lifting the key. This is the behaviour I thought you wanted.

Something funny must be going on here don't you think :lol:

BTW If you aren't using Nils' Editor, you will need to add the required '$' characters before Kontakt's Script Editor will accept the code.


----------



## EvilDragon (Jun 4, 2010)

I'm beginning to wonder what's contained in OP's 


... (more code)


----------



## Big Bob (Jun 4, 2010)

EvilDragon @ Fri Jun 04 said:


> I'm beginning to wonder what's contained in OP's
> 
> 
> ... (more code)



Hey ED, huh! :? Please explain for my sanity. Oh, I finally got it :lol: 

And, *tonewill*, I think Dan's code (polypx) should also work Ok and has the advantage of not requiring any code in the RCB. However, I generally try to avoid using the minus one thing because there have been situations in the past where it didn't always work properly.

Maybe we also need to ask (besides what's contained in ... (more code)), are you using additional scripts with your instrument?


----------



## Big Bob (Jun 4, 2010)

> PS> Hi Bob, I've been back for a few months and it's great to see you here.



Hi Tod,

Long time no see! How have you been old friend? Boy, this is getting to be like 'old home week'. Come on Benjamin and Nickie, time for you both to add a post to this thread where old friends meet :lol:


----------



## Moonchilde (Jun 4, 2010)

tonewill, you can do this without scripting via AHDSR. Experiment with that, I don't remember all the details but this is definitely possible via AHDSR. The H is for hold, how long the note plays before it starts to decay. D is for decay, how it it will decay and fade out. S is for sustain, if you turn it up it means you press the note and it plays for the duration of the HD, if set to zero, plays for as long as you hold the note until you let go. R is for release, how long it will continue to play after you release the key. 

This should take care of your problems.


----------



## Big Bob (Jun 4, 2010)

> tonewill, you can do this without scripting via AHDSR.



WITHOUT SCRIPTING!!!!! We don't allow such progressive thinking around here :lol: 

OK I'm done having fun. I'm going back to work.


----------



## EvilDragon (Jun 4, 2010)

Moonchilde @ 4.6.2010 said:


> tonewill, you can do this without scripting via AHDSR. Experiment with that, I don't remember all the details but this is definitely possible via AHDSR. The H is for hold, how long the note plays before it starts to decay. D is for decay, how it it will decay and fade out. S is for sustain, if you turn it up it means you press the note and it plays for the duration of the HD, if set to zero, plays for as long as you hold the note until you let go. R is for release, how long it will continue to play after you release the key.
> 
> This should take care of your problems.



*facepalm*

This is definitely possible and will work:

Attack 0 ms
Hold 8 s
Decay 0 ms
Sustain -infinity
Release 0 ms

Finetune if needed.


----------



## Moonchilde (Jun 4, 2010)

EvilDragon @ June 4th 2010 said:


> *facepalm*



LOL, sometimes I think Kontakt's Scripting feature makes people forget what's already under the hood.


----------



## tonewill (Jun 4, 2010)

Thanks a lot everyone, you've all been very helpful.
I obviously did something wrong the first time because I just retried EvilDragon's suggestion and it worked this time. Now, the "more code..." bit has code like this:


```
if($EVENT_NOTE < 78)
        play_note($EVENT_NOTE - 12,$EVENT_VELOCITY,0,-1)
      else
        play_note($EVENT_NOTE - 24,$EVENT_VELOCITY,0,-1)
      end if
```
So getting those notes to stop as well would mean more variables. Then I read Moonchilde's suggestion about using the ADHSR. Why is it that sometime we overlook the simple things? It does the job perfectly! Actually found a use for HOLD in an envelope generator .

Thanks again everyone, along with the subtle random pitch LFO and a touch of Lo-FI I've managed to make my 21st century software sampler sound like a 40 year old tape playback machine :wink:.


----------



## olmerk (Jul 8, 2016)

I'll bring this post up. I encountered a similar issue.

The simple line like $id := play_note($current_note, $current_velocity,0,100) triggers the entire sample not just its part for 100 microseconds. What can be wrong? Maybe smth in the patch.


----------

