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.

2560 lines
83KB

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