From a5072fc596bd4b05b2959e314f02dda1040573ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludwig=20Fr=C3=BChsch=C3=BCtz?= Date: Tue, 22 Sep 2020 21:10:58 +0200 Subject: [PATCH] 4PointCutting: fix track alignment of pasted items if first source tracks are empty in time selection. closes #4 --- 4PointCutting/4Pcut_execute.lua | 62 ++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/4PointCutting/4Pcut_execute.lua b/4PointCutting/4Pcut_execute.lua index 9f51168..4d64fd9 100644 --- a/4PointCutting/4Pcut_execute.lua +++ b/4PointCutting/4Pcut_execute.lua @@ -5,7 +5,7 @@ -- Git: https://files.eleton-audio.de/gitea/Ludwig/Reaper-Scripts.git -- License: GPL v3.0 -- Requires: Reaper 5 or 6 --- Requires: SWS extensions, if not present no crossfades are created. http://standingwaterstudios.com +-- Requires: SWS extensions. http://standingwaterstudios.com -- Requires: This script goes with several other scripts that work closely together. local stored_cursorPos = 0 @@ -23,6 +23,7 @@ local mark_dstin_pos = -1 local mark_dstin_id = -1 local mark_dstout_pos = -1 local mark_dstout_id = -1 +local dst_track_offset = 0 -- if the uppermost item is not on the first sourcetrack, this offset needs to be applied to the dst-track selection local edit_mode = 0 local sws_present = false -- edit modes: 0=not enough markers set; 1=src-in, src-out, dst-in; 2=src-in, src-out, dst-out; @@ -39,6 +40,35 @@ function round(num, numDecimalPlaces) return math.floor(num * mult + 0.5) / mult end +-- Calculate offset between first source track and uppermost source item (how many +-- tracks are empty in source selection on top). This needs to be applied to destination +-- track selection, otherwise the items will be pasted shifted upwards. +-- This function depends on the source tracks AND items to be already selected, +-- it does not care about any markers or stored sourcetracks! +function calculate_track_offset() + local first_sel_track = 9999 + local uppermost_item_track = 9999 + + -- get number of first selected track + for i = 0, reaper.CountSelectedTracks(0)-1 do + local nof_track = reaper.GetMediaTrackInfo_Value(reaper.GetSelectedTrack(0, i), "IP_TRACKNUMBER") + if nof_track < first_sel_track then + first_sel_track = nof_track + end + end + + -- get tracknumber of first selected item + for i = 0, reaper.CountSelectedMediaItems(0)-1 do + local trackno_item = reaper.GetMediaTrackInfo_Value(reaper.GetMediaItemTrack(reaper.GetSelectedMediaItem(0, i)), "IP_TRACKNUMBER") + if trackno_item < uppermost_item_track then + uppermost_item_track = trackno_item + end + end + + -- calculate offset + dst_track_offset = uppermost_item_track - first_sel_track +end + -- Selects the source tracks, or, if not stored in rpp, selects all tracks function select_src_tracks() local tracks_str = '' @@ -64,15 +94,36 @@ function select_src_tracks() end -- Select the destination tracks, or, if not stored in rpp, the first track +-- Also a offset must be specified which will be applied to the tracknumber to select function select_dst_track_only() local track_str = '' local retval + local raw_dst_track + + -- Get "raw" track, without offset 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)) + raw_dst_track = reaper.BR_GetMediaTrackByGUID(0, track_str) else --default to first track - reaper.SetOnlyTrackSelected(reaper.GetTrack(0, 0)) + raw_dst_track = reaper.GetTrack(0, 0) end + + if dst_track_offset <= 0 then + -- no offset to respect, easy + reaper.SetOnlyTrackSelected(raw_dst_track) + else + local trackno_dst_track = reaper.GetMediaTrackInfo_Value(raw_dst_track, "IP_TRACKNUMBER") + dst_track_offset + -- Run trough all tracks, select the one with the matching tracknumber and return + for i = 0, reaper.CountTracks(0)-1 do + local nof_track = reaper.GetMediaTrackInfo_Value(reaper.GetTrack(0, i), "IP_TRACKNUMBER") + if nof_track == trackno_dst_track then + reaper.SetOnlyTrackSelected(reaper.GetTrack(0, i)) + return + end + end + end + -- In theory should never be reached, use raw track as fallback, probably something is wrong with the offset: + reaper.SetOnlyTrackSelected(raw_dst_track) end -- START HERE vvvvvvvvvvvvvvvvvvvvvvvvvv @@ -176,9 +227,12 @@ end 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 +-- Calculate track offset between first selected item and first dst-track +calculate_track_offset() + -- Move edit cursor to Dest-In and Paste reaper.GoToMarker(0, mark_dstin_id, false) -select_dst_track_only() +select_dst_track_only() --reaper.Main_OnCommand(40058, 0) -- paste item reaper.Main_OnCommand( reaper.NamedCommandLookup('_SWS_AWPASTE'), 0 ) -- SWS Paste