in reaper daw lua create me a script that duplicates the selected midi notes to the…
- User
in reaper daw lua create me a script that duplicates the selected midi notes to the front of the note that lasts longer of the selected
- Assistant
- User
do you know ableton midi note duplication? i want a reaper script that does the same
- Assistant
- User
create me the script where -- copies the selected midi notes and their relative positions -- pastes them in the end of the last note in the selection, where the note with the earliest start of the selection starts where the last note of the selection ends -- if there are notes underneath the new copied notes (with the same pitch), the new ones overlap them -- the copied notes are selected
- Assistant
- User
give me only the script without comments
- Assistant
Sure! Here's the script without comments:
function Msg(str) reaper.ShowConsoleMsg(tostring(str) .. "\n") end editor = reaper.MIDIEditor_GetActive() if not editor then return end take = reaper.MIDIEditor_GetTake(editor) if not take then return end retval, notes, ccevtcnt, textsyxevtcnt = reaper.MIDI_CountEvts(take) if notes == 0 then return end reaper.Undo_BeginBlock() local selectedNotes = {} local earliestStart = math.huge local latestEnd = 0 for i = 0, notes - 1 do local retval, selected, muted, startppqpos, endppqpos, chan, pitch, vel = reaper.MIDI_GetNote(take, i) if selected then table.insert(selectedNotes, {startppqpos = startppqpos, endppqpos = endppqpos, chan = chan, pitch = pitch, vel = vel}) if startppqpos < earliestStart then earliestStart = startppqpos end if endppqpos > latestEnd then latestEnd = endppqpos end end end local offset = latestEnd - earliestStart reaper.MIDI_SelectAll(take, false) for _, note in ipairs(selectedNotes) do local newStart = note.startppqpos + offset local newEnd = note.endppqpos + offset reaper.MIDI_InsertNote(take, true, false, newStart, newEnd, note.chan, note.pitch, note.vel, false) end reaper.MIDI_Sort(take) reaper.UpdateArrange() local newNoteStartIndex = notes for i = newNoteStartIndex, newNoteStartIndex + #selectedNotes - 1 do reaper.MIDI_SetNote(take, i, true, nil, nil, nil, nil, nil, nil, false) end reaper.Undo_EndBlock("Duplicate selected MIDI notes at the end of the last note", -1)