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.

2564 lines
83KB

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