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.

2311 lines
71KB

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