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.

2595 lines
87KB

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