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.

2368 lines
75KB

  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. vstTimeOffset = 0;
  776. double aInsPeak[2] = { 0.0 };
  777. double aOutsPeak[2] = { 0.0 };
  778. // reset MIDI
  779. events.numEvents = 0;
  780. midiEvents[0].type = 0;
  781. CARLA_PROCESS_CONTINUE_CHECK;
  782. // --------------------------------------------------------------------------------------------------------
  783. // Input VU
  784. if (aIn.count > 0 && x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  785. {
  786. if (aIn.count == 1)
  787. {
  788. for (k=0; k < frames; k++)
  789. {
  790. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  791. aInsPeak[0] = std::abs(inBuffer[0][k]);
  792. }
  793. }
  794. else if (aIn.count > 1)
  795. {
  796. for (k=0; k < frames; k++)
  797. {
  798. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  799. aInsPeak[0] = std::abs(inBuffer[0][k]);
  800. if (std::abs(inBuffer[1][k]) > aInsPeak[1])
  801. aInsPeak[1] = std::abs(inBuffer[1][k]);
  802. }
  803. }
  804. }
  805. CARLA_PROCESS_CONTINUE_CHECK;
  806. // --------------------------------------------------------------------------------------------------------
  807. // Parameters Input [Automation]
  808. if (param.portCin && m_active && m_activeBefore)
  809. {
  810. bool allNotesOffSent = false;
  811. const CarlaEngineControlEvent* cinEvent;
  812. uint32_t time, nEvents = param.portCin->getEventCount();
  813. for (i=0; i < nEvents; i++)
  814. {
  815. cinEvent = param.portCin->getEvent(i);
  816. if (! cinEvent)
  817. continue;
  818. time = cinEvent->time;
  819. if (time >= frames)
  820. continue;
  821. // Control change
  822. switch (cinEvent->type)
  823. {
  824. case CarlaEngineNullEvent:
  825. break;
  826. case CarlaEngineParameterChangeEvent:
  827. {
  828. double value;
  829. // Control backend stuff
  830. if (cinEvent->channel == m_ctrlInChannel)
  831. {
  832. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  833. {
  834. value = cinEvent->value;
  835. setDryWet(value, false, false);
  836. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  837. continue;
  838. }
  839. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  840. {
  841. value = cinEvent->value*127/100;
  842. setVolume(value, false, false);
  843. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  844. continue;
  845. }
  846. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  847. {
  848. double left, right;
  849. value = cinEvent->value/0.5 - 1.0;
  850. if (value < 0.0)
  851. {
  852. left = -1.0;
  853. right = (value*2)+1.0;
  854. }
  855. else if (value > 0.0)
  856. {
  857. left = (value*2)-1.0;
  858. right = 1.0;
  859. }
  860. else
  861. {
  862. left = -1.0;
  863. right = 1.0;
  864. }
  865. setBalanceLeft(left, false, false);
  866. setBalanceRight(right, false, false);
  867. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  868. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  869. continue;
  870. }
  871. }
  872. // Control plugin parameters
  873. for (k=0; k < param.count; k++)
  874. {
  875. if (param.data[k].midiChannel != cinEvent->channel)
  876. continue;
  877. if (param.data[k].midiCC != cinEvent->parameter)
  878. continue;
  879. if (param.data[k].type != PARAMETER_INPUT)
  880. continue;
  881. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  882. {
  883. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  884. {
  885. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  886. }
  887. else
  888. {
  889. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  890. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  891. value = rint(value);
  892. }
  893. setParameterValue(k, value, false, false, false);
  894. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  895. }
  896. }
  897. break;
  898. }
  899. case CarlaEngineMidiBankChangeEvent:
  900. break;
  901. case CarlaEngineMidiProgramChangeEvent:
  902. if (cinEvent->channel == m_ctrlInChannel)
  903. {
  904. uint32_t progId = rint(cinEvent->value);
  905. if (progId < prog.count)
  906. {
  907. setProgram(progId, false, false, false, false);
  908. postponeEvent(PluginPostEventProgramChange, progId, 0, 0.0);
  909. }
  910. }
  911. break;
  912. case CarlaEngineAllSoundOffEvent:
  913. if (cinEvent->channel == m_ctrlInChannel)
  914. {
  915. if (midi.portMin && ! allNotesOffSent)
  916. sendMidiAllNotesOff();
  917. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  918. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  919. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  920. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  921. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  922. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  923. allNotesOffSent = true;
  924. }
  925. break;
  926. case CarlaEngineAllNotesOffEvent:
  927. if (cinEvent->channel == m_ctrlInChannel)
  928. {
  929. if (midi.portMin && ! allNotesOffSent)
  930. sendMidiAllNotesOff();
  931. allNotesOffSent = true;
  932. }
  933. break;
  934. }
  935. }
  936. } // End of Parameters Input
  937. CARLA_PROCESS_CONTINUE_CHECK;
  938. // --------------------------------------------------------------------------------------------------------
  939. // MIDI Input
  940. if (midi.portMin && m_active && m_activeBefore)
  941. {
  942. // ----------------------------------------------------------------------------------------------------
  943. // MIDI Input (External)
  944. {
  945. engineMidiLock();
  946. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  947. {
  948. if (extMidiNotes[i].channel < 0)
  949. break;
  950. VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
  951. memset(midiEvent, 0, sizeof(VstMidiEvent));
  952. midiEvent->type = kVstMidiType;
  953. midiEvent->byteSize = sizeof(VstMidiEvent);
  954. midiEvent->midiData[0] = uint8_t(extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + extMidiNotes[i].channel;
  955. midiEvent->midiData[1] = extMidiNotes[i].note;
  956. midiEvent->midiData[2] = extMidiNotes[i].velo;
  957. extMidiNotes[i].channel = -1; // mark as invalid
  958. midiEventCount += 1;
  959. }
  960. engineMidiUnlock();
  961. } // End of MIDI Input (External)
  962. CARLA_PROCESS_CONTINUE_CHECK;
  963. // ----------------------------------------------------------------------------------------------------
  964. // MIDI Input (System)
  965. {
  966. const CarlaEngineMidiEvent* minEvent;
  967. uint32_t time, nEvents = midi.portMin->getEventCount();
  968. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  969. {
  970. minEvent = midi.portMin->getEvent(i);
  971. if (! minEvent)
  972. continue;
  973. time = minEvent->time;
  974. if (time >= frames)
  975. continue;
  976. uint8_t status = minEvent->data[0];
  977. uint8_t channel = status & 0x0F;
  978. // Fix bad note-off
  979. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  980. status -= 0x10;
  981. VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
  982. memset(midiEvent, 0, sizeof(VstMidiEvent));
  983. midiEvent->type = kVstMidiType;
  984. midiEvent->byteSize = sizeof(VstMidiEvent);
  985. midiEvent->deltaFrames = minEvent->time;
  986. if (MIDI_IS_STATUS_NOTE_OFF(status))
  987. {
  988. uint8_t note = minEvent->data[1];
  989. midiEvent->midiData[0] = status;
  990. midiEvent->midiData[1] = note;
  991. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  992. }
  993. else if (MIDI_IS_STATUS_NOTE_ON(status))
  994. {
  995. uint8_t note = minEvent->data[1];
  996. uint8_t velo = minEvent->data[2];
  997. midiEvent->midiData[0] = status;
  998. midiEvent->midiData[1] = note;
  999. midiEvent->midiData[2] = velo;
  1000. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  1001. }
  1002. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  1003. {
  1004. uint8_t note = minEvent->data[1];
  1005. uint8_t pressure = minEvent->data[2];
  1006. midiEvent->midiData[0] = status;
  1007. midiEvent->midiData[1] = note;
  1008. midiEvent->midiData[2] = pressure;
  1009. }
  1010. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  1011. {
  1012. uint8_t pressure = minEvent->data[1];
  1013. midiEvent->midiData[0] = status;
  1014. midiEvent->midiData[1] = pressure;
  1015. }
  1016. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  1017. {
  1018. uint8_t lsb = minEvent->data[1];
  1019. uint8_t msb = minEvent->data[2];
  1020. midiEvent->midiData[0] = status;
  1021. midiEvent->midiData[1] = lsb;
  1022. midiEvent->midiData[2] = msb;
  1023. }
  1024. else
  1025. continue;
  1026. midiEventCount += 1;
  1027. }
  1028. } // End of MIDI Input (System)
  1029. } // End of MIDI Input
  1030. CARLA_PROCESS_CONTINUE_CHECK;
  1031. // --------------------------------------------------------------------------------------------------------
  1032. // Plugin processing
  1033. if (m_active)
  1034. {
  1035. if (! m_activeBefore)
  1036. {
  1037. if (midi.portMin)
  1038. {
  1039. for (k=0; k < MAX_MIDI_CHANNELS; k++)
  1040. {
  1041. memset(&midiEvents[k], 0, sizeof(VstMidiEvent));
  1042. midiEvents[k].type = kVstMidiType;
  1043. midiEvents[k].byteSize = sizeof(VstMidiEvent);
  1044. midiEvents[k].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  1045. midiEvents[k].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1046. memset(&midiEvents[k*2], 0, sizeof(VstMidiEvent));
  1047. midiEvents[k*2].type = kVstMidiType;
  1048. midiEvents[k*2].byteSize = sizeof(VstMidiEvent);
  1049. midiEvents[k*2].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  1050. midiEvents[k*2].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1051. }
  1052. midiEventCount = MAX_MIDI_CHANNELS*2;
  1053. }
  1054. if (m_latency > 0)
  1055. {
  1056. for (i=0; i < aIn.count; i++)
  1057. memset(m_latencyBuffers[i], 0, sizeof(float)*m_latency);
  1058. }
  1059. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1060. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1061. }
  1062. if (midiEventCount > 0)
  1063. {
  1064. events.numEvents = midiEventCount;
  1065. events.reserved = 0;
  1066. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  1067. }
  1068. // FIXME - make this a global option
  1069. // don't process if not needed
  1070. //if ((effect->flags & effFlagsNoSoundInStop) > 0 && aInsPeak[0] == 0.0 && aInsPeak[1] == 0.0 && midiEventCount == 0 && ! midi.portMout)
  1071. //{
  1072. if (m_hints & PLUGIN_CAN_PROCESS_REPLACING)
  1073. {
  1074. isProcessing = true;
  1075. effect->processReplacing(effect, inBuffer, outBuffer, frames);
  1076. isProcessing = false;
  1077. }
  1078. else
  1079. {
  1080. for (i=0; i < aOut.count; i++)
  1081. carla_zeroF(outBuffer[i], frames);
  1082. #if ! VST_FORCE_DEPRECATED
  1083. isProcessing = true;
  1084. effect->process(effect, inBuffer, outBuffer, frames);
  1085. isProcessing = false;
  1086. #endif
  1087. }
  1088. //}
  1089. }
  1090. else
  1091. {
  1092. if (m_activeBefore)
  1093. {
  1094. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1095. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1096. }
  1097. }
  1098. CARLA_PROCESS_CONTINUE_CHECK;
  1099. // --------------------------------------------------------------------------------------------------------
  1100. // Post-processing (dry/wet, volume and balance)
  1101. if (m_active)
  1102. {
  1103. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  1104. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  1105. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  1106. double bal_rangeL, bal_rangeR;
  1107. float bufValue, oldBufLeft[do_balance ? frames : 1];
  1108. for (i=0; i < aOut.count; i++)
  1109. {
  1110. // Dry/Wet
  1111. if (do_drywet)
  1112. {
  1113. for (k=0; k < frames; k++)
  1114. {
  1115. if (k < m_latency && m_latency < frames)
  1116. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  1117. else
  1118. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1119. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1120. }
  1121. }
  1122. // Balance
  1123. if (do_balance)
  1124. {
  1125. if (i%2 == 0)
  1126. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1127. bal_rangeL = (x_balanceLeft+1.0)/2;
  1128. bal_rangeR = (x_balanceRight+1.0)/2;
  1129. for (k=0; k < frames; k++)
  1130. {
  1131. if (i%2 == 0)
  1132. {
  1133. // left output
  1134. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1135. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1136. }
  1137. else
  1138. {
  1139. // right
  1140. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1141. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1142. }
  1143. }
  1144. }
  1145. // Volume
  1146. if (do_volume)
  1147. {
  1148. for (k=0; k < frames; k++)
  1149. outBuffer[i][k] *= x_volume;
  1150. }
  1151. // Output VU
  1152. if (x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  1153. {
  1154. for (k=0; i < 2 && k < frames; k++)
  1155. {
  1156. if (std::abs(outBuffer[i][k]) > aOutsPeak[i])
  1157. aOutsPeak[i] = std::abs(outBuffer[i][k]);
  1158. }
  1159. }
  1160. }
  1161. // Latency, save values for next callback
  1162. if (m_latency > 0 && m_latency < frames)
  1163. {
  1164. for (i=0; i < aIn.count; i++)
  1165. memcpy(m_latencyBuffers[i], inBuffer[i] + (frames - m_latency), sizeof(float)*m_latency);
  1166. }
  1167. }
  1168. else
  1169. {
  1170. // disable any output sound if not active
  1171. for (i=0; i < aOut.count; i++)
  1172. carla_zeroF(outBuffer[i], frames);
  1173. aOutsPeak[0] = 0.0;
  1174. aOutsPeak[1] = 0.0;
  1175. } // End of Post-processing
  1176. CARLA_PROCESS_CONTINUE_CHECK;
  1177. // --------------------------------------------------------------------------------------------------------
  1178. // MIDI Output
  1179. if (midi.portMout && m_active)
  1180. {
  1181. uint8_t data[3] = { 0 };
  1182. for (int32_t i = midiEventCount; i < events.numEvents; i++)
  1183. {
  1184. data[0] = midiEvents[i].midiData[0];
  1185. data[1] = midiEvents[i].midiData[1];
  1186. data[2] = midiEvents[i].midiData[2];
  1187. // Fix bad note-off
  1188. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  1189. data[0] -= 0x10;
  1190. midi.portMout->writeEvent(midiEvents[i].deltaFrames, data, 3);
  1191. }
  1192. } // End of MIDI Output
  1193. CARLA_PROCESS_CONTINUE_CHECK;
  1194. // --------------------------------------------------------------------------------------------------------
  1195. // Peak Values
  1196. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1197. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1198. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1199. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1200. m_activeBefore = m_active;
  1201. #endif
  1202. }
  1203. void bufferSizeChanged(uint32_t newBufferSize)
  1204. {
  1205. if (kData->active)
  1206. {
  1207. dispatcher(effStopProcess, 0, 0, nullptr, 0.0f);
  1208. dispatcher(effMainsChanged, 0, 0, nullptr, 0.0f);
  1209. }
  1210. #if ! VST_FORCE_DEPRECATED
  1211. dispatcher(effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, kData->engine->getSampleRate());
  1212. #endif
  1213. dispatcher(effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
  1214. if (kData->active)
  1215. {
  1216. dispatcher(effMainsChanged, 0, 1, nullptr, 0.0f);
  1217. dispatcher(effStartProcess, 0, 0, nullptr, 0.0f);
  1218. }
  1219. }
  1220. // -------------------------------------------------------------------
  1221. // Post-poned events
  1222. void uiParameterChange(const uint32_t index, const double value)
  1223. {
  1224. CARLA_ASSERT(index < kData->param.count);
  1225. if (index >= kData->param.count)
  1226. return;
  1227. if (! fGui.isOsc)
  1228. return;
  1229. if (kData->osc.data.target == nullptr)
  1230. return;
  1231. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1232. }
  1233. void uiProgramChange(const uint32_t index)
  1234. {
  1235. CARLA_ASSERT(index < kData->prog.count);
  1236. if (index >= kData->prog.count)
  1237. return;
  1238. if (! fGui.isOsc)
  1239. return;
  1240. if (kData->osc.data.target == nullptr)
  1241. return;
  1242. osc_send_program(&kData->osc.data, index);
  1243. }
  1244. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1245. {
  1246. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1247. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1248. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1249. if (channel >= MAX_MIDI_CHANNELS)
  1250. return;
  1251. if (note >= MAX_MIDI_NOTE)
  1252. return;
  1253. if (velo >= MAX_MIDI_VALUE)
  1254. return;
  1255. if (! fGui.isOsc)
  1256. return;
  1257. if (kData->osc.data.target == nullptr)
  1258. return;
  1259. uint8_t midiData[4] = { 0 };
  1260. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1261. midiData[2] = note;
  1262. midiData[3] = velo;
  1263. osc_send_midi(&kData->osc.data, midiData);
  1264. }
  1265. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1266. {
  1267. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1268. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1269. if (channel >= MAX_MIDI_CHANNELS)
  1270. return;
  1271. if (note >= MAX_MIDI_NOTE)
  1272. return;
  1273. if (! fGui.isOsc)
  1274. return;
  1275. if (kData->osc.data.target == nullptr)
  1276. return;
  1277. uint8_t midiData[4] = { 0 };
  1278. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1279. midiData[2] = note;
  1280. osc_send_midi(&kData->osc.data, midiData);
  1281. }
  1282. // -------------------------------------------------------------------
  1283. #if 0
  1284. intptr_t handleAudioMasterTempoAt()
  1285. {
  1286. const CarlaEngineTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1287. if (timeInfo->valid & CarlaEngineTimeBBT)
  1288. return timeInfo->bbt.beats_per_minute * 10000;
  1289. return 0;
  1290. }
  1291. intptr_t handleAudioMasterIOChanged()
  1292. {
  1293. carla_debug("VstPlugin::handleAudioMasterIOChanged()");
  1294. CARLA_ASSERT(m_enabled);
  1295. // TESTING
  1296. if (! m_enabled)
  1297. return 1;
  1298. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1299. {
  1300. carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1301. return 0;
  1302. }
  1303. engineProcessLock();
  1304. m_enabled = false;
  1305. engineProcessUnlock();
  1306. if (m_active)
  1307. {
  1308. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1309. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1310. }
  1311. reload();
  1312. if (m_active)
  1313. {
  1314. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1315. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1316. }
  1317. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1318. return 1;
  1319. }
  1320. intptr_t handleAudioMasterProcessEvents(const VstEvents* const vstEvents)
  1321. {
  1322. CARLA_ASSERT(m_enabled);
  1323. CARLA_ASSERT(midi.portMout);
  1324. CARLA_ASSERT(isProcessing);
  1325. if (! m_enabled)
  1326. return 0;
  1327. if (! midi.portMout)
  1328. return 0;
  1329. if (! isProcessing)
  1330. {
  1331. carla_stderr2("VstPlugin::handleAudioMasterProcessEvents(%p) - received MIDI out events outside audio thread, ignoring", vstEvents);
  1332. return 0;
  1333. }
  1334. for (int32_t i=0; i < vstEvents->numEvents && events.numEvents < MAX_MIDI_EVENTS*2; i++)
  1335. {
  1336. if (! vstEvents->events[i])
  1337. break;
  1338. const VstMidiEvent* const vstMidiEvent = (const VstMidiEvent*)vstEvents->events[i];
  1339. if (vstMidiEvent->type == kVstMidiType)
  1340. memcpy(&midiEvents[events.numEvents++], vstMidiEvent, sizeof(VstMidiEvent));
  1341. }
  1342. return 1;
  1343. }
  1344. intptr_t handleAdioMasterSizeWindow(int32_t width, int32_t height)
  1345. {
  1346. carla_debug("VstPlugin::handleAudioMasterSizeWindow(%i, %i)", width, height);
  1347. gui.width = width;
  1348. gui.height = height;
  1349. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0, nullptr);
  1350. return 1;
  1351. }
  1352. void handleAudioMasterUpdateDisplay()
  1353. {
  1354. carla_debug("VstPlugin::handleAudioMasterUpdateDisplay()");
  1355. // Update current program name
  1356. if (prog.count > 0 && prog.current >= 0)
  1357. {
  1358. const int32_t index = prog.current;
  1359. char strBuf[STR_MAX] = { 0 };
  1360. effect->dispatcher(effect, effGetProgramName, 0, 0, strBuf, 0.0f);
  1361. if (! prog.names[index])
  1362. {
  1363. prog.names[index] = strdup(strBuf);
  1364. }
  1365. else if (strBuf[0] != 0 && std::strcmp(strBuf, prog.names[index]) != 0)
  1366. {
  1367. free((void*)prog.names[index]);
  1368. prog.names[index] = strdup(strBuf);
  1369. }
  1370. }
  1371. // Tell backend to update
  1372. x_engine->callback(CALLBACK_UPDATE, m_id, 0, 0, 0.0, nullptr);
  1373. }
  1374. #endif
  1375. protected:
  1376. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1377. {
  1378. carla_debug("VstPlugin::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1379. CARLA_ASSERT(fEffect != nullptr);
  1380. return (fEffect != nullptr) ? fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt) : 0;
  1381. }
  1382. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1383. {
  1384. #if 0
  1385. // Cockos VST extensions
  1386. if (/*effect &&*/ ptr && (uint32_t)opcode == 0xdeadbeef && (uint32_t)index == 0xdeadf00d)
  1387. {
  1388. const char* const func = (char*)ptr;
  1389. if (std::strcmp(func, "GetPlayPosition") == 0)
  1390. return 0;
  1391. if (std::strcmp(func, "GetPlayPosition2") == 0)
  1392. return 0;
  1393. if (std::strcmp(func, "GetCursorPosition") == 0)
  1394. return 0;
  1395. if (std::strcmp(func, "GetPlayState") == 0)
  1396. return 0;
  1397. if (std::strcmp(func, "SetEditCurPos") == 0)
  1398. return 0;
  1399. if (std::strcmp(func, "GetSetRepeat") == 0)
  1400. return 0;
  1401. if (std::strcmp(func, "GetProjectPath") == 0)
  1402. return 0;
  1403. if (std::strcmp(func, "OnPlayButton") == 0)
  1404. return 0;
  1405. if (std::strcmp(func, "OnStopButton") == 0)
  1406. return 0;
  1407. if (std::strcmp(func, "OnPauseButton") == 0)
  1408. return 0;
  1409. if (std::strcmp(func, "IsInRealTimeAudio") == 0)
  1410. return 0;
  1411. if (std::strcmp(func, "Audio_IsRunning") == 0)
  1412. return 0;
  1413. }
  1414. #endif
  1415. intptr_t ret = 0;
  1416. switch (opcode)
  1417. {
  1418. case audioMasterAutomate:
  1419. //CARLA_ASSERT(fEnabled);
  1420. CARLA_ASSERT_INT(index < static_cast<int32_t>(kData->param.count), index);
  1421. if (index < 0 || index >= static_cast<int32_t>(kData->param.count) /*|| ! fEnabled*/)
  1422. break;
  1423. if (fIsProcessing && ! kData->engine->isOffline())
  1424. {
  1425. setParameterValue(index, opt, false, false, false);
  1426. postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, opt);
  1427. }
  1428. else
  1429. {
  1430. CARLA_ASSERT(fGui.isVisible); // FIXME - remove when offline is implemented
  1431. setParameterValue(index, opt, fIsProcessing, true, true);
  1432. }
  1433. break;
  1434. case audioMasterCurrentId:
  1435. // TODO
  1436. // if using old sdk, return effect->uniqueID
  1437. break;
  1438. case audioMasterIdle:
  1439. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1440. break;
  1441. #if ! VST_FORCE_DEPRECATED
  1442. case audioMasterPinConnected:
  1443. // Deprecated in VST SDK 2.4
  1444. // TODO
  1445. break;
  1446. case audioMasterWantMidi:
  1447. // Deprecated in VST SDK 2.4
  1448. fHints |= PLUGIN_WANTS_MIDI_INPUT;
  1449. break;
  1450. #endif
  1451. case audioMasterGetTime:
  1452. CARLA_ASSERT(fIsProcessing);
  1453. #ifdef VESTIGE_HEADER
  1454. ret = getAddressFromPointer(&fTimeInfo);
  1455. #else
  1456. ret = ToVstPtr<VstTimeInfo_R>(&fTimeInfo);
  1457. #endif
  1458. break;
  1459. #if 0
  1460. case audioMasterProcessEvents:
  1461. CARLA_ASSERT(self && ptr);
  1462. if (self)
  1463. {
  1464. if (ptr)
  1465. ret = self->handleAudioMasterProcessEvents((const VstEvents*)ptr);
  1466. else
  1467. carla_stderr("VstPlugin::hostCallback::audioMasterProcessEvents called with invalid pointer");
  1468. }
  1469. else
  1470. carla_stderr("VstPlugin::hostCallback::audioMasterProcessEvents called without valid object");
  1471. break;
  1472. #endif
  1473. #if ! VST_FORCE_DEPRECATED
  1474. case audioMasterSetTime:
  1475. // Deprecated in VST SDK 2.4
  1476. break;
  1477. #if 0
  1478. case audioMasterTempoAt:
  1479. // Deprecated in VST SDK 2.4
  1480. CARLA_ASSERT(self);
  1481. if (self)
  1482. ret = self->handleAudioMasterTempoAt();
  1483. else
  1484. carla_stderr("VstPlugin::hostCallback::audioMasterTempoAt called without valid object");
  1485. if (ret == 0)
  1486. ret = 120 * 10000;
  1487. break;
  1488. #endif
  1489. case audioMasterGetNumAutomatableParameters:
  1490. // Deprecated in VST SDK 2.4
  1491. ret = carla_min<intptr_t>(0, fEffect->numParams, kData->engine->getOptions().maxParameters);
  1492. break;
  1493. case audioMasterGetParameterQuantization:
  1494. // Deprecated in VST SDK 2.4
  1495. ret = 1; // full single float precision
  1496. break;
  1497. #endif
  1498. #if 0
  1499. case audioMasterIOChanged:
  1500. CARLA_ASSERT(self);
  1501. if (self)
  1502. ret = self->handleAudioMasterIOChanged();
  1503. else
  1504. carla_stderr("VstPlugin::hostCallback::audioMasterIOChanged called without valid object");
  1505. break;
  1506. #endif
  1507. case audioMasterNeedIdle:
  1508. // Deprecated in VST SDK 2.4
  1509. fNeedIdle = true;
  1510. break;
  1511. #if 0
  1512. case audioMasterSizeWindow:
  1513. CARLA_ASSERT(self);
  1514. if (self)
  1515. {
  1516. if (index > 0 && value > 0)
  1517. ret = self->handleAdioMasterSizeWindow(index, value);
  1518. else
  1519. carla_stderr("VstPlugin::hostCallback::audioMasterSizeWindow called with invalid size");
  1520. }
  1521. else
  1522. carla_stderr("VstPlugin::hostCallback::audioMasterSizeWindow called without valid object");
  1523. break;
  1524. #endif
  1525. case audioMasterGetSampleRate:
  1526. ret = kData->engine->getSampleRate();
  1527. break;
  1528. case audioMasterGetBlockSize:
  1529. ret = kData->engine->getBufferSize();
  1530. break;
  1531. case audioMasterGetInputLatency:
  1532. ret = 0;
  1533. break;
  1534. case audioMasterGetOutputLatency:
  1535. ret = 0;
  1536. break;
  1537. #if ! VST_FORCE_DEPRECATED
  1538. case audioMasterGetPreviousPlug:
  1539. // Deprecated in VST SDK 2.4
  1540. // TODO
  1541. break;
  1542. case audioMasterGetNextPlug:
  1543. // Deprecated in VST SDK 2.4
  1544. // TODO
  1545. break;
  1546. case audioMasterWillReplaceOrAccumulate:
  1547. // Deprecated in VST SDK 2.4
  1548. ret = 1; // replace
  1549. break;
  1550. #endif
  1551. case audioMasterGetCurrentProcessLevel:
  1552. if (kData->engine->isOffline())
  1553. ret = kVstProcessLevelOffline;
  1554. else if (fIsProcessing)
  1555. ret = kVstProcessLevelRealtime;
  1556. else
  1557. ret = kVstProcessLevelUser;
  1558. break;
  1559. case audioMasterGetAutomationState:
  1560. ret = kVstAutomationReadWrite;
  1561. break;
  1562. case audioMasterOfflineStart:
  1563. case audioMasterOfflineRead:
  1564. case audioMasterOfflineWrite:
  1565. case audioMasterOfflineGetCurrentPass:
  1566. case audioMasterOfflineGetCurrentMetaPass:
  1567. // TODO
  1568. break;
  1569. #if ! VST_FORCE_DEPRECATED
  1570. case audioMasterSetOutputSampleRate:
  1571. // Deprecated in VST SDK 2.4
  1572. break;
  1573. case audioMasterGetOutputSpeakerArrangement:
  1574. // Deprecated in VST SDK 2.4
  1575. // TODO
  1576. break;
  1577. #endif
  1578. case audioMasterVendorSpecific:
  1579. // TODO - cockos extensions
  1580. break;
  1581. #if ! VST_FORCE_DEPRECATED
  1582. case audioMasterSetIcon:
  1583. // Deprecated in VST SDK 2.4
  1584. break;
  1585. #endif
  1586. case audioMasterGetLanguage:
  1587. ret = kVstLangEnglish;
  1588. break;
  1589. #if ! VST_FORCE_DEPRECATED
  1590. case audioMasterOpenWindow:
  1591. case audioMasterCloseWindow:
  1592. // Deprecated in VST SDK 2.4
  1593. // TODO
  1594. break;
  1595. #endif
  1596. case audioMasterGetDirectory:
  1597. // TODO
  1598. //if (ptr)
  1599. // std::strcpy((char*)ptr, "stuff");
  1600. //else
  1601. // carla_stderr("VstPlugin::hostCallback::audioMasterGetDirectory called with invalid pointer");
  1602. break;
  1603. #if 0
  1604. case audioMasterUpdateDisplay:
  1605. CARLA_ASSERT(effect);
  1606. if (self)
  1607. self->handleAudioMasterUpdateDisplay();
  1608. if (effect)
  1609. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  1610. ret = 1;
  1611. break;
  1612. #endif
  1613. case audioMasterBeginEdit:
  1614. case audioMasterEndEdit:
  1615. // TODO
  1616. break;
  1617. case audioMasterOpenFileSelector:
  1618. case audioMasterCloseFileSelector:
  1619. // TODO
  1620. break;
  1621. #if ! VST_FORCE_DEPRECATED
  1622. case audioMasterEditFile:
  1623. // Deprecated in VST SDK 2.4
  1624. // TODO
  1625. break;
  1626. case audioMasterGetChunkFile:
  1627. // Deprecated in VST SDK 2.4
  1628. // TODO
  1629. break;
  1630. case audioMasterGetInputSpeakerArrangement:
  1631. // Deprecated in VST SDK 2.4
  1632. // TODO
  1633. break;
  1634. #endif
  1635. default:
  1636. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1637. break;
  1638. }
  1639. return ret;
  1640. }
  1641. public:
  1642. // -------------------------------------------------------------------
  1643. bool init(const char* const filename, const char* const name, const char* const label)
  1644. {
  1645. CARLA_ASSERT(kData->engine != nullptr);
  1646. CARLA_ASSERT(kData->client == nullptr);
  1647. CARLA_ASSERT(filename != nullptr);
  1648. CARLA_ASSERT(label != nullptr);
  1649. // ---------------------------------------------------------------
  1650. // open DLL
  1651. if (! libOpen(filename))
  1652. {
  1653. kData->engine->setLastError(libError(filename));
  1654. return false;
  1655. }
  1656. // ---------------------------------------------------------------
  1657. // get DLL main entry
  1658. VST_Function vstFn = (VST_Function)libSymbol("VSTPluginMain");
  1659. if (vstFn == nullptr)
  1660. {
  1661. vstFn = (VST_Function)libSymbol("main");
  1662. if (vstFn == nullptr)
  1663. {
  1664. kData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1665. return false;
  1666. }
  1667. }
  1668. // ---------------------------------------------------------------
  1669. // initialize plugin (part 1)
  1670. sLastVstPlugin = this;
  1671. fEffect = vstFn(carla_vst_audioMasterCallback);
  1672. sLastVstPlugin = nullptr;
  1673. if (fEffect == nullptr || fEffect->magic != kEffectMagic)
  1674. {
  1675. kData->engine->setLastError("Plugin failed to initialize");
  1676. return false;
  1677. }
  1678. #ifdef VESTIGE_HEADER
  1679. fEffect->ptr1 = this;
  1680. #else
  1681. fEffect->resvd1 = getAddressFromPointer(this);
  1682. #endif
  1683. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1684. // ---------------------------------------------------------------
  1685. // get info
  1686. if (name != nullptr)
  1687. {
  1688. fName = kData->engine->getNewUniquePluginName(name);
  1689. }
  1690. else
  1691. {
  1692. char strBuf[STR_MAX] = { 0 };
  1693. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1694. if (strBuf[0] != 0)
  1695. fName = kData->engine->getNewUniquePluginName(strBuf);
  1696. else
  1697. fName = kData->engine->getNewUniquePluginName(label);
  1698. }
  1699. fFilename = filename;
  1700. // ---------------------------------------------------------------
  1701. // register client
  1702. kData->client = kData->engine->addClient(this);
  1703. if (kData->client == nullptr || ! kData->client->isOk())
  1704. {
  1705. kData->engine->setLastError("Failed to register plugin client");
  1706. return false;
  1707. }
  1708. // ---------------------------------------------------------------
  1709. // initialize plugin (part 2)
  1710. #if ! VST_FORCE_DEPRECATED
  1711. dispatcher(effSetBlockSizeAndSampleRate, 0, kData->engine->getBufferSize(), nullptr, kData->engine->getSampleRate());
  1712. #endif
  1713. dispatcher(effSetSampleRate, 0, 0, nullptr, kData->engine->getSampleRate());
  1714. dispatcher(effSetBlockSize, 0, kData->engine->getBufferSize(), nullptr, 0.0f);
  1715. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1716. dispatcher(effStopProcess, 0, 0, nullptr, 0.0f);
  1717. dispatcher(effMainsChanged, 0, 0, nullptr, 0.0f);
  1718. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1719. fHints |= PLUGIN_USES_OLD_VSTSDK;
  1720. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, (void*)"hasCockosExtensions", 0.0f)) == 0xbeef0000)
  1721. fHints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1722. // ---------------------------------------------------------------
  1723. // gui stuff
  1724. if (fEffect->flags & effFlagsHasEditor)
  1725. {
  1726. const EngineOptions& engineOptions(kData->engine->getOptions());
  1727. if (engineOptions.preferUiBridges && engineOptions.bridge_vstx11.isNotEmpty() && (fEffect->flags & effFlagsProgramChunks) == 0)
  1728. {
  1729. kData->osc.thread.setOscData(engineOptions.bridge_vstx11, label);
  1730. fGui.isOsc = true;
  1731. }
  1732. }
  1733. return true;
  1734. }
  1735. private:
  1736. int fUnique1;
  1737. AEffect* fEffect;
  1738. QByteArray fChunk;
  1739. VstMidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1740. VstTimeInfo_R fTimeInfo;
  1741. struct FixedVstEvents {
  1742. int32_t numEvents;
  1743. intptr_t reserved;
  1744. VstEvent* data[MAX_MIDI_EVENTS*2];
  1745. #ifndef QTCREATOR_TEST // missing proper C++11 support
  1746. FixedVstEvents()
  1747. : numEvents(0),
  1748. reserved(0),
  1749. data{0} {}
  1750. #endif
  1751. } fEvents;
  1752. struct GuiInfo {
  1753. bool isOsc;
  1754. bool isVisible;
  1755. int width;
  1756. int height;
  1757. GuiInfo()
  1758. : isOsc(false),
  1759. isVisible(false),
  1760. width(0),
  1761. height(0) {}
  1762. } fGui;
  1763. bool fIsProcessing;
  1764. bool fNeedIdle;
  1765. int fUnique2;
  1766. static VstPlugin* sLastVstPlugin;
  1767. // -------------------------------------------------------------------
  1768. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1769. {
  1770. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1771. if (std::strcmp(feature, "supplyIdle") == 0)
  1772. return 1;
  1773. if (std::strcmp(feature, "sendVstEvents") == 0)
  1774. return 1;
  1775. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1776. return 1;
  1777. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1778. return 1;
  1779. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1780. return 1;
  1781. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1782. return 1;
  1783. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1784. return 1;
  1785. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1786. return -1;
  1787. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1788. return -1;
  1789. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1790. return 1;
  1791. if (std::strcmp(feature, "sizeWindow") == 0)
  1792. return 1;
  1793. if (std::strcmp(feature, "offline") == 0)
  1794. return -1;
  1795. if (std::strcmp(feature, "openFileSelector") == 0)
  1796. return -1;
  1797. if (std::strcmp(feature, "closeFileSelector") == 0)
  1798. return -1;
  1799. if (std::strcmp(feature, "startStopProcess") == 0)
  1800. return 1;
  1801. if (std::strcmp(feature, "supportShell") == 0)
  1802. return -1;
  1803. if (std::strcmp(feature, "shellCategory") == 0)
  1804. return -1;
  1805. // unimplemented
  1806. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1807. return 0;
  1808. }
  1809. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1810. {
  1811. #ifdef DEBUG
  1812. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1813. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1814. #endif
  1815. switch (opcode)
  1816. {
  1817. case audioMasterVersion:
  1818. return kVstVersion;
  1819. case audioMasterGetVendorString:
  1820. CARLA_ASSERT(ptr != nullptr);
  1821. if (ptr != nullptr)
  1822. {
  1823. std::strcpy((char*)ptr, "falkTX");
  1824. return 1;
  1825. }
  1826. else
  1827. {
  1828. carla_stderr("carla_vst_audioMasterCallback() - audioMasterGetVendorString called with invalid pointer");
  1829. return 0;
  1830. }
  1831. case audioMasterGetProductString:
  1832. CARLA_ASSERT(ptr != nullptr);
  1833. if (ptr != nullptr)
  1834. {
  1835. std::strcpy((char*)ptr, "Carla");
  1836. return 1;
  1837. }
  1838. else
  1839. {
  1840. carla_stderr("carla_vst_audioMasterCallback() - audioMasterGetProductString called with invalid pointer");
  1841. return 0;
  1842. }
  1843. case audioMasterGetVendorVersion:
  1844. return 0x1000; // 1.0.0
  1845. case audioMasterCanDo:
  1846. CARLA_ASSERT(ptr != nullptr);
  1847. if (ptr != nullptr)
  1848. {
  1849. return carla_vst_hostCanDo((const char*)ptr);
  1850. }
  1851. else
  1852. {
  1853. carla_stderr("carla_vst_audioMasterCallback() - audioMasterCanDo called with invalid pointer");
  1854. return 0;
  1855. }
  1856. }
  1857. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  1858. VstPlugin* self = nullptr;
  1859. if (effect != nullptr)
  1860. {
  1861. #ifdef VESTIGE_HEADER
  1862. if (effect->ptr1 != nullptr)
  1863. {
  1864. self = (VstPlugin*)effect->ptr1;
  1865. #else
  1866. if (effect->resvd1 != 0)
  1867. {
  1868. self = FromVstPtr<VstPlugin>(effect->resvd1);
  1869. #endif
  1870. if (self->fUnique1 != self->fUnique2)
  1871. self = nullptr;
  1872. }
  1873. if (self != nullptr)
  1874. {
  1875. if (self->fEffect == nullptr)
  1876. self->fEffect = effect;
  1877. if (self->fEffect != effect)
  1878. {
  1879. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  1880. self = nullptr;
  1881. }
  1882. }
  1883. else if (sLastVstPlugin != nullptr)
  1884. {
  1885. #ifdef VESTIGE_HEADER
  1886. effect->ptr1 = sLastVstPlugin;
  1887. #else
  1888. effect->resvd1 = ToVstPtr<VstPlugin>(sLastVstPlugin);
  1889. #endif
  1890. self = sLastVstPlugin;
  1891. }
  1892. }
  1893. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  1894. }
  1895. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VstPlugin)
  1896. };
  1897. VstPlugin* VstPlugin::sLastVstPlugin = nullptr;
  1898. CARLA_BACKEND_END_NAMESPACE
  1899. #else // WANT_VST
  1900. # warning Building without VST support
  1901. #endif
  1902. CARLA_BACKEND_START_NAMESPACE
  1903. CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
  1904. {
  1905. carla_debug("CarlaPlugin::newVST(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1906. #ifdef WANT_VST
  1907. VstPlugin* const plugin = new VstPlugin(init.engine, init.id);
  1908. if (! plugin->init(init.filename, init.name, init.label))
  1909. {
  1910. delete plugin;
  1911. return nullptr;
  1912. }
  1913. plugin->reload();
  1914. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1915. {
  1916. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  1917. delete plugin;
  1918. return nullptr;
  1919. }
  1920. return plugin;
  1921. #else
  1922. init.engine->setLastError("VST support not available");
  1923. return nullptr;
  1924. #endif
  1925. }
  1926. CARLA_BACKEND_END_NAMESPACE