View Single Post
Old 02-04-2014, 07:02 PM   #68
Human being with feelings
Join Date: Nov 2010
Posts: 1,809

Originally Posted by heda View Post
The automatic paste into an area without tempo markers preserving time would be great!
Here's a quick python script that does exactly that before I create it for real in SWS. A bit slow for big items, but should work.
Also note that any text events aren't preserved. On, and you need the latest reaper pre!
from reaper_python import *

createdItems = []
editCursor = RPR_GetCursorPosition()

def GetNewPpq (ppq, oldTake, newTake):
	oldStart = RPR_GetMediaItemInfo_Value(RPR_GetMediaItemTake_Item(oldTake), "D_POSITION")
	newPos = RPR_MIDI_GetProjTimeFromPPQPos(currentTake, ppq) - oldStart + editCursor
	return RPR_MIDI_GetPPQPosFromProjTime(newTake, newPos)

if (RPR_CountSelectedMediaItems(0)):


	for i in range(RPR_CountSelectedMediaItems(0)):
		currentItem = RPR_GetSelectedMediaItem(0, i)
		currentTake = RPR_GetActiveTake(currentItem)

		midiEvents = RPR_MIDI_CountEvts(currentTake, 0, 0, 0)
		if midiEvents[0] == 0:

		endtime = RPR_GetCursorPosition() + RPR_GetMediaItemInfo_Value(currentItem, "D_LENGTH")
		newItem = RPR_CreateNewMIDIItemInProj(RPR_GetMediaItem_Track(currentItem), RPR_GetCursorPosition(), endtime)
		newTake = RPR_GetActiveTake(newItem)

		for i in range(midiEvents[2]):
			note = RPR_MIDI_GetNote(currentTake, i, 0, 0, 0, 0, 0, 0, 0)
			RPR_MIDI_InsertNote(newTake, note[3], note[4], GetNewPpq(note[5], currentTake, newTake), GetNewPpq(note[6], currentTake, newTake), note[7], note[8], note[9])
		for i in range(midiEvents[3]):
			cc = RPR_MIDI_GetCC(currentTake, i, 0, 0, 0, 0, 0, 0, 0)
			RPR_MIDI_InsertCC(newTake, cc[3], cc[4], GetNewPpq(cc[5], currentTake, newTake), cc[6], cc[7], cc[8], cc[9])
		for i in range(midiEvents[4]):
			sys = RPR_MIDI_GetTextSysexEvt(currentTake, i, 0, 0, 0, 0, 0, 0)
			RPR_MIDI_InsertTextSysexEvt(newTake, sys[3], sys[4], GetNewPpq(sys[5], currentTake, newTake), sys[6], sys[7], sys[8])

	if (len(createdItems)):
		RPR_Main_OnCommand(40289, 0)	# unselect all items
		for i in range(len(createdItems)):
				RPR_SetMediaItemInfo_Value(createdItems[i], "B_UISEL", 1)

	RPR_Undo_EndBlock2(0,"Paste selected MIDI items at edit cursor with preserved relative time position of events",-1)

Last edited by Breeder; 02-04-2014 at 07:10 PM.
Breeder is offline   Reply With Quote