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.

2567 lines
82KB

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