# Enabling scripts to receive aftertouch



## kotori (Aug 29, 2007)

Hi everyone,
I find it very frustrating that channel aftertouch can be used as a modulator in Kontakt 2 but is not available to scripts. What I will write below doesn't completely solve the issue since the user needs to use a special multi so please support my feature request for aftertouch in ksp if you haven't already done so and think this is important. Anyway, here's how to construct a multi which copies any channel aftertouch message to a CC message which the script can then access (the instructions apply to Windows, but the resulting multi should be possible to use on both platforms): 
 Close Kontakt 2 in case it's open.
 Launch regedit (open the start menu, choose "Run", type "regedit" and press enter)
 Locate HKEY_CURRENT_USER\Software\Native Instruments\Kontakt2\showMultiScript. Double click this value and change it from 0 to 1.
 Launch Kontakt 2.
 Enter the following multi script and click Apply:

```
on init
    declare const $CHANNEL_PRESSURE := 208
    declare const $CONTINUOUS_CONTROLLER := 176
    declare const $CC_NUMBER := 100
end on

on midi_in 
    if ($MIDI_COMMAND = $CHANNEL_PRESSURE) 
        set_midi($MIDI_CHANNEL, $CONTINUOUS_CONTROLLER, $CC_NUMBER, $MIDI_BYTE_1)    
    end if
end on
```

 Save the multi 
 Switch to regedit and restore the showMultiScript value to 0 (double click on it and enter zero).
 Restart Kontakt 2 and load the multi you previously saved. Now any mono aftertouch messages will be converted to CC#100 (or whatever you specified in the script) and it will be available in your normal scripts.
All credit for discovering the showMultiScript registry setting goes to Benjamin.

Cheers,
Nils


----------



## mbncp (Aug 29, 2007)

That's nice to know, thanks to both of you.

Is it possible to trap Patch Change as well ?

Btw, where did you get the info about the extra syntax, or at least where can it be found 

I'm wondering if there isn't a command to load a file (instrument) in a slot.

Cheers,
Marc


----------



## kotori (Aug 29, 2007)

_Is it possible to trap Patch Change as well ?_

I think so. At least it's possible to generate patch changes. Here's a script which converts keyswitches C0 to A0 into program changes. It makes it possible to switch between the patches in any instrument bank using these keyswitches.

```
on init
  declare const $KS_LOW  := 24 { lowest keyswitch:  C0 }
  declare const $KS_HIGH := 33 { highest keyswitch: A0 }
  declare const $NOTE_ON := 144
  declare const $NOTE_OFF := 128
  declare const $PROGRAM_CHANGE := 192  
end on

on midi_in 
  if (($MIDI_COMMAND = $NOTE_ON or $MIDI_COMMAND = $NOTE_OFF) and in_range($MIDI_BYTE_1, $KS_LOW, $KS_HIGH)) 
    ignore_midi
    if ($MIDI_COMMAND = $NOTE_ON)
      set_midi($MIDI_CHANNEL, $PROGRAM_CHANGE, $MIDI_BYTE_1 - $KS_LOW, 0)    
    end if
  end if
end on
```

_Btw, where did you get the info about the extra syntax, or at least where can it be found_
I found it out myself but for various reasons I cannot say how (if you PM me I can explain my reasons). 
Although I found out the names of the functions I was lucky enough to guess the names of the $MIDI_COMMAND, $MIDI_CHANNEL, $MIDI_BYTE_1, and $MIDI_BYTE_2 variables.  

_I'm wondering if there isn't a command to load a file (instrument) in a slot._
There seems to be a function called _load_patch but I have no idea how to use it. Maybe it's similar to the _load_ir_sample function which is documented.


----------



## mbncp (Aug 29, 2007)

Thanks a bunch Nils, I'll give it a try :D 

