# Script snippet: Adding convolution mini tutorial



## Sasje (Jan 17, 2013)

Hello everyone,

Often I see the question about how to link FX's to the GUI of an instrument. It's actually very easy to do. So I wanted to share a little snippet, maybe this mini tutorial will help someone. The below tutorial should work with any sort of FX you want to add.

We are going to create a simple menu that loads IR samples into a convolution. The first thing we want to do is add a "Convolution" effect into "Instrument insertFx"

1. Click on an empty slot "Add Fx" and select convolution from the list.
2. Then click the button "Bypass" because we do not want to load anything yet.







Now, the position of the slot is important. Slot ID's start at zero. So, if you add a convolution effect to slot 1, the slot ID is zero. When you add another FX, the next slot ID becomes 1. This the way it is, and it's important to remember, because we need the slot ID to assign our script.

Open the script editor:






And click on "edit". Remember that any changes you make to a script must be saved first. Click on the APPLY button to save. 

We create the menu by doing this:


```
declare ui_menu $Reverb

	add_menu_item ($Reverb, "Reverb:",0)
	add_menu_item ($Reverb, "Concert Hall A",1)
	add_menu_item ($Reverb, "Concert Hall B",2)
	add_menu_item ($Reverb, "Exhibition Hall",3)
	add_menu_item ($Reverb, "Cathedral A",4)
	add_menu_item ($Reverb, "Cathedral B",5)
	add_menu_item ($Reverb, "NDB Cathedral",6)
	add_menu_item ($Reverb, "Club A",7)
	add_menu_item ($Reverb, "Club B",8)
	add_menu_item ($Reverb, "Tavern Close",9)
	add_menu_item ($Reverb, "Tavern Near",10)
	add_menu_item ($Reverb, "Tavern Mid",11)
	add_menu_item ($Reverb, "Tavern Far",12)
	add_menu_item ($Reverb, "Auditorium A",13)
	add_menu_item ($Reverb, "Auditorium B",14)
	add_menu_item ($Reverb, "Chamber A",15)
	add_menu_item ($Reverb, "Chamber B",16)
	add_menu_item ($Reverb, "Rehearsal Room",17)
	add_menu_item ($Reverb, "Hard Wood",18)
	add_menu_item ($Reverb, "Music Studio",19)
	add_menu_item ($Reverb, "Small Room A",20)
	add_menu_item ($Reverb, "Small Room B",21)

        move_control($Reverb,1,2)
```

So we get this:







To load an IR sample, we just need this code:


```
load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall A.wav",0,1)
```


We use this piece of code to Bypass or Activate the effect:

This will bypass the effect:

```
set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 1, -1, SLOTID, 1 )
```

And this will activate the effect again:

```
set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, SLOTID, 1 )
```

Notice the SLOTID. Here we put the ID of the slot we want to communicate with. 

Easy! we have put our convolution in slot 1, so we use 0.

This will bypass the effect:

```
set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 1, -1, 0, 1 )
```

And this will activate the effect again:

```
set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
```


Our full script is fairly straightforward:



```
{*********************************
Demo script for kontakt
**********************************}

on init

    set_ui_height_px(300)
    set_script_title("My Script")

    make_perfview

    message("")

	declare ui_menu $Reverb
	add_menu_item ($Reverb, "Reverb:",0)
	add_menu_item ($Reverb, "Concert Hall A",1)
	add_menu_item ($Reverb, "Concert Hall B",2)
	add_menu_item ($Reverb, "Exhibition Hall",3)
	add_menu_item ($Reverb, "Cathedral A",4)
	add_menu_item ($Reverb, "Cathedral B",5)
	add_menu_item ($Reverb, "NDB Cathedral",6)
	add_menu_item ($Reverb, "Club A",7)
	add_menu_item ($Reverb, "Club B",8)
	add_menu_item ($Reverb, "Tavern Close",9)
	add_menu_item ($Reverb, "Tavern Near",10)
	add_menu_item ($Reverb, "Tavern Mid",11)
	add_menu_item ($Reverb, "Tavern Far",12)
	add_menu_item ($Reverb, "Auditorium A",13)
	add_menu_item ($Reverb, "Auditorium B",14)
	add_menu_item ($Reverb, "Chamber A",15)
	add_menu_item ($Reverb, "Chamber B",16)
	add_menu_item ($Reverb, "Rehearsal Room",17)
	add_menu_item ($Reverb, "Hard Wood",18)
	add_menu_item ($Reverb, "Music Studio",19)
	add_menu_item ($Reverb, "Small Room A",20)
	add_menu_item ($Reverb, "Small Room B",21)
      
    move_control($Reverb,1,2)
 
end on


on ui_control($Reverb)

if($Reverb # -1)

    select($Reverb) 
    
      case 0 
      set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 1, -1, 0, 1 )
      
      case 1
      set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall A.wav",0,1)
      case 2
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall B.wav",0,1)
      case 3
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Exhibition Hall.wav",0,1)
      case 4
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Cathedral A.wav",0,1)
      case 5
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Cathedral B.wav",0,1)
      case 6
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/10 Big Rooms/IR Samples/ndb_cathedral_ir_32bit.wav",0,1)
      case 7
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Club A.wav",0,1)
      case 8
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Club B.wav",0,1)
      case 9
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Close.wav",0,1)
      case 10
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Near.wav",0,1)
      case 11
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Medium.wav",0,1)
      case 12
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Far.wav",0,1)
      case 13
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Auditorium A.wav",0,1)
      case 14
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Auditorium B.wav",0,1)
      case 15
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Chamber A.wav",0,1)
      case 16
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Chamber B.wav",0,1)
      case 17
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Rehearsal Room.wav",0,1)
      case 18
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Hard Wood Room B.wav",0,1)
      case 19
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Music Studio C.wav",0,1)
      case 20
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Small Room A.wav",0,1)
      case 21
          set_engine_par( $ENGINE_PAR_EFFECT_BYPASS, 0, -1, 0, 1 )
          load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ... 
          & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Small Room B.wav",0,1)
    
    end select

end if

end on
```


