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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
|
#include "PluginDesc.h"
#include "CpuArch.h"
#include "MiscUtils.h"
#include "FileUtils.h"
#include "Log.h"
#include "Sync.h"
#include <algorithm>
#include <sstream>
#include <cstring>
// for Windows.h
#undef IGNORE
namespace vst {
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
const char * getVersionString(){
static std::string version = [](){
std::stringstream ss;
ss << VERSION_MAJOR << "." << VERSION_MINOR;
if (VERSION_PATCH > 0){
ss << "." << VERSION_PATCH;
}
if (VERSION_PRERELEASE > 0){
ss << "-pre" << VERSION_PRERELEASE;
}
return ss.str();
}();
return version.c_str();
}
/*////////////////////// preset ///////////////////////*/
static SharedMutex gFileLock;
#if !defined(_WIN32) && !defined(__APPLE__)
static bool gDidCreateVstFolders = false;
#endif
static std::string getPresetLocation(PresetType presetType, PluginType pluginType){
std::string result;
#if defined(_WIN32)
switch (presetType){
case PresetType::User:
result = expandPath("%USERPROFILE%") + "\\Documents";
break;
case PresetType::UserFactory:
result = expandPath("%APPDATA%");
break;
case PresetType::SharedFactory:
result = expandPath("%PROGRAMDATA%");
break;
default:
return "";
}
if (pluginType == PluginType::VST3){
return result + "\\VST3 Presets";
} else {
return result + "\\VST2 Presets";
}
#elif defined(__APPLE__)
switch (presetType){
case PresetType::User:
return expandPath("~/Library/Audio/Presets");
case PresetType::SharedFactory:
return "/Library/Audio/Presets";
default:
return "";
}
#else
switch (presetType){
case PresetType::User:
{
result = expandPath("~/.");
// VST directories might not exist yet
std::shared_lock rdlock(gFileLock);
if (!gDidCreateVstFolders){
rdlock.unlock();
std::lock_guard wrlock(gFileLock);
// LATER do some error handling
#if USE_VST2
createDirectory(result + "vst");
#endif
#if USE_VST3
createDirectory(result + "vst3");
#endif
gDidCreateVstFolders = true;
}
break;
}
case PresetType::SharedFactory:
result = "/usr/local/share/";
break;
case PresetType::Global:
result = "/usr/share/";
break;
default:
return "";
}
if (pluginType == PluginType::VST3){
return result + "vst3/presets";
} else {
return result + "vst/presets";
}
#endif
}
/*///////////////////// PluginDesc /////////////////////*/
PluginDesc::PluginDesc(std::shared_ptr<const IFactory> f){
if (f){
setFactory(std::move(f));
}
}
PluginDesc::~PluginDesc(){}
void PluginDesc::setFactory(std::shared_ptr<const IFactory> factory){
if (path_.empty()){
path_ = factory->path();
}
if (factory->arch() != getHostCpuArchitecture()){
flags |= Bridged;
}
factory_ = std::move(factory);
}
CpuArch PluginDesc::arch() const {
auto factory = factory_.lock();
return factory ? factory->arch() : CpuArch::unknown;
}
// ThreadedPlugin.cpp
IPlugin::ptr createThreadedPlugin(IPlugin::ptr plugin);
#if USE_BRIDGE
// PluginClient.cpp
IPlugin::ptr createBridgedPlugin(IFactory::const_ptr factory, const std::string& name,
bool editor, bool sandbox);
#endif
IPlugin::ptr PluginDesc::create(bool editor, bool threaded, RunMode mode) const {
std::shared_ptr<const IFactory> factory = factory_.lock();
if (!factory){
return nullptr;
}
IPlugin::ptr plugin;
#if USE_BRIDGE
if ((mode == RunMode::Bridge) || (mode == RunMode::Sandbox) ||
((mode == RunMode::Auto) && bridged())){
plugin = createBridgedPlugin(factory, name, editor, mode == RunMode::Sandbox);
}
else
#endif
plugin = factory->create(name, editor);
if (threaded){
plugin = createThreadedPlugin(std::move(plugin));
}
return plugin;
}
#if USE_VST2
void PluginDesc::setUniqueID(int _id){
type_ = PluginType::VST2;
char buf[9];
// should we write in little endian?
snprintf(buf, sizeof(buf), "%08X", _id);
buf[8] = 0;
uniqueID = buf;
id_.id = _id;
}
#endif
#if USE_VST3
void PluginDesc::setUID(const char *uid){
type_ = PluginType::VST3;
char buf[33];
for (int i = 0; i < 16; ++i){
// we have to cast to uint8_t!
sprintf(buf + 2 * i, "%02X", (uint8_t)uid[i]);
}
buf[32] = 0;
uniqueID = buf;
memcpy(id_.uid, uid, 16);
}
#endif
static void conformPath(std::string& path){
// replace backslashes
for (auto& c : path){
if (c == '\\'){
c = '/';
}
}
}
void PluginDesc::addParameter(Param param){
auto index = parameters.size();
// name -> index mapping
paramMap_.insert(param.name, index);
#if USE_VST3
// index -> ID mapping
indexToIdMap_.insert(index, param.id);
// ID -> index mapping
idToIndexMap_.insert(param.id, index);
#endif
// finally add parameter
parameters.push_back(std::move(param));
}
void PluginDesc::addParamAlias(int index, std::string_view key) {
paramMap_.insert(key, index);
}
void PluginDesc::scanPresets(){
const std::vector<PresetType> presetTypes = {
#if defined(_WIN32)
PresetType::User, PresetType::UserFactory, PresetType::SharedFactory
#elif defined(__APPLE__)
PresetType::User, PresetType::SharedFactory
#else
PresetType::User, PresetType::SharedFactory, PresetType::Global
#endif
};
PresetList results;
for (auto& presetType : presetTypes){
auto folder = getPresetFolder(presetType);
if (pathExists(folder)){
vst::search(folder, [&](const std::string& path){
auto ext = fileExtension(path);
if ((type_ == PluginType::VST3 && ext != ".vstpreset") ||
(type_ == PluginType::VST2 && ext != ".fxp" && ext != ".FXP")){
return;
}
Preset preset;
preset.type = presetType;
preset.name = fileBaseName(path);
preset.path = path;
#ifdef _WIN32
conformPath(preset.path);
#endif
results.push_back(std::move(preset));
}, false);
}
}
presets = std::move(results);
sortPresets(false);
#if 0
if (numPresets()){
LOG_DEBUG("presets:");
for (auto& preset : presets){
LOG_DEBUG("\t" << preset.path);
}
}
#endif
}
void PluginDesc::sortPresets(bool userOnly){
// don't lock! private method
auto it1 = presets.begin();
auto it2 = it1;
if (userOnly){
// get iterator past user presets
while (it2 != presets.end() && it2->type == PresetType::User) ++it2;
} else {
it2 = presets.end();
}
std::sort(it1, it2, [](const auto& lhs, const auto& rhs) {
return stringCompare(lhs.name, rhs.name);
});
}
int PluginDesc::findPreset(std::string_view name) const {
for (int i = 0; i < presets.size(); ++i){
if (presets[i].name == name){
return i;
}
}
return -1;
}
bool PluginDesc::removePreset(int index, bool del){
if (index >= 0 && index < presets.size()
&& presets[index].type == PresetType::User
&& (!del || removeFile(presets[index].path))){
presets.erase(presets.begin() + index);
return true;
}
return false;
}
bool PluginDesc::renamePreset(int index, std::string_view newName){
// make preset before creating the lock!
if (index >= 0 && index < presets.size()
&& presets[index].type == PresetType::User){
auto preset = makePreset(newName);
if (!preset.name.empty()){
if (renameFile(presets[index].path, preset.path)){
presets[index] = std::move(preset);
sortPresets();
return true;
}
}
}
return false;
}
static std::string bashPath(std::string path){
for (auto& c : path) {
switch (c){
case '/':
case '\\':
case '\"':
case '?':
case '*':
case ':':
case '<':
case '>':
case '|':
c = '_';
break;
default:
break;
}
}
return path;
}
int PluginDesc::addPreset(Preset preset) {
auto it = presets.begin();
// insert lexicographically
while (it != presets.end() && it->type == PresetType::User){
if (preset.name == it->name){
// replace
*it = std::move(preset);
return (int)(it - presets.begin());
}
if (stringCompare(preset.name, it->name)){
break;
}
++it;
}
int index = (int)(it - presets.begin()); // before reallocation!
presets.insert(it, std::move(preset));
return index;
}
Preset PluginDesc::makePreset(std::string_view name, PresetType type) const {
Preset preset;
auto folder = getPresetFolder(type, true);
if (!folder.empty()){
preset.path = folder + "/" + bashPath(std::string{name}) +
(type_ == PluginType::VST3 ? ".vstpreset" : ".fxp");
preset.name = name;
preset.type = type;
}
return preset;
}
std::string PluginDesc::getPresetFolder(PresetType type, bool create) const {
auto location = getPresetLocation(type, type_);
if (!location.empty()){
auto vendorFolder = location + "/" + bashPath(vendor);
auto pluginFolder = vendorFolder + "/" + bashPath(name);
// create folder(s) if necessary
if (create && !didCreatePresetFolder && type == PresetType::User){
// LATER do some error handling
createDirectory(location);
createDirectory(vendorFolder);
createDirectory(pluginFolder);
didCreatePresetFolder = true;
}
#ifdef _WIN32
conformPath(pluginFolder);
#endif
return pluginFolder;
} else {
return "";
}
}
/// .ini file structure for each plugin:
///
/// [plugin]
/// path=<string>
/// name=<string>
/// vendor=<string>
/// category=<string>
/// version=<string>
/// sdkversion=<string>
/// id=<int>
/// inputs=<int>
/// outputs=<int>
/// flags=<int>
/// [parameters]
/// n=<int>
/// name,label,id,flags
/// name,label,id,flags
/// ...
/// [programs]
/// n=<int>
/// <program0>
/// <program1>
/// <program2>
/// ...
static std::string bashString(std::string name){
// replace "forbidden" characters
for (auto& c : name){
switch (c){
case ',':
case '\n':
case '\r':
c = '_';
break;
default:
break;
}
}
return name;
}
#define toHex(x) std::hex << (x) << std::dec
#define fromHex(x) std::stol(x, nullptr, 16); // hex
void writeBusses(std::ostream& file, const std::vector<PluginDesc::Bus>& vec){
int n = vec.size();
file << "n=" << n << "\n";
for (int i = 0; i < n; ++i){
file << vec[i].numChannels << ","
<< (int)vec[i].type << ","
<< bashString(vec[i].label) << "\n";
}
}
void PluginDesc::serialize(std::ostream& file) const {
// list sub plugins (only when probing)
if (!subPlugins.empty()){
file << "[subplugins]\n";
file << "n=" << (int)subPlugins.size() << "\n";
for (auto& sub : subPlugins){
file << sub.name << "," << toHex(sub.id) << "\n";
}
return;
}
file << "[plugin]\n";
file << "path=" << path() << "\n";
file << "id=" << uniqueID << "\n";
file << "name=" << name << "\n";
file << "vendor=" << vendor << "\n";
file << "category=" << category << "\n";
file << "version=" << version << "\n";
file << "sdkversion=" << sdkVersion << "\n";
file << "flags=" << toHex(flags) << "\n";
#if USE_VST3
if (programChange != NoParamID){
file << "pgmchange=" << toHex(programChange) << "\n";
}
if (bypass != NoParamID){
file << "bypass=" << toHex(bypass) << "\n";
}
#endif
// inputs
file << "[inputs]\n";
writeBusses(file, inputs);
// outputs
file << "[outputs]\n";
writeBusses(file, outputs);
// parameters
file << "[parameters]\n";
file << "n=" << parameters.size() << "\n";
for (auto& param : parameters) {
uint32_t flags = param.automatable;
file << bashString(param.name) << ","
<< bashString(param.label) << ","
<< toHex(param.id) << ","
<< toHex(flags) << "\n";
}
// programs
file << "[programs]\n";
file << "n=" << programs.size() << "\n";
for (auto& pgm : programs) {
file << pgm << "\n";
}
}
namespace {
std::string ltrim(std::string_view str) {
auto pos = str.find_first_not_of(" \t");
if (pos != std::string::npos && pos > 0){
return std::string{str.substr(pos)};
} else {
return std::string{str};
}
}
std::string rtrim(std::string_view str) {
auto pos = str.find_last_not_of(" \t") + 1;
if (pos != std::string::npos && pos < str.size()){
return std::string{str.substr(0, pos)};
} else {
return std::string{str};
}
}
bool isComment(std::string_view line){
auto c = line.front();
return c == ';' || c == '#';
}
std::pair<std::string, std::string> getKeyValuePair(std::string_view line) {
auto pos = line.find('=');
if (pos == std::string::npos){
throw Error("missing '=' after key: " + std::string{line});
}
auto key = rtrim(line.substr(0, pos));
auto value = ltrim(line.substr(pos + 1));
return std::make_pair(std::move(key), std::move(value));
}
std::vector<std::string> splitString(std::string_view str, char sep){
std::vector<std::string> result;
auto pos = 0;
while (true){
auto newpos = str.find(sep, pos);
if (newpos != std::string::npos){
int len = newpos - pos;
result.push_back(std::string{str.substr(pos, len)});
pos = newpos + 1;
} else {
result.push_back(std::string{str.substr(pos)}); // remaining string
break;
}
}
return result;
}
void parseArg(int32_t& lh, const std::string& rh){
lh = std::stol(rh); // decimal
}
void parseArg(uint32_t& lh, const std::string& rh){
lh = fromHex(rh);
}
void parseArg(std::string& lh, const std::string& rh){
lh = rh;
}
} // namespace
bool getLine(std::istream& stream, std::string& line){
std::string temp;
while (std::getline(stream, temp)){
if (!temp.empty() && !isComment(temp)){
line = std::move(temp);
return true;
}
}
return false;
}
int getCount(const std::string& line){
auto pos = line.find('=');
if (pos == std::string::npos){
throw Error("missing '=' after key: " + line);
}
try {
return std::stol(line.substr(pos + 1));
}
catch (...){
throw Error("expected number after 'n='");
}
}
std::vector<PluginDesc::Bus> readBusses(std::istream& file){
std::vector<PluginDesc::Bus> result;
std::string line;
std::getline(file, line);
int n = getCount(line);
while (n-- && std::getline(file, line)){
auto args = splitString(line, ',');
PluginDesc::Bus bus;
bus.numChannels = std::stol(args[0]);
bus.type = (PluginDesc::Bus::Type)std::stol(args[1]);
bus.label = ltrim(args[2]);
result.push_back(std::move(bus));
}
return result;
}
#define DEBUG_PARAMETERS 0
void PluginDesc::deserialize(std::istream& file, int versionMajor,
int versionMinor, int versionBugfix) {
// first check for sections, then for keys!
bool start = false;
bool future = (versionMajor > VERSION_MAJOR) ||
(versionMajor == VERSION_MAJOR && versionMinor > VERSION_MINOR);
std::string line;
while (getLine(file, line)){
if (line == "[plugin]"){
start = true;
} else if (line == "[inputs]"){
inputs = readBusses(file);
} else if (line == "[outputs]"){
outputs = readBusses(file);
} else if (line == "[parameters]"){
parameters.clear();
std::getline(file, line);
int n = getCount(line);
while (n-- && std::getline(file, line)){
auto args = splitString(line, ',');
Param param;
if (args.size() >= 2){
param.name = rtrim(args[0]);
param.label = ltrim(args[1]);
}
if (args.size() >= 3){
try {
param.id = fromHex(args[2]);
} catch (...) {
throw Error("bad parameter ID");
}
}
if (args.size() >= 4){
try {
auto flags = fromHex(args[3]);
param.automatable = flags & 1;
} catch (...) {
throw Error("bad parameter flags");
}
}
addParameter(std::move(param));
}
} else if (line == "[programs]"){
programs.clear();
std::getline(file, line);
int n = getCount(line);
while (n-- && std::getline(file, line)){
programs.push_back(line);
}
break; // done
} else if (line == "[subplugins]"){
// get list of subplugins (only when probing)
subPlugins.clear();
std::getline(file, line);
int n = getCount(line);
while (n-- && std::getline(file, line)){
auto pos = line.find(',');
SubPlugin sub;
sub.name = rtrim(line.substr(0, pos));
sub.id = fromHex(line.substr(pos + 1));
// LOG_DEBUG("got subplugin " << sub.name << " " << sub.id);
subPlugins.push_back(std::move(sub));
}
break; // done
} else if (start){
auto [key, value] = getKeyValuePair(line);
#define MATCH(name, field) else if (name == key) { parseArg(field, value); }
#define IGNORE(name) else if (name == key) {}
try {
if (key == "id"){
if (value.size() == 8){
type_ = PluginType::VST2;
sscanf(&value[0], "%08X", &id_.id);
} else if (value.size() == 32){
type_ = PluginType::VST3;
for (int i = 0; i < 16; ++i){
unsigned int temp;
sscanf(&value[i * 2], "%02X", &temp);
id_.uid[i] = temp;
}
} else {
throw Error("bad id!");
}
uniqueID = value;
}
MATCH("path", path_)
MATCH("name", name)
MATCH("vendor", vendor)
MATCH("category", category)
MATCH("version", version)
MATCH("sdkversion", sdkVersion)
#if USE_VST3
MATCH("pgmchange", programChange)
MATCH("bypass", bypass)
#else
IGNORE("pgmchange")
IGNORE("bypass")
#endif
MATCH("flags", flags) // hex
else {
if (future){
LOG_WARNING("VSTPlugin: unknown key: " << key);
} else {
throw Error("unknown key: " + key);
}
}
} catch (const Error&){
throw; // rethrow
} catch (const std::invalid_argument&) {
throw Error("invalid argument for key '" + key + "': " + value);
} catch (const std::out_of_range&) {
throw Error("out of range argument for key '" + key + "': " + value);
} catch (const std::exception& e){
throw Error("unknown error: " + std::string(e.what()));
}
} else {
if (future){
LOG_WARNING("VSTPlugin: bad data: " << line);
} else {
throw Error("bad data: " + line);
}
}
}
// restore "Bridge" flag
auto factory = factory_.lock();
if (factory && factory->arch() != getHostCpuArchitecture()){
flags |= Bridged;
}
#if WARN_VST3_PARAMETERS
// warn if VST3 plugin has any non-automatable parameters
// *before* automatable parameters. See WARN_VST3_PARAMETERS.
if (type() == PluginType::VST3) {
int lastAutomatable = -1;
int firstNonAutomatable = -1;
for (int i = 0; i < parameters.size(); ++i) {
auto& param = parameters[i];
if (param.automatable) {
lastAutomatable = i;
} else {
if (firstNonAutomatable < 0) {
firstNonAutomatable = i;
#if DEBUG_PARAMETERS
LOG_DEBUG("non-automatable parameters in '" << name << "':");
#endif
}
#if DEBUG_PARAMETERS
LOG_DEBUG(param.name);
#endif
}
}
if (firstNonAutomatable >= 0 && firstNonAutomatable < lastAutomatable) {
warnParameters = true;
#if DEBUG_PARAMETERS
LOG_DEBUG("automatable and non-automatable parameters intersect!");
#endif
}
}
#endif // WARN_VST3_PARAMETERS
}
} // vst
|