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.

2545 lines
83KB

  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. #ifdef WANT_VST
  20. #include "CarlaVstUtils.hpp"
  21. #include "CarlaMathUtils.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. FLOAT_CLEAR(outBuffer[i], frames);
  797. return;
  798. }
  799. fMidiEventCount = 0;
  800. carla_zeroStruct<VstMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  801. // --------------------------------------------------------------------------------------------------------
  802. // Check if needs reset
  803. if (pData->needsReset)
  804. {
  805. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  806. {
  807. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  808. {
  809. fMidiEvents[k].type = kVstMidiType;
  810. fMidiEvents[k].byteSize = sizeof(VstMidiEvent);
  811. fMidiEvents[k].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  812. fMidiEvents[k].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  813. fMidiEvents[k+i].type = kVstMidiType;
  814. fMidiEvents[k+i].byteSize = sizeof(VstMidiEvent);
  815. fMidiEvents[k+i].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  816. fMidiEvents[k+i].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  817. }
  818. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  819. }
  820. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  821. {
  822. for (k=0; k < MAX_MIDI_NOTE; ++k)
  823. {
  824. fMidiEvents[k].type = kVstMidiType;
  825. fMidiEvents[k].byteSize = sizeof(VstMidiEvent);
  826. fMidiEvents[k].midiData[0] = MIDI_STATUS_NOTE_OFF + pData->ctrlChannel;
  827. fMidiEvents[k].midiData[1] = k;
  828. }
  829. fMidiEventCount = MAX_MIDI_NOTE;
  830. }
  831. if (pData->latency > 0)
  832. {
  833. for (i=0; i < pData->audioIn.count; ++i)
  834. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  835. }
  836. pData->needsReset = false;
  837. }
  838. CARLA_PROCESS_CONTINUE_CHECK;
  839. // --------------------------------------------------------------------------------------------------------
  840. // Set TimeInfo
  841. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  842. fTimeInfo.flags = kVstTransportChanged;
  843. if (timeInfo.playing)
  844. fTimeInfo.flags |= kVstTransportPlaying;
  845. fTimeInfo.samplePos = timeInfo.frame;
  846. fTimeInfo.sampleRate = pData->engine->getSampleRate();
  847. if (timeInfo.usecs != 0)
  848. {
  849. fTimeInfo.nanoSeconds = timeInfo.usecs/1000;
  850. fTimeInfo.flags |= kVstNanosValid;
  851. }
  852. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  853. {
  854. double ppqBar = double(timeInfo.bbt.bar - 1) * timeInfo.bbt.beatsPerBar;
  855. double ppqBeat = double(timeInfo.bbt.beat - 1);
  856. double ppqTick = double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat;
  857. // PPQ Pos
  858. fTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  859. fTimeInfo.flags |= kVstPpqPosValid;
  860. // Tempo
  861. fTimeInfo.tempo = timeInfo.bbt.beatsPerMinute;
  862. fTimeInfo.flags |= kVstTempoValid;
  863. // Bars
  864. fTimeInfo.barStartPos = ppqBar;
  865. fTimeInfo.flags |= kVstBarsValid;
  866. // Time Signature
  867. fTimeInfo.timeSigNumerator = timeInfo.bbt.beatsPerBar;
  868. fTimeInfo.timeSigDenominator = timeInfo.bbt.beatType;
  869. fTimeInfo.flags |= kVstTimeSigValid;
  870. }
  871. else
  872. {
  873. // Tempo
  874. fTimeInfo.tempo = 120.0;
  875. fTimeInfo.flags |= kVstTempoValid;
  876. // Time Signature
  877. fTimeInfo.timeSigNumerator = 4;
  878. fTimeInfo.timeSigDenominator = 4;
  879. fTimeInfo.flags |= kVstTimeSigValid;
  880. // Missing info
  881. fTimeInfo.ppqPos = 0.0;
  882. fTimeInfo.barStartPos = 0.0;
  883. }
  884. CARLA_PROCESS_CONTINUE_CHECK;
  885. // --------------------------------------------------------------------------------------------------------
  886. // Event Input and Processing
  887. if (pData->event.portIn != nullptr)
  888. {
  889. // ----------------------------------------------------------------------------------------------------
  890. // MIDI Input (External)
  891. if (pData->extNotes.mutex.tryLock())
  892. {
  893. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  894. {
  895. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  896. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  897. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  898. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  899. fMidiEvents[fMidiEventCount].midiData[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  900. fMidiEvents[fMidiEventCount].midiData[0] += note.channel;
  901. fMidiEvents[fMidiEventCount].midiData[1] = note.note;
  902. fMidiEvents[fMidiEventCount].midiData[2] = note.velo;
  903. fMidiEventCount += 1;
  904. }
  905. pData->extNotes.mutex.unlock();
  906. } // End of MIDI Input (External)
  907. // ----------------------------------------------------------------------------------------------------
  908. // Event Input (System)
  909. bool allNotesOffSent = false;
  910. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  911. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  912. uint32_t startTime = 0;
  913. uint32_t timeOffset = 0;
  914. for (i=0; i < nEvents; ++i)
  915. {
  916. const EngineEvent& event(pData->event.portIn->getEvent(i));
  917. time = event.time;
  918. if (time >= frames)
  919. continue;
  920. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  921. if (time > timeOffset && sampleAccurate)
  922. {
  923. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  924. {
  925. startTime = 0;
  926. timeOffset = time;
  927. if (fMidiEventCount > 0)
  928. {
  929. carla_zeroStruct<VstMidiEvent>(fMidiEvents, fMidiEventCount);
  930. fMidiEventCount = 0;
  931. }
  932. }
  933. else
  934. startTime += timeOffset;
  935. }
  936. // Control change
  937. switch (event.type)
  938. {
  939. case kEngineEventTypeNull:
  940. break;
  941. case kEngineEventTypeControl:
  942. {
  943. const EngineControlEvent& ctrlEvent = event.ctrl;
  944. switch (ctrlEvent.type)
  945. {
  946. case kEngineControlEventTypeNull:
  947. break;
  948. case kEngineControlEventTypeParameter:
  949. {
  950. #ifndef BUILD_BRIDGE
  951. // Control backend stuff
  952. if (event.channel == pData->ctrlChannel)
  953. {
  954. float value;
  955. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  956. {
  957. value = ctrlEvent.value;
  958. setDryWet(value, false, false);
  959. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  960. }
  961. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  962. {
  963. value = ctrlEvent.value*127.0f/100.0f;
  964. setVolume(value, false, false);
  965. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  966. }
  967. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  968. {
  969. float left, right;
  970. value = ctrlEvent.value/0.5f - 1.0f;
  971. if (value < 0.0f)
  972. {
  973. left = -1.0f;
  974. right = (value*2.0f)+1.0f;
  975. }
  976. else if (value > 0.0f)
  977. {
  978. left = (value*2.0f)-1.0f;
  979. right = 1.0f;
  980. }
  981. else
  982. {
  983. left = -1.0f;
  984. right = 1.0f;
  985. }
  986. setBalanceLeft(left, false, false);
  987. setBalanceRight(right, false, false);
  988. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  989. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  990. }
  991. }
  992. #endif
  993. // Control plugin parameters
  994. for (k=0; k < pData->param.count; ++k)
  995. {
  996. if (pData->param.data[k].midiChannel != event.channel)
  997. continue;
  998. if (pData->param.data[k].midiCC != ctrlEvent.param)
  999. continue;
  1000. if ((pData->param.data[k].hints & PARAMETER_IS_INPUT) == 0)
  1001. continue;
  1002. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1003. continue;
  1004. float value;
  1005. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1006. {
  1007. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1008. }
  1009. else
  1010. {
  1011. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1012. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1013. value = std::rint(value);
  1014. }
  1015. setParameterValue(k, value, false, false, false);
  1016. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1017. }
  1018. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1019. {
  1020. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1021. continue;
  1022. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1023. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1024. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1025. fMidiEvents[fMidiEventCount].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1026. fMidiEvents[fMidiEventCount].midiData[1] = ctrlEvent.param;
  1027. fMidiEvents[fMidiEventCount].midiData[2] = ctrlEvent.value*127.0f;
  1028. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1029. fMidiEventCount += 1;
  1030. }
  1031. break;
  1032. }
  1033. case kEngineControlEventTypeMidiBank:
  1034. break;
  1035. case kEngineControlEventTypeMidiProgram:
  1036. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1037. {
  1038. if (ctrlEvent.param < pData->prog.count)
  1039. {
  1040. setProgram(ctrlEvent.param, false, false, false);
  1041. pData->postponeRtEvent(kPluginPostRtEventProgramChange, ctrlEvent.param, 0, 0.0f);
  1042. break;
  1043. }
  1044. }
  1045. break;
  1046. case kEngineControlEventTypeAllSoundOff:
  1047. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1048. {
  1049. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1050. continue;
  1051. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1052. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1053. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1054. fMidiEvents[fMidiEventCount].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1055. fMidiEvents[fMidiEventCount].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1056. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1057. fMidiEventCount += 1;
  1058. }
  1059. break;
  1060. case kEngineControlEventTypeAllNotesOff:
  1061. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1062. {
  1063. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1064. {
  1065. allNotesOffSent = true;
  1066. sendMidiAllNotesOffToCallback();
  1067. }
  1068. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1069. continue;
  1070. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1071. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1072. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1073. fMidiEvents[fMidiEventCount].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1074. fMidiEvents[fMidiEventCount].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1075. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1076. fMidiEventCount += 1;
  1077. }
  1078. break;
  1079. }
  1080. break;
  1081. }
  1082. case kEngineEventTypeMidi:
  1083. {
  1084. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1085. continue;
  1086. const EngineMidiEvent& midiEvent(event.midi);
  1087. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1088. uint8_t channel = event.channel;
  1089. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1090. continue;
  1091. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1092. continue;
  1093. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1094. continue;
  1095. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1096. continue;
  1097. // Fix bad note-off
  1098. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1099. status -= 0x10;
  1100. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1101. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1102. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1103. fMidiEvents[fMidiEventCount].midiData[0] = status + channel;
  1104. fMidiEvents[fMidiEventCount].midiData[1] = midiEvent.data[1];
  1105. fMidiEvents[fMidiEventCount].midiData[2] = midiEvent.data[2];
  1106. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1107. fMidiEventCount += 1;
  1108. if (status == MIDI_STATUS_NOTE_ON)
  1109. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1110. else if (status == MIDI_STATUS_NOTE_OFF)
  1111. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. pData->postRtEvents.trySplice();
  1117. if (frames > timeOffset)
  1118. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1119. } // End of Event Input and Processing
  1120. // --------------------------------------------------------------------------------------------------------
  1121. // Plugin processing (no events)
  1122. else
  1123. {
  1124. processSingle(inBuffer, outBuffer, frames, 0);
  1125. } // End of Plugin processing (no events)
  1126. CARLA_PROCESS_CONTINUE_CHECK;
  1127. // --------------------------------------------------------------------------------------------------------
  1128. // MIDI Output
  1129. if (pData->event.portOut != nullptr)
  1130. {
  1131. // reverse lookup MIDI events
  1132. for (k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1133. {
  1134. if (fMidiEvents[k].type == 0)
  1135. break;
  1136. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].midiData);
  1137. uint8_t midiData[3] = { 0 };
  1138. midiData[0] = fMidiEvents[k].midiData[0];
  1139. midiData[1] = fMidiEvents[k].midiData[1];
  1140. midiData[2] = fMidiEvents[k].midiData[2];
  1141. pData->event.portOut->writeMidiEvent(fMidiEvents[k].deltaFrames, channel, 0, 3, midiData);
  1142. }
  1143. } // End of Control and MIDI Output
  1144. }
  1145. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1146. {
  1147. CARLA_ASSERT(frames > 0);
  1148. if (frames == 0)
  1149. return false;
  1150. if (pData->audioIn.count > 0)
  1151. {
  1152. CARLA_ASSERT(inBuffer != nullptr);
  1153. if (inBuffer == nullptr)
  1154. return false;
  1155. }
  1156. if (pData->audioOut.count > 0)
  1157. {
  1158. CARLA_ASSERT(outBuffer != nullptr);
  1159. if (outBuffer == nullptr)
  1160. return false;
  1161. }
  1162. uint32_t i, k;
  1163. // --------------------------------------------------------------------------------------------------------
  1164. // Try lock, silence otherwise
  1165. if (pData->engine->isOffline())
  1166. {
  1167. pData->singleMutex.lock();
  1168. }
  1169. else if (! pData->singleMutex.tryLock())
  1170. {
  1171. for (i=0; i < pData->audioOut.count; ++i)
  1172. {
  1173. for (k=0; k < frames; ++k)
  1174. outBuffer[i][k+timeOffset] = 0.0f;
  1175. }
  1176. return false;
  1177. }
  1178. // --------------------------------------------------------------------------------------------------------
  1179. // Set audio buffers
  1180. float* vstInBuffer[pData->audioIn.count];
  1181. float* vstOutBuffer[pData->audioOut.count];
  1182. for (i=0; i < pData->audioIn.count; ++i)
  1183. vstInBuffer[i] = inBuffer[i]+timeOffset;
  1184. for (i=0; i < pData->audioOut.count; ++i)
  1185. vstOutBuffer[i] = outBuffer[i]+timeOffset;
  1186. // --------------------------------------------------------------------------------------------------------
  1187. // Set MIDI events
  1188. if (fMidiEventCount > 0)
  1189. {
  1190. fEvents.numEvents = fMidiEventCount;
  1191. fEvents.reserved = 0;
  1192. dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
  1193. }
  1194. // --------------------------------------------------------------------------------------------------------
  1195. // Run plugin
  1196. fIsProcessing = true;
  1197. if (pData->hints & PLUGIN_CAN_PROCESS_REPLACING)
  1198. {
  1199. fEffect->processReplacing(fEffect,
  1200. (pData->audioIn.count > 0) ? vstInBuffer : nullptr,
  1201. (pData->audioOut.count > 0) ? vstOutBuffer : nullptr,
  1202. frames);
  1203. }
  1204. else
  1205. {
  1206. for (i=0; i < pData->audioOut.count; ++i)
  1207. FLOAT_CLEAR(vstOutBuffer[i], frames);
  1208. #if ! VST_FORCE_DEPRECATED
  1209. fEffect->process(fEffect,
  1210. (pData->audioIn.count > 0) ? vstInBuffer : nullptr,
  1211. (pData->audioOut.count > 0) ? vstOutBuffer : nullptr,
  1212. frames);
  1213. #endif
  1214. }
  1215. fIsProcessing = false;
  1216. fTimeInfo.samplePos += frames;
  1217. #ifndef BUILD_BRIDGE
  1218. // --------------------------------------------------------------------------------------------------------
  1219. // Post-processing (dry/wet, volume and balance)
  1220. {
  1221. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
  1222. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1223. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1224. bool isPair;
  1225. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1226. for (i=0; i < pData->audioOut.count; ++i)
  1227. {
  1228. // Dry/Wet
  1229. if (doDryWet)
  1230. {
  1231. for (k=0; k < frames; ++k)
  1232. {
  1233. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  1234. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1235. }
  1236. }
  1237. // Balance
  1238. if (doBalance)
  1239. {
  1240. isPair = (i % 2 == 0);
  1241. if (isPair)
  1242. {
  1243. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1244. FLOAT_COPY(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1245. }
  1246. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1247. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1248. for (k=0; k < frames; ++k)
  1249. {
  1250. if (isPair)
  1251. {
  1252. // left
  1253. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1254. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1255. }
  1256. else
  1257. {
  1258. // right
  1259. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1260. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1261. }
  1262. }
  1263. }
  1264. // Volume
  1265. if (doVolume)
  1266. {
  1267. for (k=0; k < frames; ++k)
  1268. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  1269. }
  1270. }
  1271. } // End of Post-processing
  1272. #endif
  1273. // --------------------------------------------------------------------------------------------------------
  1274. pData->singleMutex.unlock();
  1275. return true;
  1276. }
  1277. void bufferSizeChanged(const uint32_t newBufferSize) override
  1278. {
  1279. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1280. carla_debug("VstPlugin::bufferSizeChanged(%i)", newBufferSize);
  1281. if (pData->active)
  1282. deactivate();
  1283. #if ! VST_FORCE_DEPRECATED
  1284. dispatcher(effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, pData->engine->getSampleRate());
  1285. #endif
  1286. dispatcher(effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
  1287. if (pData->active)
  1288. activate();
  1289. }
  1290. void sampleRateChanged(const double newSampleRate) override
  1291. {
  1292. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1293. carla_debug("VstPlugin::sampleRateChanged(%g)", newSampleRate);
  1294. if (pData->active)
  1295. deactivate();
  1296. #if ! VST_FORCE_DEPRECATED
  1297. dispatcher(effSetBlockSizeAndSampleRate, 0, pData->engine->getBufferSize(), nullptr, newSampleRate);
  1298. #endif
  1299. dispatcher(effSetSampleRate, 0, 0, nullptr, newSampleRate);
  1300. if (pData->active)
  1301. activate();
  1302. }
  1303. // -------------------------------------------------------------------
  1304. // Plugin buffers
  1305. // nothing
  1306. // -------------------------------------------------------------------
  1307. // Post-poned UI Stuff
  1308. void uiParameterChange(const uint32_t index, const float value) override
  1309. {
  1310. CARLA_ASSERT(index < pData->param.count);
  1311. if (index >= pData->param.count)
  1312. return;
  1313. if (! fGui.isOsc)
  1314. return;
  1315. if (pData->osc.data.target == nullptr)
  1316. return;
  1317. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  1318. }
  1319. void uiProgramChange(const uint32_t index) override
  1320. {
  1321. CARLA_ASSERT(index < pData->prog.count);
  1322. if (index >= pData->prog.count)
  1323. return;
  1324. if (! fGui.isOsc)
  1325. return;
  1326. if (pData->osc.data.target == nullptr)
  1327. return;
  1328. osc_send_program(pData->osc.data, index);
  1329. }
  1330. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1331. {
  1332. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1333. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1334. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1335. if (channel >= MAX_MIDI_CHANNELS)
  1336. return;
  1337. if (note >= MAX_MIDI_NOTE)
  1338. return;
  1339. if (velo >= MAX_MIDI_VALUE)
  1340. return;
  1341. if (! fGui.isOsc)
  1342. return;
  1343. if (pData->osc.data.target == nullptr)
  1344. return;
  1345. uint8_t midiData[4] = { 0 };
  1346. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1347. midiData[2] = note;
  1348. midiData[3] = velo;
  1349. osc_send_midi(pData->osc.data, midiData);
  1350. }
  1351. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1352. {
  1353. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1354. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1355. if (channel >= MAX_MIDI_CHANNELS)
  1356. return;
  1357. if (note >= MAX_MIDI_NOTE)
  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_OFF + channel;
  1365. midiData[2] = note;
  1366. osc_send_midi(pData->osc.data, midiData);
  1367. }
  1368. // -------------------------------------------------------------------
  1369. protected:
  1370. // void guiClosedCallback() override
  1371. // {
  1372. // showGui(false);
  1373. // pData->engine->callback(CALLBACK_SHOW_GUI, pData->id, 0, 0, 0.0f, nullptr);
  1374. // }
  1375. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt) const
  1376. {
  1377. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1378. if (opcode != effEditIdle && opcode != effProcessEvents)
  1379. carla_debug("VstPlugin::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1380. #endif
  1381. CARLA_ASSERT(fEffect != nullptr);
  1382. return (fEffect != nullptr) ? fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt) : 0;
  1383. }
  1384. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1385. {
  1386. #if 0
  1387. // Cockos VST extensions
  1388. if (ptr != nullptr && static_cast<uint32_t>(opcode) == 0xdeadbeef && static_cast<uint32_t>(index) == 0xdeadf00d)
  1389. {
  1390. const char* const func = (char*)ptr;
  1391. if (std::strcmp(func, "GetPlayPosition") == 0)
  1392. return 0;
  1393. if (std::strcmp(func, "GetPlayPosition2") == 0)
  1394. return 0;
  1395. if (std::strcmp(func, "GetCursorPosition") == 0)
  1396. return 0;
  1397. if (std::strcmp(func, "GetPlayState") == 0)
  1398. return 0;
  1399. if (std::strcmp(func, "SetEditCurPos") == 0)
  1400. return 0;
  1401. if (std::strcmp(func, "GetSetRepeat") == 0)
  1402. return 0;
  1403. if (std::strcmp(func, "GetProjectPath") == 0)
  1404. return 0;
  1405. if (std::strcmp(func, "OnPlayButton") == 0)
  1406. return 0;
  1407. if (std::strcmp(func, "OnStopButton") == 0)
  1408. return 0;
  1409. if (std::strcmp(func, "OnPauseButton") == 0)
  1410. return 0;
  1411. if (std::strcmp(func, "IsInRealTimeAudio") == 0)
  1412. return 0;
  1413. if (std::strcmp(func, "Audio_IsRunning") == 0)
  1414. return 0;
  1415. }
  1416. #endif
  1417. intptr_t ret = 0;
  1418. switch (opcode)
  1419. {
  1420. case audioMasterAutomate:
  1421. if (! pData->enabled)
  1422. break;
  1423. // plugins should never do this:
  1424. CARLA_SAFE_ASSERT_INT(index < static_cast<int32_t>(pData->param.count), index);
  1425. if (index < 0 || index >= static_cast<int32_t>(pData->param.count))
  1426. break;
  1427. if (fGui.isVisible && ! fIsProcessing)
  1428. {
  1429. // Called from GUI
  1430. setParameterValue(index, opt, false, true, true);
  1431. }
  1432. else if (fIsProcessing)
  1433. {
  1434. // Called from engine
  1435. const float fixedValue(pData->param.getFixedValue(index, opt));
  1436. if (pData->engine->isOffline())
  1437. {
  1438. CarlaPlugin::setParameterValue(index, fixedValue, true, true, true);
  1439. }
  1440. else
  1441. {
  1442. CarlaPlugin::setParameterValue(index, fixedValue, false, false, false);
  1443. pData->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1444. }
  1445. }
  1446. else
  1447. {
  1448. carla_stdout("audioMasterAutomate called from unknown source");
  1449. }
  1450. break;
  1451. case audioMasterCurrentId:
  1452. // TODO
  1453. // if using old sdk, return effect->uniqueID
  1454. break;
  1455. case audioMasterIdle:
  1456. if (fGui.isVisible)
  1457. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1458. break;
  1459. #if ! VST_FORCE_DEPRECATED
  1460. case audioMasterPinConnected:
  1461. // Deprecated in VST SDK 2.4
  1462. // TODO
  1463. break;
  1464. case audioMasterWantMidi:
  1465. // Deprecated in VST SDK 2.4
  1466. pData->hints |= PLUGIN_WANTS_MIDI_INPUT;
  1467. break;
  1468. #endif
  1469. case audioMasterGetTime:
  1470. ret = (intptr_t)&fTimeInfo;
  1471. break;
  1472. case audioMasterProcessEvents:
  1473. CARLA_ASSERT(pData->enabled);
  1474. CARLA_ASSERT(fIsProcessing);
  1475. CARLA_ASSERT(pData->event.portOut != nullptr);
  1476. CARLA_ASSERT(ptr != nullptr);
  1477. if (! pData->enabled)
  1478. return 0;
  1479. if (! fIsProcessing)
  1480. return 0;
  1481. if (pData->event.portOut == nullptr)
  1482. return 0;
  1483. if (ptr == nullptr)
  1484. return 0;
  1485. if (! fIsProcessing)
  1486. {
  1487. carla_stderr2("audioMasterProcessEvents(%p) - received MIDI out events outside audio thread, ignoring", ptr);
  1488. return 0;
  1489. }
  1490. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1491. return 0;
  1492. {
  1493. const VstEvents* const vstEvents((const VstEvents*)ptr);
  1494. for (int32_t i=0; i < vstEvents->numEvents && i < kPluginMaxMidiEvents*2; ++i)
  1495. {
  1496. if (vstEvents->events[i] == nullptr)
  1497. break;
  1498. const VstMidiEvent* const vstMidiEvent = (const VstMidiEvent*)vstEvents->events[i];
  1499. if (vstMidiEvent->type != kVstMidiType)
  1500. continue;
  1501. // reverse-find first free event, and put it there
  1502. for (uint32_t j=(kPluginMaxMidiEvents*2)-1; j >= fMidiEventCount; --j)
  1503. {
  1504. if (fMidiEvents[j].type == 0)
  1505. {
  1506. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1507. break;
  1508. }
  1509. }
  1510. }
  1511. }
  1512. ret = 1;
  1513. break;
  1514. #if ! VST_FORCE_DEPRECATED
  1515. case audioMasterSetTime:
  1516. // Deprecated in VST SDK 2.4
  1517. break;
  1518. case audioMasterTempoAt:
  1519. // Deprecated in VST SDK 2.4
  1520. CARLA_ASSERT(fIsProcessing);
  1521. ret = fTimeInfo.tempo * 10000;
  1522. break;
  1523. case audioMasterGetNumAutomatableParameters:
  1524. // Deprecated in VST SDK 2.4
  1525. ret = carla_min<intptr_t>(0, fEffect->numParams, pData->engine->getOptions().maxParameters);
  1526. break;
  1527. case audioMasterGetParameterQuantization:
  1528. // Deprecated in VST SDK 2.4
  1529. ret = 1; // full single float precision
  1530. break;
  1531. #endif
  1532. #if 0
  1533. case audioMasterIOChanged:
  1534. CARLA_ASSERT(pData->enabled);
  1535. // TESTING
  1536. if (! pData->enabled)
  1537. {
  1538. ret = 1;
  1539. break;
  1540. }
  1541. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1542. {
  1543. carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1544. return 0;
  1545. }
  1546. engineProcessLock();
  1547. m_enabled = false;
  1548. engineProcessUnlock();
  1549. if (m_active)
  1550. {
  1551. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1552. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1553. }
  1554. reload();
  1555. if (m_active)
  1556. {
  1557. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1558. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1559. }
  1560. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1561. ret = 1;
  1562. break;
  1563. #endif
  1564. case audioMasterNeedIdle:
  1565. // Deprecated in VST SDK 2.4
  1566. fNeedIdle = true;
  1567. ret = 1;
  1568. break;
  1569. case audioMasterSizeWindow:
  1570. //if (pData->gui != nullptr)
  1571. {
  1572. CARLA_SAFE_ASSERT(fGui.isVisible);
  1573. //if (fGui.isVisible)
  1574. // pData->gui->setSize(index, value);
  1575. ret = 1;
  1576. }
  1577. break;
  1578. case audioMasterGetSampleRate:
  1579. ret = pData->engine->getSampleRate();
  1580. break;
  1581. case audioMasterGetBlockSize:
  1582. ret = pData->engine->getBufferSize();
  1583. break;
  1584. case audioMasterGetInputLatency:
  1585. ret = 0;
  1586. break;
  1587. case audioMasterGetOutputLatency:
  1588. ret = 0;
  1589. break;
  1590. #if ! VST_FORCE_DEPRECATED
  1591. case audioMasterGetPreviousPlug:
  1592. // Deprecated in VST SDK 2.4
  1593. // TODO
  1594. break;
  1595. case audioMasterGetNextPlug:
  1596. // Deprecated in VST SDK 2.4
  1597. // TODO
  1598. break;
  1599. case audioMasterWillReplaceOrAccumulate:
  1600. // Deprecated in VST SDK 2.4
  1601. ret = 1; // replace
  1602. break;
  1603. #endif
  1604. case audioMasterGetCurrentProcessLevel:
  1605. if (pData->engine->isOffline())
  1606. ret = kVstProcessLevelOffline;
  1607. else if (fIsProcessing)
  1608. ret = kVstProcessLevelRealtime;
  1609. else
  1610. ret = kVstProcessLevelUser;
  1611. break;
  1612. case audioMasterGetAutomationState:
  1613. ret = pData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1614. break;
  1615. case audioMasterOfflineStart:
  1616. case audioMasterOfflineRead:
  1617. case audioMasterOfflineWrite:
  1618. case audioMasterOfflineGetCurrentPass:
  1619. case audioMasterOfflineGetCurrentMetaPass:
  1620. // TODO
  1621. break;
  1622. #if ! VST_FORCE_DEPRECATED
  1623. case audioMasterSetOutputSampleRate:
  1624. // Deprecated in VST SDK 2.4
  1625. break;
  1626. case audioMasterGetOutputSpeakerArrangement:
  1627. // Deprecated in VST SDK 2.4
  1628. // TODO
  1629. break;
  1630. #endif
  1631. case audioMasterVendorSpecific:
  1632. // TODO - cockos extensions
  1633. break;
  1634. #if ! VST_FORCE_DEPRECATED
  1635. case audioMasterSetIcon:
  1636. // Deprecated in VST SDK 2.4
  1637. break;
  1638. #endif
  1639. #if ! VST_FORCE_DEPRECATED
  1640. case audioMasterOpenWindow:
  1641. case audioMasterCloseWindow:
  1642. // Deprecated in VST SDK 2.4
  1643. // TODO
  1644. break;
  1645. #endif
  1646. case audioMasterGetDirectory:
  1647. // TODO
  1648. break;
  1649. case audioMasterUpdateDisplay:
  1650. // Idle UI if visible
  1651. if (fGui.isVisible)
  1652. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1653. // Update current program
  1654. if (pData->prog.count > 0)
  1655. {
  1656. const int32_t current = dispatcher(effGetProgram, 0, 0, nullptr, 0.0f);
  1657. if (current >= 0 && current < static_cast<int32_t>(pData->prog.count))
  1658. {
  1659. char strBuf[STR_MAX+1] = { '\0' };
  1660. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1661. if (pData->prog.names[current] != nullptr)
  1662. delete[] pData->prog.names[current];
  1663. pData->prog.names[current] = carla_strdup(strBuf);
  1664. if (pData->prog.current != current)
  1665. {
  1666. pData->prog.current = current;
  1667. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, current, 0, 0.0f, nullptr);
  1668. }
  1669. }
  1670. }
  1671. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  1672. ret = 1;
  1673. break;
  1674. case audioMasterBeginEdit:
  1675. case audioMasterEndEdit:
  1676. // TODO
  1677. break;
  1678. case audioMasterOpenFileSelector:
  1679. case audioMasterCloseFileSelector:
  1680. // TODO
  1681. break;
  1682. #if ! VST_FORCE_DEPRECATED
  1683. case audioMasterEditFile:
  1684. // Deprecated in VST SDK 2.4
  1685. // TODO
  1686. break;
  1687. case audioMasterGetChunkFile:
  1688. // Deprecated in VST SDK 2.4
  1689. // TODO
  1690. break;
  1691. case audioMasterGetInputSpeakerArrangement:
  1692. // Deprecated in VST SDK 2.4
  1693. // TODO
  1694. break;
  1695. #endif
  1696. default:
  1697. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1698. break;
  1699. }
  1700. return ret;
  1701. }
  1702. // -------------------------------------------------------------------
  1703. public:
  1704. bool init(const char* const filename, const char* const name)
  1705. {
  1706. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1707. // ---------------------------------------------------------------
  1708. // first checks
  1709. if (pData->client != nullptr)
  1710. {
  1711. pData->engine->setLastError("Plugin client is already registered");
  1712. return false;
  1713. }
  1714. if (filename == nullptr || filename[0] == '\0')
  1715. {
  1716. pData->engine->setLastError("null filename");
  1717. return false;
  1718. }
  1719. // ---------------------------------------------------------------
  1720. // open DLL
  1721. if (! pData->libOpen(filename))
  1722. {
  1723. pData->engine->setLastError(pData->libError(filename));
  1724. return false;
  1725. }
  1726. // ---------------------------------------------------------------
  1727. // get DLL main entry
  1728. VST_Function vstFn = (VST_Function)pData->libSymbol("VSTPluginMain");
  1729. if (vstFn == nullptr)
  1730. {
  1731. vstFn = (VST_Function)pData->libSymbol("main");
  1732. if (vstFn == nullptr)
  1733. {
  1734. pData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1735. return false;
  1736. }
  1737. }
  1738. // ---------------------------------------------------------------
  1739. // initialize plugin (part 1)
  1740. sLastVstPlugin = this;
  1741. fEffect = vstFn(carla_vst_audioMasterCallback);
  1742. sLastVstPlugin = nullptr;
  1743. if (fEffect == nullptr)
  1744. {
  1745. pData->engine->setLastError("Plugin failed to initialize");
  1746. return false;
  1747. }
  1748. if (fEffect->magic != kEffectMagic)
  1749. {
  1750. pData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1751. return false;
  1752. }
  1753. #ifdef VESTIGE_HEADER
  1754. fEffect->ptr1 = this;
  1755. #else
  1756. fEffect->resvd1 = ToVstPtr<VstPlugin>(this);
  1757. #endif
  1758. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1759. // ---------------------------------------------------------------
  1760. // get info
  1761. if (name != nullptr && name[0] != '\0')
  1762. {
  1763. pData->name = pData->engine->getUniquePluginName(name);
  1764. }
  1765. else
  1766. {
  1767. char strBuf[STR_MAX+1] = { '\0' };
  1768. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1769. if (strBuf[0] != '\0')
  1770. {
  1771. pData->name = pData->engine->getUniquePluginName(strBuf);
  1772. }
  1773. else if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1774. {
  1775. pData->name = pData->engine->getUniquePluginName(shortname+1);
  1776. }
  1777. else
  1778. {
  1779. pData->name = pData->engine->getUniquePluginName("unknown");
  1780. }
  1781. }
  1782. pData->filename = filename;
  1783. // ---------------------------------------------------------------
  1784. // register client
  1785. pData->client = pData->engine->addClient(this);
  1786. if (pData->client == nullptr || ! pData->client->isOk())
  1787. {
  1788. pData->engine->setLastError("Failed to register plugin client");
  1789. return false;
  1790. }
  1791. // ---------------------------------------------------------------
  1792. // initialize plugin (part 2)
  1793. #if ! VST_FORCE_DEPRECATED
  1794. dispatcher(effSetBlockSizeAndSampleRate, 0, pData->engine->getBufferSize(), nullptr, pData->engine->getSampleRate());
  1795. #endif
  1796. dispatcher(effSetSampleRate, 0, 0, nullptr, pData->engine->getSampleRate());
  1797. dispatcher(effSetBlockSize, 0, pData->engine->getBufferSize(), nullptr, 0.0f);
  1798. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1799. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1800. pData->hints |= PLUGIN_USES_OLD_VSTSDK;
  1801. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, (void*)"hasCockosExtensions", 0.0f)) == 0xbeef0000)
  1802. pData->hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1803. // ---------------------------------------------------------------
  1804. // gui stuff
  1805. if (fEffect->flags & effFlagsHasEditor)
  1806. {
  1807. const EngineOptions& engineOptions(pData->engine->getOptions());
  1808. // #if defined(Q_WS_X11)
  1809. // CarlaString uiBridgeBinary(engineOptions.bridge_vstX11);
  1810. // #elif defined(CARLA_OS_MAC)
  1811. // CarlaString uiBridgeBinary(engineOptions.bridge_vstMac);
  1812. // #elif defined(CARLA_OS_WIN)
  1813. // CarlaString uiBridgeBinary(engineOptions.bridge_vstHWND);
  1814. // #else
  1815. CarlaString uiBridgeBinary;
  1816. // #endif
  1817. if (engineOptions.preferUiBridges && uiBridgeBinary.isNotEmpty() && (fEffect->flags & effFlagsProgramChunks) == 0)
  1818. {
  1819. pData->osc.thread.setOscData(uiBridgeBinary, nullptr);
  1820. fGui.isOsc = true;
  1821. }
  1822. }
  1823. // ---------------------------------------------------------------
  1824. // load plugin settings
  1825. {
  1826. // set default options
  1827. pData->options = 0x0;
  1828. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1829. if (getMidiInCount() > 0)
  1830. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1831. if (fEffect->flags & effFlagsProgramChunks)
  1832. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1833. if (vstPluginCanDo(fEffect, "receiveVstEvents") || vstPluginCanDo(fEffect, "receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (pData->hints & PLUGIN_WANTS_MIDI_INPUT))
  1834. {
  1835. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1836. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1837. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1838. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1839. }
  1840. // load settings
  1841. pData->idStr = "VST/";
  1842. //pData->idStr += std::strrchr(filename, OS_SEP)+1; // FIXME!
  1843. //pData->idStr += "/";
  1844. pData->idStr += CarlaString(getUniqueId());
  1845. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1846. // ignore settings, we need this anyway
  1847. if (getMidiInCount() > 0)
  1848. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1849. }
  1850. return true;
  1851. }
  1852. private:
  1853. int fUnique1;
  1854. AEffect* fEffect;
  1855. void* fLastChunk;
  1856. uint32_t fMidiEventCount;
  1857. VstMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1858. VstTimeInfo_R fTimeInfo;
  1859. struct FixedVstEvents {
  1860. int32_t numEvents;
  1861. intptr_t reserved;
  1862. VstEvent* data[kPluginMaxMidiEvents*2];
  1863. FixedVstEvents()
  1864. : numEvents(0),
  1865. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1866. reserved(0),
  1867. data{nullptr} {}
  1868. #else
  1869. reserved(0)
  1870. {
  1871. carla_fill<VstEvent*>(data, kPluginMaxMidiEvents*2, nullptr);
  1872. }
  1873. #endif
  1874. } fEvents;
  1875. struct GuiInfo {
  1876. bool isOsc;
  1877. bool isVisible;
  1878. int lastWidth;
  1879. int lastHeight;
  1880. GuiInfo()
  1881. : isOsc(false),
  1882. isVisible(false),
  1883. lastWidth(0),
  1884. lastHeight(0) {}
  1885. } fGui;
  1886. bool fIsProcessing;
  1887. bool fNeedIdle;
  1888. int fUnique2;
  1889. static VstPlugin* sLastVstPlugin;
  1890. // -------------------------------------------------------------------
  1891. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1892. {
  1893. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1894. if (std::strcmp(feature, "supplyIdle") == 0)
  1895. return 1;
  1896. if (std::strcmp(feature, "sendVstEvents") == 0)
  1897. return 1;
  1898. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1899. return 1;
  1900. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1901. return 1;
  1902. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1903. return 1;
  1904. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1905. return 1;
  1906. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1907. return 1;
  1908. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1909. return -1;
  1910. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1911. return -1;
  1912. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1913. return 1;
  1914. if (std::strcmp(feature, "sizeWindow") == 0)
  1915. return 1;
  1916. if (std::strcmp(feature, "offline") == 0)
  1917. return -1;
  1918. if (std::strcmp(feature, "openFileSelector") == 0)
  1919. return -1;
  1920. if (std::strcmp(feature, "closeFileSelector") == 0)
  1921. return -1;
  1922. if (std::strcmp(feature, "startStopProcess") == 0)
  1923. return 1;
  1924. if (std::strcmp(feature, "supportShell") == 0)
  1925. return -1;
  1926. if (std::strcmp(feature, "shellCategory") == 0)
  1927. return -1;
  1928. // unimplemented
  1929. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1930. return 0;
  1931. }
  1932. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1933. {
  1934. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1935. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1936. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1937. #endif
  1938. switch (opcode)
  1939. {
  1940. case audioMasterVersion:
  1941. return kVstVersion;
  1942. case audioMasterGetVendorString:
  1943. CARLA_ASSERT(ptr != nullptr);
  1944. if (ptr != nullptr)
  1945. {
  1946. std::strcpy((char*)ptr, "falkTX");
  1947. return 1;
  1948. }
  1949. else
  1950. {
  1951. carla_stderr("carla_vst_audioMasterCallback() - audioMasterGetVendorString called with invalid pointer");
  1952. return 0;
  1953. }
  1954. case audioMasterGetProductString:
  1955. CARLA_ASSERT(ptr != nullptr);
  1956. if (ptr != nullptr)
  1957. {
  1958. std::strcpy((char*)ptr, "Carla");
  1959. return 1;
  1960. }
  1961. else
  1962. {
  1963. carla_stderr("carla_vst_audioMasterCallback() - audioMasterGetProductString called with invalid pointer");
  1964. return 0;
  1965. }
  1966. case audioMasterGetVendorVersion:
  1967. return 0x110; // 1.1.0
  1968. case audioMasterCanDo:
  1969. CARLA_ASSERT(ptr != nullptr);
  1970. if (ptr != nullptr)
  1971. {
  1972. return carla_vst_hostCanDo((const char*)ptr);
  1973. }
  1974. else
  1975. {
  1976. carla_stderr("carla_vst_audioMasterCallback() - audioMasterCanDo called with invalid pointer");
  1977. return 0;
  1978. }
  1979. case audioMasterGetLanguage:
  1980. return kVstLangEnglish;
  1981. }
  1982. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  1983. VstPlugin* self = nullptr;
  1984. if (effect != nullptr)
  1985. {
  1986. #ifdef VESTIGE_HEADER
  1987. if (effect->ptr1 != nullptr)
  1988. {
  1989. self = (VstPlugin*)effect->ptr1;
  1990. #else
  1991. if (effect->resvd1 != 0)
  1992. {
  1993. self = FromVstPtr<VstPlugin>(effect->resvd1);
  1994. #endif
  1995. if (self->fUnique1 != self->fUnique2)
  1996. self = nullptr;
  1997. }
  1998. if (self != nullptr)
  1999. {
  2000. if (self->fEffect == nullptr)
  2001. self->fEffect = effect;
  2002. if (self->fEffect != effect)
  2003. {
  2004. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2005. self = nullptr;
  2006. }
  2007. }
  2008. else if (sLastVstPlugin != nullptr)
  2009. {
  2010. #ifdef VESTIGE_HEADER
  2011. effect->ptr1 = sLastVstPlugin;
  2012. #else
  2013. effect->resvd1 = ToVstPtr<VstPlugin>(sLastVstPlugin);
  2014. #endif
  2015. self = sLastVstPlugin;
  2016. }
  2017. }
  2018. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2019. }
  2020. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VstPlugin)
  2021. };
  2022. VstPlugin* VstPlugin::sLastVstPlugin = nullptr;
  2023. CARLA_BACKEND_END_NAMESPACE
  2024. #endif // WANT_VST
  2025. CARLA_BACKEND_START_NAMESPACE
  2026. CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
  2027. {
  2028. carla_debug("CarlaPlugin::newVST({%p, \"%s\", \"%s\"})", init.engine, init.filename, init.name);
  2029. #ifdef WANT_VST
  2030. # if defined(HAVE_JUCE) && ! defined(VESTIGE_HEADER)
  2031. return newJuce(init, "VST");
  2032. # else
  2033. VstPlugin* const plugin(new VstPlugin(init.engine, init.id));
  2034. if (! plugin->init(init.filename, init.name))
  2035. {
  2036. delete plugin;
  2037. return nullptr;
  2038. }
  2039. plugin->reload();
  2040. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  2041. {
  2042. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  2043. delete plugin;
  2044. return nullptr;
  2045. }
  2046. return plugin;
  2047. # endif
  2048. #else
  2049. init.engine->setLastError("VST support not available");
  2050. return nullptr;
  2051. #endif
  2052. }
  2053. CARLA_BACKEND_END_NAMESPACE