forked from ReaTeam/ReaScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLokasenna_Trim items to specified length.lua
178 lines (132 loc) · 3.94 KB
/
Lokasenna_Trim items to specified length.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
--[[
Description: Trim items to specified length
Version: 1.10
Author: Lokasenna
Donation: https://paypal.me/Lokasenna
Changelog:
New: Uses the current version of my GUI library
Fix: Expand error message when the library is missing
Links:
Lokasenna's Website http://forum.cockos.com/member.php?u=10417
About:
Trims all selected items to a given length
--]]
-- Licensed under the GNU GPL v3
-- Script UI generated by Lokasenna's GUI Builder
local lib_path = reaper.GetExtState("Lokasenna_GUI", "lib_path_v2")
if not lib_path or lib_path == "" then
reaper.MB("Couldn't load the Lokasenna_GUI library. Please install 'Lokasenna's GUI library v2 for Lua', available on ReaPack, then run the 'Set Lokasenna_GUI v2 library path.lua' script in your Action List.", "Whoops!", 0)
return
end
loadfile(lib_path .. "Core.lua")()
GUI.req("Classes/Class - Menubox.lua")()
GUI.req("Classes/Class - Button.lua")()
GUI.req("Classes/Class - Textbox.lua")()
-- If any of the requested libraries weren't found, abort the script.
if missing_lib then return 0 end
local function parse_settings()
-- Valid the textbox
local length = tonumber( GUI.Val("Textbox1") )
if not length then return end
-- Get a time multiplier for the specified unit
local _, units = GUI.Val("Menubox1")
-- Get settings
if units == "milliseconds" then
length = length / 1000
elseif units == "minutes" then
length = length * 60
elseif units == "beats" then
length = length * (60 / reaper.Master_GetTempo() )
elseif units == "measures" then
length = length * 4 * (60 / reaper.Master_GetTempo() )
elseif units == "frames" then
length = get_frames(length)
elseif units == "gridlines" then
local _, divisionIn, _, _ = reaper.GetSetProjectGrid(0, false)
length = length * divisionIn * 4 * (60 / reaper.Master_GetTempo() )
elseif units == "visible gridlines" then
local pos = reaper.GetCursorPosition()
local cur = 0.001
while true do
local cur_pos = reaper.SnapToGrid(0, pos + cur)
if cur_pos ~= pos then
length = length * (cur_pos - pos)
break
else
cur = cur * 2
end
end
end
return length
end
local function go()
local num_items = reaper.CountSelectedMediaItems()
if num_items == 0 then
reaper.MB("No items selected.", "Whoops!", 0)
return
end
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock()
-- Parse the user settings to get a length
local l = parse_settings()
if not l then return end
-- For each selected item
for i = 0, num_items - 1 do
local item = reaper.GetSelectedMediaItem(0, i)
reaper.SetMediaItemInfo_Value(item, "D_LENGTH", l)
end
reaper.Undo_EndBlock("Trim items to specified length", -1)
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
end
GUI.name = "Trim items to..."
GUI.x, GUI.y, GUI.w, GUI.h = 0, 0, 280, 96
GUI.anchor, GUI.corner = "mouse", "C"
GUI.New("Button1", "Button", {
z = 11,
x = 116.0,
y = 48,
w = 48,
h = 24,
caption = "Go!",
font = 3,
col_txt = "txt",
col_fill = "elm_frame",
func = go,
})
GUI.New("Menubox1", "Menubox", {
z = 11,
x = 132,
y = 16.0,
w = 112,
h = 20,
caption = "",
optarray = {"beats", "measures", "milliseconds", "seconds", "minutes", "gridlines", "visible gridlines", "frames"},
retval = 5.0,
font_a = 3,
font_b = 4,
col_txt = "txt",
col_cap = "txt",
bg = "wnd_bg",
pad = 4,
noarrow = false,
align = 0
})
GUI.New("Textbox1", "Textbox", {
z = 11,
x = 64.0,
y = 16.0,
w = 64,
h = 20,
caption = "Length:",
cap_pos = "left",
font_a = 3,
font_b = 4,
color = "txt",
bg = "wnd_bg",
shadow = true,
pad = 4,
undo_limit = 20
})
GUI.Init()
GUI.Main()