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.

2335 lines
74KB

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