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.

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