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.

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