View Single Post
Old 11-30-2018, 06:33 PM   #1748
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 8,957
Default

Quote:
Originally Posted by Swi View Post
I am looking for a script that will set the note velocity for all the notes inside the selected midi item. what I want is a script I can alter so I can set the velocities to 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 121, 127. I have a script that does this in the midi editor but I want to do this without opening the midi editor and selecting the notes. Very useful for bass guitar and some drum stuff when you need it to sound like Sterile Daryl.
Lua script:
Code:
-- Start off with a little 'trick' to prevent REAPER from automatically
--    creating an undo point if no changes are made.
function preventUndo()
end
reaper.defer(preventUndo)

-- Check whether the required version of REAPER is available
if not reaper.APIExists("MIDI_GetAllEvts") then
    reaper.ShowMessageBox("This script requires REAPER v5.30 or higher.", "ERROR", 0)
    return(false) 
end

targetVelocity = 100

function SetVelocityOfSelectedMIDIItems()
    
  -- First, loop through all selected items
  for i = 0, reaper.CountSelectedMediaItems(0)-1 do
      item = reaper.GetSelectedMediaItem(0, i)
      
      -- Loop through all takes within each selected item
      for t = 0, reaper.CountTakes(item)-1 do
          take = reaper.GetTake(item, t)
          if reaper.TakeIsMIDI(take) then
          
            gotAllOK, MIDIstring = reaper.MIDI_GetAllEvts(take, "")
            
            MIDIlen = MIDIstring:len()  
            
            -- As the MIDI string is parsed one-by-one, all events will be
            --    stored in this table while awaiting re-concatenation.
            tableEvents = {}
            
            stringPos = 1 -- Position in MIDIstring while parsing through events.
              
            -- Iterate through all events one-by-one   
            while stringPos < MIDIlen do
               
                offset, flags, msg, stringPos = string.unpack("i4Bs4", MIDIstring, stringPos)        
                if msg:len() == 3
                and msg:byte(1)>>4 == 9 -- Note-on MIDI event type
                and not (msg:byte(3) == 0) -- Remember to exclude note-offs
                then
                    msg = msg:sub(1,2) .. string.char(targetVelocity) -- Replace velocity
                end
                
                table.insert(tableEvents, string.pack("i4Bs4", offset, flags, msg))
            end
            
            -- Upload the edited MIDI into the take
            reaper.MIDI_SetAllEvts(take, table.concat(tableEvents))
          end -- if reaper.TakeIsMIDI(take)
      end
  end

    reaper.Undo_OnStateChange("Script: Set velocity of selected MIDI items")
end


SetVelocityOfSelectedMIDIItems()
You can change targetVelocity to suit your needs.
Credit for most practically everything of this script goes to juliansader.
https://forum.cockos.com/showpost.ph...76&postcount=6

Last edited by nofish; 11-30-2018 at 07:16 PM.
nofish is offline   Reply With Quote