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.

2565 lines
83KB

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