Home Forums General Programming A little help please

This topic contains 6 replies, has 4 voices, and was last updated by goodweather goodweather 1 month ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #73197

    Anton
    Participant

    Hey guys,

    I have the following code inserted in the “Called when the modulator value changes” method of a uiListBox. It sends four Sysex messeges per list item that is clicked.

    
    My_Knobs_1 = function(value)
    
    list = panel:getModulatorByName("list"):getModulatorValue()
    
    cc = -1
    
    if list == 0 then
    	for k = 40,61,7 do
    	string.format('%x', k)
    	cc = cc + 1
    m1 = CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, cc, 0xf7}) 
    panel:sendMidiMessageNow(m1)
    
    end
    
    elseif list == 1 then
    	cc = 3
    	for k = 40,61,7 do
    	string.format('%x', k)
    	cc = cc + 1
    m1 = CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, cc, 0xf7}) 
    panel:sendMidiMessageNow(m1)
    
    end
    
    elseif list == 2 then
    	cc = 7
    	for k = 40,61,7 do
    	string.format('%x', k)
    	cc = cc + 1
    m1 = CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, cc, 0xf7}) 
    panel:sendMidiMessageNow(m1)
    
    end
    end
    end
    

    What I’m wondering…, is there a way to do this that doesn’t require a bunch of “if” statements? The actual list contains 32
    items, but I’ve shortened the code for posting here.

    When item one is clicked it sends:
    F0 47 00 72 31 00 04 01 03 28 00 F7
    F0 47 00 72 31 00 04 01 03 2F 01 F7
    F0 47 00 72 31 00 04 01 03 36 02 F7
    F0 47 00 72 31 00 04 01 03 3D 03 F7

    When item two is clicked it sends:
    F0 47 00 72 31 00 04 01 03 28 04 F7
    F0 47 00 72 31 00 04 01 03 2F 05 F7
    F0 47 00 72 31 00 04 01 03 36 06 F7
    F0 47 00 72 31 00 04 01 03 3D 07 F7

    etc…

    The code works, it’s just I’d rather not use so many “ifs”, if possible?

    Any help is greatly appreciated.

    Thanks

    • This topic was modified 1 month, 1 week ago by  Anton.
    #73199

    human fly
    Participant

    if it works? …
    … why worry? 😀
    looks ok.

    you could have some table if refers to(?)
    but it might not end up any shorter.

    #73231
    dnaldoog
    dnaldoog
    Participant

    Hi there Anton,

    If ‘list’ can have values ranging from 0-31, then I think this would work:

    
    My_Knobs_1 = function(value)
    list = panel:getModulatorByName("list"):getModulatorValue()
    local multiplier = list * 4
    local loopEnd = multiplier + 3
    k=40
    for i=multiplier,loopEnd  do
    m1 = panel:sendMidiMessageNow(CtrlrMidiMessage({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, k, i, 0xf7}))
    k=k+7
    end
    

    Note: in the attached panel I mistakenly left in the redundant i=i+2

    Regards,

    Attachments:
    You must be logged in to view attached files.
    #73234
    goodweather
    goodweather
    Participant

    Hi, saw that question the other day and also wanted to answer it.
    Here is a simpler code I think…

    --
    -- Called when a modulator value changes
    -- @mod   http://ctrlr.org/api/class_ctrlr_modulator.html
    -- @value    new numeric value of the modulator
    --
    Combo_OnChange = function(--[[ CtrlrModulator --]] mod, --[[ number --]] value, --[[ number --]] source)
    
    	lblSysex = panel:getModulatorByName("lblSysex")
    	modValues = panel:getModulatorByName("cbValues")
    
    	sText =""
    	for i=0,3 do
    		mbTemp= MemoryBlock ({0xf0, 0x47, 0x00, 0x72, 0x31, 0x00, 0x04, 0x01, 0x03, 40+i*7, value*4+i, 0xf7})
    		panel:sendMidiMessageNow (CtrlrMidiMessage(mbTemp))
    		sText = sText..mbTemp:toHexString(1).."\n"
    	end
    	lblSysex:getComponent():setPropertyString("uiLabelText", sText)
    
    end

    and the panel from dnaldoog adapted 🙂

    Attachments:
    You must be logged in to view attached files.
    #73237
    dnaldoog
    dnaldoog
    Participant

    Wow – that’s better and even more concise!

    #73240

    Anton
    Participant

    @ dnaldoog & goodweather,

    Both scripts work as expected, and I learned a thing or two. 😀

    Thank you both, very much appreciated!

    #73275
    goodweather
    goodweather
    Participant

    cool 😉

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Comments are closed.