----------



## gregjazz (Jan 17, 2013)

Thanks for posting this mini tutorial!

Just a note--I find an efficient way to load the impulses is to refer to a string array that contains all the impulse filenames. That way you don't have to have the a separate "case" for loading each impulse, but can have a single, generic command that loads the appropriate impulse by the index of the item chosen in the dropdown menu.


----------



## mk282 (Jan 17, 2013)

Not to mention repeating the bypassing of the effect in each select case is tiresome.

Here's the way more efficient version, as per Greg's directions:


```
on init
    make_perfview
    set_ui_height_px(300)
    set_script_title("My Script")

    declare const $REVERB_SLOT := 0
    declare const $FX_TYPE:= 1    { 1 for insert fx, 0 for send fx }

    declare !IRlist[21]
    !IRlist[ 0] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall A.wav"
    !IRlist[ 1] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall B.wav"
    !IRlist[ 2] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Exhibition Hall.wav"
    !IRlist[ 3] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Cathedral A.wav"
    !IRlist[ 4] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Cathedral B.wav"
    !IRlist[ 5] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/ndb_cathedral_ir_32bit.wav"
    !IRlist[ 6] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Club A.wav"
    !IRlist[ 7] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Club B.wav"
    !IRlist[ 8] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Close.wav"
    !IRlist[ 9] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Near.wav"
    !IRlist[10] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Mid.wav"
    !IRlist[11] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Tavern Far.wav"
    !IRlist[12] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Auditorium A.wav"
    !IRlist[13] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Auditorium B.wav"
    !IRlist[14] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Chamber A.wav"
    !IRlist[15] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Chamber B.wav"
    !IRlist[16] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Rehearsal Room.wav"
    !IRlist[17] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Hard Wood.wav"
    !IRlist[18] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Music Studio.wav"
    !IRlist[19] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Small Room A.wav"
    !IRlist[19] := get_folder($GET_FOLDER_FACTORY_DIR) & "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Small Room B.wav"

    declare ui_label $Reverb (1,1)
    declare ui_menu $IR
    add_menu_item($IR,"BYPASS",-1)
    add_menu_item($IR,"Concert Hall A",0)
    add_menu_item($IR,"Concert Hall B",1)
    add_menu_item($IR,"Exhibition Hall",2)
    add_menu_item($IR,"Cathedral A",3)
    add_menu_item($IR,"Cathedral B",4)
    add_menu_item($IR,"NDB Cathedral",5)
    add_menu_item($IR,"Club A",6)
    add_menu_item($IR,"Club B",7)
    add_menu_item($IR,"Tavern Close",8)
    add_menu_item($IR,"Tavern Near",9)
    add_menu_item($IR,"Tavern Mid",10)
    add_menu_item($IR,"Tavern Far",11)
    add_menu_item($IR,"Auditorium A",12)
    add_menu_item($IR,"Auditorium B",13)
    add_menu_item($IR,"Chamber A",14)
    add_menu_item($IR,"Chamber B",15)
    add_menu_item($IR,"Rehearsal Room",16)
    add_menu_item($IR,"Hard Wood",17)
    add_menu_item($IR,"Music Studio",18)
    add_menu_item($IR,"Small Room A",19)
    add_menu_item($IR,"Small Room B",20)

    move_control($Reverb,1,1)
    move_control($IR,1,2)

    message("") 
end on


on ui_control($IR)
    if ($IR = -1)
        set_engine_par($ENGINE_PAR_EFFECT_BYPASS,0,-1,$REVERB_SLOT,$FX_TYPE)
    else
        set_engine_par($ENGINE_PAR_EFFECT_BYPASS,1,-1,$REVERB_SLOT,$FX_TYPE)
        load_ir_sample(!IRlist[$IR],$REVERB_SLOT,$FX_TYPE)
    end if
end on
```

Much easier, no? 



By declaring constants you can easily change the script to affect different slots and/or position of the FX (insert or send).


