# Simple filter type menu script



## TheMaschineWarehouse (Jul 10, 2015)

This is a small filter menu script, for anybody thats a novice like myself, that needs it for any synth based libraries. Im sure there is probably a easier way to do this, and maybe some of the more advanced guys can shorten it up a bit. This only works in version 5.5 and up. 

```
on init

    declare ui_menu $Lpfilter
    add_menu_item($Lpfilter,"Ladder LP1",0)
    add_menu_item($Lpfilter,"Ladder LP2",1)
    add_menu_item($Lpfilter,"Ladder LP3",2)
    add_menu_item($Lpfilter,"Ladder LP4",3)
    add_menu_item($Lpfilter,"Daft LP",4)
    add_menu_item($Lpfilter,"Pro 52",5)


    set_control_par(get_ui_id($Lpfilter),$CONTROL_PAR_WIDTH,106)

    move_control_px($Lpfilter,66,2)

    make_persistent($Lpfilter)

    message("")
end on

on ui_control ($Lpfilter)
    if ($Lpfilter = 0)
    set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE, $FILTER_TYPE_LDR_LP1,0,0,-1)
    end if
        if ($Lpfilter = 1)
    set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE, $FILTER_TYPE_LDR_LP2,0,0,-1)
    end if
    if ($Lpfilter = 2)
    set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE, $FILTER_TYPE_LDR_LP3,0,0,-1)
    end if
    if ($Lpfilter = 3)
    set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE, $FILTER_TYPE_LDR_LP4,0,0,-1)
    end if
    if ($Lpfilter = 4)
    set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE, $FILTER_TYPE_DAFT_LP,0,0,-1)
    end if
    if ($Lpfilter = 5)
    set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE, $FILTER_TYPE_PRO52,0,0,-1)
    end if
end on
```


----------



## EvilDragon (Jul 11, 2015)

You should use select-case instead of endless stream of if clauses. Also, you should utilize async callback because changing FX via script is not a realtime operation. Also, your script assumes that you only have one single group in the instrument, and that a filter is loaded into slot 1 of group FX. You should actually load a filter in that slot, just to be certain that a filter would be loaded - because when you run this script on an EMPTY new instrument, it won't load any filters at all!


And yes, you can shorten this quite a bit. Like so:


```
on init
   declare $asyncID := -1
   declare %filter_type[6] := ($FILTER_TYPE_LDR_LP1,$FILTER_TYPE_LDR_LP2,$FILTER_TYPE_LDR_LP3,$FILTER_TYPE_LDR_LP4,$FILTER_TYPE_DAFT_LP,$FILTER_TYPE_PRO52)

  declare ui_menu $Type
  add_menu_item($Type,"Ladder LP1",0)
  add_menu_item($Type,"Ladder LP2",1)
  add_menu_item($Type,"Ladder LP3",2)
  add_menu_item($Type,"Ladder LP4",3)
  add_menu_item($Type,"Daft LP",4)
  add_menu_item($Type,"Pro 52",5)

  make_persistent($Type)

  message("")
end on

on async_complete
   if ($asyncID # -1)
  $asyncID := -1
  end if
end on

on ui_control ($Type)
  $asyncID := set_engine_par($ENGINE_PAR_EFFECT_TYPE,$EFFECT_TYPE_FILTER,0,0,-1)
  $asyncID := set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE,%filter_type[$Type],0,0,-1)
end on
```


The important part is to query the state of $asyncID variable within the callbacks of cutoff and resonance controls. They shouldn't do anything when $async # -1. Like so:


```
on ui_control ($Cutoff)
    if ($asyncID = -1)
        $Cutoff := $prev_cut_value
    else
        set_engine_par($ENGINE_PAR_CUTOFF,$Cutoff,0,0,-1)
        set_knob_label($Cutoff,get_engine_par_disp($ENGINE_PAR_CUTOFF,0,0,-1))
        $prev_cut_value := $Cutoff
    end if
end on
```


----------



## TheMaschineWarehouse (Jul 11, 2015)

Thank you EvilDragon that helped out a lot


----------



## Lindon (Jul 16, 2015)

I'm having a LOT of problems with this code:

$fx_async_id :=set_engine_par($ENGINE_PAR_EFFECT_TYPE,$EFFECT_TYPE_FILTER,0,0,-1)

