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.

2330 lines
72KB

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