Beware: you cannot use Kontakt's factory IR in commercial products! Also note that if you're using a resource container to store your own IRs, you don't need the cumbersome get_folder() command at all, nor do you need to write .wav at the end of each IR name in !IRlist.


----------



## Sasje (Jan 18, 2013)

Great optimization!

I actually didn't aim for efficiency in the tut, I rather wanted to make it a bit exemplary leaving the optimization as an exercise. But the examples given are great, I really like it!  

btw, why can't you use the IR samples in commercial products? they are already defined into kontakt, correct? what is the difference between selecting an IR and load it through a script?


----------



## gregjazz (Jan 18, 2013)

Leave it to mk282 to go ahead and rewrite the script for you! Makes me feel so lazy...


----------



## Sasje (Jan 18, 2013)

Hehe! :D 

btw: I discovered a tiny mistake I made, I already corrected it in the tutorial.

This


```
load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ...
& "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall A.wav",1,1)
```

Had to be this:

```
load_ir_sample(_get_folder($GET_FOLDER_FACTORY_DIR) ...
& "presets/effects/convolution/<<<K4IR.nkx>>>/K4 IR Samples/Concert Hall A.wav",0,1)
```

I forgot to assign the slot it was supposed to load into. Now it loads in slot 0, and not slot 1.

My bad, I apologize. It was late at night and just copy pasted it from one of my projects. o


----------



## mk282 (Jan 18, 2013)

Sasje @ 18.1.2013 said:


> btw, why can't you use the IR samples in commercial products? they are already defined into kontakt, correct? what is the difference between selecting an IR and load it through a script?



Because NI says so. You cannot redistribute any part of the program as your own commercial product. Read the EULA.


----------



## Lindon (Jan 21, 2013)

...and of course a product with this script wont work as expected in kontakt 5....well so I found... really just put your IR's in the resources IR folder....

LP


----------



## The Darris (Nov 21, 2013)

So how would the array read if you had your own private IR's that you wanted to use? Assuming I had them in their own folder on my sample drive.


----------



## mk282 (Nov 22, 2013)

You make a resource container and put the IRs in there, then just use the IR filename (without the extension) in the array. No need to use get_folder() at all.


----------



## The Darris (Nov 22, 2013)

Okay, stupid question alert!

How do I create a resource container for them? Still in the process of finding a source for it so I will update if you have lead me in the right direction by then. Thanks in advanced .


----------



## The Darris (Nov 22, 2013)

UPDATE**

Okay, so basically I am adding my own UI to the VSL Legacy Instruments in the K5 Factory library. I have the Bricasti M7 IR's and have scripted the UI with everything but how to load the IR's from a menu. As you said, you can't use the

declare const $REVERB_SLOT method but I think the original method that Sasje used will work, fortunately I am only using 4 different IRs. So I guess my question is, what is my best approach in accomplishing this?


----------



## Reigner (May 7, 2020)

i'm reviving this great thread to ask if someone know how to add a pre delay, wet and dry knobs to this script


----------



## EvilDragon (May 8, 2020)

Engine parameters are all listed in KSP reference...


----------



## Reigner (May 8, 2020)

EvilDragon said:


> Engine parameters are all listed in KSP reference...



sorry but i have no clue how to integrate all the codes in the script, i'm getting error all time


----------



## EvilDragon (May 8, 2020)

Maybe post some code so that we can tell you where the error is?


----------



## geronimo (May 8, 2020)

Must seek, poke around; everything is here .


----------



## jeorgia (May 8, 2020)

Reigner said:


> i'm reviving this great thread to ask if someone know how to add a pre delay, wet and dry knobs to this script




```
declare ui_slider $SLRevDry(0, 500000) {  or (0,1000000) }
declare ui_slider $SLRevWet(0, 500000)

on ui_control($SLRevDry)
    set_engine_par($ENGINE_PAR_SEND_EFFECT_DRY_LEVEL,$SLRevDry,-1,0,1)
end on
on ui_control($SLRevWet)
    set_engine_par($ENGINE_PAR_SEND_EFFECT_OUTPUT_GAIN,$SLRevWet,-1,0,1)
end on
```


----------



## Reigner (May 8, 2020)

jeorgia said:


> ```
> declare ui_slider $SLRevDry(0, 500000) {  or (0,1000000) }
> declare ui_slider $SLRevWet(0, 500000)
> 
> ...



many thanks! now i'm trying to figure how to put the sliders below lol


----------



## McSound (May 11, 2020)

mk282 said:


> Beware: you cannot use Kontakt's factory IR in commercial products! .....
> You cannot redistribute any part of the program as your own commercial product. Read the EULA.


Just to clarify. I asked the question about legality of using of built-in ir's of kontakt samples and fonts via script for commercial purposes and ED's answer was yes, I can do it. My instrument doesn't contain these samples or fonts, It just uses ones that my every possible customer has in his own version of kontakt. So what redistribution is it? Or I understood this post wrong? Or rules have changed since the time of this old post? Please enlighten me.


----------

