Kontakt KSP and objects

lidoravitan
lidoravitan Member Posts: 12 Newcomer
edited February 2022 in Scripting Workshop

I've found only arrays. however, to reduce the complexity of my code (searching) to o(1) I need to use dictionary data structure which also known as Associative_array (objects or dictionary).

in simple words i'm looking for a way to declare arrays with string as key, like:

%arr["key"] := value 

message(%arr["key"])

Is it possible to define / declare objects with ksp?

Comments

  • Reid115
    Reid115 Member Posts: 47 Member
    edited February 2022

    No. But, in any scenario I've wished I had dicts/maps, there's always a relatively elegant workaround using arrays (and without search() -- I think that function is pretty slow). It would help to know what specifically you're doing. I can't imagine o(n) would be a huge problem vs. o(1).

  • EvilDragon
    EvilDragon Moderator Posts: 947 mod

    KSP is not an object-oriented language, and all the data types that it has are in KSP reference: integers, reals, strings, integer arrays, real arrays, string arrays. That's it!

  • lidoravitan
    lidoravitan Member Posts: 12 Newcomer
    edited February 2022

    @Reid115 thanks for your response I built a chord recognition script.

    I collect all the held notes i.e [-1,0,4,9], now I have to search what chord match for this pattern.

    to find the chord, we are talking about array of 12*27=1524 (12-chords/scales [c,c#,d,d#,e...] * 27 types of chords [maj,min,m7,maj7,dim...])

    does it make sense to go over 1524 elements each time? when with map/dic it'd be o(1)

    @EvilDragon This's why KSP is so limited for dump scripts! Why not use Lua or Javascript :/


    @Reid115 i fond that I can "hash" the patterns(i.e [-1,0,4,9]) to be an int (i.e 2427) with some math calc and then create an array size 10,000. and then hold the pattern in the hash results like

    declare %arr[10000]; %arr[2427] := "Cmaj";

    With this approach I can achieve o (1) search operation, but the downside is that I have an array of size 10,000.

    I'm not sure which better array of 10,000 with o(1) operations or search in o(n) (in my use case it would be 1524 elements), wdyt?

  • EvilDragon
    EvilDragon Moderator Posts: 947 mod
    edited February 2022

    Because Lua (especially LuaJIT) didn't exist back in 2005 when KSP was introduced, and JS was definitely not good for realtime audio thread performance back then. Then everything snowballed and nobody wanted to introduce more friction and risk by completely changing the language that Kontakt uses.


    As for your last question... it's as always, trading memory vs CPU. You spend more memory in order to save CPU by not having to do search(). (By the way, search() is just basic linear search, nothing fancy.) I would personally commit to that tradeoff, if the goal is to be as efficient as possible at runtime.

  • lidoravitan
    lidoravitan Member Posts: 12 Newcomer

    @EvilDragon thanks for your response, I agree with you. IMO, It is worthless to argue in what language they'd already decided to use KSP. however, Kontakt and Ksp is still in development, so why don't add objects to the language?

    anyway, of course that at the end all comes to CPU vs memory.

    how you decided to say that the search is better than a large array(size 10,000)? how much bytes you take for this array? in terms of computers with 16gb+ of ram it could be very tiny. while search is linear which means it could run over 1524 item every chord change.

    i'm not saying that i wouldn't go with search function. but i'd like to hear why you said: "I would personally commit to that tradeoff, if the goal is to be as efficient as possible at runtime."

    again, wanna say thanks :)

  • lidoravitan
    lidoravitan Member Posts: 12 Newcomer

    @EvilDragon by the way if I use "Instrument Bank" and i move between banks/instruments

    when the "on_init" executed? each instrument bank change? or only on the first loading?

  • EvilDragon
    EvilDragon Moderator Posts: 947 mod
    edited February 2022

    "on init" is executed on initial load and audio engine reset (that "!" button in top right).

    By the way, I did not say I would choose search() over the large array. I would go with O(1) myself, too. When I said I'd commit to that tradeoff, I meant "trading memory usage to save CPU at runtime".


    Adding objects to KSP is very likely never happening.

Back To Top