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.

2416 lines
76KB

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