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.

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