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.

2348 lines
73KB

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