/
TJF Auto Route to Center v3.lua
240 lines (169 loc) · 7.87 KB
/
TJF Auto Route to Center v3.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
----------------------------------
-- DEBUG --
----------------------------------
function Msg(param) reaper.ShowConsoleMsg(tostring(param).."\n") end
--GLOBAL VARIABLES
local plugin = "TJF/JSFX/TJF Mono to Channel 3 (Center).jsfx"
local preset = "none"
local channels = 6
--local counter = 0
local olditems = {}
local lastProjectChangeCount = reaper.GetProjectStateChangeCount(0)
local cmd_id = ({reaper.get_action_context()})[4] -- gets command ID for this script
local function shallow_equal(t1, t2)
if #t1 ~= #t2 then return false end
for k, v in pairs(t1) do
if v ~= t2[k] then return false end
end
return true
end
--[[
function AddPlugin(item)
local fxheader = "<TAKEFX\nWNDRECT 0 0 0 0\nSHOW 0\nLASTSEL 0\nDOCKED 0\n"
local pluginchunk = "BYPASS 0 0 0\n<JS \"TJF/JSFX/TJF Mono to Channel 3 (Center).jsfx\" \"\"\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n>\nFLOATPOS 0 0 0 0\nFXID {30CD82D4-0A65-1E49-9FE4-98EFB486AC68}\nWAK 0 0\n>"
local _, str = reaper.GetItemStateChunk( item, "", false )
str = string.gsub(str, "BYPASS %d %d %d\n<JS \"TJF/JSFX/TJF Mono to Channel 3 %(Center%).jsfx\".-WAK %d %d\n", "") -- removes plugin if it exists
str = string.gsub(str, "\n>\n>\n", "\n>\n")
local test = string.match(str, "<TAKEFX")
if test ~= "<TAKEFX"
then
str = str .. fxheader .. pluginchunk .. ">\nTAKEFX_NCH 6\n>"
reaper.SetItemStateChunk( item, str, false )
else
local header = string.match(str, "<ITEM.*WAK %d %d\n")
local footer = string.match(str, "WAK?.*$") -- get's end of plugin chunk
if header and footer
then
str = header .. pluginchunk .. footer .. ">\nTAKEFX_NCH 6\n>"
reaper.SetItemStateChunk( item, str, false )
end
end
end--AddPlugin()
function DeletePlugin(item)
local _, str = reaper.GetItemStateChunk( item, "", false )
str = string.gsub(str, "BYPASS %d %d %d\n<JS \"TJF/JSFX/TJF Mono to Channel 3 %(Center%).jsfx\".*WAK %d %d\n", "")
reaper.SetItemStateChunk( item, str, false )
end--DeletePlugin()
]]--
function exit()
Initialize(2,0)
end--exit()
--[[
function ProcessItems(item)
if item
then
local track = reaper.GetMediaItem_Track(item)
local trackchan = reaper.GetMediaTrackInfo_Value(track, "I_NCHAN")
local take = reaper.GetActiveTake(item)
if take
then
local source = reaper.GetMediaItemTake_Source(take)
local sourcechan = reaper.GetMediaSourceNumChannels(source)
local takemode = reaper.GetMediaItemTakeInfo_Value(take, "I_CHANMODE")
if trackchan > 2 and (sourcechan == 1 or (takemode > 1 and takemode < 65))
then AddPlugin(item)
else DeletePlugin(item)
end
end
--reaper.UpdateArrange()
end
end--function
]]--
function ProcessItems(item)
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock()
local track = reaper.GetMediaItem_Track(item)
local trackchan = reaper.GetMediaTrackInfo_Value(track, "I_NCHAN")
for j=0, reaper.CountTakes(item)-1 do
local take = reaper.GetMediaItemTake(item, j)
local source = reaper.GetMediaItemTake_Source(take)
local sourcechan = reaper.GetMediaSourceNumChannels(source)
local takemode = reaper.GetMediaItemTakeInfo_Value(take, "I_CHANMODE")
if trackchan > 2 and (sourcechan == 1 or (takemode > 1 and takemode < 65)) then
local fxnumber = reaper.TakeFX_AddByName( take, plugin, 1)
if fxnumber ~= reaper.TakeFX_GetCount(take)-1
then reaper.TakeFX_Delete(take, fxnumber)
reaper.TakeFX_AddByName(take, plugin, 1)
end--if
else local fxnumber = reaper.TakeFX_AddByName( take, plugin, 0)
if fxnumber >= 0
then reaper.TakeFX_Delete(take, fxnumber)
end
end--if
end--for
reaper.Undo_EndBlock("TJF Set/Clear Mono Items to Center Channel", -1)
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
end--function
function Initialize(chan, state)
if reaper.EnumProjects(0) then
reaper.SetToggleCommandState( 0, cmd_id, state)
reaper.RefreshToolbar2(0, cmd_id)
local retval, userinput = reaper.GetUserInputs("Adjust Session to ".. chan .." Channels?", 3, "Items,Tracks,Master", "YES,YES,YES" )
local itemsok, tracksok, masterok = userinput:match("(.-),(.-),(.*)")
if retval
then
reaper.PreventUIRefresh(1)
--reaper.Undo_BeginBlock()
-------------SET MASTER TRACK
if masterok == "YES"
then reaper.SetMediaTrackInfo_Value(reaper.GetMasterTrack(0), "I_NCHAN", chan)
end
-------------SET ALL OTHER TRACKS
for i=0, reaper.CountTracks(0)-1 -- set all tracks to channels
do
local track = reaper.GetTrack(0,i)
local trackchan = reaper.GetMediaTrackInfo_Value(track, "I_NCHAN")
if trackchan ~= chan
then
if tracksok == "YES"
then reaper.SetMediaTrackInfo_Value( reaper.GetTrack(0,i), "I_NCHAN", chan)
end
if itemsok == "YES"
then
for j=0, reaper.CountTrackMediaItems(track)-1
do ProcessItems(reaper.GetTrackMediaItem(track,j))
end --for
end--if
end--if
end--for
--reaper.Undo_EndBlock("TJF Set/Clear Mono Items to Center Channel", -1)
reaper.PreventUIRefresh(-1)
end--if
end--if
end--Initialize()
function Main()
local projectChangeCount = reaper.GetProjectStateChangeCount(0)
--if lastProjectChangeCount < projectChangeCount then
-- local action = reaper.Undo_CanUndo2(0)
-- action = string.find(action, "FX")
--end
local items = {}
if reaper.GetSelectedMediaItem(0,0)
then
for i=1, reaper.CountSelectedMediaItems(0)
do items[i] = reaper.GetSelectedMediaItem(0,i-1)
end
--if not shallow_equal(items, olditems) or (counter > 30 and #items<100)
if not shallow_equal(items, olditems) or (lastProjectChangeCount < projectChangeCount)
then
if #items < 100
then
for i=1, #items
do ProcessItems(items[i])
end
reaper.UpdateArrange()
end
end
end--if
olditems = items
lastProjectChangeCount = projectChangeCount
reaper.defer(Main)
end--Main()
if reaper.GetProjectStateChangeCount(0) > 3 then Initialize(channels, 1)
else
reaper.SetToggleCommandState( 0, cmd_id, 1)
reaper.RefreshToolbar2(0, cmd_id)
end
reaper.atexit(exit)
Main()