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.

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