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.

2342 lines
72KB

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