Skip to content

Commit 349a26b

Browse files
authored
Update Auto-set loop points to selected items.lua
1 parent ed55fe4 commit 349a26b

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
-- @description Auto-set loop points to selected items (background)
22
-- @author smandrap
3-
-- @version 1.0
3+
-- @version 1.1
4+
-- @changelog
5+
-- fix my brain
46
-- @donation https://paypal.me/smandrap
57
-- @about
68
-- Select items -> loop points are set to those items. Useful for quickly auditioning loops.
79

810
reaper.set_action_options(5)
911

1012
local reaper = reaper
13+
14+
local sel_items = {}
1115
local sel_item_cnt = reaper.CountSelectedMediaItems(0)
16+
local proj_state_cnt = reaper.GetProjectStateChangeCount(0)
17+
18+
local function get_items(tbl)
19+
tbl = tbl or {}
20+
local cnt = reaper.CountSelectedMediaItems(0)
21+
22+
for i = 1, cnt do
23+
tbl[i] = reaper.GetSelectedMediaItem(0, i-1)
24+
end
25+
26+
-- trim
27+
for j = cnt+1, #tbl do
28+
tbl[j] = nil
29+
end
30+
31+
return tbl, cnt
32+
end
33+
34+
35+
local function items_equal(a, b, cnt)
36+
if #a ~= cnt or #b ~= cnt then return false end
37+
for i = 1, cnt do
38+
if a[i] ~= b[i] then return false end
39+
end
40+
return true
41+
end
42+
1243

1344
local function isProjChange()
14-
local new_state_cnt = reaper.GetProjectStateChangeCount(0)
45+
local new_state_cnt = reaper.GetProjectStateChangeCount(0)
1546
if new_state_cnt ~= proj_state_cnt then
1647
proj_state_cnt = new_state_cnt
1748
return true
@@ -22,18 +53,23 @@ end
2253
local function main()
2354
if isProjChange() then
2455
local newcnt = reaper.CountSelectedMediaItems(0)
25-
if newcnt ~= sel_item_cnt then
26-
reaper.Main_OnCommand(41039, 0)
27-
sel_item_cnt = newcnt
56+
local new_items = {}
57+
get_items(new_items)
58+
59+
if not items_equal(new_items, sel_items, newcnt) then
60+
reaper.Main_OnCommand(41039, 0) -- "set loop points"
61+
sel_items, sel_item_cnt = new_items, newcnt
2862
end
2963
end
30-
64+
3165
reaper.defer(main)
3266
end
3367

3468
local function exit()
3569
reaper.set_action_options(8)
3670
end
3771

72+
sel_items, sel_item_cnt = get_items(sel_items)
73+
3874
reaper.atexit(exit)
3975
reaper.defer(main)

0 commit comments

Comments
 (0)