# FINISHED! Kontakt 4 Script - Advanced Tuning (Made Easy)



## anathematized_one (Jul 3, 2011)

Well, I got the idea for this script because I needed a re-tune for Great Highland Bagpipes. I could have made a tuning table in Scala and exported it as a Kontakt script but, well I couldn't figure out Scala and I thought, well what if I need other tunings? I looked at the scripts that just came with Kontakt and there wasn't a single script that I could use to get the correct bagpipe tuning, I'd have to use two or three of them.

So, here's the setup I'm working on, it's basically a really really REALLY modified "Kontakt 4 Factory Library - Microtuning" script by Native Instruments (Josef Natterer, Nicki Marinic). The far left, you have a Key knob that adjusts the key for two UI tables. Beneath this knob, you have a drop-down menu where you can select some preset tunings (designed so that they don't change the key unless it is specific for an instrument for it to be so). Next, you have two labels (side-by-side) that show the tone for the table sliders beneath it. And of course, under that you have two tables (side-by-side). The left table adjusts the tuning of the notes by semitones (either up or down by a maximum of 4 semitones). The right table adjusts the tuning by cents (either up or down by a maximum of 100 cents). Adjusting both tables combines them together. For example, you can make A tuned to +1 semitone on the left and +33 cents on the right, or A# + 33 cents, or A + 133 cents (this is A on a GHB, and the GHB music is written with this listed as A, even though it's really slightly higher than A#). Then below those two tables I just have a simple label that tells you which table does what.

Now, this isn't finished yet, and I have tested what I have so far minus the unfinished sections and it renders with no errors.

I will attach a zip with the text file of the script.

I need some help in figuring out how to make this preset menu work exactly... I mean for some presets, it will HAVE to change the key to a specific key (no problem), but how can I have it set that for other presets, it doesn't change the key at all and only changes the tables? Also, how do I apply the conditions and stuff for the "on ui_control $preset_menu ... on end" to change all of those parameters?

As you'll see, I've already gotten the presets coding started but, I'm not sure how to get what I want from here.

Basically I want it so that once you select a preset from the menu, the drop down menu goes back to displaying "Select preset" but it changes the tables and/or keys as needed. However, the preset needs to change at least both tables, but certain presets will also have to change the key, but I want to set other presets up so it doesn't do anything to the key.

I also need help in figuring out how to get it so that it stores the changes you manually make on the tables to the memory or whatever where it's uh, persistent.

I haven't tested to see if the code actually works for adjusting the tuning yet though.

EDIT: Updated Script


----------



## anathematized_one (Jul 4, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*

Well, I updated the script but I'm not sure if when I updated the file in the first post it actually did it, anyway...

Updates from the last post:

I tested it and it does re-tune, though I didn't test to see if it does so by the correct amount (I.E. retuning a note by 1 semitone up actually makes it up 1 semitone).

Everything works except the presets menu. I've started coding that but can't quite figure it out, there are two things:
1. I can't get it to set the !preset_key[$a] := $key[$a], when I hit apply, it tells me in the script editor status that there is an error, "newline expected" but, if I take that one part out, there are no problems.
2. When I select a preset, not in the script status, but at the bottom of the Konkakt window, in that status, it says "SCRIPT WARNING: array out of bounds (using default value 0)! Line 251 Program 768"

So yeah, so far, the only problems I'm having with this are the presets menu.


----------



## Lindon (Jul 4, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*

