Script to delay a specific CC?

Hi Brian,

You can use the wait() function to delay midi events.

You can delay a specific cc like this:

-- delay midi cc28 events by 250 ms
function onController(event)
  if event.controller == 28 then
    wait(250)
    postEvent(event)
  else
    postEvent(event)
  end
end

You can do the same for note on messages:

-- delay a specific note by 100 ms (midi note 60)
function onNote(event)
  if event.note == 60 then
    wait(100)
    postEvent(event)
  else
    postEvent(event)
  end
end

If you want more control about the delay time without having to change the script each time you can create a delay parameter:

defineParameter("delay", nil, 100, 0, 1000) -- delay in ms, numbers are default, min, max

-- delay each note
function onNote(event)
  wait(delay)
  postEvent(event)
end

And a few more experimental ideas:

-- wait for next note on event if specified cc event has a value bigger then 64
defineSlotLocal("CCValue")
CCValue = false
cc = 28 -- change this to match the desired cc number

defineParameter("delay", nil, 100, 0, 1000) -- delay time - default 100 ms

function onController(event)
  if event.controller == cc and event.value > 64 then
    CCValue = event.value
  else
    postEvent(event)
  end
end

function onNote(event)
  postEvent(event)
  if CCValue then
    local ccvalue = CCValue
    wait(delay)
    controlChange(cc, ccvalue)
    CCValue = false
  end
end



-- delay cc 28 if no note is being played
defineSlotLocal("anyNotePressed")
anyNotePressed = 0

function onNote(event)
  anyNotePressed = anyNotePressed + 1
  postEvent(event)  
end

function onRelease(event)
  anyNotePressed = math.max(anyNotePressed - 1, 0)
  postEvent(event)
end

function onController(event)
  if event.controller == 28 and anyNotePressed == 0 then
    wait(100)
    postEvent(event)
  else
    postEvent(event)
  end
end

Edit:
But I’m not sure if any of this would help you. Just tried in Dorico and it doesn’t seem to work with keyswitches.