Lua in Ctrlr – list of frequently used commands

Home Forums General Programming Lua in Ctrlr – list of frequently used commands

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #5217
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    I sometimes take breaks from working on panels etc and tend to forget some of the basics when digging back in to where I left off. I’ve kept this list of frequently used scripts and figured what the heck, I’ll share with everyone as it might be really helpful for people besides just myself. I don’t know why but some of these tend to slip my memory so it helps to just reference my list of…

    COMMONLY USED LUA SCRIPTS
    —————————————————————————————————————

    get value of a modulator, assign it to a variable:
    variableName = panel:getModulatorByName(“Modulator Name”):getModulatorValue()

    —————————————————————————————————————
    get a modulator and assign a value to it:
    panel:getModulatorByName(“ModulatorName”):setModulatorValue((2), false, true, false)
    panel:getModulatorByName(“ModulatorName”):setModulatorValue((variableName), false, true, false)

    —————————————————————————————————————
    print some message in the console:
    console (“this is my message that will appear in the console…”)

    —————————————————————————————————————
    print the value of something in the console:
    console (string.format (“this is a number %d”, number ))

    —————————————————————————————————————
    Print value to console:
    a = 10
    console(“a = “..a)

    —————————————————————————————————————
    call some other script to run:

    scriptNameToRun()

    —————————————————————————————————————
    display the value of a variable on a uiLabel
    label = panel:getComponent(“label”)
    if label ~= nil then
    label:setPropertyString (“uiLabelText”, “text”)
    end

    or by using the special component method

    label:setComponentText (string.format(“my text”))

    —————————————————————————————————————
    Write variables to string:
    a = “a”
    b = 10
    c = string.format(“%s%s”,a,b)
    console(“c = “..c)

    ——————————————–

    ——————————————–

     

    Those are some of the basics that I tend to frequently use and for some reason tend to frequently forget the exact syntax so sometimes a cheat-sheet like this helps. Hopefully it helps you, I’ll probably add some stuff here later, if others want to add some too that’s cool.

    Happy Holidays 🙂

    • This topic was modified 9 years, 5 months ago by msepsis.

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #5236
    Hecticcc
    Participant
    • Topics: 25
    • Replies: 160
    • Total: 185
    • ★★

    Good idea!

    I am using alot of sysex and i use these all the time:

     

    Insert values into a hexstring:

     

    val1 = 1
    val2 = 10
    hexstring = string.format(“f0 18 21 %.2x  55 01 02 5f 01 %.2x 00 6b f7″, val1, val2)

    produces:
    f0 18 21 01 55 01 02 5f 01 0a 00 6b f7

     

    Of course you can also get values from modulators and use those

    val = panel:getModulatorByName(“myModulator”):getModulatorValue()

    _______________________________________________________________________

     

    Grabbing values from an incoming hexstring

     

    Suppose this is the string you received:

    f0 18 21 01 55 01 02 5f 01 0a 00 6b f7

     

    Now to extract the byte with value 55:

    extractedByte = midi:getLuaData():getByte(4)

     

    Notice that although 01 is on the 5th place it is infact the 4th byte, as ctrlr starts counting AFTER the header “f0” .

     

    You can also extract a range of bytes:

     

    rangeOfBytes = midi:getLuaData():getRange(3, 7)

    this will store the values “01 55 01 02 5f” in rangeOfBytes

     

    _____________________________________________________________________

     

    To check the results of these operations in console you need to do some conversion or you get errors like “sting expected, received number”:

    string1 = string.format(someHexString)
    console(string.format(string1))

     

    _______________________________________________________________________

     

    Will add more in time

    #5634
    Bomma72
    Participant
    • Topics: 20
    • Replies: 68
    • Total: 88

    Thought I would add one —

    panel:sendMidiMessageNow(CtrlrMidiMessage(“f0 11 63  7d 3d 00 00 00 f7”))

    Sends message to Synth

    #5640
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Tehre is a lot of useful shortcuts, handy lua tricks in the latest DEMO panel

    #7185
    zeoka
    Participant
    • Topics: 73
    • Replies: 466
    • Total: 539
    • ★★★
    • This reply was modified 9 years ago by zeoka.
    #14112
    lfo2vco
    Participant
    • Topics: 26
    • Replies: 162
    • Total: 188
    • ★★

    OK, I have a couple of useful functions that can be applied to strings containing patch names for instance.
    —————————————————————————————————————
    The following Function extracts a piece of the string from characters 1 to 16

    function edit (s)
    	return (string.sub(s,1,16))
    end
    
    nameEdit = (edit(myString))

    —————————————————————————————————————
    The following Function removes whitespace (spaces) from either end of a string

    function trim (s)
    	return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
    end
    
    nameTrim = (trim(myString))

    —————————————————————————————————————

    Hope these prove useful to others.

    Here is some noise I organised into an acceptable format:
    https://soundcloud.com/lfo2vco/a-dark-crystal

    #23678
    zeoka
    Participant
    • Topics: 73
    • Replies: 466
    • Total: 539
    • ★★★

    Hi
    add this example : a shared textbox parameter name/value linked to one/several
    mods + a default color for default value (here 64)

    --
    -- Called when a modulator value changes
    -- @mod   http://ctrlr.org/api/class_ctrlr_modulator.html
    -- @value    new numeric value of the modulator
    --
    Slidval64 = function(mod, newValue)
    sl64 = mod:getComponent()
    function ValueCallback(timerId)
    if timer ~= nil then
    timer:stopTimer(40)
    end
    panel:getComponent("my label text"):setText(mod:getModulatorName())
    end
    panel:getComponent("my label text"):setText(sl64:getComponentText())
    if newValue ~= 64 then
    sl64:setProperty("uiSliderThumbColour",col1:toString(),false) 
    sl64:setProperty("uiSliderTrackColour",col2:toString(),false)
    sl64:setProperty("uiSliderValueTextColour",col5:toString(),false)
    else
    sl64:setProperty("uiSliderThumbColour",col3:toString(),false) 
    sl64:setProperty("uiSliderTrackColour",col4:toString(),false)
    sl64:setProperty("uiSliderValueTextColour",col6:toString(),false)
    end
           timer:setCallback (40,ValueCallback)
           timer:startTimer(40,500)
    end

    colors are defined elsewhere(Called when the panel is created).ex col4 = Colour(0x5dc68e6c)

    It is possible to share parameter name/value directly on the modulator too : just use the adequate properties

    #35285
    m.tarenskeen
    Participant
    • Topics: 30
    • Replies: 113
    • Total: 143
    • ★★

    This old thread is still very useful – but hard to find – for starting panel designers like me. Unfortunately I found these posts *after* I already found out how things like this work 🙂

    Maybe a “Lua in Ctrlr – list of frequently used commands” page can be added to the “Documentation” section of this website.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Programming’ is closed to new topics and replies.
There is currently 0 users and 70 guests online
No users are currently active
Forum Statistics
Threads: 2,495, Posts: 17,374, Members: 77,605
Most users ever online was 12 on January 22, 2019 3:47 pm
Ctrlr