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.

2603 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. char* const empty3Ptr = &fEffect->empty3[0];
  711. int32_t initialDelay = *(int32_t*)empty3Ptr;
  712. const uint32_t latency = (initialDelay > 0) ? static_cast<uint32_t>(initialDelay) : 0;
  713. if (latency != 0)
  714. {
  715. pData->client->setLatency(latency);
  716. #ifndef BUILD_BRIDGE
  717. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  718. #endif
  719. }
  720. //bufferSizeChanged(pData->engine->getBufferSize());
  721. reloadPrograms(true);
  722. if (pData->active)
  723. activate();
  724. carla_debug("CarlaPluginVST2::reload() - end");
  725. }
  726. void reloadPrograms(const bool doInit) override
  727. {
  728. carla_debug("CarlaPluginVST2::reloadPrograms(%s)", bool2str(doInit));
  729. const uint32_t oldCount = pData->prog.count;
  730. const int32_t current = pData->prog.current;
  731. // Delete old programs
  732. pData->prog.clear();
  733. // Query new programs
  734. uint32_t newCount = (fEffect->numPrograms > 0) ? static_cast<uint32_t>(fEffect->numPrograms) : 0;
  735. if (newCount > 0)
  736. {
  737. pData->prog.createNew(newCount);
  738. // Update names
  739. for (int32_t i=0; i < fEffect->numPrograms; ++i)
  740. {
  741. char strBuf[STR_MAX+1] = { '\0' };
  742. if (dispatcher(effGetProgramNameIndexed, i, 0, strBuf, 0.0f) != 1)
  743. {
  744. // program will be [re-]changed later
  745. dispatcher(effSetProgram, 0, i, nullptr, 0.0f);
  746. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  747. }
  748. pData->prog.names[i] = carla_strdup(strBuf);
  749. }
  750. }
  751. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  752. // Update OSC Names
  753. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  754. {
  755. pData->engine->oscSend_control_set_program_count(pData->id, newCount);
  756. for (uint32_t i=0; i < newCount; ++i)
  757. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  758. }
  759. #endif
  760. if (doInit)
  761. {
  762. if (newCount > 0)
  763. setProgram(0, false, false, false, true);
  764. else
  765. dispatcher(effSetProgram, 0, 0, nullptr, 0.0f);
  766. }
  767. else
  768. {
  769. // Check if current program is invalid
  770. bool programChanged = false;
  771. if (newCount == oldCount+1)
  772. {
  773. // one program added, probably created by user
  774. pData->prog.current = static_cast<int32_t>(oldCount);
  775. programChanged = true;
  776. }
  777. else if (current < 0 && newCount > 0)
  778. {
  779. // programs exist now, but not before
  780. pData->prog.current = 0;
  781. programChanged = true;
  782. }
  783. else if (current >= 0 && newCount == 0)
  784. {
  785. // programs existed before, but not anymore
  786. pData->prog.current = -1;
  787. programChanged = true;
  788. }
  789. else if (current >= static_cast<int32_t>(newCount))
  790. {
  791. // current program > count
  792. pData->prog.current = 0;
  793. programChanged = true;
  794. }
  795. else
  796. {
  797. // no change
  798. pData->prog.current = current;
  799. }
  800. if (programChanged)
  801. {
  802. setProgram(pData->prog.current, true, true, true, false);
  803. }
  804. else
  805. {
  806. // Program was changed during update, re-set it
  807. if (pData->prog.current >= 0)
  808. dispatcher(effSetProgram, 0, pData->prog.current, nullptr, 0.0f);
  809. }
  810. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  811. }
  812. }
  813. // -------------------------------------------------------------------
  814. // Plugin processing
  815. void activate() noexcept override
  816. {
  817. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  818. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  819. dispatcher(effSetBlockSizeAndSampleRate, 0,
  820. static_cast<int32_t>(pData->engine->getBufferSize()), nullptr,
  821. static_cast<float>(pData->engine->getSampleRate()));
  822. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  823. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  824. try {
  825. dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  826. } catch(...) {}
  827. try {
  828. dispatcher(effStartProcess, 0, 0, nullptr, 0.0f);
  829. } catch(...) {}
  830. }
  831. void deactivate() noexcept override
  832. {
  833. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr,);
  834. try {
  835. dispatcher(effStopProcess, 0, 0, nullptr, 0.0f);
  836. } catch(...) {}
  837. try {
  838. dispatcher(effMainsChanged, 0, 0, nullptr, 0.0f);
  839. } catch(...) {}
  840. }
  841. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  842. {
  843. fProcThread = pthread_self();
  844. // --------------------------------------------------------------------------------------------------------
  845. // Check if active
  846. if (! pData->active)
  847. {
  848. // disable any output sound
  849. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  850. carla_zeroFloats(audioOut[i], frames);
  851. return;
  852. }
  853. fMidiEventCount = 0;
  854. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  855. // --------------------------------------------------------------------------------------------------------
  856. // Check if needs reset
  857. if (pData->needsReset)
  858. {
  859. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  860. {
  861. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  862. for (uint8_t i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  863. {
  864. fMidiEvents[k].type = kVstMidiType;
  865. fMidiEvents[k].byteSize = kVstMidiEventSize;
  866. fMidiEvents[k].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  867. fMidiEvents[k].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  868. fMidiEvents[k+i].type = kVstMidiType;
  869. fMidiEvents[k+i].byteSize = kVstMidiEventSize;
  870. fMidiEvents[k+i].midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  871. fMidiEvents[k+i].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  872. }
  873. }
  874. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  875. {
  876. fMidiEventCount = MAX_MIDI_NOTE;
  877. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  878. {
  879. fMidiEvents[i].type = kVstMidiType;
  880. fMidiEvents[i].byteSize = kVstMidiEventSize;
  881. fMidiEvents[i].midiData[0] = char(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  882. fMidiEvents[i].midiData[1] = char(i);
  883. }
  884. }
  885. pData->needsReset = false;
  886. }
  887. // --------------------------------------------------------------------------------------------------------
  888. // Set TimeInfo
  889. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  890. fTimeInfo.flags = kVstTransportChanged;
  891. if (timeInfo.playing)
  892. fTimeInfo.flags |= kVstTransportPlaying;
  893. fTimeInfo.samplePos = double(timeInfo.frame);
  894. fTimeInfo.sampleRate = pData->engine->getSampleRate();
  895. if (timeInfo.usecs != 0)
  896. {
  897. fTimeInfo.nanoSeconds = double(timeInfo.usecs)/1000.0;
  898. fTimeInfo.flags |= kVstNanosValid;
  899. }
  900. if (timeInfo.bbt.valid)
  901. {
  902. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
  903. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
  904. const double ppqBar = double(timeInfo.bbt.bar - 1) * timeInfo.bbt.beatsPerBar;
  905. const double ppqBeat = double(timeInfo.bbt.beat - 1);
  906. const double ppqTick = double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat;
  907. // PPQ Pos
  908. fTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  909. fTimeInfo.flags |= kVstPpqPosValid;
  910. // Tempo
  911. fTimeInfo.tempo = timeInfo.bbt.beatsPerMinute;
  912. fTimeInfo.flags |= kVstTempoValid;
  913. // Bars
  914. fTimeInfo.barStartPos = ppqBar;
  915. fTimeInfo.flags |= kVstBarsValid;
  916. // Time Signature
  917. fTimeInfo.timeSigNumerator = static_cast<int32_t>(timeInfo.bbt.beatsPerBar);
  918. fTimeInfo.timeSigDenominator = static_cast<int32_t>(timeInfo.bbt.beatType);
  919. fTimeInfo.flags |= kVstTimeSigValid;
  920. }
  921. else
  922. {
  923. // Tempo
  924. fTimeInfo.tempo = 120.0;
  925. fTimeInfo.flags |= kVstTempoValid;
  926. // Time Signature
  927. fTimeInfo.timeSigNumerator = 4;
  928. fTimeInfo.timeSigDenominator = 4;
  929. fTimeInfo.flags |= kVstTimeSigValid;
  930. // Missing info
  931. fTimeInfo.ppqPos = 0.0;
  932. fTimeInfo.barStartPos = 0.0;
  933. }
  934. // --------------------------------------------------------------------------------------------------------
  935. // Event Input and Processing
  936. if (pData->event.portIn != nullptr)
  937. {
  938. // ----------------------------------------------------------------------------------------------------
  939. // MIDI Input (External)
  940. if (pData->extNotes.mutex.tryLock())
  941. {
  942. ExternalMidiNote note = { 0, 0, 0 };
  943. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  944. {
  945. note = pData->extNotes.data.getFirst(note, true);
  946. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  947. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  948. vstMidiEvent.type = kVstMidiType;
  949. vstMidiEvent.byteSize = kVstMidiEventSize;
  950. vstMidiEvent.midiData[0] = char((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  951. vstMidiEvent.midiData[1] = char(note.note);
  952. vstMidiEvent.midiData[2] = char(note.velo);
  953. }
  954. pData->extNotes.mutex.unlock();
  955. } // End of MIDI Input (External)
  956. // ----------------------------------------------------------------------------------------------------
  957. // Event Input (System)
  958. #ifndef BUILD_BRIDGE
  959. bool allNotesOffSent = false;
  960. #endif
  961. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  962. uint32_t startTime = 0;
  963. uint32_t timeOffset = 0;
  964. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  965. {
  966. const EngineEvent& event(pData->event.portIn->getEvent(i));
  967. if (event.time >= frames)
  968. continue;
  969. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  970. if (isSampleAccurate && event.time > timeOffset)
  971. {
  972. if (processSingle(audioIn, audioOut, event.time - timeOffset, timeOffset))
  973. {
  974. startTime = 0;
  975. timeOffset = event.time;
  976. if (fMidiEventCount > 0)
  977. {
  978. carla_zeroStructs(fMidiEvents, fMidiEventCount);
  979. fMidiEventCount = 0;
  980. }
  981. }
  982. else
  983. startTime += timeOffset;
  984. }
  985. switch (event.type)
  986. {
  987. case kEngineEventTypeNull:
  988. break;
  989. case kEngineEventTypeControl: {
  990. const EngineControlEvent& ctrlEvent(event.ctrl);
  991. switch (ctrlEvent.type)
  992. {
  993. case kEngineControlEventTypeNull:
  994. break;
  995. case kEngineControlEventTypeParameter: {
  996. #ifndef BUILD_BRIDGE
  997. // Control backend stuff
  998. if (event.channel == pData->ctrlChannel)
  999. {
  1000. float value;
  1001. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1002. {
  1003. value = ctrlEvent.value;
  1004. setDryWetRT(value);
  1005. }
  1006. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1007. {
  1008. value = ctrlEvent.value*127.0f/100.0f;
  1009. setVolumeRT(value);
  1010. }
  1011. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1012. {
  1013. float left, right;
  1014. value = ctrlEvent.value/0.5f - 1.0f;
  1015. if (value < 0.0f)
  1016. {
  1017. left = -1.0f;
  1018. right = (value*2.0f)+1.0f;
  1019. }
  1020. else if (value > 0.0f)
  1021. {
  1022. left = (value*2.0f)-1.0f;
  1023. right = 1.0f;
  1024. }
  1025. else
  1026. {
  1027. left = -1.0f;
  1028. right = 1.0f;
  1029. }
  1030. setBalanceLeftRT(left);
  1031. setBalanceRightRT(right);
  1032. }
  1033. }
  1034. #endif
  1035. // Control plugin parameters
  1036. uint32_t k;
  1037. for (k=0; k < pData->param.count; ++k)
  1038. {
  1039. if (pData->param.data[k].midiChannel != event.channel)
  1040. continue;
  1041. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1042. continue;
  1043. if (pData->param.data[k].type != PARAMETER_INPUT)
  1044. continue;
  1045. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1046. continue;
  1047. float value;
  1048. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1049. {
  1050. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1051. }
  1052. else
  1053. {
  1054. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1055. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1056. else
  1057. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1058. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1059. value = std::rint(value);
  1060. }
  1061. setParameterValueRT(k, value);
  1062. }
  1063. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1064. {
  1065. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1066. continue;
  1067. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1068. carla_zeroStruct(vstMidiEvent);
  1069. vstMidiEvent.type = kVstMidiType;
  1070. vstMidiEvent.byteSize = kVstMidiEventSize;
  1071. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1072. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1073. vstMidiEvent.midiData[1] = char(ctrlEvent.param);
  1074. vstMidiEvent.midiData[2] = char(ctrlEvent.value*127.0f);
  1075. }
  1076. break;
  1077. } // case kEngineControlEventTypeParameter
  1078. case kEngineControlEventTypeMidiBank:
  1079. if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  1080. {
  1081. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1082. continue;
  1083. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1084. carla_zeroStruct(vstMidiEvent);
  1085. vstMidiEvent.type = kVstMidiType;
  1086. vstMidiEvent.byteSize = kVstMidiEventSize;
  1087. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1088. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1089. vstMidiEvent.midiData[1] = MIDI_CONTROL_BANK_SELECT;
  1090. vstMidiEvent.midiData[2] = char(ctrlEvent.param);
  1091. }
  1092. break;
  1093. case kEngineControlEventTypeMidiProgram:
  1094. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1095. {
  1096. if (ctrlEvent.param < pData->prog.count)
  1097. {
  1098. setProgramRT(ctrlEvent.param);
  1099. break;
  1100. }
  1101. }
  1102. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1103. {
  1104. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1105. continue;
  1106. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1107. carla_zeroStruct(vstMidiEvent);
  1108. vstMidiEvent.type = kVstMidiType;
  1109. vstMidiEvent.byteSize = kVstMidiEventSize;
  1110. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1111. vstMidiEvent.midiData[0] = char(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1112. vstMidiEvent.midiData[1] = char(ctrlEvent.param);
  1113. }
  1114. break;
  1115. case kEngineControlEventTypeAllSoundOff:
  1116. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1117. {
  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_SOUND_OFF;
  1127. }
  1128. break;
  1129. case kEngineControlEventTypeAllNotesOff:
  1130. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1131. {
  1132. #ifndef BUILD_BRIDGE
  1133. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1134. {
  1135. allNotesOffSent = true;
  1136. sendMidiAllNotesOffToCallback();
  1137. }
  1138. #endif
  1139. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1140. continue;
  1141. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1142. carla_zeroStruct(vstMidiEvent);
  1143. vstMidiEvent.type = kVstMidiType;
  1144. vstMidiEvent.byteSize = kVstMidiEventSize;
  1145. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1146. vstMidiEvent.midiData[0] = char(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1147. vstMidiEvent.midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1148. }
  1149. break;
  1150. } // switch (ctrlEvent.type)
  1151. break;
  1152. } // case kEngineEventTypeControl
  1153. case kEngineEventTypeMidi: {
  1154. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1155. continue;
  1156. const EngineMidiEvent& midiEvent(event.midi);
  1157. if (midiEvent.size > 3)
  1158. continue;
  1159. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1160. static_assert(3 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1161. #endif
  1162. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1163. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1164. continue;
  1165. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1166. continue;
  1167. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1168. continue;
  1169. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1170. continue;
  1171. // Fix bad note-off
  1172. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1173. status = MIDI_STATUS_NOTE_OFF;
  1174. VstMidiEvent& vstMidiEvent(fMidiEvents[fMidiEventCount++]);
  1175. carla_zeroStruct(vstMidiEvent);
  1176. vstMidiEvent.type = kVstMidiType;
  1177. vstMidiEvent.byteSize = kVstMidiEventSize;
  1178. vstMidiEvent.deltaFrames = static_cast<int32_t>(isSampleAccurate ? startTime : event.time);
  1179. vstMidiEvent.midiData[0] = char(status | (event.channel & MIDI_CHANNEL_BIT));
  1180. vstMidiEvent.midiData[1] = char(midiEvent.size >= 2 ? midiEvent.data[1] : 0);
  1181. vstMidiEvent.midiData[2] = char(midiEvent.size >= 3 ? midiEvent.data[2] : 0);
  1182. if (status == MIDI_STATUS_NOTE_ON)
  1183. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1184. else if (status == MIDI_STATUS_NOTE_OFF)
  1185. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1186. } break;
  1187. } // switch (event.type)
  1188. }
  1189. pData->postRtEvents.trySplice();
  1190. if (frames > timeOffset)
  1191. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1192. } // End of Event Input and Processing
  1193. // --------------------------------------------------------------------------------------------------------
  1194. // Plugin processing (no events)
  1195. else
  1196. {
  1197. processSingle(audioIn, audioOut, frames, 0);
  1198. } // End of Plugin processing (no events)
  1199. // --------------------------------------------------------------------------------------------------------
  1200. // MIDI Output
  1201. if (pData->event.portOut != nullptr)
  1202. {
  1203. // reverse lookup MIDI events
  1204. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1205. {
  1206. if (fMidiEvents[k].type == 0)
  1207. break;
  1208. const VstMidiEvent& vstMidiEvent(fMidiEvents[k]);
  1209. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.deltaFrames >= 0);
  1210. CARLA_SAFE_ASSERT_CONTINUE(vstMidiEvent.midiData[0] != 0);
  1211. uint8_t midiData[3];
  1212. midiData[0] = static_cast<uint8_t>(vstMidiEvent.midiData[0]);
  1213. midiData[1] = static_cast<uint8_t>(vstMidiEvent.midiData[1]);
  1214. midiData[2] = static_cast<uint8_t>(vstMidiEvent.midiData[2]);
  1215. if (! pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(vstMidiEvent.deltaFrames), 3, midiData))
  1216. break;
  1217. }
  1218. } // End of MIDI Output
  1219. }
  1220. bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1221. {
  1222. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1223. if (pData->audioIn.count > 0)
  1224. {
  1225. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1226. }
  1227. if (pData->audioOut.count > 0)
  1228. {
  1229. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1230. }
  1231. // --------------------------------------------------------------------------------------------------------
  1232. // Try lock, silence otherwise
  1233. #ifndef STOAT_TEST_BUILD
  1234. if (pData->engine->isOffline())
  1235. {
  1236. pData->singleMutex.lock();
  1237. }
  1238. else
  1239. #endif
  1240. if (! pData->singleMutex.tryLock())
  1241. {
  1242. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1243. {
  1244. for (uint32_t k=0; k < frames; ++k)
  1245. outBuffer[i][k+timeOffset] = 0.0f;
  1246. }
  1247. return false;
  1248. }
  1249. // --------------------------------------------------------------------------------------------------------
  1250. // Set audio buffers
  1251. float* vstInBuffer[pData->audioIn.count];
  1252. float* vstOutBuffer[pData->audioOut.count];
  1253. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1254. vstInBuffer[i] = const_cast<float*>(inBuffer[i]+timeOffset);
  1255. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1256. vstOutBuffer[i] = outBuffer[i]+timeOffset;
  1257. // --------------------------------------------------------------------------------------------------------
  1258. // Set MIDI events
  1259. if (fMidiEventCount > 0)
  1260. {
  1261. fEvents.numEvents = static_cast<int32_t>(fMidiEventCount);
  1262. fEvents.reserved = 0;
  1263. dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
  1264. }
  1265. // --------------------------------------------------------------------------------------------------------
  1266. // Run plugin
  1267. fIsProcessing = true;
  1268. if (pData->hints & PLUGIN_CAN_PROCESS_REPLACING)
  1269. {
  1270. fEffect->processReplacing(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1271. }
  1272. else
  1273. {
  1274. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1275. carla_zeroFloats(vstOutBuffer[i], frames);
  1276. #if ! VST_FORCE_DEPRECATED
  1277. fEffect->process(fEffect, (pData->audioIn.count > 0) ? vstInBuffer : nullptr, (pData->audioOut.count > 0) ? vstOutBuffer : nullptr, static_cast<int32_t>(frames));
  1278. #endif
  1279. }
  1280. fIsProcessing = false;
  1281. fTimeInfo.samplePos += frames;
  1282. #ifndef BUILD_BRIDGE
  1283. // --------------------------------------------------------------------------------------------------------
  1284. // Post-processing (dry/wet, volume and balance)
  1285. {
  1286. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && carla_isNotEqual(pData->postProc.volume, 1.0f);
  1287. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1288. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1289. bool isPair;
  1290. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1291. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1292. {
  1293. // Dry/Wet
  1294. if (doDryWet)
  1295. {
  1296. for (uint32_t k=0; k < frames; ++k)
  1297. {
  1298. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  1299. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1300. }
  1301. }
  1302. // Balance
  1303. if (doBalance)
  1304. {
  1305. isPair = (i % 2 == 0);
  1306. if (isPair)
  1307. {
  1308. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1309. carla_copyFloats(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1310. }
  1311. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1312. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1313. for (uint32_t k=0; k < frames; ++k)
  1314. {
  1315. if (isPair)
  1316. {
  1317. // left
  1318. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1319. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1320. }
  1321. else
  1322. {
  1323. // right
  1324. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1325. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1326. }
  1327. }
  1328. }
  1329. // Volume
  1330. if (doVolume)
  1331. {
  1332. for (uint32_t k=0; k < frames; ++k)
  1333. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1334. }
  1335. }
  1336. } // End of Post-processing
  1337. #endif
  1338. // --------------------------------------------------------------------------------------------------------
  1339. pData->singleMutex.unlock();
  1340. return true;
  1341. }
  1342. void bufferSizeChanged(const uint32_t newBufferSize) override
  1343. {
  1344. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1345. carla_debug("CarlaPluginVST2::bufferSizeChanged(%i)", newBufferSize);
  1346. if (pData->active)
  1347. deactivate();
  1348. #if ! VST_FORCE_DEPRECATED
  1349. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(newBufferSize), nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1350. #endif
  1351. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(newBufferSize), nullptr, 0.0f);
  1352. if (pData->active)
  1353. activate();
  1354. }
  1355. void sampleRateChanged(const double newSampleRate) override
  1356. {
  1357. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1358. carla_debug("CarlaPluginVST2::sampleRateChanged(%g)", newSampleRate);
  1359. if (pData->active)
  1360. deactivate();
  1361. #if ! VST_FORCE_DEPRECATED
  1362. dispatcher(effSetBlockSizeAndSampleRate, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, static_cast<float>(newSampleRate));
  1363. #endif
  1364. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(newSampleRate));
  1365. if (pData->active)
  1366. activate();
  1367. }
  1368. // -------------------------------------------------------------------
  1369. // Plugin buffers
  1370. // nothing
  1371. // -------------------------------------------------------------------
  1372. // Post-poned UI Stuff
  1373. // nothing
  1374. // -------------------------------------------------------------------
  1375. protected:
  1376. void handlePluginUIClosed() override
  1377. {
  1378. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1379. carla_debug("CarlaPluginVST2::handlePluginUIClosed()");
  1380. showCustomUI(false);
  1381. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1382. }
  1383. void handlePluginUIResized(const uint width, const uint height) override
  1384. {
  1385. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  1386. carla_debug("CarlaPluginVST2::handlePluginUIResized(%u, %u)", width, height);
  1387. return; // unused
  1388. (void)width; (void)height;
  1389. }
  1390. // -------------------------------------------------------------------
  1391. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const noexcept
  1392. {
  1393. CARLA_SAFE_ASSERT_RETURN(fEffect != nullptr, 0);
  1394. #ifdef DEBUG
  1395. if (opcode != effIdle && opcode != effEditIdle && opcode != effProcessEvents)
  1396. carla_debug("CarlaPluginVST2::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1397. #endif
  1398. try {
  1399. return fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt);
  1400. } CARLA_SAFE_EXCEPTION_RETURN("Vst dispatcher", 0);
  1401. }
  1402. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1403. {
  1404. #ifdef DEBUG
  1405. if (opcode != audioMasterGetTime)
  1406. carla_debug("CarlaPluginVST2::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1407. #endif
  1408. intptr_t ret = 0;
  1409. switch (opcode)
  1410. {
  1411. case audioMasterAutomate: {
  1412. CARLA_SAFE_ASSERT_BREAK(pData->enabled);
  1413. // plugins should never do this:
  1414. CARLA_SAFE_ASSERT_INT(index >= 0 && index < static_cast<int32_t>(pData->param.count), index);
  1415. if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
  1416. break;
  1417. const uint32_t uindex(static_cast<uint32_t>(index));
  1418. const float fixedValue(pData->param.getFixedValue(uindex, opt));
  1419. // Called from plugin process thread, nasty!
  1420. if (pthread_equal(pthread_self(), fProcThread))
  1421. {
  1422. CARLA_SAFE_ASSERT(fIsProcessing);
  1423. pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1424. }
  1425. // Called from UI
  1426. else if (fUI.isVisible)
  1427. {
  1428. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1429. }
  1430. // Unknown
  1431. // TODO - check if plugin or UI is initializing
  1432. else
  1433. {
  1434. carla_stdout("audioMasterAutomate called from unknown source");
  1435. setParameterValue(uindex, fixedValue, true, true, true);
  1436. //pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1437. }
  1438. break;
  1439. }
  1440. case audioMasterCurrentId:
  1441. ret = fEffect->uniqueID;
  1442. break;
  1443. case audioMasterIdle:
  1444. if (pthread_equal(pthread_self(), fMainThread))
  1445. {
  1446. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1447. if (pData->engine->getType() != kEngineTypePlugin)
  1448. pData->engine->idle();
  1449. }
  1450. break;
  1451. #if ! VST_FORCE_DEPRECATED
  1452. case audioMasterPinConnected:
  1453. // Deprecated in VST SDK 2.4
  1454. // TODO
  1455. break;
  1456. case audioMasterWantMidi:
  1457. // Deprecated in VST SDK 2.4
  1458. pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
  1459. break;
  1460. #endif
  1461. case audioMasterGetTime:
  1462. ret = (intptr_t)&fTimeInfo;
  1463. break;
  1464. case audioMasterProcessEvents:
  1465. CARLA_SAFE_ASSERT_RETURN(pData->enabled, 0);
  1466. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, 0);
  1467. CARLA_SAFE_ASSERT_RETURN(pData->event.portOut != nullptr, 0);
  1468. if (fMidiEventCount >= kPluginMaxMidiEvents*2-1)
  1469. return 0;
  1470. if (const VstEvents* const vstEvents = (const VstEvents*)ptr)
  1471. {
  1472. for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
  1473. {
  1474. if (vstEvents->events[i] == nullptr)
  1475. break;
  1476. const VstMidiEvent* const vstMidiEvent((const VstMidiEvent*)vstEvents->events[i]);
  1477. if (vstMidiEvent->type != kVstMidiType)
  1478. continue;
  1479. // reverse-find first free event, and put it there
  1480. for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
  1481. {
  1482. if (fMidiEvents[j].type == 0)
  1483. {
  1484. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1485. break;
  1486. }
  1487. }
  1488. }
  1489. }
  1490. ret = 1;
  1491. break;
  1492. #if ! VST_FORCE_DEPRECATED
  1493. case audioMasterSetTime:
  1494. // Deprecated in VST SDK 2.4
  1495. break;
  1496. case audioMasterTempoAt:
  1497. // Deprecated in VST SDK 2.4
  1498. ret = static_cast<intptr_t>(fTimeInfo.tempo * 10000);
  1499. break;
  1500. case audioMasterGetNumAutomatableParameters:
  1501. // Deprecated in VST SDK 2.4
  1502. ret = static_cast<intptr_t>(pData->engine->getOptions().maxParameters);
  1503. ret = carla_minPositive<intptr_t>(ret, fEffect->numParams);
  1504. break;
  1505. case audioMasterGetParameterQuantization:
  1506. // Deprecated in VST SDK 2.4
  1507. ret = 1; // full single float precision
  1508. break;
  1509. #endif
  1510. #if 0
  1511. case audioMasterIOChanged:
  1512. CARLA_ASSERT(pData->enabled);
  1513. // TESTING
  1514. if (! pData->enabled)
  1515. {
  1516. ret = 1;
  1517. break;
  1518. }
  1519. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1520. {
  1521. carla_stderr2("CarlaPluginVST2::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1522. return 0;
  1523. }
  1524. engineProcessLock();
  1525. m_enabled = false;
  1526. engineProcessUnlock();
  1527. if (m_active)
  1528. {
  1529. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1530. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1531. }
  1532. reload();
  1533. if (m_active)
  1534. {
  1535. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1536. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1537. }
  1538. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1539. ret = 1;
  1540. break;
  1541. #endif
  1542. #if ! VST_FORCE_DEPRECATED
  1543. case audioMasterNeedIdle:
  1544. // Deprecated in VST SDK 2.4
  1545. fNeedIdle = true;
  1546. ret = 1;
  1547. break;
  1548. #endif
  1549. case audioMasterSizeWindow:
  1550. CARLA_SAFE_ASSERT_BREAK(fUI.window != nullptr);
  1551. CARLA_SAFE_ASSERT_BREAK(index > 0);
  1552. CARLA_SAFE_ASSERT_BREAK(value > 0);
  1553. fUI.window->setSize(static_cast<uint>(index), static_cast<uint>(value), true);
  1554. ret = 1;
  1555. break;
  1556. case audioMasterGetSampleRate:
  1557. ret = static_cast<intptr_t>(pData->engine->getSampleRate());
  1558. break;
  1559. case audioMasterGetBlockSize:
  1560. ret = static_cast<intptr_t>(pData->engine->getBufferSize());
  1561. break;
  1562. case audioMasterGetInputLatency:
  1563. ret = 0;
  1564. break;
  1565. case audioMasterGetOutputLatency:
  1566. ret = 0;
  1567. break;
  1568. #if ! VST_FORCE_DEPRECATED
  1569. case audioMasterGetPreviousPlug:
  1570. // Deprecated in VST SDK 2.4
  1571. // TODO
  1572. break;
  1573. case audioMasterGetNextPlug:
  1574. // Deprecated in VST SDK 2.4
  1575. // TODO
  1576. break;
  1577. case audioMasterWillReplaceOrAccumulate:
  1578. // Deprecated in VST SDK 2.4
  1579. ret = 1; // replace
  1580. break;
  1581. #endif
  1582. case audioMasterGetCurrentProcessLevel:
  1583. if (pthread_equal(pthread_self(), fProcThread))
  1584. {
  1585. CARLA_SAFE_ASSERT(fIsProcessing);
  1586. if (pData->engine->isOffline())
  1587. ret = kVstProcessLevelOffline;
  1588. else
  1589. ret = kVstProcessLevelRealtime;
  1590. }
  1591. else
  1592. {
  1593. ret = kVstProcessLevelUser;
  1594. }
  1595. break;
  1596. case audioMasterGetAutomationState:
  1597. ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1598. break;
  1599. case audioMasterOfflineStart:
  1600. case audioMasterOfflineRead:
  1601. case audioMasterOfflineWrite:
  1602. case audioMasterOfflineGetCurrentPass:
  1603. case audioMasterOfflineGetCurrentMetaPass:
  1604. // TODO
  1605. break;
  1606. #if ! VST_FORCE_DEPRECATED
  1607. case audioMasterSetOutputSampleRate:
  1608. // Deprecated in VST SDK 2.4
  1609. break;
  1610. case audioMasterGetOutputSpeakerArrangement:
  1611. // Deprecated in VST SDK 2.4
  1612. // TODO
  1613. break;
  1614. #endif
  1615. case audioMasterVendorSpecific:
  1616. // TODO - cockos extensions
  1617. break;
  1618. #if ! VST_FORCE_DEPRECATED
  1619. case audioMasterSetIcon:
  1620. // Deprecated in VST SDK 2.4
  1621. break;
  1622. #endif
  1623. #if ! VST_FORCE_DEPRECATED
  1624. case audioMasterOpenWindow:
  1625. case audioMasterCloseWindow:
  1626. // Deprecated in VST SDK 2.4
  1627. // TODO
  1628. break;
  1629. #endif
  1630. case audioMasterGetDirectory:
  1631. // TODO
  1632. break;
  1633. case audioMasterUpdateDisplay:
  1634. // Idle UI if visible
  1635. if (fUI.isVisible)
  1636. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1637. // Update current program
  1638. if (pData->prog.count > 0)
  1639. {
  1640. const int32_t current = static_cast<int32_t>(dispatcher(effGetProgram, 0, 0, nullptr, 0.0f));
  1641. if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
  1642. {
  1643. char strBuf[STR_MAX+1] = { '\0' };
  1644. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1645. if (pData->prog.names[current] != nullptr)
  1646. delete[] pData->prog.names[current];
  1647. pData->prog.names[current] = carla_strdup(strBuf);
  1648. if (pData->prog.current != current)
  1649. {
  1650. pData->prog.current = current;
  1651. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
  1652. }
  1653. }
  1654. }
  1655. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  1656. ret = 1;
  1657. break;
  1658. case audioMasterBeginEdit:
  1659. case audioMasterEndEdit:
  1660. // TODO
  1661. break;
  1662. case audioMasterOpenFileSelector:
  1663. case audioMasterCloseFileSelector:
  1664. // TODO
  1665. break;
  1666. #if ! VST_FORCE_DEPRECATED
  1667. case audioMasterEditFile:
  1668. // Deprecated in VST SDK 2.4
  1669. // TODO
  1670. break;
  1671. case audioMasterGetChunkFile:
  1672. // Deprecated in VST SDK 2.4
  1673. // TODO
  1674. break;
  1675. case audioMasterGetInputSpeakerArrangement:
  1676. // Deprecated in VST SDK 2.4
  1677. // TODO
  1678. break;
  1679. #endif
  1680. default:
  1681. carla_debug("CarlaPluginVST2::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f) UNDEF", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1682. break;
  1683. }
  1684. return ret;
  1685. // unused
  1686. (void)opt;
  1687. }
  1688. bool canDo(const char* const feature) const noexcept
  1689. {
  1690. try {
  1691. return (fEffect->dispatcher(fEffect, effCanDo, 0, 0, const_cast<char*>(feature), 0.0f) == 1);
  1692. } CARLA_SAFE_EXCEPTION_RETURN("vstPluginCanDo", false);
  1693. }
  1694. bool hasMidiInput() const noexcept
  1695. {
  1696. return (canDo("receiveVstEvents") || canDo("receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT));
  1697. }
  1698. bool hasMidiOutput() const noexcept
  1699. {
  1700. return (canDo("sendVstEvents") || canDo("sendVstMidiEvent"));
  1701. }
  1702. // -------------------------------------------------------------------
  1703. const void* getNativeDescriptor() const noexcept override
  1704. {
  1705. return fEffect;
  1706. }
  1707. // -------------------------------------------------------------------
  1708. public:
  1709. bool init(const char* const filename, const char* const name, const int64_t uniqueId, const uint options)
  1710. {
  1711. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1712. // ---------------------------------------------------------------
  1713. // first checks
  1714. if (pData->client != nullptr)
  1715. {
  1716. pData->engine->setLastError("Plugin client is already registered");
  1717. return false;
  1718. }
  1719. if (filename == nullptr || filename[0] == '\0')
  1720. {
  1721. pData->engine->setLastError("null filename");
  1722. return false;
  1723. }
  1724. // ---------------------------------------------------------------
  1725. VST_Function vstFn;
  1726. #ifdef CARLA_OS_MAC
  1727. CarlaString filenameCheck(filename);
  1728. filenameCheck.toLower();
  1729. if (filenameCheck.endsWith(".vst") || filenameCheck.endsWith(".vst/"))
  1730. {
  1731. // FIXME assert returns, set engine error
  1732. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)strlen(filename), true);
  1733. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr, false);
  1734. fMacBundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  1735. CFRelease(urlRef);
  1736. CARLA_SAFE_ASSERT_RETURN(fMacBundleRef != nullptr, false);
  1737. if (! CFBundleLoadExecutable(fMacBundleRef))
  1738. {
  1739. CFRelease(fMacBundleRef);
  1740. pData->engine->setLastError("Failed to load VST bundle executable");
  1741. return false;
  1742. }
  1743. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("main_macho"));
  1744. if (vstFn == nullptr)
  1745. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(fMacBundleRef, CFSTR("VSTPluginMain"));
  1746. if (vstFn == nullptr)
  1747. {
  1748. CFBundleUnloadExecutable(fMacBundleRef);
  1749. CFRelease(fMacBundleRef);
  1750. pData->engine->setLastError("Not a VST plugin");
  1751. return false;
  1752. }
  1753. fMacBundleRefNum = CFBundleOpenBundleResourceMap(fMacBundleRef);
  1754. }
  1755. else
  1756. #endif
  1757. {
  1758. // -----------------------------------------------------------
  1759. // open DLL
  1760. if (! pData->libOpen(filename))
  1761. {
  1762. pData->engine->setLastError(pData->libError(filename));
  1763. return false;
  1764. }
  1765. // -----------------------------------------------------------
  1766. // get DLL main entry
  1767. vstFn = pData->libSymbol<VST_Function>("VSTPluginMain");
  1768. if (vstFn == nullptr)
  1769. {
  1770. vstFn = pData->libSymbol<VST_Function>("main");
  1771. if (vstFn == nullptr)
  1772. {
  1773. pData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1774. return false;
  1775. }
  1776. }
  1777. }
  1778. // ---------------------------------------------------------------
  1779. // initialize plugin (part 1)
  1780. sCurrentUniqueId = uniqueId;
  1781. sLastCarlaPluginVST2 = this;
  1782. try {
  1783. fEffect = vstFn(carla_vst_audioMasterCallback);
  1784. } CARLA_SAFE_EXCEPTION_RETURN("Vst init", false);
  1785. sLastCarlaPluginVST2 = nullptr;
  1786. sCurrentUniqueId = 0;
  1787. if (fEffect == nullptr)
  1788. {
  1789. pData->engine->setLastError("Plugin failed to initialize");
  1790. return false;
  1791. }
  1792. if (fEffect->magic != kEffectMagic)
  1793. {
  1794. pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1795. return false;
  1796. }
  1797. fEffect->ptr1 = this;
  1798. dispatcher(effIdentify, 0, 0, 0, 0);
  1799. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1800. dispatcher(effSetBlockSizeAndSampleRate, 0,
  1801. static_cast<int32_t>(pData->engine->getBufferSize()), nullptr,
  1802. static_cast<float>(pData->engine->getSampleRate()));
  1803. dispatcher(effSetSampleRate, 0, 0, nullptr, static_cast<float>(pData->engine->getSampleRate()));
  1804. dispatcher(effSetBlockSize, 0, static_cast<int32_t>(pData->engine->getBufferSize()), nullptr, 0.0f);
  1805. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1806. // ---------------------------------------------------------------
  1807. // get info
  1808. if (name != nullptr && name[0] != '\0')
  1809. {
  1810. pData->name = pData->engine->getUniquePluginName(name);
  1811. }
  1812. else
  1813. {
  1814. char strBuf[STR_MAX+1];
  1815. carla_zeroChars(strBuf, STR_MAX+1);
  1816. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1817. if (strBuf[0] != '\0')
  1818. pData->name = pData->engine->getUniquePluginName(strBuf);
  1819. else if (const char* const shortname = std::strrchr(filename, CARLA_OS_SEP))
  1820. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1821. else
  1822. pData->name = pData->engine->getUniquePluginName("unknown");
  1823. }
  1824. pData->filename = carla_strdup(filename);
  1825. // ---------------------------------------------------------------
  1826. // register client
  1827. pData->client = pData->engine->addClient(this);
  1828. if (pData->client == nullptr || ! pData->client->isOk())
  1829. {
  1830. pData->engine->setLastError("Failed to register plugin client");
  1831. return false;
  1832. }
  1833. // ---------------------------------------------------------------
  1834. // initialize plugin (part 2)
  1835. for (int i = fEffect->numInputs; --i >= 0;) dispatcher(effConnectInput, i, 1, 0, 0);
  1836. for (int i = fEffect->numOutputs; --i >= 0;) dispatcher(effConnectOutput, i, 1, 0, 0);
  1837. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1838. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  1839. static const char kHasCockosExtensions[] = "hasCockosExtensions";
  1840. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, const_cast<char*>(kHasCockosExtensions), 0.0f)) == 0xbeef0000)
  1841. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1842. // ---------------------------------------------------------------
  1843. // set default options
  1844. pData->options = 0x0;
  1845. if (pData->latency.frames != 0 || hasMidiOutput())
  1846. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1847. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1848. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1849. if (fEffect->numPrograms > 1)
  1850. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1851. if (fEffect->flags & effFlagsProgramChunks)
  1852. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1853. if (hasMidiInput())
  1854. {
  1855. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1856. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1857. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1858. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1859. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1860. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1861. if (options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1862. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1863. }
  1864. return true;
  1865. // unused
  1866. (void)uniqueId;
  1867. }
  1868. private:
  1869. int fUnique1;
  1870. AEffect* fEffect;
  1871. uint32_t fMidiEventCount;
  1872. VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1873. VstTimeInfo fTimeInfo;
  1874. bool fNeedIdle;
  1875. void* fLastChunk;
  1876. bool fIsProcessing;
  1877. pthread_t fMainThread;
  1878. pthread_t fProcThread;
  1879. #ifdef CARLA_OS_MAC
  1880. CFBundleRef fMacBundleRef;
  1881. CFBundleRefNum fMacBundleRefNum;
  1882. #endif
  1883. struct FixedVstEvents {
  1884. int32_t numEvents;
  1885. intptr_t reserved;
  1886. VstEvent* data[kPluginMaxMidiEvents*2];
  1887. FixedVstEvents() noexcept
  1888. : numEvents(0),
  1889. reserved(0)
  1890. {
  1891. carla_zeroPointers(data, kPluginMaxMidiEvents*2);
  1892. }
  1893. CARLA_DECLARE_NON_COPY_STRUCT(FixedVstEvents);
  1894. } fEvents;
  1895. struct UI {
  1896. bool isVisible;
  1897. CarlaPluginUI* window;
  1898. UI() noexcept
  1899. : isVisible(false),
  1900. window(nullptr) {}
  1901. ~UI()
  1902. {
  1903. CARLA_ASSERT(! isVisible);
  1904. if (window != nullptr)
  1905. {
  1906. delete window;
  1907. window = nullptr;
  1908. }
  1909. }
  1910. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  1911. } fUI;
  1912. int fUnique2;
  1913. static intptr_t sCurrentUniqueId;
  1914. static CarlaPluginVST2* sLastCarlaPluginVST2;
  1915. // -------------------------------------------------------------------
  1916. static bool compareMagic (int32_t magic, const char* name) noexcept
  1917. {
  1918. return magic == (int32_t)ByteOrder::littleEndianInt (name)
  1919. || magic == (int32_t)ByteOrder::bigEndianInt (name);
  1920. }
  1921. static int32_t fxbSwap(const int32_t x) noexcept
  1922. {
  1923. return (int32_t)ByteOrder::swapIfLittleEndian ((uint32_t) x);
  1924. }
  1925. bool loadOldSaveFormat(const void* const data, const std::size_t dataSize)
  1926. {
  1927. if (dataSize < 28)
  1928. return false;
  1929. const int32_t* const set = (const int32_t*)data;
  1930. if (! compareMagic(set[0], "CcnK"))
  1931. return false;
  1932. if (! compareMagic(set[2], "FBCh"))
  1933. return false;
  1934. if (fxbSwap(set[3]) > 1)
  1935. return false;
  1936. const int32_t chunkSize = fxbSwap(set[39]);
  1937. CARLA_SAFE_ASSERT_RETURN(chunkSize > 0, false);
  1938. if (static_cast<const std::size_t>(chunkSize + 160) > dataSize)
  1939. return false;
  1940. setChunkData(&set[40], static_cast<const std::size_t>(chunkSize));
  1941. return true;
  1942. }
  1943. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1944. {
  1945. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1946. if (std::strcmp(feature, "supplyIdle") == 0)
  1947. return 1;
  1948. if (std::strcmp(feature, "sendVstEvents") == 0)
  1949. return 1;
  1950. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1951. return 1;
  1952. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1953. return 1;
  1954. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1955. return 1;
  1956. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1957. return 1;
  1958. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1959. return 1;
  1960. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1961. return -1;
  1962. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1963. return -1;
  1964. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1965. return 1;
  1966. if (std::strcmp(feature, "sizeWindow") == 0)
  1967. return 1;
  1968. if (std::strcmp(feature, "offline") == 0)
  1969. return -1;
  1970. if (std::strcmp(feature, "openFileSelector") == 0)
  1971. return -1;
  1972. if (std::strcmp(feature, "closeFileSelector") == 0)
  1973. return -1;
  1974. if (std::strcmp(feature, "startStopProcess") == 0)
  1975. return 1;
  1976. if (std::strcmp(feature, "supportShell") == 0)
  1977. return 1;
  1978. if (std::strcmp(feature, "shellCategory") == 0)
  1979. return 1;
  1980. if (std::strcmp(feature, "NIMKPIVendorSpecificCallbacks") == 0)
  1981. return -1;
  1982. // unimplemented
  1983. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1984. return 0;
  1985. }
  1986. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1987. {
  1988. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1989. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1990. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1991. #endif
  1992. switch (opcode)
  1993. {
  1994. case audioMasterVersion:
  1995. return kVstVersion;
  1996. case audioMasterCurrentId:
  1997. if (sCurrentUniqueId != 0)
  1998. return sCurrentUniqueId;
  1999. break;
  2000. case audioMasterGetVendorString:
  2001. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2002. std::strcpy((char*)ptr, "falkTX");
  2003. return 1;
  2004. case audioMasterGetProductString:
  2005. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2006. std::strcpy((char*)ptr, "Carla");
  2007. return 1;
  2008. case audioMasterGetVendorVersion:
  2009. return CARLA_VERSION_HEX;
  2010. case audioMasterCanDo:
  2011. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2012. return carla_vst_hostCanDo((const char*)ptr);
  2013. case audioMasterGetLanguage:
  2014. return kVstLangEnglish;
  2015. }
  2016. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  2017. CarlaPluginVST2* self = nullptr;
  2018. if (effect != nullptr)
  2019. {
  2020. if (effect->ptr1 != nullptr)
  2021. {
  2022. self = (CarlaPluginVST2*)effect->ptr1;
  2023. if (self->fUnique1 != self->fUnique2)
  2024. self = nullptr;
  2025. }
  2026. if (self != nullptr)
  2027. {
  2028. if (self->fEffect == nullptr)
  2029. self->fEffect = effect;
  2030. if (self->fEffect != effect)
  2031. {
  2032. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2033. self = nullptr;
  2034. }
  2035. }
  2036. else if (sLastCarlaPluginVST2 != nullptr)
  2037. {
  2038. effect->ptr1 = sLastCarlaPluginVST2;
  2039. self = sLastCarlaPluginVST2;
  2040. }
  2041. }
  2042. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2043. }
  2044. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginVST2)
  2045. };
  2046. intptr_t CarlaPluginVST2::sCurrentUniqueId = 0;
  2047. CarlaPluginVST2* CarlaPluginVST2::sLastCarlaPluginVST2 = nullptr;
  2048. CARLA_BACKEND_END_NAMESPACE
  2049. // -------------------------------------------------------------------------------------------------------------------
  2050. CARLA_BACKEND_START_NAMESPACE
  2051. CarlaPlugin* CarlaPlugin::newVST2(const Initializer& init)
  2052. {
  2053. carla_debug("CarlaPlugin::newVST2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
  2054. CarlaPluginVST2* const plugin(new CarlaPluginVST2(init.engine, init.id));
  2055. if (! plugin->init(init.filename, init.name, init.uniqueId, init.options))
  2056. {
  2057. delete plugin;
  2058. return nullptr;
  2059. }
  2060. return plugin;
  2061. }
  2062. // -------------------------------------------------------------------------------------------------------------------
  2063. CARLA_BACKEND_END_NAMESPACE