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.

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