-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_scan.py
247 lines (216 loc) · 8.28 KB
/
plugin_scan.py
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
# Copied and modified from pedalboard-pluginary
# License: Apache Software License
#
# Changes:
# - Only scan for VST3 plugins on macOS
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "pedalboard-pluginary>=1.1.0",
# "pedalboard>=0.9.16",
# "rich>=13.9.4",
# "setuptools>=75.6.0",
# ]
# ///
import json
import re
import os
import platform
import subprocess
from pathlib import Path
from urllib.parse import unquote, urlparse
import itertools
import logging
import pedalboard
from tqdm import tqdm
from pedalboard_pluginary.data import (
load_json_file,
save_json_file,
get_cache_path,
load_ignores,
copy_default_ignores,
)
from pedalboard_pluginary.utils import ensure_folder, from_pb_param
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("Scanner")
class PedalboardScanner:
RE_AUFX = re.compile(r"aufx\s+(\w+)\s+(\w+)\s+-\s+(.*?):\s+(.*?)\s+\((.*?)\)")
def __init__(self):
self.plugins_path = get_cache_path("plugins")
self.plugins = {}
self.safe_save = True
self.ensure_ignores()
def ensure_ignores(self):
self.ignores_path = get_cache_path("ignores")
if not self.ignores_path.exists():
copy_default_ignores(self.ignores_path)
self.ignores = load_ignores(self.ignores_path)
def save_plugins(self):
ensure_folder(self.plugins_path)
save_json_file(dict(sorted(self.plugins.items())), self.plugins_path)
def _list_aufx_plugins(self):
try:
result = subprocess.run(
["auval", "-l"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
check=True,
)
return result.stdout.splitlines()
except subprocess.CalledProcessError as e:
logger.error(f"Error running auval: {e}")
return []
def _find_aufx_plugins(self, plugin_paths=None):
if plugin_paths:
plugin_paths = [Path(p).resolve() for p in plugin_paths]
aufx_plugins = []
plugin_type = "aufx"
for line in self._list_aufx_plugins():
match = self.RE_AUFX.match(line)
if match:
(
plugin_code,
vendor_code,
vendor_name,
plugin_name,
plugin_url,
) = match.groups()
plugin_path = Path(unquote(urlparse(plugin_url).path)).resolve()
plugin_fn = plugin_path.stem
plugin_key = f"{plugin_type}/{plugin_fn}"
if plugin_key not in self.ignores:
if plugin_paths and plugin_path not in plugin_paths:
continue
aufx_plugins.append(plugin_path)
return aufx_plugins
def _get_vst3_folders(self, extra_folders=None):
os_name = platform.system()
if os_name == "Windows":
folders = [
Path(os.getenv("ProgramFiles", "") + r"\Common Files\VST3"),
Path(os.getenv("ProgramFiles(x86)", "") + r"\Common Files\VST3"),
]
elif os_name == "Darwin": # macOS
folders = [
Path("~/Library/Audio/Plug-Ins/VST3").expanduser(),
Path("/Library/Audio/Plug-Ins/VST3"),
]
elif os_name == "Linux":
folders = [
Path("~/.vst3").expanduser(),
Path("/usr/lib/vst3"),
Path("/usr/local/lib/vst3"),
]
else:
folders = []
if extra_folders:
folders.extend(Path(p) for p in extra_folders)
return [folder for folder in folders if folder.exists()]
def _find_vst3_plugins(self, extra_folders=None, plugin_paths=None):
vst3_plugins = []
plugin_type = "vst3"
if plugin_paths:
plugin_paths = [Path(p).resolve() for p in plugin_paths]
plugin_paths = plugin_paths or list(
itertools.chain.from_iterable(
folder.glob(f"*.{plugin_type}")
for folder in self._get_vst3_folders(extra_folders=extra_folders)
)
)
for plugin_path in plugin_paths:
plugin_fn = plugin_path.stem
plugin_key = f"{plugin_type}/{plugin_fn}"
if plugin_key not in self.ignores:
vst3_plugins.append(plugin_path)
return vst3_plugins
def get_plugin_params(self, plugin_path, plugin_name):
plugin = pedalboard.load_plugin(str(plugin_path), plugin_name=plugin_name)
plugin_params = {
k: from_pb_param(plugin.__getattr__(k)) for k in plugin.parameters.keys()
}
return plugin_params
def scan_typed_plugin_path(
self, plugin_type, plugin_key, plugin_path, plugin_fn, plugin_loader
):
plugin_path = str(plugin_path)
plugin_names = plugin_loader.get_plugin_names_for_file(plugin_path)
for plugin_name in plugin_names:
if plugin_name in self.plugins:
continue
plugin_params = self.get_plugin_params(plugin_path, plugin_name)
plugin_entry = {
"name": plugin_name,
"path": plugin_path,
"filename": plugin_fn,
"type": plugin_type,
"params": plugin_params,
}
self.plugins[plugin_name] = plugin_entry
def scan_typed_plugins(self, plugin_type, found_plugins, plugin_loader):
with tqdm(found_plugins, desc=f"Scanning {plugin_type}", unit="") as pbar:
for plugin_path in pbar:
plugin_fn = str(Path(plugin_path).stem)
plugin_key = f"{plugin_type}/{plugin_fn}"
pbar.set_description(plugin_key)
self.scan_typed_plugin_path(
plugin_type, plugin_key, str(plugin_path), plugin_fn, plugin_loader
)
if self.safe_save:
self.save_plugins()
def scan_aufx_plugins(self, plugin_paths=None):
self.scan_typed_plugins(
"aufx",
list(self._find_aufx_plugins(plugin_paths=plugin_paths)),
pedalboard.AudioUnitPlugin,
)
def scan_vst3_plugins(self, extra_folders=None, plugin_paths=None):
self.scan_typed_plugins(
"vst3",
list(
self._find_vst3_plugins(
extra_folders=extra_folders, plugin_paths=plugin_paths
)
),
pedalboard.VST3Plugin,
)
def scan_plugins(self, extra_folders=None, plugin_paths=None):
# self.scan_vst3_plugins(extra_folders=extra_folders, plugin_paths=plugin_paths)
if platform.system() == "Darwin":
self.scan_aufx_plugins(plugin_paths=plugin_paths)
def scan(self, extra_folders=None, plugin_paths=None):
logger.info("\n>> Scanning plugins...")
self.scan_plugins(extra_folders=None, plugin_paths=plugin_paths)
# self.save_plugins()
logger.info("\n>> Done!")
def rescan(self, extra_folders=None):
self.plugins = {}
self.scan(extra_folders=extra_folders)
def update(self, extra_folders=None):
logger.info("\n>> Scanning updated plugins...")
if not self.plugins_path.exists():
self.rescan(extra_folders=extra_folders)
return
self.plugins = load_json_file(self.plugins_path)
new_vst3_paths = sorted(
list(
set(self._find_vst3_plugins(extra_folders=extra_folders))
- set(
Path(p["path"]).resolve()
for p in self.plugins.values()
if p["type"] == "vst3"
)
)
)
self.scan_vst3_plugins(extra_folders=extra_folders, plugin_paths=new_vst3_paths)
# new_aufx_paths = sorted(list(set(self._find_aufx_plugins()) - set(Path(p["path"]).resolve() for p in self.plugins.values() if p["type"] == "aufx")))
# if platform.system() == "Darwin":
# self.scan_aufx_plugins(plugin_paths=new_aufx_paths)
self.save_plugins()
logger.info("\n>> Done!")
def get_json(self):
return json.dumps(self.plugins, indent=4)
def main():
PedalboardScanner().rescan()
if __name__ == "__main__":
main()