Sublime text keeps telling me:

"Expected expression of integer type, got None."

Am I missing some sublime config here? or is anyone else having this ?


----------



## EvilDragon (Jul 16, 2015)

Disable Extra syntax checks, try again.


If you need to use Extra syntax checks (for math lib, for example), you can fix this manually.

In ksp_plugin.py, go to around line 220, you will see a comment "for a certain set of functions with return values one often discards the return value add them to the second set too". Add '_set_engine_par' and 'set_engine_par' there (for func in [...]).

Then, in ksp_builtins_data, find _set_engine_par and set_engine_par declarations there, and append :integer to the end of both those lines.

Exit Sublime and reopen it. Try compiling with Extra syntax checks, should work.


----------



## Lindon (Jul 16, 2015)

OK, yep that got it... thanks Mario.


----------



## Linn134 (Sep 30, 2021)

Yo.

Bumping this topic.
I'm in need of a filter selector via a 6 pos slider (not a menu) and I try to get it work. But I failed.
Now testing with the list above.
Anyone can tell me where I'm wrong?


```
on init

    make_perfview
  
    set_ui_height_px(750)
    set_ui_width_px(1000)
    set_control_par_str($INST_WALLPAPER_ID,$CONTROL_PAR_PICTURE,"wallpaper")

    declare $asyncID := -1
    declare %filter_type[6] := ($FILTER_TYPE_LDR_LP1,$FILTER_TYPE_LDR_LP2,$FILTER_TYPE_LDR_LP3,$FILTER_TYPE_LDR_LP4,$FILTER_TYPE_DAFT_LP,$FILTER_TYPE_PRO52)

            { TOP SELECTOR : $FILTERTYPE }
                      declare ui_slider $FILTERTYPE(0, 1000000)
                       set_control_par(get_ui_id($FILTERTYPE),$CONTROL_PAR_WIDTH,152)
                       set_control_par(get_ui_id($FILTERTYPE),$CONTROL_PAR_HEIGHT,40)
                       set_control_par_str(get_ui_id($FILTERTYPE),  $CONTROL_PAR_PICTURE, "ZYNT")
                       set_control_par(get_ui_id($FILTERTYPE),$CONTROL_PAR_MOUSE_BEHAVIOUR,1000)
                            move_control_px($FILTERTYPE,424,83)
                            make_persistent($FILTERTYPE)

            { FILTER MACRO : $FILTER }
                        declare ui_slider $FILTER(0, 1000000)
                            set_control_par(get_ui_id($FILTER),$CONTROL_PAR_WIDTH,64)
                            set_control_par(get_ui_id($FILTER),$CONTROL_PAR_HEIGHT,64)
                            set_control_par_str(get_ui_id($FILTER),  $CONTROL_PAR_PICTURE, "BASIC_64")
                            set_control_par(get_ui_id($FILTER),$CONTROL_PAR_MOUSE_BEHAVIOUR,-500)
                            move_control_px($FILTER,278,532)
                            make_persistent($FILTER)

end on


                    on async_complete
                        if ($asyncID # -1)
                        $asyncID := -1
end if

end on

                    on ui_control ($FILTERTYPE)
                            $asyncID := set_engine_par($ENGINE_PAR_EFFECT_TYPE,$EFFECT_TYPE_FILTER,-1,0,0)
                            $asyncID := set_engine_par($ENGINE_PAR_EFFECT_SUBTYPE,%filter_type[$FILTERTYPE],-1,0,0)

end on

            { FILTER MACRO : $FILTER }
                        on ui_control ($FILTER)
                        set_engine_par($ENGINE_PAR_CUTOFF,$FILTER,-1,0,0)

          
end on
```

What I get :
1- No filter change
2- Cutof only working while in first position


----------



## geronimo (Sep 30, 2021)

I think I have the beginnings of an explanation; everything resides in the init of your $FILTERTYPE slider. It's simple and get in the habit, please use code tags to make it more readable. This subject will thus travel correctly in time. 


```
declare ui_slider $FILTERTYPE(0, 5)
```


----------



## Linn134 (Sep 30, 2021)

Ow. Wow.
I'm dumb.  
TY Geronimo.


----------



## geronimo (Sep 30, 2021)

Oh no; just an increase in experience _


----------

