# Note duration script



## Freesamples (Apr 25, 2009)

Please help me write script, which will increase the duration of playing note until the next note will start playing (and make note off before the next note will start).

That is, I have the first note with 0.5 sec. duration and second note wich start in 2 sec. after the start point of first note . So I need to set duration of the first note to 1.999 second and make note off before the next note will start playing.

Thanks,
F.


----------



## gregjazz (Apr 25, 2009)

Just to clarify--you can make a script that puts a note off on the next note callback, but you can not anticipate a note on in MIDI.


----------



## Freesamples (Apr 26, 2009)

Thanks Greg. Anyway I don't need anticipate a note on in MIDI. I kust need to make the current note OFF before before start a next note...

Thanks,
F.


----------



## polypx (Apr 26, 2009)

```
on init
declare $newnote
declare $lastnote
end on


on note
$newnote := $EVENT_ID
note_off ($lastnote)
$lastnote := $EVENT_ID

end on

on release
ignore_event($newnote)
end on
```


----------



## Big Bob (Apr 26, 2009)

Hey Mr F, it's not clear if you want the first note stretched to just before the next note. If so, the posted script doesn't do that. ie if you hit a key a then release it, do you want the note to continue until 'just before' you hit the next note?



> Thanks Greg. Anyway I don't need anticipate a note on in MIDI. I kust need to make the current note OFF before *before start a next note*...



This is a contradictory statement :? As Greg pointed out, scripts cannot predict the future. While the above statement says you don't need that, it *also says *that the current note-off must occur *before the start *of the next note. That is of course impossible if your statement is taken literally.

Then there is the issue of chords. Are you only going to play one key at a time or is this script also supposed to work if you play a chord? All chords in Kontakt are essentiallly arpegiated and appear as separate notes in time. Therefore, one has to define the minimum time for which another note is considered part of the current chord or considered as a 'new note'.

If you can provide more specifics of what you require, someone will be able to help you much better.

God Bless,

Bob


----------



## Hans Adamson (Apr 26, 2009)

Sounds like you want a script that makes an instrument monophonic?


----------



## Freesamples (Apr 26, 2009)

No, I just want to make something like automatic sustain pedal effect. But there is every new note should cut off the previous note...

P.S. Thank you *polypx*


----------



## polypx (Apr 27, 2009)

> on note
> ignore_event($EVENT_ID)
> note_off($id)
> $id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,0)
> end on



No matter how concise I think my code's getting, Bob always wins. Brilliant.

cheers
Dan


----------



## Big Bob (Apr 27, 2009)

Hi Dan,

Actually, I'm a bit embarrased :oops: . There's nothing wrong with the way you coded it, I just missed seeing your release callback and so I thought your script wasn't providing the requested sustain. There may actually be advantages to the way you coded it, depending on how the last note is to be 'killed'.

Anyway, I'm sorry if it appeared that I was trying to 'one-up' you because I wasn't.

God Bless,

Bob


----------



## polypx (Apr 27, 2009)

Oh no, I didn't think that at all.

I'm just impressed. Not being a real coder, when I see an algorithm expressed very succinctly, I get a strange thrill. Maybe coders do too. 

cheers
Dan


----------



## Thonex (Apr 27, 2009)

polypx @ Mon Apr 27 said:


> Oh no, I didn't think that at all.
> 
> I'm just impressed. Not being a real coder, when I see an algorithm expressed very succinctly, I get a strange thrill. Maybe coders do too.
> 
> ...



yeah... Bob's been impressing me with his succinctness for years now. Nils too. Nickie too.

My favorite so far (credit goes to Nickie) is the reversing of polarity of a Bypass engine parameter with a ui button.

set_bypass_engine_param = 1 - bypass_button


----------



## Big Bob (Apr 27, 2009)

Yes indeed Andrew,

I've complimented Nickie on that one several times before because it is elegantly simple. Previous to seeing Nickie use that construct, I was using something clumsy like:

```
set_bypass_engine_param = bypass_button + 1 .and. 1
```


----------



## polypx (Apr 27, 2009)

It's a kind of lateral thinking, isn't it? I think we all love those.

Anyway, good to hear the Great Bob can be impressed by a simple "turn of the code" as well...

There was another great shortcut I remember that was basically an on/off switch using "mod 1" which I liked as well. I'd dig it up, but it's too late here.

Oh, if I had another life I'd be coding C and wouldn't have a clue what C lydian meant.

Great fun,

Dan


----------



## Big Bob (Apr 28, 2009)

> There was another great shortcut I remember that was basically an on/off switch using "mod 1" which I liked as well. I'd dig it up, but it's too late here.



Hi Dan,

For toggling a boolean, perhaps you are thinking of mod 2? For example:

```
X := (X + 1) mod 2
```
Of course, that's still not as concise as Nickie's 1 - X.

Or for maintaining a circular queue pointer following a sequence such as: 0, 1, 2, 3, 0, 1, 2, 3, 0, ... the following is often used:


```
p := (p + 1) mod 4
```
 or, when the sequence length is an even power of two:

```
p := (p + 1) .and. 0x3
```
This latter form was often used in the early days when microprocessors didn't have hardware multiply/divide and a bit-wise 'and' would execute a great deal faster. Now days, 'mod' is often just as fast and it has the advantage of working for non-integer powers of 2 (and, may be a bit easier to read). Along these lines, you might be interested in reading this thread.

http://vi-control.net/forum/viewtopic.php?p=146620

God Bless,

Bob


----------

