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.

2159 lines
66KB

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