144 lines
5.0 KiB
Lua
144 lines
5.0 KiB
Lua
local stored_cursorPos = 0
|
|
local stored_timeSelStart = 0
|
|
local stored_timeSelEnd = 0
|
|
local stored_sel_tracks = {}
|
|
local stored_sel_items = {}
|
|
local mark_srcin_pos = -1
|
|
local mark_srcin_id = -1
|
|
local mark_srcout_pos = -1
|
|
local mark_srcout_id = -1
|
|
local mark_dstin_pos = -1
|
|
local mark_dstin_id = -1
|
|
local mark_dstout_pos = -1
|
|
local mark_dstout_id = -1
|
|
|
|
-- Send a message to the console
|
|
function msg(m)
|
|
reaper.ShowConsoleMsg(tostring(m) .. "\n")
|
|
end
|
|
|
|
function select_src_tracks()
|
|
local tracks_str = ''
|
|
local tracks = {}
|
|
local retval
|
|
|
|
retval, tracks_str = reaper.GetProjExtState(0, '4PointCut', 'src_tracks')
|
|
if retval > 0 then -- variable exists in rpp
|
|
-- separate GUIDs and populate track table
|
|
for str in string.gmatch(tracks_str, "([^"..'{'.."]+)") do
|
|
table.insert(tracks, reaper.BR_GetMediaTrackByGUID(0, '{' .. str))
|
|
end
|
|
-- select tracks
|
|
--reaper.Main_OnCommand(40297, 0) -- Unselect all tracks
|
|
for _, track in ipairs(tracks) do
|
|
reaper.SetTrackSelected(track, true)
|
|
end
|
|
else
|
|
-- default to all tracks
|
|
for i = 0, reaper.CountTracks(0)-1 do
|
|
reaper.SetTrackSelected(reaper.GetTrack(0, i), true)
|
|
end
|
|
end
|
|
end
|
|
|
|
function select_dst_track_only()
|
|
local track_str = ''
|
|
local retval
|
|
retval, track_str = reaper.GetProjExtState(0, '4PointCut', 'dst_track')
|
|
if retval > 0 then -- variable exists in rpp
|
|
reaper.SetOnlyTrackSelected(reaper.BR_GetMediaTrackByGUID(0, track_str))
|
|
else --default to first track
|
|
reaper.SetOnlyTrackSelected(reaper.GetTrack(0, 0))
|
|
end
|
|
end
|
|
|
|
-- START HERE vvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
-- Store cursor position, time selection, selected tracks, selected items
|
|
stored_cursorPos = reaper.GetCursorPosition()
|
|
stored_timeSelStart, stored_timeSelEnd = reaper.GetSet_LoopTimeRange(false, true, 0, 1, false)
|
|
for i = 0, reaper.CountSelectedTracks(0)-1 do
|
|
stored_sel_tracks[i+1] = reaper.GetSelectedTrack(0, i)
|
|
end
|
|
for i = 0, reaper.CountSelectedMediaItems(0)-1 do
|
|
stored_sel_items[i+1] = reaper.GetSelectedMediaItem(0, i)
|
|
end
|
|
if reaper.CountSelectedMediaItems(0) > 0 then
|
|
reaper.Main_OnCommand(40289, 0) -- Unselect all items
|
|
end
|
|
if reaper.CountSelectedTracks(0) > 0then
|
|
reaper.Main_OnCommand(40297, 0) -- Unselect all tracks
|
|
end
|
|
--reaper.Main_OnCommand(40769, 0)
|
|
select_src_tracks() -- select tracks specified in rpp (by using 4Pcut_set_src-tracks.lua)
|
|
|
|
-- run through all markers and get position of the 4 points
|
|
local nof_markers = 0
|
|
_, nof_markers, _ = reaper.CountProjectMarkers(0)
|
|
for i = 0, nof_markers - 1 do
|
|
local name = ''
|
|
local pos = 0
|
|
local id = 0
|
|
local isregion = false
|
|
_, isregion, pos, _, name, id = reaper.EnumProjectMarkers(i)
|
|
if not isregion then
|
|
if name == 'SRC-IN_4Pcut' then
|
|
mark_srcin_pos = pos
|
|
mark_srcin_id = id
|
|
elseif name == 'SRC-OUT_4Pcut' then
|
|
mark_srcout_pos = pos
|
|
mark_srcout_id = id
|
|
elseif name == 'DST-IN_4Pcut' then
|
|
mark_dstin_pos = pos
|
|
mark_dstin_id = id
|
|
elseif name == 'DST-OUT_4Pcut' then
|
|
mark_dstout_pos = pos
|
|
mark_dstout_id = id
|
|
end
|
|
end
|
|
end
|
|
|
|
-- error messages
|
|
if mark_srcin_pos < 0 then
|
|
msg('Please set Source-In Marker first!')
|
|
return
|
|
end
|
|
if mark_srcout_pos < 0 then
|
|
msg('Please set Source-Out Marker first!')
|
|
end
|
|
if mark_dstin_pos < 0 then
|
|
msg('Please set Destination-In Marker first!')
|
|
return
|
|
end
|
|
|
|
-- Do stuff before actual edits...
|
|
reaper.Undo_BeginBlock()
|
|
|
|
|
|
-- Set Source Time selection and Copy Items
|
|
reaper.GetSet_LoopTimeRange(true, false, mark_srcin_pos, mark_srcout_pos, false)
|
|
reaper.Main_OnCommand(40718, 0) -- select items on selected tracks under time selection
|
|
reaper.Main_OnCommand(40060, 0) -- copy selected items under time selection 41383
|
|
-- reaper.Main_OnCommand( reaper.NamedCommandLookup('_SWS_SMARTCOPY'), 0 )
|
|
|
|
|
|
-- Move edit cursor to Dest-In and Paste
|
|
--reaper.GoToMarker(0, mark_dstin_id, false)
|
|
-- reaper.GetSet_LoopTimeRange(true, false, mark_dstin_pos, mark_dstout_pos, false)
|
|
reaper.MoveEditCursor(mark_dstin_pos - reaper.GetCursorPosition(), false)
|
|
select_dst_track_only()
|
|
reaper.Main_OnCommand(40058, 0) -- paste item
|
|
reaper.SetProjectMarker(mark_dstin_id, false, mark_dstin_pos + (mark_srcout_pos - mark_srcin_pos), 0, 'DST-IN_4Pcut')
|
|
|
|
-- Restore cursor position, time selection, selected tracks, selected items
|
|
reaper.MoveEditCursor(stored_cursorPos - reaper.GetCursorPosition(), false)
|
|
reaper.Main_OnCommand(40297, 0) -- Unselect all tracks
|
|
for _, track in ipairs(stored_sel_tracks) do
|
|
reaper.SetTrackSelected(track, true)
|
|
end
|
|
reaper.Main_OnCommand(40289, 0) -- Unselect all items
|
|
for _, item in ipairs(stored_sel_items) do
|
|
reaper.SetMediaItemSelected(item, true)
|
|
end
|
|
reaper.GetSet_LoopTimeRange(true, true, stored_timeSelStart, stored_timeSelEnd, false)
|
|
|
|
reaper.Undo_EndBlock('4 point cut: Execute', 4) -- 4 is a flag for actions concerning items |