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.

2258 lines
70KB

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