View Single Post
Old 11-16-2019, 09:37 PM   #4
Human being with feelings
Lokasenna's Avatar
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,536

I've just uploaded Lokasenna_Invert selected CC event values to ReaPack; should be available in a few minutes.

As I suggested, it's a lot more straightforward to do when we don't have to fiddle with native actions. Here's the code with some extra comments:
local editor = reaper.MIDIEditor_GetActive()
if not editor then return end

local take = reaper.MIDIEditor_GetTake(editor)
if not take then return end


-- This loop structure keeps asking Reaper for the next selected
-- CC event until Reaper says there aren't any more.
local ccIdx = -1
while true do
  ccIdx = reaper.MIDI_EnumSelCC(take, ccIdx)
  if ccIdx == -1 then break end

  -- msg3 is the CC value
  local _, _, _, _, _, _, _, msg3 = reaper.MIDI_GetCC(take, ccIdx)

  reaper.MIDI_SetCC(take, ccIdx, nil, nil, nil, nil, nil, nil, 127 - msg3, true)

-- For performance reasons, it's better to tell Reaper not to do
-- any sorting while we loop through the events and then sort them
-- all at once afterward

-- Hack to make Reaper recognize that the item was altered so we
-- get a correct Undo point
local item = reaper.GetMediaItemTake_Item(take)
reaper.SetMediaItemSelected(item, false)
reaper.SetMediaItemSelected(item, true)

reaper.Undo_EndBlock("Invert selected CC event values", 4)
Lokasenna is online now   Reply With Quote