|
1 | 1 | -- @description Auto-set loop points to selected items (background) |
2 | 2 | -- @author smandrap |
3 | | --- @version 1.0 |
| 3 | +-- @version 1.1 |
| 4 | +-- @changelog |
| 5 | +-- fix my brain |
4 | 6 | -- @donation https://paypal.me/smandrap |
5 | 7 | -- @about |
6 | 8 | -- Select items -> loop points are set to those items. Useful for quickly auditioning loops. |
7 | 9 |
|
8 | 10 | reaper.set_action_options(5) |
9 | 11 |
|
10 | 12 | local reaper = reaper |
| 13 | + |
| 14 | +local sel_items = {} |
11 | 15 | 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 | + |
12 | 43 |
|
13 | 44 | local function isProjChange() |
14 | | - local new_state_cnt = reaper.GetProjectStateChangeCount(0) |
| 45 | + local new_state_cnt = reaper.GetProjectStateChangeCount(0) |
15 | 46 | if new_state_cnt ~= proj_state_cnt then |
16 | 47 | proj_state_cnt = new_state_cnt |
17 | 48 | return true |
|
22 | 53 | local function main() |
23 | 54 | if isProjChange() then |
24 | 55 | 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 |
28 | 62 | end |
29 | 63 | end |
30 | | - |
| 64 | + |
31 | 65 | reaper.defer(main) |
32 | 66 | end |
33 | 67 |
|
34 | 68 | local function exit() |
35 | 69 | reaper.set_action_options(8) |
36 | 70 | end |
37 | 71 |
|
| 72 | +sel_items, sel_item_cnt = get_items(sel_items) |
| 73 | + |
38 | 74 | reaper.atexit(exit) |
39 | 75 | reaper.defer(main) |
0 commit comments