forked from ReaTeam/ReaScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfillion_Toggle track FX bypass by name.lua
122 lines (102 loc) · 3.79 KB
/
cfillion_Toggle track FX bypass by name.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
-- @description Toggle track FX bypass by name
-- @author cfillion
-- @version 1.2
-- @changelog Add a "create action" script for creating bypass actions without user input
-- @provides
-- .
-- [main] . > cfillion_Toggle track FX bypass by name (create action).lua
-- @link http://forum.cockos.com/showthread.php?t=184623
-- @screenshot
-- Basic Usage https://i.imgur.com/jVgwbi3.gif
-- Undo Points https://i.imgur.com/dtNwlsn.png
-- @about
-- # Toggle track FX bypass by name
--
-- This script asks for a string to match against all track FX in the current
-- project, matching tracks or selected tracks. The search is case insensitive.
-- Bypass is toggled for all matching FXs. Undo points are consolidated into one.
--
-- This script can also be used to create custom actions that bypass matching
-- track effects without always requesting user input.
if not reaper.GetTrackName then
-- for REAPER prior to v5.30 (native GetTrackName returns "Track N" when it's empty)
function reaper.GetTrackName(track, _)
return reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', '', false)
end
end
local function matchTrack(track, filter)
if filter == '/selected' then
return reaper.IsTrackSelected(track)
else
local _, name = reaper.GetTrackName(track, '')
return name:lower():find(filter)
end
end
local function prompt()
local default_track_filter = ''
if reaper.CountSelectedTracks() > 0 then
default_track_filter = '/selected'
end
local ok, csv = reaper.GetUserInputs(script_name, 2,
"Toggle track FX bypass matching:,On tracks (name or /selected):,extrawidth=100",
',' .. default_track_filter)
if not ok or csv:len() <= 1 then return end
local fx_filter, track_filter = csv:match("^(.*),(.*)$")
return fx_filter:lower(), track_filter:lower()
end
local function sanitizeFilename(name)
-- replace special characters that are reserved on Windows
return name:gsub("[*\\:<>?/|\"%c]+", '-')
end
local function createAction()
local fx_filter_fn = sanitizeFilename(fx_filter)
local action_name = string.format('Toggle track FX bypass by name - %s', fx_filter_fn)
local output_fn = string.format('%s/Scripts/%s.lua',
reaper.GetResourcePath(), action_name)
local base_name = script_path:match('([^/\\]+)$')
local rel_path = script_path:sub(reaper.GetResourcePath():len() + 2)
local code = string.format([[
-- This file was created by %s on %s
fx_filter = %q
track_filter = %q
dofile(string.format(%q, reaper.GetResourcePath()))
]], base_name, os.date('%c'), fx_filter, track_filter, '%s/'..rel_path)
local file = assert(io.open(output_fn, 'w'))
file:write(code)
file:close()
if reaper.AddRemoveReaScript(true, 0, output_fn, true) == 0 then
reaper.ShowMessageBox(
'Failed to create or register the new action.', script_name, 0)
return
end
reaper.ShowMessageBox(
string.format('Created the action "%s".', action_name), script_name, 0)
end
script_path = ({reaper.get_action_context()})[2]
script_name = script_path:match("([^/\\_]+)%.lua$")
if not fx_filter or not track_filter then
fx_filter, track_filter = prompt()
if not fx_filter then
reaper.defer(function() end) -- no undo point if nothing to do
return
end
end
if script_name == 'Toggle track FX bypass by name (create action)' then
createAction()
return
end
reaper.Undo_BeginBlock()
for ti=0,reaper.CountTracks()-1 do
local track = reaper.GetTrack(0, ti)
if matchTrack(track, track_filter) then
for fi=0,reaper.TrackFX_GetCount(track)-1 do
local _, fx_name = reaper.TrackFX_GetFXName(track, fi, '')
if fx_name:lower():find(fx_filter) then
reaper.TrackFX_SetEnabled(track, fi,
not reaper.TrackFX_GetEnabled(track, fi))
end
end
end
end
reaper.Undo_EndBlock(
string.format("Toggle track FX bypass matching '%s'", fx_filter), -1)