Collection of tools useful for audio production
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.

2432 lines
76KB

  1. /*
  2. * Carla VST Plugin
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_plugin.hpp"
  18. #ifdef WANT_VST
  19. #include "carla_vst_utils.hpp"
  20. #ifdef Q_WS_X11
  21. # include <QtGui/QX11Info>
  22. #endif
  23. CARLA_BACKEND_START_NAMESPACE
  24. /*!
  25. * @defgroup CarlaBackendVstPlugin Carla Backend VST Plugin
  26. *
  27. * The Carla Backend VST Plugin.
  28. * @{
  29. */
  30. /*!
  31. * @defgroup PluginHints Plugin Hints
  32. * @{
  33. */
  34. const unsigned int PLUGIN_CAN_PROCESS_REPLACING = 0x1000; //!< VST Plugin cas use processReplacing()
  35. const unsigned int PLUGIN_HAS_COCKOS_EXTENSIONS = 0x2000; //!< VST Plugin has Cockos extensions
  36. const unsigned int PLUGIN_USES_OLD_VSTSDK = 0x4000; //!< VST Plugin uses an old VST SDK
  37. const unsigned int PLUGIN_WANTS_MIDI_INPUT = 0x8000; //!< VST Plugin wants MIDI input
  38. /**@}*/
  39. class VstPlugin : public CarlaPlugin
  40. {
  41. public:
  42. VstPlugin(CarlaEngine* const engine, const unsigned short id)
  43. : CarlaPlugin(engine, id)
  44. {
  45. qDebug("VstPlugin::VstPlugin()");
  46. m_type = PLUGIN_VST;
  47. effect = nullptr;
  48. events.numEvents = 0;
  49. events.reserved = 0;
  50. gui.type = GUI_NONE;
  51. gui.visible = false;
  52. gui.width = 0;
  53. gui.height = 0;
  54. isProcessing = false;
  55. needIdle = false;
  56. vstTimeOffset = 0;
  57. memset(midiEvents, 0, sizeof(VstMidiEvent)*MAX_MIDI_EVENTS*2);
  58. for (unsigned short i=0; i < MAX_MIDI_EVENTS*2; i++)
  59. events.data[i] = (VstEvent*)&midiEvents[i];
  60. // make plugin valid
  61. srand(id);
  62. unique1 = unique2 = rand();
  63. }
  64. ~VstPlugin()
  65. {
  66. qDebug("VstPlugin::~VstPlugin()");
  67. // make plugin invalid
  68. unique2 += 1;
  69. if (effect)
  70. {
  71. // close UI
  72. if (m_hints & PLUGIN_HAS_GUI)
  73. {
  74. showGui(false);
  75. if (gui.type == GUI_EXTERNAL_OSC)
  76. {
  77. if (osc.thread)
  78. {
  79. // Wait a bit first, try safe quit, then force kill
  80. if (osc.thread->isRunning() && ! osc.thread->wait(x_engine->getOptions().oscUiTimeout))
  81. {
  82. qWarning("Failed to properly stop VST OSC GUI thread");
  83. osc.thread->terminate();
  84. }
  85. delete osc.thread;
  86. }
  87. }
  88. else
  89. effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
  90. }
  91. if (m_activeBefore)
  92. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  93. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  94. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  95. }
  96. }
  97. // -------------------------------------------------------------------
  98. // Information (base)
  99. PluginCategory category()
  100. {
  101. CARLA_ASSERT(effect);
  102. if (effect)
  103. {
  104. intptr_t category = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
  105. switch (category)
  106. {
  107. case kPlugCategSynth:
  108. return PLUGIN_CATEGORY_SYNTH;
  109. case kPlugCategAnalysis:
  110. return PLUGIN_CATEGORY_UTILITY;
  111. case kPlugCategMastering:
  112. return PLUGIN_CATEGORY_DYNAMICS;
  113. case kPlugCategRoomFx:
  114. return PLUGIN_CATEGORY_DELAY;
  115. case kPlugCategRestoration:
  116. return PLUGIN_CATEGORY_UTILITY;
  117. case kPlugCategGenerator:
  118. return PLUGIN_CATEGORY_SYNTH;
  119. }
  120. if (effect->flags & effFlagsIsSynth)
  121. return PLUGIN_CATEGORY_SYNTH;
  122. }
  123. return getPluginCategoryFromName(m_name);
  124. }
  125. long uniqueId()
  126. {
  127. CARLA_ASSERT(effect);
  128. return effect ? effect->uniqueID : 0;
  129. }
  130. // -------------------------------------------------------------------
  131. // Information (current data)
  132. int32_t chunkData(void** const dataPtr)
  133. {
  134. CARLA_ASSERT(dataPtr);
  135. CARLA_ASSERT(effect);
  136. if (effect)
  137. return effect->dispatcher(effect, effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
  138. return 0;
  139. }
  140. // -------------------------------------------------------------------
  141. // Information (per-plugin data)
  142. double getParameterValue(const uint32_t parameterId)
  143. {
  144. CARLA_ASSERT(effect);
  145. CARLA_ASSERT(parameterId < param.count);
  146. if (effect)
  147. return effect->getParameter(effect, parameterId);
  148. return 0.0;
  149. }
  150. void getLabel(char* const strBuf)
  151. {
  152. CARLA_ASSERT(effect);
  153. if (effect)
  154. effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f);
  155. else
  156. CarlaPlugin::getLabel(strBuf);
  157. }
  158. void getMaker(char* const strBuf)
  159. {
  160. CARLA_ASSERT(effect);
  161. if (effect)
  162. effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
  163. else
  164. CarlaPlugin::getMaker(strBuf);
  165. }
  166. void getCopyright(char* const strBuf)
  167. {
  168. CARLA_ASSERT(effect);
  169. if (effect)
  170. effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
  171. else
  172. CarlaPlugin::getCopyright(strBuf);
  173. }
  174. void getRealName(char* const strBuf)
  175. {
  176. CARLA_ASSERT(effect);
  177. if (effect)
  178. effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
  179. else
  180. CarlaPlugin::getRealName(strBuf);
  181. }
  182. void getParameterName(const uint32_t parameterId, char* const strBuf)
  183. {
  184. CARLA_ASSERT(effect);
  185. CARLA_ASSERT(parameterId < param.count);
  186. if (effect)
  187. effect->dispatcher(effect, effGetParamName, parameterId, 0, strBuf, 0.0f);
  188. else
  189. CarlaPlugin::getParameterName(parameterId, strBuf);
  190. }
  191. void getParameterText(const uint32_t parameterId, char* const strBuf)
  192. {
  193. CARLA_ASSERT(effect);
  194. CARLA_ASSERT(parameterId < param.count);
  195. if (effect)
  196. {
  197. effect->dispatcher(effect, effGetParamDisplay, parameterId, 0, strBuf, 0.0f);
  198. if (*strBuf == 0)
  199. sprintf(strBuf, "%f", getParameterValue(parameterId));
  200. }
  201. else
  202. CarlaPlugin::getParameterText(parameterId, strBuf);
  203. }
  204. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  205. {
  206. CARLA_ASSERT(effect);
  207. CARLA_ASSERT(parameterId < param.count);
  208. if (effect)
  209. effect->dispatcher(effect, effGetParamLabel, parameterId, 0, strBuf, 0.0f);
  210. else
  211. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  212. }
  213. void getGuiInfo(GuiType* const type, bool* const resizable)
  214. {
  215. *type = gui.type;
  216. *resizable = false;
  217. }
  218. // -------------------------------------------------------------------
  219. // Set data (plugin-specific stuff)
  220. void setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  221. {
  222. CARLA_ASSERT(parameterId < param.count);
  223. effect->setParameter(effect, parameterId, fixParameterValue(value, param.ranges[parameterId]));
  224. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  225. }
  226. void setChunkData(const char* const stringData)
  227. {
  228. CARLA_ASSERT(m_hints & PLUGIN_USES_CHUNKS);
  229. CARLA_ASSERT(stringData);
  230. static QByteArray chunk;
  231. chunk = QByteArray::fromBase64(stringData);
  232. if (x_engine->isOffline())
  233. {
  234. const CarlaEngine::ScopedLocker m(x_engine);
  235. effect->dispatcher(effect, effSetChunk, 0 /* bank */, chunk.size(), chunk.data(), 0.0f);
  236. }
  237. else
  238. {
  239. const CarlaPlugin::ScopedDisabler m(this);
  240. effect->dispatcher(effect, effSetChunk, 0 /* bank */, chunk.size(), chunk.data(), 0.0f);
  241. }
  242. }
  243. void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  244. {
  245. CARLA_ASSERT(index >= -1 && index < (int32_t)prog.count);
  246. if (index < -1)
  247. index = -1;
  248. else if (index > (int32_t)prog.count)
  249. return;
  250. if (index >= 0)
  251. {
  252. if (x_engine->isOffline())
  253. {
  254. const CarlaEngine::ScopedLocker m(x_engine, block);
  255. effect->dispatcher(effect, effBeginSetProgram, 0, 0, nullptr, 0.0f);
  256. effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
  257. effect->dispatcher(effect, effEndSetProgram, 0, 0, nullptr, 0.0f);
  258. }
  259. else
  260. {
  261. const ScopedDisabler m(this, block);
  262. effect->dispatcher(effect, effBeginSetProgram, 0, 0, nullptr, 0.0f);
  263. effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
  264. effect->dispatcher(effect, effEndSetProgram, 0, 0, nullptr, 0.0f);
  265. }
  266. }
  267. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, block);
  268. }
  269. // -------------------------------------------------------------------
  270. // Set gui stuff
  271. void setGuiContainer(GuiContainer* const container)
  272. {
  273. qDebug("VstPlugin::setGuiContainer(%p)", container);
  274. CARLA_ASSERT(container);
  275. if (gui.type == GUI_EXTERNAL_OSC)
  276. return;
  277. int32_t value = 0;
  278. void* const ptr = (void*)container->winId();
  279. ERect* vstRect = nullptr;
  280. #ifdef Q_WS_X11
  281. value = (intptr_t)QX11Info::display();
  282. #endif
  283. // get UI size before opening UI, plugin may refuse this
  284. effect->dispatcher(effect, effEditGetRect, 0, 0, &vstRect, 0.0f);
  285. if (vstRect)
  286. {
  287. int width = vstRect->right - vstRect->left;
  288. int height = vstRect->bottom - vstRect->top;
  289. if (width > 0 || height > 0)
  290. {
  291. container->setFixedSize(width, height);
  292. #ifdef BUILD_BRIDGE
  293. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 1.0, nullptr);
  294. #endif
  295. }
  296. }
  297. // open UI
  298. if (effect->dispatcher(effect, effEditOpen, 0, value, ptr, 0.0f) == 1)
  299. {
  300. // get UI size again, can't fail now
  301. vstRect = nullptr;
  302. effect->dispatcher(effect, effEditGetRect, 0, 0, &vstRect, 0.0f);
  303. if (vstRect)
  304. {
  305. int width = vstRect->right - vstRect->left;
  306. int height = vstRect->bottom - vstRect->top;
  307. if (width <= 0 || height <= 0)
  308. {
  309. qCritical("VstPlugin::setGuiContainer(%p) - failed to get proper editor size", container);
  310. return;
  311. }
  312. gui.width = width;
  313. gui.height = height;
  314. container->setFixedSize(width, height);
  315. qDebug("VstPlugin::setGuiContainer(%p) -> setFixedSize(%i, %i)", container, width, height);
  316. }
  317. else
  318. qCritical("VstPlugin::setGuiContainer(%p) - failed to get plugin editor size", container);
  319. }
  320. else
  321. {
  322. // failed to open UI
  323. qWarning("VstPlugin::setGuiContainer(%p) - failed to open UI", container);
  324. m_hints &= ~PLUGIN_HAS_GUI;
  325. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0, nullptr);
  326. effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
  327. }
  328. }
  329. void showGui(const bool yesNo)
  330. {
  331. if (gui.type == GUI_EXTERNAL_OSC)
  332. {
  333. CARLA_ASSERT(osc.thread);
  334. if (! osc.thread)
  335. {
  336. qCritical("VstPlugin::showGui(%s) - attempt to show gui, but it does not exist!", bool2str(yesNo));
  337. return;
  338. }
  339. if (yesNo)
  340. {
  341. osc.thread->start();
  342. }
  343. else
  344. {
  345. if (osc.data.target)
  346. {
  347. osc_send_hide(&osc.data);
  348. osc_send_quit(&osc.data);
  349. osc.data.free();
  350. }
  351. if (! osc.thread->wait(500))
  352. osc.thread->quit();
  353. }
  354. }
  355. else
  356. {
  357. if (yesNo && gui.width > 0 && gui.height > 0)
  358. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, gui.width, gui.height, 0.0, nullptr);
  359. }
  360. gui.visible = yesNo;
  361. }
  362. void idleGui()
  363. {
  364. #ifdef VESTIGE_HEADER
  365. if (effect /*&& effect->ptr1*/)
  366. #else
  367. if (effect /*&& effect->resvd1*/)
  368. #endif
  369. {
  370. if (needIdle)
  371. effect->dispatcher(effect, effIdle, 0, 0, nullptr, 0.0f);
  372. if (gui.type != GUI_EXTERNAL_OSC)
  373. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  374. }
  375. CarlaPlugin::idleGui();
  376. }
  377. // -------------------------------------------------------------------
  378. // Plugin state
  379. void reload()
  380. {
  381. qDebug("VstPlugin::reload() - start");
  382. CARLA_ASSERT(effect);
  383. const ProcessMode processMode(x_engine->getOptions().processMode);
  384. // Safely disable plugin for reload
  385. const ScopedDisabler m(this);
  386. if (x_client->isActive())
  387. x_client->deactivate();
  388. // Remove client ports
  389. removeClientPorts();
  390. // Delete old data
  391. deleteBuffers();
  392. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  393. aIns = effect->numInputs;
  394. aOuts = effect->numOutputs;
  395. params = effect->numParams;
  396. if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) > 0 || (m_hints & PLUGIN_WANTS_MIDI_INPUT))
  397. mIns = 1;
  398. else
  399. mIns = 0;
  400. if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
  401. mOuts = 1;
  402. else
  403. mOuts = 0;
  404. if (aIns > 0)
  405. {
  406. aIn.ports = new CarlaEngineAudioPort*[aIns];
  407. aIn.rindexes = new uint32_t[aIns];
  408. }
  409. if (aOuts > 0)
  410. {
  411. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  412. aOut.rindexes = new uint32_t[aOuts];
  413. }
  414. if (params > 0)
  415. {
  416. param.data = new ParameterData[params];
  417. param.ranges = new ParameterRanges[params];
  418. }
  419. bool needsCtrlIn = (aOuts > 0 || params > 0);
  420. const int portNameSize = x_engine->maxPortNameSize();
  421. CarlaString portName;
  422. // Audio Ins
  423. for (j=0; j < aIns; j++)
  424. {
  425. portName.clear();
  426. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  427. {
  428. portName = m_name;
  429. portName += ":";
  430. }
  431. char tmp[12] = { 0 };
  432. sprintf(tmp, "input_%02i", j+1);
  433. portName += tmp;
  434. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  435. aIn.rindexes[j] = j;
  436. }
  437. // Audio Outs
  438. for (j=0; j < aOuts; j++)
  439. {
  440. portName.clear();
  441. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  442. {
  443. portName = m_name;
  444. portName += ":";
  445. }
  446. char tmp[12] = { 0 };
  447. sprintf(tmp, "output_%02i", j+1);
  448. portName += tmp;
  449. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  450. aOut.rindexes[j] = j;
  451. }
  452. for (j=0; j < params; j++)
  453. {
  454. param.data[j].type = PARAMETER_INPUT;
  455. param.data[j].index = j;
  456. param.data[j].rindex = j;
  457. param.data[j].hints = 0;
  458. param.data[j].midiChannel = 0;
  459. param.data[j].midiCC = -1;
  460. double min, max, def, step, stepSmall, stepLarge;
  461. VstParameterProperties prop;
  462. prop.flags = 0;
  463. if (effect->dispatcher(effect, effGetParameterProperties, j, 0, &prop, 0))
  464. {
  465. double range[2] = { 0.0, 1.0 };
  466. if ((m_hints & PLUGIN_HAS_COCKOS_EXTENSIONS) > 0 && effect->dispatcher(effect, effVendorSpecific, 0xdeadbef0, j, range, 0.0) >= 0xbeef)
  467. {
  468. min = range[0];
  469. max = range[1];
  470. }
  471. else if (prop.flags & kVstParameterUsesIntegerMinMax)
  472. {
  473. min = prop.minInteger;
  474. max = prop.maxInteger;
  475. }
  476. else
  477. {
  478. min = 0.0;
  479. max = 1.0;
  480. }
  481. if (min > max)
  482. max = min;
  483. else if (max < min)
  484. min = max;
  485. if (max - min == 0.0)
  486. {
  487. qWarning("Broken plugin parameter: max - min == 0");
  488. max = min + 0.1;
  489. }
  490. if ((m_hints & PLUGIN_HAS_COCKOS_EXTENSIONS) > 0 && effect->dispatcher(effect, effVendorSpecific, kVstParameterUsesIntStep, j, nullptr, 0.0f) >= 0xbeef)
  491. {
  492. step = 1.0;
  493. stepSmall = 1.0;
  494. stepLarge = 10.0;
  495. }
  496. else if (prop.flags & kVstParameterIsSwitch)
  497. {
  498. step = max - min;
  499. stepSmall = step;
  500. stepLarge = step;
  501. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  502. }
  503. else if (prop.flags & kVstParameterUsesIntStep)
  504. {
  505. step = prop.stepInteger;
  506. stepSmall = prop.stepInteger;
  507. stepLarge = prop.largeStepInteger;
  508. param.data[j].hints |= PARAMETER_IS_INTEGER;
  509. }
  510. else if (prop.flags & kVstParameterUsesFloatStep)
  511. {
  512. step = prop.stepFloat;
  513. stepSmall = prop.smallStepFloat;
  514. stepLarge = prop.largeStepFloat;
  515. }
  516. else
  517. {
  518. double range = max - min;
  519. step = range/100.0;
  520. stepSmall = range/1000.0;
  521. stepLarge = range/10.0;
  522. }
  523. if (prop.flags & kVstParameterCanRamp)
  524. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  525. }
  526. else
  527. {
  528. min = 0.0;
  529. max = 1.0;
  530. step = 0.001;
  531. stepSmall = 0.0001;
  532. stepLarge = 0.1;
  533. }
  534. // no such thing as VST default parameters
  535. def = effect->getParameter(effect, j);
  536. if (def < min)
  537. def = min;
  538. else if (def > max)
  539. def = max;
  540. param.ranges[j].min = min;
  541. param.ranges[j].max = max;
  542. param.ranges[j].def = def;
  543. param.ranges[j].step = step;
  544. param.ranges[j].stepSmall = stepSmall;
  545. param.ranges[j].stepLarge = stepLarge;
  546. param.data[j].hints |= PARAMETER_IS_ENABLED;
  547. #ifndef BUILD_BRIDGE
  548. param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  549. #endif
  550. if ((m_hints & PLUGIN_USES_OLD_VSTSDK) > 0 || effect->dispatcher(effect, effCanBeAutomated, j, 0, nullptr, 0.0f) == 1)
  551. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  552. }
  553. if (needsCtrlIn)
  554. {
  555. portName.clear();
  556. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  557. {
  558. portName = m_name;
  559. portName += ":";
  560. }
  561. portName += "control-in";
  562. portName.truncate(portNameSize);
  563. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  564. }
  565. if (mIns == 1)
  566. {
  567. portName.clear();
  568. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  569. {
  570. portName = m_name;
  571. portName += ":";
  572. }
  573. portName += "midi-in";
  574. portName.truncate(portNameSize);
  575. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  576. }
  577. if (mOuts == 1)
  578. {
  579. portName.clear();
  580. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  581. {
  582. portName = m_name;
  583. portName += ":";
  584. }
  585. portName += "midi-out";
  586. portName.truncate(portNameSize);
  587. midi.portMout = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  588. }
  589. aIn.count = aIns;
  590. aOut.count = aOuts;
  591. param.count = params;
  592. // plugin checks
  593. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  594. intptr_t vstCategory = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
  595. if (vstCategory == kPlugCategSynth || vstCategory == kPlugCategGenerator)
  596. m_hints |= PLUGIN_IS_SYNTH;
  597. if (effect->flags & effFlagsProgramChunks)
  598. m_hints |= PLUGIN_USES_CHUNKS;
  599. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  600. m_hints |= PLUGIN_CAN_DRYWET;
  601. if (aOuts > 0)
  602. m_hints |= PLUGIN_CAN_VOLUME;
  603. if (aOuts >= 2 && aOuts%2 == 0)
  604. m_hints |= PLUGIN_CAN_BALANCE;
  605. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2))
  606. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  607. // check latency
  608. if (m_hints & PLUGIN_CAN_DRYWET)
  609. {
  610. #ifdef VESTIGE_HEADER
  611. char* const empty3Ptr = &effect->empty3[0];
  612. int32_t* initialDelayPtr = (int32_t*)empty3Ptr;
  613. m_latency = *initialDelayPtr;
  614. #else
  615. m_latency = effect->initialDelay;
  616. #endif
  617. x_client->setLatency(m_latency);
  618. recreateLatencyBuffers();
  619. }
  620. // special plugin fixes
  621. #ifdef __WINE__
  622. // 1. IL Harmless - disable threaded processing
  623. if (effect->uniqueID == 1229484653)
  624. {
  625. char strBuf[255] = { 0 };
  626. getLabel(strBuf);
  627. if (strcmp(strBuf, "IL Harmless") == 0)
  628. {
  629. // TODO - disable threaded processing
  630. }
  631. }
  632. #endif
  633. reloadPrograms(true);
  634. x_client->activate();
  635. qDebug("VstPlugin::reload() - end");
  636. }
  637. void reloadPrograms(const bool init)
  638. {
  639. qDebug("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, const uint32_t framesOffset)
  727. {
  728. uint32_t i, k;
  729. uint32_t midiEventCount = 0;
  730. vstTimeOffset = framesOffset;
  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 - framesOffset;
  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 - framesOffset;
  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. qDebug("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. qCritical("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. qDebug("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. qCritical("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. qDebug("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. qDebug("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 && 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. qDebug("VstPlugin::handleAudioMasterWantMidi()");
  1393. m_hints |= PLUGIN_WANTS_MIDI_INPUT;
  1394. }
  1395. // -------------------------------------------------------------------
  1396. static intptr_t hostCanDo(const char* const feature)
  1397. {
  1398. qDebug("VstPlugin::hostCanDo(\"%s\")", feature);
  1399. if (strcmp(feature, "supplyIdle") == 0)
  1400. return 1;
  1401. if (strcmp(feature, "sendVstEvents") == 0)
  1402. return 1;
  1403. if (strcmp(feature, "sendVstMidiEvent") == 0)
  1404. return 1;
  1405. if (strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1406. return -1;
  1407. if (strcmp(feature, "sendVstTimeInfo") == 0)
  1408. return 1;
  1409. if (strcmp(feature, "receiveVstEvents") == 0)
  1410. return 1;
  1411. if (strcmp(feature, "receiveVstMidiEvent") == 0)
  1412. return 1;
  1413. if (strcmp(feature, "receiveVstTimeInfo") == 0)
  1414. return -1;
  1415. if (strcmp(feature, "reportConnectionChanges") == 0)
  1416. return -1;
  1417. if (strcmp(feature, "acceptIOChanges") == 0)
  1418. {
  1419. //if (CarlaEngine::processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1420. // return -1;
  1421. return 1;
  1422. }
  1423. if (strcmp(feature, "sizeWindow") == 0)
  1424. return 1;
  1425. if (strcmp(feature, "offline") == 0)
  1426. return -1;
  1427. if (strcmp(feature, "openFileSelector") == 0)
  1428. return -1;
  1429. if (strcmp(feature, "closeFileSelector") == 0)
  1430. return -1;
  1431. if (strcmp(feature, "startStopProcess") == 0)
  1432. return 1;
  1433. if (strcmp(feature, "supportShell") == 0)
  1434. return -1;
  1435. if (strcmp(feature, "shellCategory") == 0)
  1436. return -1;
  1437. // unimplemented
  1438. qWarning("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. qDebug("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 (strcmp(func, "GetPlayPosition") == 0)
  1453. return 0;
  1454. if (strcmp(func, "GetPlayPosition2") == 0)
  1455. return 0;
  1456. if (strcmp(func, "GetCursorPosition") == 0)
  1457. return 0;
  1458. if (strcmp(func, "GetPlayState") == 0)
  1459. return 0;
  1460. if (strcmp(func, "SetEditCurPos") == 0)
  1461. return 0;
  1462. if (strcmp(func, "GetSetRepeat") == 0)
  1463. return 0;
  1464. if (strcmp(func, "GetProjectPath") == 0)
  1465. return 0;
  1466. if (strcmp(func, "OnPlayButton") == 0)
  1467. return 0;
  1468. if (strcmp(func, "OnStopButton") == 0)
  1469. return 0;
  1470. if (strcmp(func, "OnPauseButton") == 0)
  1471. return 0;
  1472. if (strcmp(func, "IsInRealTimeAudio") == 0)
  1473. return 0;
  1474. if (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. qWarning("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. qWarning("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. qWarning("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. qWarning("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. qWarning("VstPlugin::hostCallback::audioMasterProcessEvents called with invalid pointer");
  1582. }
  1583. else
  1584. qWarning("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. qWarning("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. qWarning("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. qWarning("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. qWarning("VstPlugin::hostCallback::audioMasterSizeWindow called with invalid size");
  1636. }
  1637. else
  1638. qWarning("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. qWarning("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. qWarning("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. qWarning("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. strcpy((char*)ptr, "Cadence");
  1714. ret = 1;
  1715. }
  1716. else
  1717. qWarning("VstPlugin::hostCallback::audioMasterGetVendorString called with invalid pointer");
  1718. break;
  1719. case audioMasterGetProductString:
  1720. CARLA_ASSERT(ptr);
  1721. if (ptr)
  1722. {
  1723. strcpy((char*)ptr, "Carla");
  1724. ret = 1;
  1725. }
  1726. else
  1727. qWarning("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. qWarning("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. // strcpy((char*)ptr, "stuff");
  1761. //else
  1762. // qWarning("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. qDebug("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. qDebug("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. private:
  1920. int unique1;
  1921. AEffect* effect;
  1922. struct {
  1923. int32_t numEvents;
  1924. intptr_t reserved;
  1925. VstEvent* data[MAX_MIDI_EVENTS*2];
  1926. } events;
  1927. VstMidiEvent midiEvents[MAX_MIDI_EVENTS*2];
  1928. uint32_t vstTimeOffset;
  1929. VstTimeInfo_R vstTimeInfo;
  1930. struct {
  1931. GuiType type;
  1932. bool visible;
  1933. int width;
  1934. int height;
  1935. } gui;
  1936. bool isProcessing;
  1937. bool needIdle;
  1938. static VstPlugin* lastVstPlugin;
  1939. int unique2;
  1940. };
  1941. VstPlugin* VstPlugin::lastVstPlugin = nullptr;
  1942. /**@}*/
  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. qDebug("CarlaPlugin::newVST(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1951. #ifdef WANT_VST
  1952. short id = init.engine->getNewPluginId();
  1953. if (id < 0 || id > init.engine->maxPluginNumber())
  1954. {
  1955. init.engine->setLastError("Maximum number of plugins reached");
  1956. return nullptr;
  1957. }
  1958. VstPlugin* const plugin = new VstPlugin(init.engine, id);
  1959. if (! plugin->init(init.filename, init.name, init.label))
  1960. {
  1961. delete plugin;
  1962. return nullptr;
  1963. }
  1964. plugin->reload();
  1965. if (init.engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1966. {
  1967. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  1968. {
  1969. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  1970. delete plugin;
  1971. return nullptr;
  1972. }
  1973. }
  1974. plugin->registerToOscClient();
  1975. return plugin;
  1976. #else
  1977. init.engine->setLastError("VST support not available");
  1978. return nullptr;
  1979. #endif
  1980. }
  1981. CARLA_BACKEND_END_NAMESPACE