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.

2621 lines
89KB

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