# Scripts that replace keyswitching



## Hans Adamson (Apr 15, 2006)

Are they possible?

The scripts that are included with K2 do not offer intelligent switch between groups. Is this possible? Anyone have any examples?

The included scripts seem to only alter event playback parameters such as timing, tuning, fade-in etc....


----------



## kotori (Apr 15, 2006)

Sure, of course this is possible. Eg. I have written a script that changes articulation based on playing speed. See my http://nilsliberg.se/ksp/scripts/scripts.html (scripts web page). There's a number of other scripts that dynamically turns groups on and off.
Is there anything special that you would want to achieve?

Best,
Nils


----------



## Hans Adamson (Apr 15, 2006)

Thanks Nils,

I want to be able to write scripts that choose group depending on characteristics of previous note, or depending on certain controller state + previous note's variables.

If I was to study this in your scripts which one would you recommend?

Best,


----------



## kotori (Apr 15, 2006)

Hans Adamson @ Sat Apr 15 said:


> Thanks Nils,
> I want to be able to write scripts that choose group depending on characteristics of previous note, or depending on certain controller state + previous note's variables.
> 
> If I was to study this in your scripts which one would you recommend?



Tough question. My http://nilsliberg.se/ksp/scripts/speed_control_source.html (Speed control) and http://nilsliberg.se/ksp/scripts/alternation_source.html (Alternation) scripts does this. The Alternation script is pretty big, so I'd suggest you look at the speed control script. Sorry that there aren't more comments in it.

For general pedagocial purposes and not key-switching in particular I think my http://nilsliberg.se/ksp/scripts/partmaker_source.html (Part Maker) script is best.

Cheers,
Nils


----------



## Thonex (Apr 15, 2006)

Hans Adamson @ Sat Apr 15 said:


> Thanks Nils,
> 
> I want to be able to write scripts that choose group depending on characteristics of previous note, or depending on certain controller state + previous note's variables.
> 
> ...



Hans, choosing groups depending on previous note charateristics is remarkably easy.... it really is a bunch of "if" statements that when true or Not-true will trigger a group.

The tricky part in all of this is making it elegant... so that you can load the script into another patch and it will scan all the groups and give you options in the gui.

T


----------



## Hans Adamson (Apr 15, 2006)

Well I don't think I can aim for elegance quite yet... I will be more that satisfied if I can make the script do what I want it to, first of all.


----------



## kotori (Apr 16, 2006)

Like Thonex said it's pretty simple. In fact I think reading the scripting tutorial pdf and trying some things out oneself is the best way to learn.

I wrote a short script that demonstrates how to switch between groups based on whether a note is played legato (overlapping the previous note) or non-legato.
The script isn't supposed to do anything particularily useful - it just demonstrates some basic concepts. To use it you need an instrument with at least two groups to let it switch between (make sure the two first groups don't have any group start options).

Check out this http://nilsliberg.se/ksp/scripts/temp/sample_script_legato.html (legato demonstration script).

It's useful to know that there are two different ways of selecting which groups to use from a script. Either you let the script automatically play keyswitches or set CC values which in their turn activate groups using group start options, _or_ you don't use group start options and let the script activate groups using the allow_group/disallow_group functions (as in the sample script above).

The page above was produced using the HTML export function in my script editor. Btw. I found out that there's a small error with the export: it's only possible to export scripts that have been saved at some point - not new untitled scripts. Just so you know. I'll fix this in an upcoming version.

Best,
Nils


----------



## Hans Adamson (Apr 16, 2006)

Thank you Nils,

This is so helpful. I have read the manual twice, but I haven't tried writing a script yet. This will be perfect to get me started.


----------



## Hans Adamson (Apr 16, 2006)

Nils,

So in this example, group(1) is the non-legato notes and group(0) is the legato note?


----------



## kotori (Apr 16, 2006)

Hans Adamson @ Sun Apr 16 said:


> Nils,
> So in this example, group(1) is the non-legato notes and group(0) is the legato note?


Yes, that's correct. 
With scripts one starts counting groups from zero - not from one as in the GUI. I guess you know that from reading the tutorial, just thought I'd mention it. A good way to test the script is to take an instrument with just one group, copy and paste the group to make a duplicate and then pan the two groups differently.


----------



## Hans Adamson (Apr 16, 2006)

Nils,

Is the analysis below correct?


----------



## Thonex (Apr 16, 2006)

I think you are pretty correct with your interpretation, except I believe that the disallow_group() command doesn't mte a note... it just disallows a group's samples from being triggered.. the group is in essence ignored. At least that is the way I understand it.

BTW... nice little graphic thingy... what are you using to do that??

I can see you'll become a scripting animal  


T


----------



## Hans Adamson (Apr 16, 2006)

So the two notes can still be heard at the same time?

(I used PhotoDraw, Microsoft discontinued software)


----------



## Thonex (Apr 16, 2006)

Hans Adamson @ Sun Apr 16 said:


> So the two notes can still be heard at the same time?
> 
> (I used PhotoDraw, Microsoft discontinued software)



no... what I'm saying is that if a sample from a group is already playing and you then disallow that group, then subsequent samples will not be triggered but the original sample will still continue playing..... I believe :neutral: 

T


----------



## Hans Adamson (Apr 16, 2006)

But is there anything in this script that makes it monophonic?


----------



## kotori (Apr 16, 2006)

Hi again
An invokation of allow_group/disallow_group will affect the note that was played even though the note came first so to say. And it will affect any notes that are generated by the script (none in my sample) in the same "on note" that time around. It will not affect any previously played already sounding notes. It works much like the change_note and change_velo functions. Only changes that occur before the sample has started playing will have any affect. The sample will not be played by Kontakt until the whole "on note" callback has been executed in its entirety (or the wait function is called which will cause Kontakt to start playing the sample), so in this case allow_group/disallow_group will affect the note that triggered "on note". Think of these functions as setting group start options dynamically from a script. It's also useful to know that they don't have the power to override the group start options set using the Kontakt user interface.

I'm sorry I should have used another wording than 'last note' which is a bit ambigous. What I really meant was 'previous note'.
note_off($last_note) will release the previous note (as opposed to the current one)
$last_note := $EVENT_ID will save the event id of the current note since this will be the 'previous note' the next time around.
Since the script automatically releases any previously played note it's monophonic.

I don't know if that was a good explanation. Please post any questions you might have.

Cheers,
Nils


----------



## Thonex (Apr 16, 2006)

kotori @ Sun Apr 16 said:


> It will not affect any previously played already sounding notes. It works much like the change_note and change_velo functions.



Yes... this is what I was trying to convey.

It doesn't actually mute an already sounding note.

T


----------

