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.

2606 lines
88KB

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