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.

2249 lines
70KB

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