Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2577 lines
88KB

  1. /*
  2. * Carla VST Plugin
  3. * Copyright (C) 2011-2017 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaVstUtils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaPluginUI.hpp"
  22. #ifdef CARLA_OS_MAC
  23. # import <Foundation/Foundation.h>
  24. #endif
  25. #include <pthread.h>
  26. #include "water/memory/ByteOrder.h"
  27. #undef VST_FORCE_DEPRECATED
  28. #define VST_FORCE_DEPRECATED 0
  29. #undef kEffectMagic
  30. #define kEffectMagic (CCONST( 'V', 's', 't', 'P' ))
  31. using water::ByteOrder;
  32. CARLA_BACKEND_START_NAMESPACE
  33. // -----------------------------------------------------
  34. const uint PLUGIN_CAN_PROCESS_REPLACING = 0x1000;
  35. const uint PLUGIN_HAS_COCKOS_EXTENSIONS = 0x2000;
  36. const uint PLUGIN_USES_OLD_VSTSDK = 0x4000;
  37. const uint PLUGIN_WANTS_MIDI_INPUT = 0x8000;
  38. static const int32_t kVstMidiEventSize = static_cast<int32_t>(sizeof(VstMidiEvent));
  39. // -----------------------------------------------------
  40. class CarlaPluginVST2 : public CarlaPlugin,
  41. private CarlaPluginUI::Callback
  42. {
  43. public:
  44. CarlaPluginVST2(CarlaEngine* const engine, const uint id)
  45. : CarlaPlugin(engine, id),
  46. fUnique1(1),
  47. fEffect(nullptr),
  48. fMidiEventCount(0),
  49. fTimeInfo(),
  50. fNeedIdle(false),
  51. fLastChunk(nullptr),
  52. fIsProcessing(false),
  53. fMainThread(pthread_self()),
  54. #ifdef PTW32_DLLPORT
  55. fProcThread({nullptr, 0}),
  56. #else
  57. fProcThread(0),
  58. #endif
  59. #ifdef CARLA_OS_MAC
  60. fMacBundleRef(nullptr),
  61. fMacBundleRefNum(0),
  62. #endif
  63. fEvents(),
  64. fUI(),
  65. fUnique2(2)
  66. {
  67. carla_debug("CarlaPluginVST2::CarlaPluginVST2(%p, %i)", engine, id);
  68. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  69. carla_zeroStruct(fTimeInfo);
  70. for (ushort i=0; i < kPluginMaxMidiEvents*2; ++i)
  71. fEvents.data[i] = (VstEvent*)&fMidiEvents[i];
  72. // make plugin valid
  73. srand(id);
  74. fUnique1 = fUnique2 = rand();
  75. }
  76. ~CarlaPluginVST2() override
  77. {
  78. carla_debug("CarlaPluginVST2::~CarlaPluginVST2()");
  79. // close UI
  80. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  81. {
  82. showCustomUI(false);
  83. }
  84. pData->singleMutex.lock();
  85. pData->masterMutex.lock();
  86. if (pData->client != nullptr && pData->client->isActive())
  87. pData->client->deactivate();
  88. CARLA_ASSERT(! fIsProcessing);
  89. if (pData->active)
  90. {
  91. deactivate();
  92. pData->active = false;
  93. }
  94. if (fEffect != nullptr)
  95. {
  96. dispatcher(effClose, 0, 0, nullptr, 0.0f);
  97. fEffect = nullptr;
  98. }
  99. // make plugin invalid
  100. fUnique2 += 1;
  101. if (fLastChunk != nullptr)
  102. {
  103. std::free(fLastChunk);
  104. fLastChunk = nullptr;
  105. }
  106. clearBuffers();
  107. #ifdef CARLA_OS_MAC
  108. if (fMacBundleRef != nullptr)
  109. {
  110. CFBundleCloseBundleResourceMap(fMacBundleRef, fMacBundleRefNum);
  111. if (CFGetRetainCount(fMacBundleRef) == 1)
  112. CFBundleUnloadExecutable(fMacBundleRef);
  113. if (CFGetRetainCount(fMacBundleRef) > 0)
  114. CFRelease(fMacBundleRef);
  115. fMacBundleRef = nullptr;
  116. }
  117. #endif
  118. }
  119. // -------------------------------------------------------------------
  120. // Information (base)
  121. PluginType getType() const noexcept override
  122. {
  123. return PLUGIN_VST2;
  124. }
  125. PluginCategory getCategory() const noexcept override
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, CarlaPlugin::getCategory());
  128. const intptr_t category(dispatcher(effGetPlugCategory, 0, 0, nullptr, 0.0f));
  129. switch (category)
  130. {
  131. case kPlugCategSynth:
  132. return PLUGIN_CATEGORY_SYNTH;
  133. case kPlugCategAnalysis:
  134. return PLUGIN_CATEGORY_UTILITY;
  135. case kPlugCategMastering:
  136. return PLUGIN_CATEGORY_DYNAMICS;
  137. case kPlugCategRoomFx:
  138. return PLUGIN_CATEGORY_DELAY;
  139. case kPlugCategRestoration:
  140. return PLUGIN_CATEGORY_UTILITY;
  141. case kPlugCategGenerator:
  142. return PLUGIN_CATEGORY_SYNTH;
  143. }
  144. if (fEffect->flags & effFlagsIsSynth)
  145. return PLUGIN_CATEGORY_SYNTH;
  146. return CarlaPlugin::getCategory();
  147. }
  148. int64_t getUniqueId() const noexcept override
  149. {
  150. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  151. return static_cast<int64_t>(fEffect->uniqueID);
  152. }
  153. // -------------------------------------------------------------------
  154. // Information (count)
  155. // nothing
  156. // -------------------------------------------------------------------
  157. // Information (current data)
  158. std::size_t getChunkData(void** const dataPtr) noexcept override
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  161. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  162. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  163. *dataPtr = nullptr;
  164. try {
  165. const intptr_t ret = dispatcher(effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
  166. CARLA_SAFE_ASSERT_RETURN(ret >= 0, 0);
  167. return static_cast<std::size_t>(ret);
  168. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginVST2::getChunkData", 0);
  169. }
  170. // -------------------------------------------------------------------
  171. // Information (per-plugin data)
  172. uint getOptionsAvailable() const noexcept override
  173. {
  174. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  175. uint options = 0x0;
  176. // can't disable fixed buffers if using latency or MIDI output
  177. if (pData->latency.frames == 0 && ! hasMidiOutput())
  178. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  179. if (fEffect->numPrograms > 1)
  180. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  181. if (fEffect->flags & effFlagsProgramChunks)
  182. options |= PLUGIN_OPTION_USE_CHUNKS;
  183. if (hasMidiInput())
  184. {
  185. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  186. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  187. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  188. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  189. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  190. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  191. }
  192. return options;
  193. }
  194. float getParameterValue(const uint32_t parameterId) const noexcept override
  195. {
  196. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0.0f);
  197. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  198. return fEffect->getParameter(fEffect, static_cast<int32_t>(parameterId));
  199. }
  200. void getLabel(char* const strBuf) const noexcept override
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  203. strBuf[0] = '\0';
  204. dispatcher(effGetProductString, 0, 0, strBuf, 0.0f);
  205. }
  206. void getMaker(char* const strBuf) const noexcept override
  207. {
  208. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  209. strBuf[0] = '\0';
  210. dispatcher(effGetVendorString, 0, 0, strBuf, 0.0f);
  211. }
  212. void getCopyright(char* const strBuf) const noexcept override
  213. {
  214. getMaker(strBuf);
  215. }
  216. void getRealName(char* const strBuf) const noexcept override
  217. {
  218. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  219. strBuf[0] = '\0';
  220. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  221. }
  222. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  223. {
  224. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  225. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  226. strBuf[0] = '\0';
  227. VstParameterProperties prop;
  228. carla_zeroStruct(prop);
  229. if (dispatcher(effGetParameterProperties, static_cast<int32_t>(parameterId), 0, &prop, 0) == 1 && prop.label[0] != '\0')
  230. {
  231. std::strncpy(strBuf, prop.label, 64);
  232. strBuf[64] = '\0';
  233. return;
  234. }
  235. strBuf[0] = '\0';
  236. dispatcher(effGetParamName, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  237. }
  238. void getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  241. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  242. strBuf[0] = '\0';
  243. dispatcher(effGetParamDisplay, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  244. if (strBuf[0] == '\0')
  245. std::snprintf(strBuf, STR_MAX, "%f", getParameterValue(parameterId));
  246. }
  247. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  248. {
  249. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  250. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  251. strBuf[0] = '\0';
  252. dispatcher(effGetParamLabel, static_cast<int32_t>(parameterId), 0, strBuf, 0.0f);
  253. }
  254. // -------------------------------------------------------------------
  255. // Set data (state)
  256. // nothing
  257. // -------------------------------------------------------------------
  258. // Set data (internal stuff)
  259. void setName(const char* const newName) override
  260. {
  261. CarlaPlugin::setName(newName);
  262. if (fUI.window != nullptr)
  263. {
  264. CarlaString guiTitle(pData->name);
  265. guiTitle += " (GUI)";
  266. fUI.window->setTitle(guiTitle.buffer());
  267. }
  268. }
  269. // -------------------------------------------------------------------
  270. // Set data (plugin-specific stuff)
  271. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  272. {
  273. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  274. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  275. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  276. fEffect->setParameter(fEffect, static_cast<int32_t>(parameterId), fixedValue);
  277. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  278. }
  279. void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
  280. {
  281. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  282. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  283. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  284. fEffect->setParameter(fEffect, static_cast<int32_t>(parameterId), fixedValue);
  285. CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
  286. }
  287. void setChunkData(const void* const data, const std::size_t dataSize) override
  288. {
  289. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  290. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  291. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  292. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  293. if (loadOldSaveFormat(data, dataSize))
  294. return;
  295. if (fLastChunk != nullptr)
  296. std::free(fLastChunk);
  297. fLastChunk = std::malloc(dataSize);
  298. CARLA_SAFE_ASSERT_RETURN(fLastChunk != nullptr,);
  299. std::memcpy(fLastChunk, data, dataSize);
  300. {
  301. const ScopedSingleProcessLocker spl(this, true);
  302. dispatcher(effSetChunk, 0 /* bank */, static_cast<intptr_t>(dataSize), fLastChunk, 0.0f);
  303. }
  304. // simulate an updateDisplay callback
  305. handleAudioMasterCallback(audioMasterUpdateDisplay, 0, 0, nullptr, 0.0f);
  306. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  307. const bool sendOsc(pData->engine->isOscControlRegistered());
  308. #else
  309. const bool sendOsc(false);
  310. #endif
  311. pData->updateParameterValues(this, sendOsc, true, false);
  312. }
  313. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  314. {
  315. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  316. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  317. if (index >= 0)
  318. {
  319. try {
  320. dispatcher(effBeginSetProgram, 0, 0, nullptr, 0.0f);
  321. } CARLA_SAFE_EXCEPTION_RETURN("effBeginSetProgram",);
  322. {
  323. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  324. try {
  325. dispatcher(effSetProgram, 0, index, nullptr, 0.0f);
  326. } CARLA_SAFE_EXCEPTION("effSetProgram");
  327. }
  328. try {
  329. dispatcher(effEndSetProgram, 0, 0, nullptr, 0.0f);
  330. } CARLA_SAFE_EXCEPTION("effEndSetProgram");
  331. }
  332. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  333. }
  334. // -------------------------------------------------------------------
  335. // Set ui stuff
  336. void showCustomUI(const bool yesNo) override
  337. {
  338. if (fUI.isVisible == yesNo)
  339. return;
  340. if (yesNo)
  341. {
  342. CarlaString uiTitle(pData->name);
  343. uiTitle += " (GUI)";
  344. intptr_t value = 0;
  345. void* vstPtr = nullptr;
  346. ERect* vstRect = nullptr;
  347. if (fUI.window == nullptr)
  348. {
  349. const char* msg = nullptr;
  350. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  351. #if defined(CARLA_OS_MAC) && defined(CARLA_OS_64BIT)
  352. fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId, false);
  353. #elif defined(CARLA_OS_WIN)
  354. fUI.window = CarlaPluginUI::newWindows(this, frontendWinId, false);
  355. #elif defined(HAVE_X11)
  356. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, false);
  357. #else
  358. msg = "Unknown UI type";
  359. #endif
  360. if (fUI.window == nullptr)
  361. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  362. fUI.window->setTitle(uiTitle.buffer());
  363. }
  364. vstPtr = fUI.window->getPtr();
  365. dispatcher(effEditGetRect, 0, 0, &vstRect, 0.0f);
  366. #ifdef HAVE_X11
  367. value = (intptr_t)fUI.window->getDisplay();
  368. #endif
  369. if (dispatcher(effEditOpen, 0, value, vstPtr, 0.0f) != 0)
  370. {
  371. if (vstRect == nullptr || vstRect->right - vstRect->left < 2)
  372. dispatcher(effEditGetRect, 0, 0, &vstRect, 0.0f);
  373. if (vstRect != nullptr)
  374. {
  375. const int width(vstRect->right - vstRect->left);
  376. const int height(vstRect->bottom - vstRect->top);
  377. CARLA_SAFE_ASSERT_INT2(width > 1 && height > 1, width, height);
  378. if (width > 1 && height > 1)
  379. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  380. }
  381. fUI.window->show();
  382. fUI.isVisible = true;
  383. }
  384. else
  385. {
  386. delete fUI.window;
  387. fUI.window = nullptr;
  388. carla_stderr2("Plugin refused to open its own UI");
  389. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  390. }
  391. }
  392. else
  393. {
  394. fUI.isVisible = false;
  395. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  396. fUI.window->hide();
  397. dispatcher(effEditClose, 0, 0, nullptr, 0.0f);
  398. }
  399. }
  400. void idle() override
  401. {
  402. if (fNeedIdle)
  403. dispatcher(effIdle, 0, 0, nullptr, 0.0f);
  404. CarlaPlugin::idle();
  405. }
  406. void uiIdle() override
  407. {
  408. if (fUI.window != nullptr)
  409. {
  410. fUI.window->idle();
  411. if (fUI.isVisible)
  412. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  413. }
  414. CarlaPlugin::uiIdle();
  415. }
  416. // -------------------------------------------------------------------
  417. // Plugin state
  418. void reload() override
  419. {
  420. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  421. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  422. carla_debug("CarlaPluginVST2::reload() - start");
  423. const EngineProcessMode processMode(pData->engine->getProccessMode());
  424. // Safely disable plugin for reload
  425. const ScopedDisabler sd(this);
  426. if (pData->active)
  427. deactivate();
  428. clearBuffers();
  429. uint32_t aIns, aOuts, mIns, mOuts, params;
  430. bool needsCtrlIn, needsCtrlOut;
  431. needsCtrlIn = needsCtrlOut = false;
  432. aIns = (fEffect->numInputs > 0) ? static_cast<uint32_t>(fEffect->numInputs) : 0;
  433. aOuts = (fEffect->numOutputs > 0) ? static_cast<uint32_t>(fEffect->numOutputs) : 0;
  434. params = (fEffect->numParams > 0) ? static_cast<uint32_t>(fEffect->numParams) : 0;
  435. if (hasMidiInput())
  436. {
  437. mIns = 1;
  438. needsCtrlIn = true;
  439. }
  440. else
  441. mIns = 0;
  442. if (hasMidiOutput())
  443. {
  444. mOuts = 1;
  445. needsCtrlOut = true;
  446. }
  447. else
  448. mOuts = 0;
  449. if (aIns > 0)
  450. {
  451. pData->audioIn.createNew(aIns);
  452. }
  453. if (aOuts > 0)
  454. {
  455. pData->audioOut.createNew(aOuts);
  456. needsCtrlIn = true;
  457. }
  458. if (params > 0)
  459. {
  460. pData->param.createNew(params, false);
  461. needsCtrlIn = true;
  462. }
  463. const uint portNameSize(pData->engine->getMaxPortNameSize());
  464. CarlaString portName;
  465. // Audio Ins
  466. for (uint32_t j=0; j < aIns; ++j)
  467. {
  468. portName.clear();
  469. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  470. {
  471. portName = pData->name;
  472. portName += ":";
  473. }
  474. if (aIns > 1)
  475. {
  476. portName += "input_";
  477. portName += CarlaString(j+1);
  478. }
  479. else
  480. portName += "input";
  481. portName.truncate(portNameSize);
  482. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  483. pData->audioIn.ports[j].rindex = j;
  484. }
  485. // Audio Outs
  486. for (uint32_t j=0; j < aOuts; ++j)
  487. {
  488. portName.clear();
  489. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  490. {
  491. portName = pData->name;
  492. portName += ":";
  493. }
  494. if (aOuts > 1)
  495. {
  496. portName += "output_";
  497. portName += CarlaString(j+1);
  498. }
  499. else
  500. portName += "output";
  501. portName.truncate(portNameSize);
  502. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  503. pData->audioOut.ports[j].rindex = j;
  504. }
  505. for (uint32_t j=0; j < params; ++j)
  506. {
  507. pData->param.data[j].type = PARAMETER_INPUT;
  508. pData->param.data[j].index = static_cast<int32_t>(j);
  509. pData->param.data[j].rindex = static_cast<int32_t>(j);
  510. float min, max, def, step, stepSmall, stepLarge;
  511. VstParameterProperties prop;
  512. carla_zeroStruct(prop);
  513. if (pData->hints & PLUGIN_HAS_COCKOS_EXTENSIONS)
  514. {
  515. double vrange[2] = { 0.0, 1.0 };
  516. bool isInteger = false;
  517. if (static_cast<uintptr_t>(dispatcher(effVendorSpecific, static_cast<int32_t>(0xdeadbef0), static_cast<int32_t>(j), vrange, 0.0f)) >= 0xbeef)
  518. {
  519. min = static_cast<float>(vrange[0]);
  520. max = static_cast<float>(vrange[1]);
  521. if (min > max)
  522. {
  523. carla_stderr2("WARNING - Broken plugin parameter min > max (with cockos extensions)");
  524. min = max - 0.1f;
  525. }
  526. else if (carla_isEqual(min, max))
  527. {
  528. carla_stderr2("WARNING - Broken plugin parameter min == max (with cockos extensions)");
  529. max = min + 0.1f;
  530. }
  531. // only use values as integer if we have a proper range
  532. if (max - min >= 1.0f)
  533. isInteger = dispatcher(effVendorSpecific, kVstParameterUsesIntStep, static_cast<int32_t>(j), nullptr, 0.0f) >= 0xbeef;
  534. }
  535. else
  536. {
  537. min = 0.0f;
  538. max = 1.0f;
  539. }
  540. if (isInteger)
  541. {
  542. step = 1.0f;
  543. stepSmall = 1.0f;
  544. stepLarge = 10.0f;
  545. }
  546. else
  547. {
  548. const float range = max - min;
  549. step = range/100.0f;
  550. stepSmall = range/1000.0f;
  551. stepLarge = range/10.0f;
  552. }
  553. }
  554. else if (dispatcher(effGetParameterProperties, static_cast<int32_t>(j), 0, &prop, 0) == 1)
  555. {
  556. if (prop.flags & kVstParameterUsesIntegerMinMax)
  557. {
  558. min = static_cast<float>(prop.minInteger);
  559. max = static_cast<float>(prop.maxInteger);
  560. if (min > max)
  561. {
  562. carla_stderr2("WARNING - Broken plugin parameter min > max");
  563. min = max - 0.1f;
  564. }
  565. else if (carla_isEqual(min, max))
  566. {
  567. carla_stderr2("WARNING - Broken plugin parameter min == max");
  568. max = min + 0.1f;
  569. }
  570. }
  571. else
  572. {
  573. min = 0.0f;
  574. max = 1.0f;
  575. }
  576. if (prop.flags & kVstParameterIsSwitch)
  577. {
  578. step = max - min;
  579. stepSmall = step;
  580. stepLarge = step;
  581. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  582. }
  583. else if (prop.flags & kVstParameterUsesIntStep)
  584. {
  585. step = static_cast<float>(prop.stepInteger);
  586. stepSmall = static_cast<float>(prop.stepInteger)/10.0f;
  587. stepLarge = static_cast<float>(prop.largeStepInteger);
  588. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  589. }
  590. else if (prop.flags & kVstParameterUsesFloatStep)
  591. {
  592. step = prop.stepFloat;
  593. stepSmall = prop.smallStepFloat;
  594. stepLarge = prop.largeStepFloat;
  595. }
  596. else
  597. {
  598. const float range = max - min;
  599. step = range/100.0f;
  600. stepSmall = range/1000.0f;
  601. stepLarge = range/10.0f;
  602. }
  603. if (prop.flags & kVstParameterCanRamp)
  604. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  605. }
  606. else
  607. {
  608. min = 0.0f;
  609. max = 1.0f;
  610. step = 0.001f;
  611. stepSmall = 0.0001f;
  612. stepLarge = 0.1f;
  613. }
  614. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  615. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  616. if ((pData->hints & PLUGIN_USES_OLD_VSTSDK) != 0 || dispatcher(effCanBeAutomated, static_cast<int32_t>(j), 0, nullptr, 0.0f) == 1)
  617. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  618. // no such thing as VST default parameters
  619. def = fEffect->getParameter(fEffect, static_cast<int32_t>(j));
  620. if (def < min)
  621. def = min;
  622. else if (def > max)
  623. def = max;
  624. pData->param.ranges[j].min = min;
  625. pData->param.ranges[j].max = max;
  626. pData->param.ranges[j].def = def;
  627. pData->param.ranges[j].step = step;
  628. pData->param.ranges[j].stepSmall = stepSmall;
  629. pData->param.ranges[j].stepLarge = stepLarge;
  630. }
  631. if (needsCtrlIn)
  632. {
  633. portName.clear();
  634. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  635. {
  636. portName = pData->name;
  637. portName += ":";
  638. }
  639. portName += "events-in";
  640. portName.truncate(portNameSize);
  641. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  642. }
  643. if (needsCtrlOut)
  644. {
  645. portName.clear();
  646. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  647. {
  648. portName = pData->name;
  649. portName += ":";
  650. }
  651. portName += "events-out";
  652. portName.truncate(portNameSize);
  653. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  654. }
  655. // plugin hints
  656. const intptr_t vstCategory = dispatcher(effGetPlugCategory, 0, 0, nullptr, 0.0f);
  657. pData->hints = 0x0;
  658. if (vstCategory == kPlugCategSynth || vstCategory == kPlugCategGenerator)
  659. pData->hints |= PLUGIN_IS_SYNTH;
  660. if (fEffect->flags & effFlagsHasEditor)
  661. {
  662. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  663. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  664. }
  665. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  666. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  667. if ((fEffect->flags & effFlagsCanReplacing) != 0 && fEffect->processReplacing != fEffect->process)
  668. pData->hints |= PLUGIN_CAN_PROCESS_REPLACING;
  669. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>("hasCockosExtensions"), 0.0f)) == 0xbeef0000)
  670. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  671. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  672. pData->hints |= PLUGIN_CAN_DRYWET;
  673. if (aOuts > 0)
  674. pData->hints |= PLUGIN_CAN_VOLUME;
  675. if (aOuts >= 2 && aOuts % 2 == 0)
  676. pData->hints |= PLUGIN_CAN_BALANCE;
  677. // extra plugin hints
  678. pData->extraHints = 0x0;
  679. if (mIns > 0)
  680. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  681. if (mOuts > 0)
  682. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  683. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  684. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  685. // dummy pre-start to get latency and wantEvents() on old plugins
  686. {
  687. activate();
  688. deactivate();
  689. }
  690. // check initial latency
  691. char* const empty3Ptr = &fEffect->empty3[0];
  692. int32_t initialDelay = *(int32_t*)empty3Ptr;
  693. const uint32_t latency = (initialDelay > 0) ? static_cast<uint32_t>(initialDelay) : 0;
  694. if (latency != 0)
  695. {
  696. pData->client->setLatency(latency);
  697. #ifndef BUILD_BRIDGE
  698. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  699. #endif
  700. }
  701. //bufferSizeChanged(pData->engine->getBufferSize());
  702. reloadPrograms(true);
  703. if (pData->active)
  704. activate();
  705. carla_debug("CarlaPluginVST2::reload() - end");
  706. }
  707. void reloadPrograms(const bool doInit) override
  708. {
  709. carla_debug("CarlaPluginVST2::reloadPrograms(%s)", bool2str(doInit));
  710. const uint32_t oldCount = pData->prog.count;
  711. const int32_t current = pData->prog.current;
  712. // Delete old programs
  713. pData->prog.clear();
  714. // Query new programs
  715. uint32_t newCount = (fEffect->numPrograms > 0) ? static_cast<uint32_t>(fEffect->numPrograms) : 0;
  716. if (newCount > 0)
  717. {
  718. pData->prog.createNew(newCount);
  719. // Update names
  720. for (int32_t i=0; i < fEffect->numPrograms; ++i)
  721. {
  722. char strBuf[STR_MAX+1] = { '\0' };
  723. if (dispatcher(effGetProgramNameIndexed, i, 0, strBuf, 0.0f) != 1)
  724. {
  725. // program will be [re-]changed later
  726. dispatcher(effSetProgram, 0, i, nullptr, 0.0f);
  727. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  728. }
  729. pData->prog.names[i] = carla_strdup(strBuf);
  730. }
  731. }
  732. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  733. // Update OSC Names
  734. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  735. {
  736. pData->engine->oscSend_control_set_program_count(pData->id, newCount);
  737. for (uint32_t i=0; i < newCount; ++i)
  738. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  739. }
  740. #endif
  741. if (doInit)
  742. {
  743. if (newCount > 0)
  744. setProgram(0, false, false, false);
  745. else
  746. dispatcher(effSetProgram, 0, 0, nullptr, 0.0f);
  747. }
  748. else
  749. {
  750. // Check if current program is invalid
  751. bool programChanged = false;
  752. if (newCount == oldCount+1)
  753. {
  754. // one program added, probably created by user
  755. pData->prog.current = static_cast<int32_t>(oldCount);
  756. programChanged = true;
  757. }
  758. else if (current < 0 && newCount > 0)
  759. {
  760. // programs exist now, but not before
  761. pData->prog.current = 0;
  762. programChanged = true;
  763. }
  764. else if (current >= 0 && newCount == 0)
  765. {
  766. // programs existed before, but not anymore
  767. pData->prog.current = -1;
  768. programChanged = true;
  769. }
  770. else if (current >= static_cast<int32_t>(newCount))
  771. {
  772. // current program > count
  773. pData->prog.current = 0;
  774. programChanged = true;
  775. }
  776. else
  777. {
  778. // no change
  779. pData->prog.current = current;
  780. }
  781. if (programChanged)
  782. {
  783. setProgram(pData->prog.current, true, true, true);
  784. }
  785. else
  786. {
  787. // Program was changed during update, re-set it
  788. if (pData->prog.current >= 0)
  789. dispatcher(effSetProgram, 0, pData->prog.current, nullptr, 0.0f);
  790. }
  791. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  792. }
  793. }
  794. // -------------------------------------------------------------------
  795. // Plugin processing
  796. void activate() noexcept override
  797. {
  798. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  799. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  800. dispatcher(effSetBlockSizeAndSampleRate, 0,
  801. static_cast<int32_t>(pData->engine->getBufferSize()), nullptr,
  802. static_cast<float>(pData->engine->getSampleRate()));
  803. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  804. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  805. try {
  806. dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  807. } catch(...) {}
  808. try {
  809. dispatcher(effStartProcess, 0, 0, nullptr, 0.0f);
  810. } catch(...) {}
  811. }
  812. void deactivate() noexcept override
  813. {
  814. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  815. try {
  816. dispatcher(effStopProcess, 0, 0, nullptr, 0.0f);
  817. } catch(...) {}
  818. try {
  819. dispatcher(effMainsChanged, 0, 0, nullptr, 0.0f);
  820. } catch(...) {}
  821. }
  822. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  823. {
  824. fProcThread = pthread_self();
  825. // --------------------------------------------------------------------------------------------------------
  826. // Check if active
  827. if (! pData->active)
  828. {
  829. // disable any output sound
  830. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  831. carla_zeroFloats(audioOut[i], frames);
  832. return;
  833. }
  834. fMidiEventCount = 0;
  835. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  836. // --------------------------------------------------------------------------------------------------------
  837. // Check if needs reset
  838. if (pData->needsReset)
  839. {
  840. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  841. {
  842. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  843. for (uint8_t i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  844. {
  845. fMidiEvents[k].type = kVstMidiType;
  846. fMidiEvents[k].byteSize = kVstMidiEventSize;
  847. fMidiEvents[k].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  848. fMidiEvents[k].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  849. fMidiEvents[k+i].type = kVstMidiType;
  850. fMidiEvents[k+i].byteSize = kVstMidiEventSize;
  851. fMidiEvents[k+i].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  852. fMidiEvents[k+i].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  853. }
  854. }
  855. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  856. {
  857. fMidiEventCount = MAX_MIDI_NOTE;
  858. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  859. {
  860. fMidiEvents[i].type = kVstMidiType;
  861. fMidiEvents[i].byteSize = kVstMidiEventSize;
  862. fMidiEvents[i].midiData[0] = char(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  863. fMidiEvents[i].midiData[1] = char(i);
  864. }
  865. }
  866. pData->needsReset = false;
  867. }
  868. // --------------------------------------------------------------------------------------------------------
  869. // Set TimeInfo
  870. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  871. fTimeInfo.flags = kVstTransportChanged;
  872. if (timeInfo.playing)
  873. fTimeInfo.flags |= kVstTransportPlaying;
  874. fTimeInfo.samplePos = double(timeInfo.frame);
  875. fTimeInfo.sampleRate = pData->engine->getSampleRate();
  876. if (timeInfo.usecs != 0)
  877. {
  878. fTimeInfo.nanoSeconds = double(timeInfo.usecs)/1000.0;
  879. fTimeInfo.flags |= kVstNanosValid;
  880. }
  881. if (timeInfo.bbt.valid)
  882. {
  883. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
  884. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
  885. const double ppqBar = double(timeInfo.bbt.bar - 1) * timeInfo.bbt.beatsPerBar;
  886. const double ppqBeat = double(timeInfo.bbt.beat - 1);
  887. const double ppqTick = double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat;
  888. // PPQ Pos
  889. fTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  890. fTimeInfo.flags |= kVstPpqPosValid;
  891. // Tempo
  892. fTimeInfo.tempo = timeInfo.bbt.beatsPerMinute;
  893. fTimeInfo.flags |= kVstTempoValid;
  894. // Bars
  895. fTimeInfo.barStartPos = ppqBar;
  896. fTimeInfo.flags |= kVstBarsValid;
  897. // Time Signature
  898. fTimeInfo.timeSigNumerator = static_cast<int32_t>(timeInfo.bbt.beatsPerBar);
  899. fTimeInfo.timeSigDenominator = static_cast<int32_t>(timeInfo.bbt.beatType);
  900. fTimeInfo.flags |= kVstTimeSigValid;
  901. }
  902. else
  903. {
  904. // Tempo
  905. fTimeInfo.tempo = 120.0;
  906. fTimeInfo.flags |= kVstTempoValid;
  907. // Time Signature
  908. fTimeInfo.timeSigNumerator = 4;
  909. fTimeInfo.timeSigDenominator = 4;
  910. fTimeInfo.flags |= kVstTimeSigValid;
  911. // Missing info
  912. fTimeInfo.ppqPos = 0.0;
  913. fTimeInfo.barStartPos = 0.0;
  914. }
  915. // --------------------------------------------------------------------------------------------------------
  916. // Event Input and Processing
  917. if (pData->event.portIn != nullptr)
  918. {
  919. // ----------------------------------------------------------------------------------------------------
  920. // MIDI Input (External)
  921. if (pData->extNotes.mutex.tryLock())
  922. {
  923. ExternalMidiNote note = { 0, 0, 0 };
  924. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  925. {
  926. note = pData->extNotes.data.getFirst(note, true);
  927. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  928. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  929. vstMidiEvent.type = kVstMidiType;
  930. vstMidiEvent.byteSize = kVstMidiEventSize;
  931. vstMidiEvent.midiData[0] = char((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  932. vstMidiEvent.midiData[1] = char(note.note);
  933. vstMidiEvent.midiData[2] = char(note.velo);
  934. }
  935. pData->extNotes.mutex.unlock();
  936. } // End of MIDI Input (External)
  937. // ----------------------------------------------------------------------------------------------------
  938. // Event Input (System)
  939. #ifndef BUILD_BRIDGE
  940. bool allNotesOffSent = false;
  941. #endif
  942. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  943. uint32_t startTime = 0;
  944. uint32_t timeOffset = 0;
  945. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  946. {
  947. const EngineEvent& event(pData->event.portIn->getEvent(i));
  948. if (event.time >= frames)
  949. continue;
  950. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  951. if (isSampleAccurate && event.time > timeOffset)
  952. {
  953. if (processSingle(audioIn, audioOut, event.time - timeOffset, timeOffset))
  954. {
  955. startTime = 0;
  956. timeOffset = event.time;
  957. if (fMidiEventCount > 0)
  958. {
  959. carla_zeroStructs(fMidiEvents, fMidiEventCount);
  960. fMidiEventCount = 0;
  961. }
  962. }
  963. else
  964. startTime += timeOffset;
  965. }
  966. switch (event.type)
  967. {
  968. case kEngineEventTypeNull:
  969. break;
  970. case kEngineEventTypeControl: {
  971. const EngineControlEvent& ctrlEvent(event.ctrl);
  972. switch (ctrlEvent.type)
  973. {
  974. case kEngineControlEventTypeNull:
  975. break;
  976. case kEngineControlEventTypeParameter: {
  977. #ifndef BUILD_BRIDGE
  978. // Control backend stuff
  979. if (event.channel == pData->ctrlChannel)
  980. {
  981. float value;
  982. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  983. {
  984. value = ctrlEvent.value;
  985. setDryWetRT(value);
  986. }
  987. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  988. {
  989. value = ctrlEvent.value*127.0f/100.0f;
  990. setVolumeRT(value);
  991. }
  992. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  993. {
  994. float left, right;
  995. value = ctrlEvent.value/0.5f - 1.0f;
  996. if (value < 0.0f)
  997. {
  998. left = -1.0f;
  999. right = (value*2.0f)+1.0f;
  1000. }
  1001. else if (value > 0.0f)
  1002. {
  1003. left = (value*2.0f)-1.0f;
  1004. right = 1.0f;
  1005. }
  1006. else
  1007. {
  1008. left = -1.0f;
  1009. right = 1.0f;
  1010. }
  1011. setBalanceLeftRT(left);
  1012. setBalanceRightRT(right);
  1013. }
  1014. }
  1015. #endif
  1016. // Control plugin parameters
  1017. uint32_t k;
  1018. for (k=0; k < pData->param.count; ++k)
  1019. {
  1020. if (pData->param.data[k].midiChannel != event.channel)
  1021. continue;
  1022. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1023. continue;
  1024. if (pData->param.data[k].type != PARAMETER_INPUT)
  1025. continue;
  1026. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1027. continue;
  1028. float value;
  1029. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1030. {
  1031. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1032. }
  1033. else
  1034. {
  1035. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1036. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1037. else
  1038. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1039. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1040. value = std::rint(value);
  1041. }
  1042. setParameterValueRT(k, value);
  1043. }
  1044. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1045. {
  1046. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1047. continue;
  1048. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1049. carla_zeroStruct(vstMidiEvent);
  1050. vstMidiEvent.type = kVstMidiType;
  1051. vstMidiEvent.byteSize = kVstMidiEventSize;
  1052. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1053. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1054. vstMidiEvent.midiData[1] = char(ctrlEvent.param);
  1055. vstMidiEvent.midiData[2] = char(ctrlEvent.value*127.0f);
  1056. }
  1057. break;
  1058. } // case kEngineControlEventTypeParameter
  1059. case kEngineControlEventTypeMidiBank:
  1060. if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1061. {
  1062. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1063. continue;
  1064. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1065. carla_zeroStruct(vstMidiEvent);
  1066. vstMidiEvent.type = kVstMidiType;
  1067. vstMidiEvent.byteSize = kVstMidiEventSize;
  1068. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1069. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1070. vstMidiEvent.midiData[1] = MIDI_CONTROL_BANK_SELECT;
  1071. vstMidiEvent.midiData[2] = char(ctrlEvent.param);
  1072. }
  1073. break;
  1074. case kEngineControlEventTypeMidiProgram:
  1075. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1076. {
  1077. if (ctrlEvent.param < pData->prog.count)
  1078. {
  1079. setProgram(ctrlEvent.param, false, false, false);
  1080. pData->postponeRtEvent(kPluginPostRtEventProgramChange, ctrlEvent.param, 0, 0.0f);
  1081. break;
  1082. }
  1083. }
  1084. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1085. {
  1086. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1087. continue;
  1088. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1089. carla_zeroStruct(vstMidiEvent);
  1090. vstMidiEvent.type = kVstMidiType;
  1091. vstMidiEvent.byteSize = kVstMidiEventSize;
  1092. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1093. vstMidiEvent.midiData[0] = char(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1094. vstMidiEvent.midiData[1] = char(ctrlEvent.param);
  1095. }
  1096. break;
  1097. case kEngineControlEventTypeAllSoundOff:
  1098. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1099. {
  1100. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1101. continue;
  1102. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1103. carla_zeroStruct(vstMidiEvent);
  1104. vstMidiEvent.type = kVstMidiType;
  1105. vstMidiEvent.byteSize = kVstMidiEventSize;
  1106. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1107. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1108. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1109. }
  1110. break;
  1111. case kEngineControlEventTypeAllNotesOff:
  1112. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1113. {
  1114. #ifndef BUILD_BRIDGE
  1115. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1116. {
  1117. allNotesOffSent = true;
  1118. sendMidiAllNotesOffToCallback();
  1119. }
  1120. #endif
  1121. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1122. continue;
  1123. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1124. carla_zeroStruct(vstMidiEvent);
  1125. vstMidiEvent.type = kVstMidiType;
  1126. vstMidiEvent.byteSize = kVstMidiEventSize;
  1127. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1128. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1129. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1130. }
  1131. break;
  1132. } // switch (ctrlEvent.type)
  1133. break;
  1134. } // case kEngineEventTypeControl
  1135. case kEngineEventTypeMidi: {
  1136. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1137. continue;
  1138. const EngineMidiEvent& midiEvent(event.midi);
  1139. if (midiEvent.size > 3)
  1140. continue;
  1141. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1142. static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1143. #endif
  1144. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1145. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1146. continue;
  1147. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1148. continue;
  1149. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1150. continue;
  1151. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1152. continue;
  1153. // Fix bad note-off
  1154. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1155. status = MIDI_STATUS_NOTE_OFF;
  1156. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1157. carla_zeroStruct(vstMidiEvent);
  1158. vstMidiEvent.type = kVstMidiType;
  1159. vstMidiEvent.byteSize = kVstMidiEventSize;
  1160. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1161. vstMidiEvent.midiData[0] = char(status | (event.channel & MIDI_CHANNEL_BIT));
  1162. vstMidiEvent.midiData[1] = char(midiEvent.size >= 2 ? midiEvent.data[1] : 0);
  1163. vstMidiEvent.midiData[2] = char(midiEvent.size >= 3 ? midiEvent.data[2] : 0);
  1164. if (status == MIDI_STATUS_NOTE_ON)
  1165. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1166. else if (status == MIDI_STATUS_NOTE_OFF)
  1167. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1168. } break;
  1169. } // switch (event.type)
  1170. }
  1171. pData->postRtEvents.trySplice();
  1172. if (frames > timeOffset)
  1173. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1174. } // End of Event Input and Processing
  1175. // --------------------------------------------------------------------------------------------------------
  1176. // Plugin processing (no events)
  1177. else
  1178. {
  1179. processSingle(audioIn, audioOut, frames, 0);
  1180. } // End of Plugin processing (no events)
  1181. // --------------------------------------------------------------------------------------------------------
  1182. // MIDI Output
  1183. if (pData->event.portOut != nullptr)
  1184. {
  1185. // reverse lookup MIDI events
  1186. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1187. {
  1188. if (fMidiEvents[k].type == 0)
  1189. break;
  1190. const VstMidiEvent& vstMidiEvent(fMidiEvents[k]);
  1191. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.deltaFrames >= 0);
  1192. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.midiData[0] != 0);
  1193. uint8_t midiData[3];
  1194. midiData[0] = static_cast<uint8_t>(vstMidiEvent.midiData[0]);
  1195. midiData[1] = static_cast<uint8_t>(vstMidiEvent.midiData[1]);
  1196. midiData[2] = static_cast<uint8_t>(vstMidiEvent.midiData[2]);
  1197. if (! pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(vstMidiEvent.deltaFrames), 3, midiData))
  1198. break;
  1199. }
  1200. } // End of MIDI Output
  1201. }
  1202. bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1203. {
  1204. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1205. if (pData->audioIn.count > 0)
  1206. {
  1207. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1208. }
  1209. if (pData->audioOut.count > 0)
  1210. {
  1211. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1212. }
  1213. // --------------------------------------------------------------------------------------------------------
  1214. // Try lock, silence otherwise
  1215. if (pData->engine->isOffline())
  1216. {
  1217. pData->singleMutex.lock();
  1218. }
  1219. else if (! pData->singleMutex.tryLock())
  1220. {
  1221. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1222. {
  1223. for (uint32_t k=0; k < frames; ++k)
  1224. outBuffer[i][k+timeOffset] = 0.0f;
  1225. }
  1226. return false;
  1227. }
  1228. // --------------------------------------------------------------------------------------------------------
  1229. // Set audio buffers
  1230. float* vstInBuffer[pData->audioIn.count];
  1231. float* vstOutBuffer[pData->audioOut.count];
  1232. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1233. vstInBuffer[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1234. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1235. vstOutBuffer[i] = outBuffer[i]+timeOffset;
  1236. // --------------------------------------------------------------------------------------------------------
  1237. // Set MIDI events
  1238. if (fMidiEventCount > 0)
  1239. {
  1240. fEvents.numEvents = static_cast<int32_t>(fMidiEventCount);
  1241. fEvents.reserved = 0;
  1242. dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
  1243. }
  1244. // --------------------------------------------------------------------------------------------------------
  1245. // Run plugin
  1246. fIsProcessing = true;
  1247. if (pData->hints & PLUGIN_CAN_PROCESS_REPLACING)
  1248. {
  1249. fEffect->processReplacing(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1250. }
  1251. else
  1252. {
  1253. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1254. carla_zeroFloats(vstOutBuffer[i], frames);
  1255. #if ! VST_FORCE_DEPRECATED
  1256. fEffect->process(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1257. #endif
  1258. }
  1259. fIsProcessing = false;
  1260. fTimeInfo.samplePos += frames;
  1261. #ifndef BUILD_BRIDGE
  1262. // --------------------------------------------------------------------------------------------------------
  1263. // Post-processing (dry/wet, volume and balance)
  1264. {
  1265. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1266. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1267. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1268. bool isPair;
  1269. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1270. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1271. {
  1272. // Dry/Wet
  1273. if (doDryWet)
  1274. {
  1275. for (uint32_t k=0; k < frames; ++k)
  1276. {
  1277. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  1278. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1279. }
  1280. }
  1281. // Balance
  1282. if (doBalance)
  1283. {
  1284. isPair = (i % 2 == 0);
  1285. if (isPair)
  1286. {
  1287. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1288. carla_copyFloats(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1289. }
  1290. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1291. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1292. for (uint32_t k=0; k < frames; ++k)
  1293. {
  1294. if (isPair)
  1295. {
  1296. // left
  1297. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1298. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1299. }
  1300. else
  1301. {
  1302. // right
  1303. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1304. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1305. }
  1306. }
  1307. }
  1308. // Volume
  1309. if (doVolume)
  1310. {
  1311. for (uint32_t k=0; k < frames; ++k)
  1312. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1313. }
  1314. }
  1315. } // End of Post-processing
  1316. #endif
  1317. // --------------------------------------------------------------------------------------------------------
  1318. pData->singleMutex.unlock();
  1319. return true;
  1320. }
  1321. void bufferSizeChanged(const uint32_t newBufferSize) override
  1322. {
  1323. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1324. carla_debug("CarlaPluginVST2::bufferSizeChanged(%i)", newBufferSize);
  1325. if (pData->active)
  1326. deactivate();
  1327. #if ! VST_FORCE_DEPRECATED
  1328. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(newBufferSize), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1329. #endif
  1330. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(newBufferSize), nullptr, 0.0f);
  1331. if (pData->active)
  1332. activate();
  1333. }
  1334. void sampleRateChanged(const double newSampleRate) override
  1335. {
  1336. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1337. carla_debug("CarlaPluginVST2::sampleRateChanged(%g)", newSampleRate);
  1338. if (pData->active)
  1339. deactivate();
  1340. #if ! VST_FORCE_DEPRECATED
  1341. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(newSampleRate));
  1342. #endif
  1343. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(newSampleRate));
  1344. if (pData->active)
  1345. activate();
  1346. }
  1347. // -------------------------------------------------------------------
  1348. // Plugin buffers
  1349. // nothing
  1350. // -------------------------------------------------------------------
  1351. // Post-poned UI Stuff
  1352. // nothing
  1353. // -------------------------------------------------------------------
  1354. protected:
  1355. void handlePluginUIClosed() override
  1356. {
  1357. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1358. carla_debug("CarlaPluginVST2::handlePluginUIClosed()");
  1359. showCustomUI(false);
  1360. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1361. }
  1362. void handlePluginUIResized(const uint width, const uint height) override
  1363. {
  1364. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1365. carla_debug("CarlaPluginVST2::handlePluginUIResized(%u, %u)", width, height);
  1366. return; // unused
  1367. (void)width; (void)height;
  1368. }
  1369. // -------------------------------------------------------------------
  1370. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const noexcept
  1371. {
  1372. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  1373. #ifdef DEBUG
  1374. if (opcode != effIdle && opcode != effEditIdle && opcode != effProcessEvents)
  1375. carla_debug("CarlaPluginVST2::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1376. #endif
  1377. try {
  1378. return fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt);
  1379. } CARLA_SAFE_EXCEPTION_RETURN("Vst dispatcher", 0);
  1380. }
  1381. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1382. {
  1383. #ifdef DEBUG
  1384. if (opcode != audioMasterGetTime)
  1385. carla_debug("CarlaPluginVST2::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1386. #endif
  1387. intptr_t ret = 0;
  1388. switch (opcode)
  1389. {
  1390. case audioMasterAutomate: {
  1391. CARLA_SAFE_ASSERT_BREAK(pData->enabled);
  1392. // plugins should never do this:
  1393. CARLA_SAFE_ASSERT_INT(index >= 0 && index < static_cast<int32_t>(pData->param.count), index);
  1394. if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
  1395. break;
  1396. const uint32_t uindex(static_cast<uint32_t>(index));
  1397. const float fixedValue(pData->param.getFixedValue(uindex, opt));
  1398. // Called from plugin process thread, nasty!
  1399. if (pthread_equal(pthread_self(), fProcThread))
  1400. {
  1401. CARLA_SAFE_ASSERT(fIsProcessing);
  1402. pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1403. }
  1404. // Called from UI
  1405. else if (fUI.isVisible)
  1406. {
  1407. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1408. }
  1409. // Unknown
  1410. // TODO - check if plugin or UI is initializing
  1411. else
  1412. {
  1413. carla_stdout("audioMasterAutomate called from unknown source");
  1414. setParameterValue(uindex, fixedValue, true, true, true);
  1415. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1416. }
  1417. break;
  1418. }
  1419. case audioMasterCurrentId:
  1420. ret = fEffect->uniqueID;
  1421. break;
  1422. case audioMasterIdle:
  1423. if (pthread_equal(pthread_self(), fMainThread))
  1424. {
  1425. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1426. if (pData->engine->getType() != kEngineTypePlugin)
  1427. pData->engine->idle();
  1428. }
  1429. break;
  1430. #if ! VST_FORCE_DEPRECATED
  1431. case audioMasterPinConnected:
  1432. // Deprecated in VST SDK 2.4
  1433. // TODO
  1434. break;
  1435. case audioMasterWantMidi:
  1436. // Deprecated in VST SDK 2.4
  1437. pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
  1438. break;
  1439. #endif
  1440. case audioMasterGetTime:
  1441. ret = (intptr_t)&fTimeInfo;
  1442. break;
  1443. case audioMasterProcessEvents:
  1444. CARLA_SAFE_ASSERT_RETURN(pData->enabled, 0);
  1445. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, 0);
  1446. CARLA_SAFE_ASSERT_RETURN(pData->event.portOut != nullptr, 0);
  1447. if (fMidiEventCount >= kPluginMaxMidiEvents*2-1)
  1448. return 0;
  1449. if (const VstEvents* const vstEvents = (const VstEvents*)ptr)
  1450. {
  1451. for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
  1452. {
  1453. if (vstEvents->events[i] == nullptr)
  1454. break;
  1455. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)vstEvents->events[i]);
  1456. if (vstMidiEvent->type != kVstMidiType)
  1457. continue;
  1458. // reverse-find first free event, and put it there
  1459. for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
  1460. {
  1461. if (fMidiEvents[j].type == 0)
  1462. {
  1463. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. }
  1469. ret = 1;
  1470. break;
  1471. #if ! VST_FORCE_DEPRECATED
  1472. case audioMasterSetTime:
  1473. // Deprecated in VST SDK 2.4
  1474. break;
  1475. case audioMasterTempoAt:
  1476. // Deprecated in VST SDK 2.4
  1477. ret = static_cast<intptr_t>(fTimeInfo.tempo * 10000);
  1478. break;
  1479. case audioMasterGetNumAutomatableParameters:
  1480. // Deprecated in VST SDK 2.4
  1481. ret = static_cast<intptr_t>(pData->engine->getOptions().maxParameters);
  1482. ret = carla_minPositive<intptr_t>(ret, fEffect->numParams);
  1483. break;
  1484. case audioMasterGetParameterQuantization:
  1485. // Deprecated in VST SDK 2.4
  1486. ret = 1; // full single float precision
  1487. break;
  1488. #endif
  1489. #if 0
  1490. case audioMasterIOChanged:
  1491. CARLA_ASSERT(pData->enabled);
  1492. // TESTING
  1493. if (! pData->enabled)
  1494. {
  1495. ret = 1;
  1496. break;
  1497. }
  1498. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1499. {
  1500. carla_stderr2("CarlaPluginVST2::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1501. return 0;
  1502. }
  1503. engineProcessLock();
  1504. m_enabled = false;
  1505. engineProcessUnlock();
  1506. if (m_active)
  1507. {
  1508. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1509. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1510. }
  1511. reload();
  1512. if (m_active)
  1513. {
  1514. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1515. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1516. }
  1517. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1518. ret = 1;
  1519. break;
  1520. #endif
  1521. #if ! VST_FORCE_DEPRECATED
  1522. case audioMasterNeedIdle:
  1523. // Deprecated in VST SDK 2.4
  1524. fNeedIdle = true;
  1525. ret = 1;
  1526. break;
  1527. #endif
  1528. case audioMasterSizeWindow:
  1529. CARLA_SAFE_ASSERT_BREAK(fUI.window != nullptr);
  1530. CARLA_SAFE_ASSERT_BREAK(index > 0);
  1531. CARLA_SAFE_ASSERT_BREAK(value > 0);
  1532. fUI.window->setSize(static_cast<uint>(index), static_cast<uint>(value), true);
  1533. ret = 1;
  1534. break;
  1535. case audioMasterGetSampleRate:
  1536. ret = static_cast<intptr_t>(pData->engine->getSampleRate());
  1537. break;
  1538. case audioMasterGetBlockSize:
  1539. ret = static_cast<intptr_t>(pData->engine->getBufferSize());
  1540. break;
  1541. case audioMasterGetInputLatency:
  1542. ret = 0;
  1543. break;
  1544. case audioMasterGetOutputLatency:
  1545. ret = 0;
  1546. break;
  1547. #if ! VST_FORCE_DEPRECATED
  1548. case audioMasterGetPreviousPlug:
  1549. // Deprecated in VST SDK 2.4
  1550. // TODO
  1551. break;
  1552. case audioMasterGetNextPlug:
  1553. // Deprecated in VST SDK 2.4
  1554. // TODO
  1555. break;
  1556. case audioMasterWillReplaceOrAccumulate:
  1557. // Deprecated in VST SDK 2.4
  1558. ret = 1; // replace
  1559. break;
  1560. #endif
  1561. case audioMasterGetCurrentProcessLevel:
  1562. if (pthread_equal(pthread_self(), fProcThread))
  1563. {
  1564. CARLA_SAFE_ASSERT(fIsProcessing);
  1565. if (pData->engine->isOffline())
  1566. ret = kVstProcessLevelOffline;
  1567. else
  1568. ret = kVstProcessLevelRealtime;
  1569. }
  1570. else
  1571. {
  1572. ret = kVstProcessLevelUser;
  1573. }
  1574. break;
  1575. case audioMasterGetAutomationState:
  1576. ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1577. break;
  1578. case audioMasterOfflineStart:
  1579. case audioMasterOfflineRead:
  1580. case audioMasterOfflineWrite:
  1581. case audioMasterOfflineGetCurrentPass:
  1582. case audioMasterOfflineGetCurrentMetaPass:
  1583. // TODO
  1584. break;
  1585. #if ! VST_FORCE_DEPRECATED
  1586. case audioMasterSetOutputSampleRate:
  1587. // Deprecated in VST SDK 2.4
  1588. break;
  1589. case audioMasterGetOutputSpeakerArrangement:
  1590. // Deprecated in VST SDK 2.4
  1591. // TODO
  1592. break;
  1593. #endif
  1594. case audioMasterVendorSpecific:
  1595. // TODO - cockos extensions
  1596. break;
  1597. #if ! VST_FORCE_DEPRECATED
  1598. case audioMasterSetIcon:
  1599. // Deprecated in VST SDK 2.4
  1600. break;
  1601. #endif
  1602. #if ! VST_FORCE_DEPRECATED
  1603. case audioMasterOpenWindow:
  1604. case audioMasterCloseWindow:
  1605. // Deprecated in VST SDK 2.4
  1606. // TODO
  1607. break;
  1608. #endif
  1609. case audioMasterGetDirectory:
  1610. // TODO
  1611. break;
  1612. case audioMasterUpdateDisplay:
  1613. // Idle UI if visible
  1614. if (fUI.isVisible)
  1615. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1616. // Update current program
  1617. if (pData->prog.count > 0)
  1618. {
  1619. const int32_t current = static_cast<int32_t>(dispatcher(effGetProgram, 0, 0, nullptr, 0.0f));
  1620. if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
  1621. {
  1622. char strBuf[STR_MAX+1] = { '\0' };
  1623. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1624. if (pData->prog.names[current] != nullptr)
  1625. delete[] pData->prog.names[current];
  1626. pData->prog.names[current] = carla_strdup(strBuf);
  1627. if (pData->prog.current != current)
  1628. {
  1629. pData->prog.current = current;
  1630. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
  1631. }
  1632. }
  1633. }
  1634. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  1635. ret = 1;
  1636. break;
  1637. case audioMasterBeginEdit:
  1638. case audioMasterEndEdit:
  1639. // TODO
  1640. break;
  1641. case audioMasterOpenFileSelector:
  1642. case audioMasterCloseFileSelector:
  1643. // TODO
  1644. break;
  1645. #if ! VST_FORCE_DEPRECATED
  1646. case audioMasterEditFile:
  1647. // Deprecated in VST SDK 2.4
  1648. // TODO
  1649. break;
  1650. case audioMasterGetChunkFile:
  1651. // Deprecated in VST SDK 2.4
  1652. // TODO
  1653. break;
  1654. case audioMasterGetInputSpeakerArrangement:
  1655. // Deprecated in VST SDK 2.4
  1656. // TODO
  1657. break;
  1658. #endif
  1659. default:
  1660. carla_debug("CarlaPluginVST2::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f) UNDEF", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1661. break;
  1662. }
  1663. return ret;
  1664. // unused
  1665. (void)opt;
  1666. }
  1667. bool canDo(const char* const feature) const noexcept
  1668. {
  1669. try {
  1670. return (fEffect->dispatcher(fEffect, effCanDo, 0, 0, const_cast<char*>(feature), 0.0f) == 1);
  1671. } CARLA_SAFE_EXCEPTION_RETURN("vstPluginCanDo", false);
  1672. }
  1673. bool hasMidiInput() const noexcept
  1674. {
  1675. return (canDo("receiveVstEvents") || canDo("receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT));
  1676. }
  1677. bool hasMidiOutput() const noexcept
  1678. {
  1679. return (canDo("sendVstEvents") || canDo("sendVstMidiEvent"));
  1680. }
  1681. // -------------------------------------------------------------------
  1682. const void* getNativeDescriptor() const noexcept override
  1683. {
  1684. return fEffect;
  1685. }
  1686. // -------------------------------------------------------------------
  1687. public:
  1688. bool init(const char* const filename, const char* const name, const int64_t uniqueId, const uint options)
  1689. {
  1690. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1691. // ---------------------------------------------------------------
  1692. // first checks
  1693. if (pData->client != nullptr)
  1694. {
  1695. pData->engine->setLastError("Plugin client is already registered");
  1696. return false;
  1697. }
  1698. if (filename == nullptr || filename[0] == '\0')
  1699. {
  1700. pData->engine->setLastError("null filename");
  1701. return false;
  1702. }
  1703. // ---------------------------------------------------------------
  1704. VST_Function vstFn;
  1705. #ifdef CARLA_OS_MAC
  1706. CarlaString filenameCheck(filename);
  1707. filenameCheck.toLower();
  1708. if (filenameCheck.endsWith(".vst") || filenameCheck.endsWith(".vst/"))
  1709. {
  1710. // FIXME assert returns, set engine error
  1711. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)strlen(filename), true);
  1712. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
  1713. fMacBundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  1714. CFRelease(urlRef);
  1715. CARLA_SAFE_ASSERT_RETURN(fMacBundleRef != nullptr, false);
  1716. if (! CFBundleLoadExecutable(fMacBundleRef))
  1717. {
  1718. CFRelease(fMacBundleRef);
  1719. pData->engine->setLastError("Failed to load VST bundle executable");
  1720. return false;
  1721. }
  1722. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("main_macho"));
  1723. if (vstFn == nullptr)
  1724. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("VSTPluginMain"));
  1725. if (vstFn == nullptr)
  1726. {
  1727. CFBundleUnloadExecutable(fMacBundleRef);
  1728. CFRelease(fMacBundleRef);
  1729. pData->engine->setLastError("Not a VST plugin");
  1730. return false;
  1731. }
  1732. fMacBundleRefNum = CFBundleOpenBundleResourceMap(fMacBundleRef);
  1733. }
  1734. else
  1735. #endif
  1736. {
  1737. // -----------------------------------------------------------
  1738. // open DLL
  1739. if (! pData->libOpen(filename))
  1740. {
  1741. pData->engine->setLastError(pData->libError(filename));
  1742. return false;
  1743. }
  1744. // -----------------------------------------------------------
  1745. // get DLL main entry
  1746. vstFn = pData->libSymbol<VST_Function>("VSTPluginMain");
  1747. if (vstFn == nullptr)
  1748. {
  1749. vstFn = pData->libSymbol<VST_Function>("main");
  1750. if (vstFn == nullptr)
  1751. {
  1752. pData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1753. return false;
  1754. }
  1755. }
  1756. }
  1757. // ---------------------------------------------------------------
  1758. // initialize plugin (part 1)
  1759. sCurrentUniqueId = uniqueId;
  1760. sLastCarlaPluginVST2 = this;
  1761. try {
  1762. fEffect = vstFn(carla_vst_audioMasterCallback);
  1763. } CARLA_SAFE_EXCEPTION_RETURN("Vst init", false);
  1764. sLastCarlaPluginVST2 = nullptr;
  1765. sCurrentUniqueId = 0;
  1766. if (fEffect == nullptr)
  1767. {
  1768. pData->engine->setLastError("Plugin failed to initialize");
  1769. return false;
  1770. }
  1771. if (fEffect->magic != kEffectMagic)
  1772. {
  1773. pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1774. return false;
  1775. }
  1776. fEffect->ptr1 = this;
  1777. dispatcher(effIdentify, 0, 0, 0, 0);
  1778. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1779. dispatcher(effSetBlockSizeAndSampleRate, 0,
  1780. static_cast<int32_t>(pData->engine->getBufferSize()), nullptr,
  1781. static_cast<float>(pData->engine->getSampleRate()));
  1782. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1783. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  1784. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1785. // ---------------------------------------------------------------
  1786. // get info
  1787. if (name != nullptr && name[0] != '\0')
  1788. {
  1789. pData->name = pData->engine->getUniquePluginName(name);
  1790. }
  1791. else
  1792. {
  1793. char strBuf[STR_MAX+1];
  1794. carla_zeroChars(strBuf, STR_MAX+1);
  1795. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1796. if (strBuf[0] != '\0')
  1797. pData->name = pData->engine->getUniquePluginName(strBuf);
  1798. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1799. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1800. else
  1801. pData->name = pData->engine->getUniquePluginName("unknown");
  1802. }
  1803. pData->filename = carla_strdup(filename);
  1804. // ---------------------------------------------------------------
  1805. // register client
  1806. pData->client = pData->engine->addClient(this);
  1807. if (pData->client == nullptr || ! pData->client->isOk())
  1808. {
  1809. pData->engine->setLastError("Failed to register plugin client");
  1810. return false;
  1811. }
  1812. // ---------------------------------------------------------------
  1813. // initialize plugin (part 2)
  1814. for (int i = fEffect->numInputs; --i >= 0;) dispatcher(effConnectInput, i, 1, 0, 0);
  1815. for (int i = fEffect->numOutputs; --i >= 0;) dispatcher(effConnectOutput, i, 1, 0, 0);
  1816. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1817. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  1818. static const char kHasCockosExtensions[] = "hasCockosExtensions";
  1819. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>(kHasCockosExtensions), 0.0f)) == 0xbeef0000)
  1820. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1821. // ---------------------------------------------------------------
  1822. // set default options
  1823. pData->options = 0x0;
  1824. if (pData->latency.frames != 0 || hasMidiOutput())
  1825. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1826. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1827. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1828. if (fEffect->numPrograms > 1)
  1829. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1830. if (fEffect->flags & effFlagsProgramChunks)
  1831. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1832. if (hasMidiInput())
  1833. {
  1834. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1835. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1836. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1837. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1838. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1839. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1840. if (options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1841. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1842. }
  1843. return true;
  1844. // unused
  1845. (void)uniqueId;
  1846. }
  1847. private:
  1848. int fUnique1;
  1849. AEffect* fEffect;
  1850. uint32_t fMidiEventCount;
  1851. VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1852. VstTimeInfo fTimeInfo;
  1853. bool fNeedIdle;
  1854. void* fLastChunk;
  1855. bool fIsProcessing;
  1856. pthread_t fMainThread;
  1857. pthread_t fProcThread;
  1858. #ifdef CARLA_OS_MAC
  1859. CFBundleRef fMacBundleRef;
  1860. CFBundleRefNum fMacBundleRefNum;
  1861. #endif
  1862. struct FixedVstEvents {
  1863. int32_t numEvents;
  1864. intptr_t reserved;
  1865. VstEvent* data[kPluginMaxMidiEvents*2];
  1866. FixedVstEvents() noexcept
  1867. : numEvents(0),
  1868. reserved(0)
  1869. {
  1870. carla_zeroPointers(data, kPluginMaxMidiEvents*2);
  1871. }
  1872. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  1873. } fEvents;
  1874. struct UI {
  1875. bool isVisible;
  1876. CarlaPluginUI* window;
  1877. UI() noexcept
  1878. : isVisible(false),
  1879. window(nullptr) {}
  1880. ~UI()
  1881. {
  1882. CARLA_ASSERT(! isVisible);
  1883. if (window != nullptr)
  1884. {
  1885. delete window;
  1886. window = nullptr;
  1887. }
  1888. }
  1889. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  1890. } fUI;
  1891. int fUnique2;
  1892. static intptr_t sCurrentUniqueId;
  1893. static CarlaPluginVST2* sLastCarlaPluginVST2;
  1894. // -------------------------------------------------------------------
  1895. static bool compareMagic (int32_t magic, const char* name) noexcept
  1896. {
  1897. return magic == (int32_t)ByteOrder::littleEndianInt (name)
  1898. || magic == (int32_t)ByteOrder::bigEndianInt (name);
  1899. }
  1900. static int32_t fxbSwap(const int32_t x) noexcept
  1901. {
  1902. return (int32_t)ByteOrder::swapIfLittleEndian ((uint32_t) x);
  1903. }
  1904. bool loadOldSaveFormat(const void* const data, const std::size_t dataSize)
  1905. {
  1906. if (dataSize < 28)
  1907. return false;
  1908. const int32_t* const set = (const int32_t*)data;
  1909. if (! compareMagic(set[0], "CcnK"))
  1910. return false;
  1911. if (! compareMagic(set[2], "FBCh"))
  1912. return false;
  1913. if (fxbSwap(set[3]) > 1)
  1914. return false;
  1915. const int32_t chunkSize = fxbSwap(set[39]);
  1916. CARLA_SAFE_ASSERT_RETURN(chunkSize > 0, false);
  1917. if (static_cast<const std::size_t>(chunkSize + 160) > dataSize)
  1918. return false;
  1919. setChunkData(&set[40], static_cast<const std::size_t>(chunkSize));
  1920. return true;
  1921. }
  1922. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1923. {
  1924. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1925. if (std::strcmp(feature, "supplyIdle") == 0)
  1926. return 1;
  1927. if (std::strcmp(feature, "sendVstEvents") == 0)
  1928. return 1;
  1929. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1930. return 1;
  1931. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1932. return 1;
  1933. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1934. return 1;
  1935. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1936. return 1;
  1937. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1938. return 1;
  1939. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1940. return -1;
  1941. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1942. return -1;
  1943. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1944. return 1;
  1945. if (std::strcmp(feature, "sizeWindow") == 0)
  1946. return 1;
  1947. if (std::strcmp(feature, "offline") == 0)
  1948. return -1;
  1949. if (std::strcmp(feature, "openFileSelector") == 0)
  1950. return -1;
  1951. if (std::strcmp(feature, "closeFileSelector") == 0)
  1952. return -1;
  1953. if (std::strcmp(feature, "startStopProcess") == 0)
  1954. return 1;
  1955. if (std::strcmp(feature, "supportShell") == 0)
  1956. return 1;
  1957. if (std::strcmp(feature, "shellCategory") == 0)
  1958. return 1;
  1959. if (std::strcmp(feature, "NIMKPIVendorSpecificCallbacks") == 0)
  1960. return -1;
  1961. // unimplemented
  1962. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1963. return 0;
  1964. }
  1965. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1966. {
  1967. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1968. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1969. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1970. #endif
  1971. switch (opcode)
  1972. {
  1973. case audioMasterVersion:
  1974. return kVstVersion;
  1975. case audioMasterCurrentId:
  1976. if (sCurrentUniqueId != 0)
  1977. return sCurrentUniqueId;
  1978. break;
  1979. case audioMasterGetVendorString:
  1980. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1981. std::strcpy((char*)ptr, "falkTX");
  1982. return 1;
  1983. case audioMasterGetProductString:
  1984. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1985. std::strcpy((char*)ptr, "Carla");
  1986. return 1;
  1987. case audioMasterGetVendorVersion:
  1988. return CARLA_VERSION_HEX;
  1989. case audioMasterCanDo:
  1990. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  1991. return carla_vst_hostCanDo((const char*)ptr);
  1992. case audioMasterGetLanguage:
  1993. return kVstLangEnglish;
  1994. }
  1995. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  1996. CarlaPluginVST2* self = nullptr;
  1997. if (effect != nullptr)
  1998. {
  1999. if (effect->ptr1 != nullptr)
  2000. {
  2001. self = (CarlaPluginVST2*)effect->ptr1;
  2002. if (self->fUnique1 != self->fUnique2)
  2003. self = nullptr;
  2004. }
  2005. if (self != nullptr)
  2006. {
  2007. if (self->fEffect == nullptr)
  2008. self->fEffect = effect;
  2009. if (self->fEffect != effect)
  2010. {
  2011. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2012. self = nullptr;
  2013. }
  2014. }
  2015. else if (sLastCarlaPluginVST2 != nullptr)
  2016. {
  2017. effect->ptr1 = sLastCarlaPluginVST2;
  2018. self = sLastCarlaPluginVST2;
  2019. }
  2020. }
  2021. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2022. }
  2023. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginVST2)
  2024. };
  2025. intptr_t CarlaPluginVST2::sCurrentUniqueId = 0;
  2026. CarlaPluginVST2* CarlaPluginVST2::sLastCarlaPluginVST2 = nullptr;
  2027. CARLA_BACKEND_END_NAMESPACE
  2028. // -------------------------------------------------------------------------------------------------------------------
  2029. CARLA_BACKEND_START_NAMESPACE
  2030. CarlaPlugin* CarlaPlugin::newVST2(const Initializer& init)
  2031. {
  2032. carla_debug("CarlaPlugin::newVST2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
  2033. CarlaPluginVST2* const plugin(new CarlaPluginVST2(init.engine, init.id));
  2034. if (! plugin->init(init.filename, init.name, init.uniqueId, init.options))
  2035. {
  2036. delete plugin;
  2037. return nullptr;
  2038. }
  2039. return plugin;
  2040. }
  2041. // -------------------------------------------------------------------------------------------------------------------
  2042. CARLA_BACKEND_END_NAMESPACE