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.

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