So I was putting together a quick script for a thread in General Discussion, but noticed that setting the end position of a note to the start position of the next note of equal pitch sometimes results in a note length of zero. I can subtract one PPQ from the length of course, but if we draw adjoining notes with the MIDI editor, the List View displays the start and end being equal.
If you run this on a selected note a couple of times you should see the issue...
Code:
--get the take active in the MIDI editor
local take=reaper.MIDIEditor_GetTake(reaper.MIDIEditor_GetActive())
local cnt=0 --counter for notes, we access notes by their index number
local notes_to_insert={} -- create a table of notes we want to insert
-- because inserting whilst getting them
-- messes with getting them all in the right order
--try and get the first note, ok=true if we get one
local ok, selected, muted, sppq, eppq, chan, pitch, vel=reaper.MIDI_GetNote(take, 0)
--this bit only runs if we have a note (ok = true)
while ok do
if selected then -- only do this if the note we just got is selected
length=eppq-sppq -- end position (in ppq) minus start = length
new_length=math.floor(length/2) -- always round after dividing
-- for stuff that needs a whole number
eppq=sppq+new_length
reaper.MIDI_SetNote(take,cnt,selected,muted,sppq,eppq,chan,pitch,vel,false) --false=don't sort
-- we want to insert a new note of the same length at the end of the one
-- we just halved, so we'll store the details to insert later
notes_to_insert[#notes_to_insert+1]={
take=take,selected=selected,muted=muted,sppq=eppq,
eppq=eppq+new_length, chan=chan, pitch=pitch, vel=vel}
end
-- increase the counter and try to get the next note, this
-- "while loop" we are in will repeat if we get one
cnt=cnt+1
ok, selected, muted, sppq, eppq, chan, pitch, vel=reaper.MIDI_GetNote(take, cnt)
end
-- now we go through the table of new notes we stored, inserting them into take
for i=1,#notes_to_insert,1 do
local n=notes_to_insert[i]
reaper.MIDI_InsertNote(n.take,n.selected,n.muted,n.sppq,n.eppq,n.chan,n.pitch,n.vel,false)
end
reaper.MIDI_Sort(take) --make sure everything is back in order