Reaper-Scripts/4PointCutting/4Pcut_execute.lua

158 lines
5.6 KiB
Lua

-- Reaper Scripts for 4 point editing. This script executes "one edit" and copies source material from
-- already set source in/out markers to also set destination in/out markers.
-- Author: Ludwig Frühschütz
-- Source: https://www.eleton-audio.de
-- Git: https://files.eleton-audio.de/gitea/Ludwig/Reaper-Scripts.git
-- License: GPL v3.0
-- Requires: Reaper 5 or 6
-- Requires: This script goes with several other scripts that work closely together.
local stored_cursorPos = 0
local stored_timeSelStart = 0
local stored_timeSelEnd = 0
local stored_sel_tracks = {}
local stored_sel_items = {}
local stored_view_start = 0
local stored_view_end = 0
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
-- Round number
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-- Selects the source tracks, or, if not stored in rpp, selects all tracks
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
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
-- Select the destination tracks, or, if not stored in rpp, the first track
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
-- 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()
--store viewport off arranger
stored_view_start, stored_view_end = reaper.GetSet_ArrangeView2(0, false, 0, 0)
-- 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
-- unselect items and tracks, select source tracks
reaper.Main_OnCommand(40289, 0) -- Unselect all items
reaper.Main_OnCommand(40297, 0) -- Unselect all tracks
select_src_tracks()
-- 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
-- Move edit cursor to Dest-In and Paste
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)
-- Restore view port of arranger
reaper.GetSet_ArrangeView2(0, true, 0, 0, stored_view_start, stored_view_end)
reaper.Undo_EndBlock('4 point cut: Execute', 4) -- 4 is a flag for actions concerning items