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.

2566 lines
86KB

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