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.

2200 lines
67KB

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