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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*****************************************************************************
Copyright (C) 2016-2017 by Colin Edwards.
Additional Code Copyright (C) 2016-2017 by c3r1c3 <c3r1c3@nevermindonline.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "headers/VSTPlugin.h"
#include <QCryptographicHash>
#define OPEN_VST_SETTINGS "open_vst_settings"
#define CLOSE_VST_SETTINGS "close_vst_settings"
#define OPEN_WHEN_ACTIVE_VST_SETTINGS "open_when_active_vst_settings"
#define PLUG_IN_NAME obs_module_text("VstPlugin")
#define OPEN_VST_TEXT obs_module_text("OpenPluginInterface")
#define CLOSE_VST_TEXT obs_module_text("ClosePluginInterface")
#define OPEN_WHEN_ACTIVE_VST_TEXT obs_module_text("OpenInterfaceWhenActive")
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-vst", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
{
return "VST 2.x Plug-in filter";
}
static bool open_editor_button_clicked(obs_properties_t *props,
obs_property_t *property, void *data)
{
VSTPlugin *vstPlugin = (VSTPlugin *)data;
if (vstPlugin && vstPlugin->vstLoaded()) {
QMetaObject::invokeMethod(vstPlugin, "openEditor");
obs_property_set_visible(
obs_properties_get(props, OPEN_VST_SETTINGS), false);
obs_property_set_visible(
obs_properties_get(props, CLOSE_VST_SETTINGS), true);
}
UNUSED_PARAMETER(props);
UNUSED_PARAMETER(property);
UNUSED_PARAMETER(data);
return true;
}
static bool close_editor_button_clicked(obs_properties_t *props,
obs_property_t *property, void *data)
{
VSTPlugin *vstPlugin = (VSTPlugin *)data;
if (vstPlugin && vstPlugin->vstLoaded() && vstPlugin->isEditorOpen()) {
QMetaObject::invokeMethod(vstPlugin, "closeEditor");
obs_property_set_visible(
obs_properties_get(props, OPEN_VST_SETTINGS), true);
obs_property_set_visible(
obs_properties_get(props, CLOSE_VST_SETTINGS), false);
}
UNUSED_PARAMETER(property);
return true;
}
std::string getFileMD5(const char *file)
{
QFile f(file);
if (f.open(QFile::ReadOnly)) {
QCryptographicHash hash(QCryptographicHash::Md5);
if (hash.addData(&f))
return std::string(hash.result().toHex());
}
return std::string();
}
static const char *vst_name(void *unused)
{
UNUSED_PARAMETER(unused);
return PLUG_IN_NAME;
}
static void vst_destroy(void *data)
{
VSTPlugin *vstPlugin = (VSTPlugin *)data;
QMetaObject::invokeMethod(vstPlugin, "closeEditor");
vstPlugin->deleteLater();
}
static void vst_update(void *data, obs_data_t *settings)
{
VSTPlugin *vstPlugin = (VSTPlugin *)data;
vstPlugin->openInterfaceWhenActive =
obs_data_get_bool(settings, OPEN_WHEN_ACTIVE_VST_SETTINGS);
const char *path = obs_data_get_string(settings, "plugin_path");
#ifdef __linux__
// Migrate freedesktop.org Flatpak runtime 21.08 VST paths to 22.08.
if (QFile::exists("/.flatpak-info") &&
QString(path).startsWith("/app/extensions/Plugins/lxvst")) {
QString newPath(path);
newPath.replace("/app/extensions/Plugins/lxvst",
"/app/extensions/Plugins/vst");
obs_data_set_string(settings, "plugin_path",
newPath.toStdString().c_str());
path = obs_data_get_string(settings, "plugin_path");
}
#endif
if (!*path) {
vstPlugin->unloadEffect();
return;
}
vstPlugin->loadEffectFromPath(std::string(path));
std::string hash = getFileMD5(path);
const char *chunkHash = obs_data_get_string(settings, "chunk_hash");
const char *chunkData = obs_data_get_string(settings, "chunk_data");
bool chunkHashesMatch = chunkHash && *chunkHash &&
hash.compare(chunkHash) == 0;
if (chunkData && *chunkData &&
(chunkHashesMatch || !chunkHash || !*chunkHash)) {
vstPlugin->setChunk(std::string(chunkData));
}
}
static void *vst_create(obs_data_t *settings, obs_source_t *filter)
{
VSTPlugin *vstPlugin = new VSTPlugin(filter);
vst_update(vstPlugin, settings);
return vstPlugin;
}
static void vst_save(void *data, obs_data_t *settings)
{
VSTPlugin *vstPlugin = (VSTPlugin *)data;
obs_data_set_string(settings, "chunk_data",
vstPlugin->getChunk().c_str());
obs_data_set_string(
settings, "chunk_hash",
getFileMD5(vstPlugin->getEffectPath().c_str()).c_str());
}
static struct obs_audio_data *vst_filter_audio(void *data,
struct obs_audio_data *audio)
{
VSTPlugin *vstPlugin = (VSTPlugin *)data;
vstPlugin->process(audio);
/*
* OBS can only guarantee getting the filter source's parent and own name
* in this call, so we grab it and return the results for processing
* by the EditorWidget.
*/
vstPlugin->getSourceNames();
return audio;
}
static void fill_out_plugins(obs_property_t *list)
{
QStringList dir_list;
#ifdef __APPLE__
dir_list << "/Library/Audio/Plug-Ins/VST/"
<< "~/Library/Audio/Plug-ins/VST/";
#elif WIN32
#ifndef _WIN64
HANDLE hProcess = GetCurrentProcess();
BOOL isWow64;
IsWow64Process(hProcess, &isWow64);
if (!isWow64) {
#endif
dir_list << qEnvironmentVariable("ProgramFiles") +
"/Steinberg/VstPlugins/"
<< qEnvironmentVariable("CommonProgramFiles") +
"/Steinberg/Shared Components/"
<< qEnvironmentVariable("CommonProgramFiles") + "/VST2"
<< qEnvironmentVariable("CommonProgramFiles") +
"/Steinberg/VST2"
<< qEnvironmentVariable("CommonProgramFiles") +
"/VSTPlugins/"
<< qEnvironmentVariable("ProgramFiles") +
"/VSTPlugins/";
#ifndef _WIN64
} else {
dir_list << qEnvironmentVariable("ProgramFiles(x86)") +
"/Steinberg/VstPlugins/"
<< qEnvironmentVariable("CommonProgramFiles(x86)") +
"/Steinberg/Shared Components/"
<< qEnvironmentVariable("CommonProgramFiles(x86)") +
"/VST2"
<< qEnvironmentVariable("CommonProgramFiles(x86)") +
"/VSTPlugins/"
<< qEnvironmentVariable("ProgramFiles(x86)") +
"/VSTPlugins/";
}
#endif
#elif __linux__
// If the user has set the VST_PATH environmental
// variable, then use it. Else default to a list
// of common locations.
QString vstPathEnv(getenv("VST_PATH"));
if (!vstPathEnv.isNull()) {
dir_list.append(vstPathEnv.split(":"));
} else {
QString home(getenv("HOME"));
// Choose the most common locations
// clang-format off
dir_list << "/usr/lib/vst/"
<< "/usr/lib/lxvst/"
<< "/usr/lib/linux_vst/"
<< "/usr/lib64/vst/"
<< "/usr/lib64/lxvst/"
<< "/usr/lib64/linux_vst/"
<< "/usr/local/lib/vst/"
<< "/usr/local/lib/lxvst/"
<< "/usr/local/lib/linux_vst/"
<< "/usr/local/lib64/vst/"
<< "/usr/local/lib64/lxvst/"
<< "/usr/local/lib64/linux_vst/"
<< home + "/.vst/"
<< home + "/.lxvst/";
// clang-format on
}
#endif
QStringList filters;
#ifdef __APPLE__
filters << "*.vst";
#elif WIN32
filters << "*.dll";
#elif __linux__
filters << "*.so"
<< "*.o";
#endif
QStringList vst_list;
// Read all plugins into a list...
for (int a = 0; a < dir_list.size(); ++a) {
QDir search_dir(dir_list[a]);
search_dir.setNameFilters(filters);
QDirIterator it(search_dir,
QDirIterator::Subdirectories |
QDirIterator::FollowSymlinks);
while (it.hasNext()) {
QString path = it.next();
QString name = it.fileName();
#ifdef __APPLE__
name.remove(".vst", Qt::CaseInsensitive);
#elif WIN32
name.remove(".dll", Qt::CaseInsensitive);
#elif __linux__
name.remove(".so", Qt::CaseInsensitive);
name.remove(".o", Qt::CaseInsensitive);
#endif
name.append("=").append(path);
vst_list << name;
}
}
// Now sort list alphabetically (still case-sensitive though).
std::stable_sort(vst_list.begin(), vst_list.end(),
std::less<QString>());
// Now add said list to the plug-in list of OBS
obs_property_list_add_string(list, "{Please select a plug-in}",
nullptr);
for (int b = 0; b < vst_list.size(); ++b) {
QString vst_sorted = vst_list[b];
obs_property_list_add_string(
list,
vst_sorted.left(vst_sorted.indexOf('='))
.toStdString()
.c_str(),
vst_sorted.mid(vst_sorted.indexOf('=') + 1)
.toStdString()
.c_str());
}
}
static bool vst_changed(void *data, obs_properties_t *props,
obs_property_t *list, obs_data_t *settings)
{
UNUSED_PARAMETER(settings);
UNUSED_PARAMETER(list);
bool open_settings_vis = true;
bool close_settings_vis = false;
if (data) {
VSTPlugin *vstPlugin = (VSTPlugin *)data;
if (!vstPlugin->vstLoaded()) {
close_settings_vis = false;
open_settings_vis = false;
} else {
if (vstPlugin->isEditorOpen()) {
close_settings_vis = true;
open_settings_vis = false;
}
}
}
obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
open_settings_vis);
obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
close_settings_vis);
return true;
}
static obs_properties_t *vst_properties(void *data)
{
obs_properties_t *props = obs_properties_create();
obs_property_t *list = obs_properties_add_list(props, "plugin_path",
PLUG_IN_NAME,
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
fill_out_plugins(list);
obs_properties_add_button(props, OPEN_VST_SETTINGS, OPEN_VST_TEXT,
open_editor_button_clicked);
obs_properties_add_button(props, CLOSE_VST_SETTINGS, CLOSE_VST_TEXT,
close_editor_button_clicked);
bool open_settings_vis = true;
bool close_settings_vis = false;
if (data) {
VSTPlugin *vstPlugin = (VSTPlugin *)data;
if (!vstPlugin->vstLoaded()) {
close_settings_vis = false;
open_settings_vis = false;
} else {
if (vstPlugin->isEditorOpen()) {
close_settings_vis = true;
open_settings_vis = false;
}
}
}
obs_property_set_visible(obs_properties_get(props, OPEN_VST_SETTINGS),
open_settings_vis);
obs_property_set_visible(obs_properties_get(props, CLOSE_VST_SETTINGS),
close_settings_vis);
obs_properties_add_bool(props, OPEN_WHEN_ACTIVE_VST_SETTINGS,
OPEN_WHEN_ACTIVE_VST_TEXT);
obs_property_set_modified_callback2(list, vst_changed, data);
return props;
}
bool obs_module_load(void)
{
struct obs_source_info vst_filter = {};
vst_filter.id = "vst_filter";
vst_filter.type = OBS_SOURCE_TYPE_FILTER;
vst_filter.output_flags = OBS_SOURCE_AUDIO;
vst_filter.get_name = vst_name;
vst_filter.create = vst_create;
vst_filter.destroy = vst_destroy;
vst_filter.update = vst_update;
vst_filter.filter_audio = vst_filter_audio;
vst_filter.get_properties = vst_properties;
vst_filter.save = vst_save;
obs_register_source(&vst_filter);
return true;
}
|