I tried to look for the syntax in all NI's dll and exe, but couldn't find them :( 
If I find something new, I'll post it here. 
Why on earth isn't NI making this info available (stamped at your own risk or whatever), would make K2 a little less crappy than it is now :mrgreen: .

Btw, if someone has bandstand, would be great to load the multi with the registry trick and see if it's possible to get a copy of the scripts. Though the script(s) are probably protected.

For those who are scared to even look at the registry, I attached 2 little reg files (K2_MultiScript_ON.reg and K2_MultiScript_OFF.reg, winxp) that will turn ShowMultiScrip On or Off. Drop the files anywhere, then just double-click one of the files and click yes. You need to activate them before launching K2, standalone or in your host.

Cheers,
Marc


----------



## mbncp (Aug 29, 2007)

Ok, here's a little multi-script that lets you convert Channel Pressure (mono Aftertouch) and Patch Change to any CC.
Set the CC# to -1 to let K2 deal with it the standard way. If you set $PC_CC_NUMBER to anything else than -1, you won't have any patch change in a bank.
If you're not using banks, patch change is a neat controller as it allows setting any CC value the easy way, especially if your keyboard can handle and show them.


```
on init 
{ Converting ChannelPressure (mono aftertouch) and Patch Change to CC }
{ Set the desired CC# 0-127 or -1 for standard handling by K2}
  declare const $AFT_CC_NUMBER := 124 
  declare const $PC_CC_NUMBER := 125
{------------------------------------}
  declare const $CONTINUOUS_CONTROLLER := 176 
  declare $CHANNEL_PRESSURE := 208 
  declare $PROGRAM_CHANGE := 192
  if($AFT_CC_NUMBER = -1)
    $CHANNEL_PRESSURE := -1
  end if
  if($PC_CC_NUMBER = -1)
    $PROGRAM_CHANGE := -1
  end if
end on 

on midi_in
  if ($MIDI_COMMAND = $CHANNEL_PRESSURE) 
      ignore_midi
      set_midi($MIDI_CHANNEL, $CONTINUOUS_CONTROLLER, $AFT_CC_NUMBER, $MIDI_BYTE_1)
    else 
        if($MIDI_COMMAND = $PROGRAM_CHANGE)
          ignore_midi
          set_midi($MIDI_CHANNEL, $CONTINUOUS_CONTROLLER, $PC_CC_NUMBER, $MIDI_BYTE_1) 
        end if
    end if 
end on
```

And thanks again, Ben and Nils :D 

Now I'm wondering if it's possible to directly send an NRPN value from there ...


----------



## Fernando Warez (Aug 29, 2007)

Hi Nils, where should we add our voice for support of this feature?


----------



## kotori (Aug 29, 2007)

Fernando Warez @ Wed Aug 29 said:


> Hi Nils, where should we add our voice for support of this feature?



Hi Fernando,
Do you mean making multi scripts available without having to resort to the registry hack? The NI forum perhaps.

Personally I think it's really useful to have access to the raw midi like this, but it's also a bit limiting to have this operate on the multi level. Having scripts operate on something similar to banks would be more useful in my view.

Cheers,
Nils


----------



## Fernando Warez (Aug 29, 2007)

kotori @ Wed Aug 29 said:


> Fernando Warez @ Wed Aug 29 said:
> 
> 
> > Hi Nils, where should we add our voice for support of this feature?
> ...



Sorry there i just saw the thread at NI. 



> Having scripts operate on something similar to banks would be more useful in my view.



That sounds like a good idea.


----------



## Big Bob (Aug 29, 2007)

Hey Nils,

I haven't had a chance to try this yet but, this is exciting stuff o=< !!!! Thanks for sharing this, and thanks Benjamin for poking around where 'angels fear to tread' :lol: 

Hey Marc,

It now looks like we may have a 3rd way of making Aftertouch and Program Change available. Please be sure to post if you find the 'magic' keywords to generate NRPNs and such and I'll do the same.

To be continued .....

God Bless,

Bob


----------



## kotori (Aug 29, 2007)

Except for the set_midi function and the $MIDI_... variables above everything else is based on the general midi specifications. Eg. note-on is 0x90 (144). I assume the set_midi function automatically ORs the channel and the command for you. So, regarding NRPNs I can't see why it wouldn't be possible to do were on only to follow the midi specifications.


----------



## mbncp (Aug 29, 2007)

The problem is the way that K2 interprets NRPN. I was hoping to be able to decode them myself in the multi-script and send directly a full NRPN message to K2, like when using set_nrpn, but with a channel number.
But I don't think it's possible :(


----------



## kotori (Aug 29, 2007)

mbncp @ Wed Aug 29 said:


> [...]But I don't think it's possible :(


Why not?


----------



## mbncp (Aug 29, 2007)

kotori @ Wed Aug 29 said:


> mbncp @ Wed Aug 29 said:
> 
> 
> > [...]But I don't think it's possible :(
> ...



Well, I'm just assuming that only set_midi is available on the multi level, so to send an NRPN you have to send the different CC, and K2 will probably behave the way it does now, sending wrong values or message numbers to the script.

The problem is that K2 sends an NRPN whenever it gets a CC change, not trying to get a full 14 bit message.

Now I need to try it first, but I'm so used to be disapointed with K2 

Or is there a set_nrpn function on the multi level ?


----------



## Big Bob (Aug 29, 2007)

> Or is there a set_nrpn function on the multi level ?



That's what I'm hoping for Marc. I finally had a chance to try this stuff but it took me a little while because I don't normally use multis (or banks) or any of that 'grownup' stuff :wink: .

So far I've just tried out the AT (mapping to CC124 as we are doing with your plugin). Using the new SIPS slot-1 script, the rest of the SIPS scripts now recognize AT without needing a plugin or MIDI-OX, *so we do have a 3rd way to do it*. We'll see whether or not we can also use the multi to go directly to the planned NRPN messages. Maybe we could offer a 'refrigerator or a trip around the world' or something for the first one to come up with a multi-script that converts AT and PC to NRPNs :D .

To clarify, Marc and I have been working on a standardized way of extending the KSP to provide Aftertouch and Program Change messages to scripts via NRPN messages and, a periodic timer 'interrupt' carried by a CC. Using a simple slot-1 script that converts several encoded proxies for AT and PC, the rest of the scripts simply receive NRP #208 for AT and NRP #192 for Program Change.

However, the drawback to all the methods thus far is that some 'outside' encoding has been required. The actual AT and PC messages are encoded either as a few subsonic notes or as (unused by K2) channel-message CCs. The Slot-1 utility script then converts the encoded messages to NRPNs for the rest of the scripts.

The 'outside' encoding can be done with MIDI-OX (a freeware Windows program) or with a special plugin that Marc is working on. However, this Multi-script idea that Nils has presented, gives us another way of doing the same thing without the need for an 'outside' program. Now if only Nils will show us how to write a Multiscript that will generate a periodic CC message (like CC122 every 100ms), then you can stop working on the plugin Marc :lol: 

Like I said initially, this is exciting stuff!!! Now I have to go experiment to see if I can win the 'refrigerator'.

God Bless,

Bob


----------



## Big Bob (Aug 29, 2007)

Hey Nils,

Would it be a lot of work to add these spiffy MultiScript functions and variables, etc to your editor? I tried editing my ksp_uitl.txt file but it just produced a bunch of error logs. Maybe I did something wrong, or, maybe this isn't the way to do it?? :oops: 

BTW Still no luck on finding a way to produce an NRPN (or RPN for that matter) from a Multi-Script. So far I can only tell you several dozen things that don't work. :( 

There is also a general problem with getting K2 to respond to NRPNs. So far neither Marc nor I can seem to find any kind of external means of stimulating the KSP properly with an NRPN (or RPN). I've tried my keyboard (which can produce the NRPN series) and I tried mapping a standard MIDI event with MIDI-OX because it has a few different options on outputting NRPN events. Marc has tried various schemes with his plugins. The only reliable NRPN generator so far is the set_rpn functions themselves. But that of course is of dubious value since I thought the idea was that K2 could respond to real-world NRPN events. When you hit K2 with real-world NRPN it seems to detect some of them but it also drops a lot of them and instead recognizes some of the CC98, CC99, CC6, and CC38 elements instead. 

Also, doesn't it seem kind of strange that NI chose to allow scripts to detect NRPN and RPNs (even if it doesn't really work right) but not AT :roll: I wonder what was behind that decision?

Anyway, thanks again for sharing your Multi-Script discovery. I'm sure that this will be very useful for many of us.

God Bless,

Bob

PS Did you ever get the last VXF prototype update package I sent you?


----------



## mmosc (Aug 29, 2007)

Big Bob @ Wed Aug 29 said:


> Hey Nils,
> 
> Would it be a lot of work to add these spiffy MultiScript functions and variables, etc to your editor?



Nils, I second Bob's request if it's not too much work.

admin
Would it make sence to have a sticky on Multiscript? This would keep all new found information in one place - documenting the un-documented. 

Also big thanks to Benjamin & Nils & for uncovering this. BTW multi scripts will work on the K2 player if added to a multi saved by the player

Mike


----------



## mbncp (Aug 30, 2007)

Big Bob @ Thu Aug 30 said:


> Also, doesn't it seem kind of strange that NI chose to allow scripts to detect NRPN and RPNs (even if it doesn't really work right) but not AT I wonder what was behind that decision?



I suppose they needed it for Bandstand, at least RPN for the Pitch Bend Range. I've seen different way on how to deal with this, but this is certainly the worst 


Here's a little test I made with Nills suggestion about a _load_patch function:

```
on init 
  declare $PROGRAM_CHANGE := 192 
  declare $chn := 0
end on 

on midi_in 
  if($MIDI_COMMAND = $PROGRAM_CHANGE) 
    ignore_midi 
    _load_patch("Y:\K_INST\Ethnic\Balafon.nki", $chn)
   message($chn)
   $chn := $chn +1
  end if 
end on
```

_load_patch will load an instrument in the slot with the given channel. If the slot doesn't exist it creates a new one automatically.
This is pretty interesting as it's a quick way to rebuild a multi from different instruments, to create a GM (or whatever) bank, ...
Also great to work with external banks, sending a CC00/32 (bank change) and a patch change to load any instrument on the fly from a sequencer or the keyboard.

I didn't really tested it fully, and suspect there are some other functions, as the name of the instrument is the full path, which is a bit annoying, but there is probably a set_patch name/title or something. 

Cheers,
Marc


----------



## kotori (Aug 30, 2007)

I updated the editor to support all multi script features known at this point. Download the new version.
Please note that since I don't know the parameters for some functions. If you want to use them you will have to experiment yourself. If you do this it can be useful to temporarily turn off "Extra syntax checks" to disable checking of parameters.


----------



## mbncp (Aug 30, 2007)

Thanks Nils, highly appreciated 8) 

For those who wants to play with this new stuff just be aware that you can crash K2.
Just append over here trying a set_nrpn in a multi-script module. 

I believe that most functions that we use in the standard scripts won't work or may crash K2 when used in a multi-script, which is finally expected as these functions can't deal with multis (no channel/slot parameter).

Nevertheless, this is very interesting. I recall some people had problems with CC not being reported on a patch change in a K2 banks, it should now be possible to handle this within K2 directly instead of using an external plug.

There is an old, but nice, toaster to win for the first scripter that will find the meaning and usage of all _get_engine_par_m parameters/constants ... 

Cheers,
Marc


----------



## kotori (Aug 30, 2007)

mbncp @ Thu Aug 30 said:


> I believe that most functions that we use in the standard scripts won't work or may crash K2 when used in a multi-script, which is finally expected as these functions can't deal with multis (no channel/slot parameter).


Perhaps. But it's also the case that I've found numerous error messages saying you can't use this or that function in a multi script, so a certain degree of error checking seems to be performed at least. Maybe the NI developer didn't find it necessary to make it completely stable though since it's normally not exposed to the user.



> Nevertheless, this is very interesting. I recall some people had problems with CC not being reported on a patch change in a K2 banks, it should now be possible to handle this within K2 directly instead of using an external plug.


Indeed. Such a script would be useful.



> There is an old, but nice, toaster to win for the first scripter that will find the meaning and usage of all _get_engine_par_m parameters/constants ...


Yes. Unfortunately I haven't found any information about variable names. But since I could guess the midi variables above, maybe someone will be able to guess some engine param names.

Cheers,
Nils


----------



## Big Bob (Aug 30, 2007)

kotori @ Thu Aug 30 said:


> I updated the editor to support all multi script features known at this point. Download the new version.
> Please note that since I don't know the parameters for some functions. If you want to use them you will have to experiment yourself. If you do this it can be useful to temporarily turn off "Extra syntax checks" to disable checking of parameters.



Hey that's great Nils but, you missed one very useful one: ignore_midi

I've gotten so spoiled using your editor that I almost don't remember any more how to write scripts so that K2 will take them directly :lol: 

God Bless,

Bob


----------



## kotori (Aug 30, 2007)

Big Bob @ Thu Aug 30 said:


> Hey that's great Nils but, you missed one very useful one: ignore_midi



Hi Bob,
Thanks! Fixed now. Just download the installation file again.

Cheers,
Nils


----------



## mbncp (Aug 30, 2007)

Some really interesting stuff :D 

Now it is possible to access more than 16 channels, even on the vst version as the commands seem to work for channel 0 to 63, where channel 0 is port A Channel 1 and 63 is port D channel 16.
It still needs an external plug to define ports by some encoding, but it's possible. Also in this case we just need an extra value to define the port, and send the events to the correct channel within the multi.

The following example will send all MIDI to Port D, Channel 16:

```
on midi_in
  ignore_midi
  set_midi(63, $MIDI_COMMAND, $MIDI_BYTE_1, $MIDI_BYTE_2)
end on
```

Another thing that looks funny, is that NRPN generated within a multiscript (creating the 4 CC) don't generate a NRPN in the scripts, but showing up as CC. 
The good part is that there doesn't seem to be skipped CCs, even if there are sent at the same time.


----------



## mmosc (Aug 30, 2007)

kotori @ Thu Aug 30 said:


> Big Bob @ Thu Aug 30 said:
> 
> 
> > Hey that's great Nils but, you missed one very useful one: ignore_midi
> ...



Nils,

Thanks a million - who could ask for better customer service

I'm working on a humanize multi script that's looking very promising and like Bob I've become very dependent on your editor 

Mike


----------



## Fernando Warez (Aug 30, 2007)

mbncp @ Thu Aug 30 said:


> Nevertheless, this is very interesting. I recall some people had problems with CC not being reported on a patch change in a K2 banks, it should now be possible to handle this within K2 directly instead of using an external plug.



Holy crap this is cool. I've been scratching my head to find a work around this problem for a while now. 



> I believe that most functions that we use in the standard scripts won't work or may crash K2 when used in a multi-script, which is finally expected as these functions can't deal with multis (no channel/slot parameter).



So it should be compatible with SIPS but is it compatible with script like TKT and the matrix?

And is this the script you were talking about and do i need to do anything special? I guess i need to tweak the registry like Nils suggested?



> on init
> declare $PROGRAM_CHANGE := 192
> declare $chn := 0
> end on
> ...


----------



## Fernando Warez (Aug 30, 2007)

Ok I've read the whole thread and i think i understand a little better now. 



> Nevertheless, this is very interesting. I recall some people had problems with CC not being reported on a patch change in a K2 banks, it should now be possible to handle this within K2 directly instead of using an external plug.



What extra plug are you talking about here? I'm on PC.

Thank you.


----------



## Dynamitec (Aug 30, 2007)

>8o You are crazy guys! ... I never thougt of this scripts and things when discovering the multi script option. Those scripts of yours look very interesting!


----------



## Big Bob (Aug 30, 2007)

[quote:3cff2b51òì   `—Kì   `—Lì   `—Mì   `—Nì   `—Oì   `—Pì   `—Qì   `—Rì   `—Sì   `—Tì   `—Uì   `—Vì   `—Wì   `—Xì   `—Yì   `—Zì   `—[ì   `—\ì   `—]ì   `—^ì   `—_ì   `—`ì   `—aì   `—bì   `—cì   `—dì   `—eì   `—fì   `—gì   `—hì   `—iì   `—jì   `—kì   `—lì   `—mì   `—nì   `—


----------



## Freesamples (Apr 25, 2009)

Is it posible to make POLYPHONIC AFTERTOUCH working in ksp?


----------



## Big Bob (Apr 26, 2009)

Freesamples @ Sat Apr 25 said:


> Is it posible to make POLYPHONIC AFTERTOUCH working in ksp?



Poly AT requries two bytes of data (the MIDI note # and the value byte). In order to provide a Multiscript that delivers Poly AT to the scripts, you would have to do something like use a *pair of CCs *as proxies for the Poly AT. Once that is done, then you could write code for the CCB that would detect and decode such messages.

God Bless,

Bob


----------



## Freesamples (Apr 26, 2009)

Thank you Bob. I can use a pair of CCs as proxies for the Poly AT, but I don't know how to write code, which would detect and decode such messages :(

Thanks,
F.


----------



## Big Bob (Apr 26, 2009)

Freesamples @ Sun Apr 26 said:


> Thank you Bob. I can use a pair of CCs as proxies for the Poly AT, but I don't know how to write code, which would detect and decode such messages :(
> 
> Thanks,
> F.



In that case you probaly won't be able to write the MS either :lol: So, if some multiscripter wants to tackle this, he/she will have to write a sample decoder for you also :wink: It will be a while before I have the time to do something like this but, if and when I do have some time (provided no one else has posted something for you by then), maybe I'll give it a shot.

Maranatha,

Bob


----------



## Freesamples (Apr 26, 2009)

> In that case you probaly won't be able to write the MS either


Oh, I think I can. I just need a start point 

Thanks,
F.


----------



## Big Bob (Apr 26, 2009)

> Oh, I think I can. I just need a start point



Well, until you become familiar with how to write a MS, any 'pointing' I do wouldn't be very helpful. :lol: 

Unfortunately, I don't think anyone has written a tutorial on multiscripts yet and NI has chosen not to disclose that they exist. You could however search this forum for threads dealing with Multiscripts. Several years ago there were some lively discussions about them.

I have also mentioned once or twice that if there was enough interest, I might try to cobble together something like a *Multiscripts 101 *but, so far, the interest has been very minimal so I don't think I'll devote the time to it. However, if someone has already produced some kind of tutorial document for MSs, will you please post a link for Mr F? 

God Bless,

Bob


----------

