Skip to content

Commit

Permalink
Add overlap check for classical crossfade
Browse files Browse the repository at this point in the history
  • Loading branch information
chmaha committed Feb 13, 2025
1 parent 585931c commit 6487352
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion ReaClassical/ReaClassical_Classical Crossfade.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

for key in pairs(reaper) do _G[key] = reaper[key] end

local main, return_xfade_length
local main, return_xfade_length, is_cursor_between_items

---------------------------------------------------------------------

Expand All @@ -34,6 +34,13 @@ function main()
MB("Please create a ReaClassical project using F7 or F8 to use this function.", "ReaClassical Error", 0)
return
end
local correct_cursor_position = is_cursor_between_items()
if not correct_cursor_position then
MB("Ensure the parent items on track 1 overlap and that your cursor " ..
"is positioned somewhere within the overlap.",
"Classical Crossfade", 0)
return
end
local xfade_len = return_xfade_length()
local fade_editor_toggle = NamedCommandLookup("_RScc8cfd9f58e03fed9f8f467b7dae42089b826067")
local state = GetToggleCommandState(fade_editor_toggle)
Expand Down Expand Up @@ -139,4 +146,38 @@ end

---------------------------------------------------------------------

function is_cursor_between_items()
local track = GetTrack(0, 0)
if not track then return false end

local item_count = CountTrackMediaItems(track)
if item_count < 2 then return false end

local cursor_pos = GetCursorPosition()
local left_item, right_item = nil, nil

-- Find the left item
for i = 0, item_count - 1 do
local item = GetTrackMediaItem(track, i)
local item_start = GetMediaItemInfo_Value(item, "D_POSITION")
local item_end = item_start + GetMediaItemInfo_Value(item, "D_LENGTH")

if item_start <= cursor_pos and cursor_pos < item_end then
left_item = item
right_item = GetTrackMediaItem(track, i + 1) -- Get the next item if it exists
break
end
end

if not left_item or not right_item then return false end

local left_item_end = GetMediaItemInfo_Value(left_item, "D_POSITION") +
GetMediaItemInfo_Value(left_item, "D_LENGTH")
local right_item_start = GetMediaItemInfo_Value(right_item, "D_POSITION")

return cursor_pos >= right_item_start and cursor_pos <= left_item_end
end

---------------------------------------------------------------------

main()

0 comments on commit 6487352

Please sign in to comment.