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.

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