/
TJF Toggle Volume 2.0.lua
250 lines (153 loc) · 7.01 KB
/
TJF Toggle Volume 2.0.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
241
242
243
244
245
246
247
248
--[[
@description TJF Toggle Volume Envelope Visible for Track or Items
@version 2.3
@author Tim Farrell
@link http://github.com/sonictim/TJF-Scripts/
@date 2020 04 26
@about
# TJF Toggle Volume Envelope Visible for Track or Items
Will Toggle Volume Envelope Visibility for all selected Items, or Tracks if no items selected.
Toggle will match toggle of first item selected
@changelog
v1.2 added ability to hide ALL active envelopes for track
v1.2.1 speed adjustments
v2.0 logic rework
v2.1 removed all SWS functions (recoded from scratch)
v2.2 accounts for trim volume on folders
v2.3 bugfix for take envelopes not working correctly
--]]
----------------------------------DEBUG FUNCTIONS
--reaper.ClearConsole()
function Msg(param) reaper.ShowConsoleMsg(tostring(param).."\n") end
function take(param) return reaper.GetActiveTake(items[param]) end --can call take function to match take to item
----------------------------------SET COMMON VARIABLES
items = {}
for i=1, reaper.CountSelectedMediaItems(0) do items[i] = reaper.GetSelectedMediaItem(0,i-1) end
tracks = {}
for i=1, reaper.CountSelectedTracks(0) do tracks[i] = reaper.GetSelectedTrack(0,i-1) end
----------------------------------SET COMMON FUNCTIONS
function CreateNewEnv(chunkname)
local EnvChunk = chunkname .. [[
EGUID ]]..reaper.genGuid("")..[[
ACT 1 -1
VIS 1 1 1
LANEHEIGHT 0 0
ARM 0
DEFSHAPE 0 -1 -1
VOLTYPE 1
PT 0 1 0
>
]]
return EnvChunk
end
function GetEnvelopeVis(envelope)
local retval, str = reaper.GetEnvelopeStateChunk( envelope, "", false )
if retval then
str = string.match(str, "\nVIS %d")
str = string.match(str, "%d")
if str=="1" then return true else return false end
end
end--EnvelopeVis()
function SetEnvelopeVis(envelope, bool)
local retval, str = reaper.GetEnvelopeStateChunk( envelope, "VIS", false )
if retval then
if bool
then str = string.gsub(str, "\nVIS %d", "\nVIS 1")
else str = string.gsub(str, "\nVIS %d", "\nVIS 0")
end
end
reaper.SetEnvelopeStateChunk( envelope, str, true )
end--EnvelopeVis()
function AddTrackEnv(track)
local _, str = reaper.GetTrackStateChunk(track, "", false )
local volumeEnvelope = string.match(str, "MAINSEND %d %d\n")..CreateNewEnv("<VOLENV")
str = string.gsub(str, "MAINSEND %d %d", volumeEnvelope)
reaper.SetTrackStateChunk(track, str, false)
end--AdTrackEnv
----------------------------------CHECK IF ANY TAKES/TRACKS HAVE ANY ENVELOPES VISIBLE
function AnyTakeEnvVisible(param) -- Param is Envelop to Check ("Volume", "Pan", or "Pitch"
for i=1, #items
do
local NamedEnv = reaper.GetTakeEnvelopeByName(take(i), param)
if NamedEnv then
if GetEnvelopeVis(NamedEnv)then return true end
end--if
end--for
end--AnyTakeEnvVisible
function AnyTrackEnvVisible(param) -- Param is Envelop to Check ("Volume", "Pan", or "Pitch"
for i=1, #tracks
do
local NamedEnv = reaper.GetTrackEnvelopeByName(tracks[i], param)
if NamedEnv then
if GetEnvelopeVis(NamedEnv)then return true end
end--if
end--for
end--AnyTakeEnvVisible
----------------------------------TOGGLE TAKE ENVELOPES
function ToggleTakeEnvelope(kind) -- Toggles the take envelope visibility Volume, Pan, or Pitch
local visible = not AnyTakeEnvVisible(kind) --decide if we should show or hide the envelope
if visible then visible = "1" else visible = "0" end
for i=1, #items do
local _, str = reaper.GetItemStateChunk( items[i], "", false )
str = string.gsub(str, "\nVIS %d", "\nVIS 0") -- hide all envelopes for item
local VolEnv = string.match(str, "<VOLENV.->") --find the volume envelope
if not VolEnv
then VolEnv = CreateNewEnv("<VOLENV")
end
VolEnv = string.gsub(VolEnv, "\nVIS %d", "\nVIS "..visible) -- adjust visibility per toggle we set earlier
str = str:match('(<ITEM.*)>.-$')
str = str .. VolEnv
reaper.SetItemStateChunk(items[i], str, false) -- write chunk
end--for
if #items == 1 then
reaper.SetCursorContext( 2, reaper.GetTakeEnvelopeByName(reaper.GetActiveTake(items[1]), "Volume" )) -- selects envelope
--reaper.Main_OnCommand(40332,0) -- Envelope: Select all points
end
end--ToggleTakeEnvelope(param)
----------------------------------TOGGLE TRACK ENVELOPES
function toggletrackvisible(param)
visible = not AnyTrackEnvVisible(param) --decide to show or hide envelope
for i=1, #tracks -- loop through selected tracks
do
local NamedEnv = reaper.GetTrackEnvelopeByName( tracks[i], param ) --check for the envelope we want
if not NamedEnv
then
AddTrackEnv(tracks[i])
NamedEnv = reaper.GetTrackEnvelopeByName( tracks[i], param ) --if it doesn't exist, create it and then point to it
end
for j=0, reaper.CountTrackEnvelopes(tracks[i])-1 do --loop through all track envelopes
local envelope = reaper.GetTrackEnvelope(tracks[i], j)
local _, name = reaper.GetEnvelopeName( envelope )
local _, trackname = reaper.GetTrackName(tracks[i])
if envelope == NamedEnv
then
SetEnvelopeVis(envelope, visible) --if the envelope matches our envelope we are looking for, adjust visibility
else
if (name == "Trim Volume" and reaper.GetMediaTrackInfo_Value(tracks[i], "I_FOLDERDEPTH" ) == 1 and trackname ~= "PIX" ) then
SetEnvelopeVis(envelope, true)
else
SetEnvelopeVis(envelope, false) --hide the rest
end
end
end--for
if #tracks == 1 then
reaper.SetCursorContext( 2, NamedEnv )
--reaper.Main_OnCommand(40332,0) -- Envelope: Select all points
end -- selects envelope
end--for
end--toggletrackvisible()
----------------------------------MAIN FUNCTION
function Main()
if #items > 0
then
ToggleTakeEnvelope("Volume")
elseif #tracks > 0
then toggletrackvisible("Volume (Pre-FX)")
end--if
end--main()
----------------------------------CALL THE SCRIPT
reaper.PreventUIRefresh(1) --reset console window
Main()
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
reaper.defer(function() end) --prevent Undo