# Defaut Midi Latch script, possible "improvement"?



## drjee (Nov 12, 2006)

what is the difference to just use the one-shot option? why would you need a script for this? (but maybe, I misunderstood what you want)

cheers, drjee


----------



## gmet (Nov 12, 2006)

There is this script that came from one of the forums. (Apologies to the author but I couldn't find the thread):

Justin



> _{ Simple note toggle script
> Press a key once to play the note.
> The second time you press it the note is release
> }_
> ...


----------



## pmd (Nov 12, 2006)

Thanks drjee & Justin M,

for response  but I still haven't found what I'm looking for :wink: 

Midi latch script do what I need but there's only one thing "missing":

1/ When I press a key, sample play one shot -ok- 
2/ if I repress same key during this one shot playing, sample stops -ok- 
3/ if I repress this key again, sample replay for one shot -no problem until there-

But,
4/ if I let this one shot sample playing until its end, and if I need to play it again later on same song, I need to press key:
- a first time, I suppose to kill "note off" 
- and a second time to be able to play the sample again.

is a way to have to press key only once ???

thanks
+pmd


----------



## pmd (Nov 12, 2006)

I try your script Justin,

but it does the same as described just below

+pmd


----------



## kotori (Nov 13, 2006)

Hi pmd,
Unfortunately I don't think it's possible to write such a script due to limitations in KSP (it would be interesting if someone could find a workaround though). Basically scripts are unable to determine the length of a sample and whether the sample is still being played back (they can only determine if the note is held which is not the same thing). It would be nice if NI extended KSP slightly in this area.

Cheers,
Nils

_Edit and clarification: the above statement only pertains to DFD mode. Beat machine is different._


----------



## Thonex (Nov 13, 2006)

What about one of these commands in the Beat machine mode.... where you set the whole loop to just 1 slice and use one of these?



> _slice_length(<group-index>,<slice-index>)
> return the length of the specified slice in the specified group with respect to the current tempo
> 
> 
> ...



Just a thought.


T


----------



## pmd (Nov 13, 2006)

Hi Nils, 

thanks for your quick response. What a pity, these fonctions are available by default in Emu hardware sampler for each key... But of course I know it's not at all same concept and logic here with scripts.

Ok, I understand limitation for this concrete example, but sure a lot of people would be interessed on a improvement; as you suggest, it would nice that this post do not disappear too quickly and that NI explore this way! 

Bravo! for your fantastic sript job Nils
+pmd


----------



## pmd (Nov 13, 2006)

Thanks Thonex,

I will see this tonight ...
Do you try it on your machine ?

+pmd

ps: I'm new there, so I don't really control script conception, so do I need to add these lines on Midi latch script or create a new one ?
Sorry for this beginner question!


----------



## Thonex (Nov 13, 2006)

pmd @ Mon Nov 13 said:


> Thanks Thonex,
> 
> I will see this tonight ...
> Do you try it on your machine ?
> ...



I'm just trying to think outside the box... I would try to write a tiny test script to see if KSP returns the length of the slice upon command. Even if it is a sample length value... you can then convert that to uSec or ms with a simple calculation.

I didn't really try it on my machine as I have too many looming deadlines, but I thought I'd chime in and maybe someone else can help out. I'll give it a try in a few weeks when I have some down time.

Cheers,

T


----------



## pmd (Nov 13, 2006)

very nice from you

cheers

pmd


----------



## Thonex (Nov 13, 2006)

pmd @ Mon Nov 13 said:


> very nice from you
> 
> cheers
> 
> pmd



Ok... I just did a quick test... and it works:


```
on note
 
 
 message (_slice_length(0,1)  )
   
end on
```

this will show you the length of a slice (the first slice in this case) in micro seconds at the bottom left of the K2 window. If you make your samples just 1 slice, this should work.

Hope this helps.

Cheers,

T


----------



## pmd (Nov 13, 2006)

Thonex @ 13th November 2006 said:


> pmd @ Mon Nov 13 said:
> 
> 
> > ```
> ...



OK !

I will try tonight at home ...
always beginner question:
I suppose I still need to open "Midi latch script" and also this one beside...

thanks !


----------



## Thonex (Nov 13, 2006)

Ok... it took me 2 minutes... here is the script the way you want it. You just have to make sure the instrument is in Beat mode and that you set the 1st slice to the length of your sample.

Cheers,

T


```
{ Simple note toggle script
Press a key once to play the note.
The second time you press it the note is release
}

on init
   { list of all 128 possible notes. When a note is played it
   contains the ID of the note event (to make it possible to
   run note-off on the note when we need to) and when a note
   is not being played it's ID is set to zero to indicate that }
   declare %note_ids[128]
   
   declare $i
   
end on

on note
   { ignore the incoming note }
   ignore_event($EVENT_ID)             
   
   { if the current note is not being played (its ID is zero) }
   if (%note_ids[$EVENT_NOTE]=0)
      { play a new note with undetermined length and remember its note ID }
      %note_ids[$EVENT_NOTE] := play_note($EVENT_NOTE, $EVENT_VELOCITY, 0, _slice_length(0,1))  
      wait (_slice_length(0,1))
      %note_ids[$EVENT_NOTE] := 0    
   else    
      { Fade out note that is plaing }
      fade_out(%note_ids[$EVENT_NOTE],5,1)
      { set id to zero to indicate that the note is not being played }
      %note_ids[$EVENT_NOTE] := 0         
   end if
end on
```


----------



## pmd (Nov 14, 2006)

Nils,

I can't make it work on my machine ?
I'm on "beat machine" mode I've created slices (one at the beginning, one at the end of the sample), I've put your script on ...

Is there something I missing ?

However, I'm quite embarrassed, because I need to used these samples on DFD mode to not overload my computer memory, because Beat machine mode load it on RAM. With all your explanations, it's sure it can be done only under this mode (beat machine) for the length of the sample to be recognized.

Thanks again for your shares and knowledges

pmd


----------



## kotori (Nov 14, 2006)

Hi pmd,
Didn't you mean to adress Andrew (Thonex) instead of me since he's the one who's done most to try to help you out here? :???: 

Anyway, have you made sure that the first slice is the length of your sample?
The only way to make it work in DFD mode that I know of would be to tabulate the sample lengths (possibly using a tool that automatically scans your samples), but this would have to be done for every instrument and entered into the script, so the solution wouldn't be very general.

Cheers,
Nils


----------



## pmd (Nov 14, 2006)

Sorry Nils,

yes, indeed, it was addressed to Thonex :oops: 
but thank you to response !
I will look again at home tonight about this first slice for Beat machine mode... but sure I will prefer to use in it in DFD mode...

pmd


----------



## Thonex (Nov 14, 2006)

pmd @ Tue Nov 14 said:


> I will prefer to use in it in DFD mode...
> 
> pmd



As Nils said, there's no way to do it in DFD mode... and Nils was absolutely right about having to create a table of sample lengths for each sample in order to do it in DFD mode.

Make sure you have a blue slice indicator at the beginning and at the end of you sample and get rid of all other blue slice indicators. Make sure the sample start is set appropriately.

Cheers,

T


----------



## pmd (Nov 14, 2006)

Sorry Thonex,

your script works fine !
Except that on my computer if I want to stop a sample playing after about 5 seconds (on pressing key a second time), it's launching another playing of the same sample, it doesn't stop it ? what the mistake with me ?

However It works under about 5 seconds !

Hope it's comprehensible enough 

thanks for your time 
pmd


----------



## Thonex (Nov 14, 2006)

pmd @ Tue Nov 14 said:


> Sorry Thonex,
> 
> your script works fine !
> Except that on my computer if I want to stop a sample playing after about 5 seconds (on pressing key a second time), it's launching another playing of the same sample, it doesn't stop it ? what the mistake with me ?
> ...



Did you make sure that there are only 2 blue slice markers in the loop editor? You probably have the second slice marker around the 5 second mark.

Hope this helps.

Cheers,

T


----------



## pmd (Nov 14, 2006)

Thonex @ 14th November 2006 said:


> Did you make sure that there are only 2 blue slice markers in the loop editor? You probably have the second slice marker around the 5 second mark.



Yes, i'm sure ...

pmd


----------



## Thonex (Nov 14, 2006)

Make sure the "Tracking" button is not orange. 

You are probably not playing the sample in it's original root and therefor the timing is not working.


Hope this helps.

T


----------



## pmd (Nov 14, 2006)

eg:

sample is 25 seconds long, I put a slice at the beginning and at the end.

ps: In "loop section" I let "loop mode" off.


pmd


----------



## pmd (Nov 14, 2006)

Thonex @ 14th November 2006 said:


> Make sure the "Tracking" button is not orange.
> 
> You are probably not playing the sample in it's original root and therefor the timing is not working.
> 
> ...



thanks Thonex,

Tracking is off ...
pmd


----------



## pmd (Nov 16, 2006)

Hi Thonex,

It's me again! Hope you're ok with me! Sometimes I feel really like a beginning guy!

I test your script several time, I had Tracking on off, no slice at 5 seconds (as said above) but I don't understand:

I can play sample in totality now, but it's when at a certain time (aroung middle of the sample length) I press the key, instead of stopping playing, it play it again  

Have I forget stupid thing ?

Take your time, it's not urgent at all 
regards
pmd


----------



## Thonex (Nov 16, 2006)

pmd @ Thu Nov 16 said:


> Hi Thonex,
> 
> It's me again! Hope you're ok with me! Sometimes I feel really like a beginning guy!
> 
> ...



No prob pmd,

I don't know what to tell you.

I'll have to look at it again when I have time. Right now I'm a little buried.

Cheers,

T


----------

