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.

2378 lines
74KB

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