You have to shows us at least some of the code if you want us to help out...(it's not attached anywhere) - but I'm guessing:

!preset_key[$a] := $key[$a]


should be more like:


%preset_key[$a] := $key[$a]


(a numeric array for numeric values...)


----------



## Lindon (Jul 4, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*

Hmm, scripts there now..I think Chrome is playing up again...


----------



## anathematized_one (Jul 5, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*

Alright, updated yet again.

So, at this point, the presets menu works for everything except the bagpipe preset I created. Basically in the code, it's where you see "case 1."

I checked and checked and checked and can't figure out why, but it just will not set the values in the tables to what they're supposed to be. It sets the semi table to have the third value to be +1 (which is right), but it doesn't have any other values for that table and it sets all the values for the cents table to 0.

Now, as far as the key knob, for some reason, when I select the bagpipe tuning, it will change the key knob, but instead of setting it to A (9 from 0 in the array), it sets it to B (11 from 0, maximum value).

Again, I get no errors of any kind.



Lindon @ Tue Jul 05 said:


> %preset_key[$a] := $key[$a]


If I set either side as $key[$a], then the code breaks/errors. The only way I can get that part to half way work is if I put it as $key := %preset_key[($presets-1)+$a]


----------



## Big Bob (Jul 5, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*



> Again, I get no errors of any kind.
> 
> 
> Lindon @ Tue Jul 05, 2011 12:47 am wrote:
> ...



$key[$a] is a syntactical error. If $key is an ordinary variable, you cannot add a subscript, if it's an array, then it needs to be %key[$a].

Rejoice,

Bob


----------



## anathematized_one (Jul 5, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*



Big Bob @ Tue Jul 05 said:


> > Again, I get no errors of any kind.
> >
> >
> > Lindon @ Tue Jul 05, 2011 12:47 am wrote:
> ...


Ha ha, yeah, thanks, I got that.

Anyway, here's the latest version of the code.

I got it where the whole thing works... except when selecting the preset option Reset All (0) and Highland Bagpipe (1) which... don't work, and I have no idea why...

http://pastebin.com/raw.php?i=vrJRA6Pt - that's the updated code.


----------



## Lindon (Jul 5, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*

Well at least part of your problem might be here...


```
$a := 0
                while ($a < 12)
                    %tuning_semi[$a] := %preset_semi[($presets-1)*12+$a]
                    %tuning_cent[$a] := %preset_cent[($presets-1)*12+$a]
                    $key := (%preset_key[($presets-1)+$a])
```


OK so first time thru $a = 0, so we can say:

$key := (%preset_key[($presets-1)+0])

and you've set "Reset All" to = 0 in the $presets menu so we can substitute 0 for $presets:

$key := (%preset_key[(0-1)+0])

so (0-1) + 0 = -1 so you have this:

$key := (%preset_key[-1])

...and you cant have negative numbers as indices to arrays...

Same for your other indices: %preset_semi[($presets-1)*12+$a]

$a = 0 and $presets = 0 so thats like saying:

%preset_semi[(0-1)*12+0]

so this resolves to %preset_semi[-12]....again this wont work.

also this may not be getting you what you hope for when $presets = 1 thus:

%preset_semi[(1-1)*12+0]

resolving to %preset_semi[0]


Hope this helps...


----------



## anathematized_one (Jul 5, 2011)

*Re: Working on Kontakt 4 Script - Advanced Tuning (Made Easy)*



Lindon @ Tue Jul 05 said:


> Well at least part of your problem might be here...
> 
> 
> ```
> ...


Well that's what I thought, but for some reason, it doesn't work. Look in the next part, the part that does work. 


```
$a := 0                
while ($a < 12)
    %tuning_semi[$a] := %preset_semi[($presets-1)*12+$a]
```
It is set up the same way, now it says Select 2 to 13, let's just go with 2. In the presets menu, 2 is set to "pure."

Therefore, %tuning_semi[0] = %preset_semi[(2-1)*12+0]
That brings it down to tuning semi = preset semi[1*12]

But if you look under the declared %preset_semi array, the first row is the highland bagpipes (in arrays, the first row is 0, then the second is 1, and so on).

Also, the thing to remember is that with these, it's not 1*12 = 12, with arrays, it's x by y, meaning X rows by Y columns. So, 3*12 means the array contains 12 numbers, and it will be the third set of 12 numbers.

I also noticed that, being $a = 0, then it shouldn't need to be included right? But if I don't include it, then it says the array is out of bounds.

I've also tried setting $a to be equal to everything from -2 to 2, I've tried adding and subtracting $a, I tried leaving $a off entirely, I tried subtracting 2 to adding 2 in the ($presets-1) spot, and nothing seems to work.

Well, that was earlier, and I had since closed my DAW program and I reopened it and I edited it (as you will see me link in a minute) and I set it how I had tried it once before, in a way that actually makes logical sense... and now it works. 

I mean all of it works like it should.

http://pastebin.com/raw.php?i=3AfEAXLu


----------



## Lindon (Jul 5, 2011)

Ok yep, that'll certainly work better if you change to just ($presets) and not ($presets-1)

I'm assuming this is all working for you now. 

Have fun.

see ya.


----------

