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.

2364 lines
73KB

  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. {
  597. #ifdef VESTIGE_HEADER
  598. char* const empty3Ptr = &effect->empty3[0];
  599. int32_t* initialDelayPtr = (int32_t*)empty3Ptr;
  600. uint32_t latency = *initialDelayPtr;
  601. #else
  602. uint32_t latency = effect->initialDelay;
  603. #endif
  604. for (uint32_t i=0; i < aIn.count; i++)
  605. aIn.ports[i]->setLatency(latency);
  606. x_client->recomputeLatencies();
  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. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  983. }
  984. if (midiEventCount > 0)
  985. {
  986. events.numEvents = midiEventCount;
  987. events.reserved = 0;
  988. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  989. }
  990. // FIXME - make this a global option
  991. // don't process if not needed
  992. //if ((effect->flags & effFlagsNoSoundInStop) > 0 && aInsPeak[0] == 0.0 && aInsPeak[1] == 0.0 && midiEventCount == 0 && ! midi.portMout)
  993. //{
  994. if (m_hints & PLUGIN_CAN_PROCESS_REPLACING)
  995. {
  996. isProcessing = true;
  997. effect->processReplacing(effect, inBuffer, outBuffer, frames);
  998. isProcessing = false;
  999. }
  1000. else
  1001. {
  1002. for (i=0; i < aOut.count; i++)
  1003. zeroF(outBuffer[i], frames);
  1004. #if ! VST_FORCE_DEPRECATED
  1005. isProcessing = true;
  1006. effect->process(effect, inBuffer, outBuffer, frames);
  1007. isProcessing = false;
  1008. #endif
  1009. }
  1010. //}
  1011. }
  1012. else
  1013. {
  1014. if (m_activeBefore)
  1015. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1016. }
  1017. CARLA_PROCESS_CONTINUE_CHECK;
  1018. // --------------------------------------------------------------------------------------------------------
  1019. // Post-processing (dry/wet, volume and balance)
  1020. if (m_active)
  1021. {
  1022. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  1023. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  1024. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  1025. double bal_rangeL, bal_rangeR;
  1026. float oldBufLeft[do_balance ? frames : 0];
  1027. for (i=0; i < aOut.count; i++)
  1028. {
  1029. // Dry/Wet
  1030. if (do_drywet)
  1031. {
  1032. for (k=0; k < frames; k++)
  1033. {
  1034. if (aOut.count == 1)
  1035. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  1036. else
  1037. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  1038. }
  1039. }
  1040. // Balance
  1041. if (do_balance)
  1042. {
  1043. if (i%2 == 0)
  1044. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1045. bal_rangeL = (x_balanceLeft+1.0)/2;
  1046. bal_rangeR = (x_balanceRight+1.0)/2;
  1047. for (k=0; k < frames; k++)
  1048. {
  1049. if (i%2 == 0)
  1050. {
  1051. // left output
  1052. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1053. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1054. }
  1055. else
  1056. {
  1057. // right
  1058. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1059. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1060. }
  1061. }
  1062. }
  1063. // Volume
  1064. if (do_volume)
  1065. {
  1066. for (k=0; k < frames; k++)
  1067. outBuffer[i][k] *= x_volume;
  1068. }
  1069. // Output VU
  1070. for (k=0; i < 2 && k < frames; k++)
  1071. {
  1072. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  1073. aOutsPeak[i] = abs(outBuffer[i][k]);
  1074. }
  1075. }
  1076. }
  1077. else
  1078. {
  1079. // disable any output sound if not active
  1080. for (i=0; i < aOut.count; i++)
  1081. zeroF(outBuffer[i], frames);
  1082. aOutsPeak[0] = 0.0;
  1083. aOutsPeak[1] = 0.0;
  1084. } // End of Post-processing
  1085. CARLA_PROCESS_CONTINUE_CHECK;
  1086. // --------------------------------------------------------------------------------------------------------
  1087. // MIDI Output
  1088. if (midi.portMout && m_active)
  1089. {
  1090. uint8_t data[3] = { 0 };
  1091. for (int32_t i = midiEventCount; i < events.numEvents; i++)
  1092. {
  1093. data[0] = midiEvents[i].midiData[0];
  1094. data[1] = midiEvents[i].midiData[1];
  1095. data[2] = midiEvents[i].midiData[2];
  1096. // Fix bad note-off
  1097. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  1098. data[0] -= 0x10;
  1099. midi.portMout->writeEvent(midiEvents[i].deltaFrames, data, 3);
  1100. }
  1101. } // End of MIDI Output
  1102. CARLA_PROCESS_CONTINUE_CHECK;
  1103. // --------------------------------------------------------------------------------------------------------
  1104. // Peak Values
  1105. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1106. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1107. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1108. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1109. m_activeBefore = m_active;
  1110. }
  1111. void bufferSizeChanged(uint32_t newBufferSize)
  1112. {
  1113. if (m_active)
  1114. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1115. #if ! VST_FORCE_DEPRECATED
  1116. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, x_engine->getSampleRate());
  1117. #endif
  1118. effect->dispatcher(effect, effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
  1119. if (m_active)
  1120. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1121. }
  1122. // -------------------------------------------------------------------
  1123. // Post-poned events
  1124. #ifndef BUILD_BRIDGE
  1125. void uiParameterChange(const uint32_t index, const double value)
  1126. {
  1127. CARLA_ASSERT(index < param.count);
  1128. if (index >= param.count)
  1129. return;
  1130. if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
  1131. osc_send_control(&osc.data, param.data[index].rindex, value);
  1132. }
  1133. void uiProgramChange(const uint32_t index)
  1134. {
  1135. CARLA_ASSERT(index < prog.count);
  1136. if (index >= prog.count)
  1137. return;
  1138. if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
  1139. osc_send_program(&osc.data, index);
  1140. }
  1141. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1142. {
  1143. CARLA_ASSERT(channel < 16);
  1144. CARLA_ASSERT(note < 128);
  1145. CARLA_ASSERT(velo > 0 && velo < 128);
  1146. if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
  1147. {
  1148. uint8_t midiData[4] = { 0 };
  1149. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1150. midiData[2] = note;
  1151. midiData[3] = velo;
  1152. osc_send_midi(&osc.data, midiData);
  1153. }
  1154. }
  1155. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1156. {
  1157. CARLA_ASSERT(channel < 16);
  1158. CARLA_ASSERT(note < 128);
  1159. if (gui.type == GUI_EXTERNAL_OSC && osc.data.target)
  1160. {
  1161. uint8_t midiData[4] = { 0 };
  1162. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1163. midiData[2] = note;
  1164. osc_send_midi(&osc.data, midiData);
  1165. }
  1166. }
  1167. #endif
  1168. // -------------------------------------------------------------------
  1169. void handleAudioMasterAutomate(const uint32_t index, const double value)
  1170. {
  1171. //CARLA_ASSERT(m_enabled);
  1172. CARLA_ASSERT_INT(index < param.count, index);
  1173. if (index >= param.count /*|| ! m_enabled*/)
  1174. return;
  1175. if (isProcessing && ! x_engine->isOffline())
  1176. {
  1177. setParameterValue(index, value, false, false, false);
  1178. postponeEvent(PluginPostEventParameterChange, index, 0, value);
  1179. }
  1180. else
  1181. setParameterValue(index, value, isProcessing, true, true);
  1182. }
  1183. intptr_t handleAudioMasterGetCurrentProcessLevel()
  1184. {
  1185. if (x_engine->isOffline())
  1186. return kVstProcessLevelOffline;
  1187. if (isProcessing)
  1188. return kVstProcessLevelRealtime;
  1189. return kVstProcessLevelUser;
  1190. }
  1191. intptr_t handleAudioMasterGetBlockSize()
  1192. {
  1193. return x_engine->getBufferSize();
  1194. }
  1195. intptr_t handleAudioMasterGetSampleRate()
  1196. {
  1197. return x_engine->getSampleRate();
  1198. }
  1199. intptr_t handleAudioMasterGetTime()
  1200. {
  1201. memset(&vstTimeInfo, 0, sizeof(VstTimeInfo_R));
  1202. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1203. vstTimeInfo.flags |= kVstTransportChanged;
  1204. if (timeInfo->playing)
  1205. vstTimeInfo.flags |= kVstTransportPlaying;
  1206. vstTimeInfo.samplePos = timeInfo->frame + vstTimeOffset;
  1207. vstTimeInfo.sampleRate = x_engine->getSampleRate();
  1208. vstTimeInfo.nanoSeconds = timeInfo->time;
  1209. vstTimeInfo.flags |= kVstNanosValid;
  1210. if (timeInfo->valid & CarlaEngineTimeBBT)
  1211. {
  1212. double ppqBar = double(timeInfo->bbt.bar - 1) * timeInfo->bbt.beats_per_bar;
  1213. double ppqBeat = double(timeInfo->bbt.beat - 1);
  1214. double ppqTick = double(timeInfo->bbt.tick) / timeInfo->bbt.ticks_per_beat;
  1215. // Bars
  1216. vstTimeInfo.barStartPos = ppqBar;
  1217. vstTimeInfo.flags |= kVstBarsValid;
  1218. // PPQ Pos
  1219. vstTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  1220. vstTimeInfo.flags |= kVstPpqPosValid;
  1221. // Tempo
  1222. vstTimeInfo.tempo = timeInfo->bbt.beats_per_minute;
  1223. vstTimeInfo.flags |= kVstTempoValid;
  1224. // Time Signature
  1225. vstTimeInfo.timeSigNumerator = timeInfo->bbt.beats_per_bar;
  1226. vstTimeInfo.timeSigDenominator = timeInfo->bbt.beat_type;
  1227. vstTimeInfo.flags |= kVstTimeSigValid;
  1228. }
  1229. else
  1230. {
  1231. // Tempo
  1232. vstTimeInfo.tempo = 120.0;
  1233. vstTimeInfo.flags |= kVstTempoValid;
  1234. // Time Signature
  1235. vstTimeInfo.timeSigNumerator = 4;
  1236. vstTimeInfo.timeSigDenominator = 4;
  1237. vstTimeInfo.flags |= kVstTimeSigValid;
  1238. }
  1239. return (intptr_t)&vstTimeInfo;
  1240. }
  1241. intptr_t handleAudioMasterTempoAt()
  1242. {
  1243. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1244. if (timeInfo->valid & CarlaEngineTimeBBT)
  1245. return timeInfo->bbt.beats_per_minute * 10000;
  1246. return 0;
  1247. }
  1248. intptr_t handleAudioMasterIOChanged()
  1249. {
  1250. qDebug("VstPlugin::handleAudioMasterIOChanged()");
  1251. CARLA_ASSERT(m_enabled);
  1252. // TESTING
  1253. if (! m_enabled)
  1254. return 1;
  1255. #ifndef BUILD_BRIDGE
  1256. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1257. {
  1258. qCritical("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1259. return 0;
  1260. }
  1261. #endif
  1262. engineProcessLock();
  1263. m_enabled = false;
  1264. engineProcessUnlock();
  1265. if (m_active)
  1266. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1267. reload();
  1268. if (m_active)
  1269. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1270. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0);
  1271. return 1;
  1272. }
  1273. void handleAudioMasterNeedIdle()
  1274. {
  1275. qDebug("VstPlugin::handleAudioMasterNeedIdle()");
  1276. needIdle = true;
  1277. }
  1278. intptr_t handleAudioMasterProcessEvents(const VstEvents* const vstEvents)
  1279. {
  1280. CARLA_ASSERT(m_enabled);
  1281. CARLA_ASSERT(midi.portMout);
  1282. CARLA_ASSERT(isProcessing);
  1283. if (! m_enabled)
  1284. return 0;
  1285. if (! midi.portMout)
  1286. return 0;
  1287. if (! isProcessing)
  1288. {
  1289. qCritical("VstPlugin::handleAudioMasterProcessEvents(%p) - received MIDI out events outside audio thread, ignoring", vstEvents);
  1290. return 0;
  1291. }
  1292. for (int32_t i=0; i < vstEvents->numEvents && events.numEvents < MAX_MIDI_EVENTS*2; i++)
  1293. {
  1294. if (! vstEvents->events[i])
  1295. break;
  1296. const VstMidiEvent* const vstMidiEvent = (const VstMidiEvent*)vstEvents->events[i];
  1297. if (vstMidiEvent->type == kVstMidiType)
  1298. memcpy(&midiEvents[events.numEvents++], vstMidiEvent, sizeof(VstMidiEvent));
  1299. }
  1300. return 1;
  1301. }
  1302. intptr_t handleAdioMasterSizeWindow(int32_t width, int32_t height)
  1303. {
  1304. qDebug("VstPlugin::handleAudioMasterSizeWindow(%i, %i)", width, height);
  1305. gui.width = width;
  1306. gui.height = height;
  1307. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0);
  1308. return 1;
  1309. }
  1310. void handleAudioMasterUpdateDisplay()
  1311. {
  1312. qDebug("VstPlugin::handleAudioMasterUpdateDisplay()");
  1313. // Update current program name
  1314. if (prog.count > 0 && prog.current >= 0)
  1315. {
  1316. const int32_t index = prog.current;
  1317. char strBuf[STR_MAX] = { 0 };
  1318. effect->dispatcher(effect, effGetProgramName, 0, 0, strBuf, 0.0f);
  1319. if (! prog.names[index])
  1320. {
  1321. prog.names[index] = strdup(strBuf);
  1322. }
  1323. else if (strBuf[0] != 0 && strcmp(strBuf, prog.names[index]) != 0)
  1324. {
  1325. free((void*)prog.names[index]);
  1326. prog.names[index] = strdup(strBuf);
  1327. }
  1328. }
  1329. // Tell backend to update
  1330. x_engine->callback(CALLBACK_UPDATE, m_id, 0, 0, 0.0);
  1331. }
  1332. void handleAudioMasterWantMidi()
  1333. {
  1334. qDebug("VstPlugin::handleAudioMasterWantMidi()");
  1335. m_hints |= PLUGIN_WANTS_MIDI_INPUT;
  1336. }
  1337. // -------------------------------------------------------------------
  1338. static intptr_t hostCanDo(const char* const feature)
  1339. {
  1340. qDebug("VstPlugin::hostCanDo(\"%s\")", feature);
  1341. if (strcmp(feature, "supplyIdle") == 0)
  1342. return 1;
  1343. if (strcmp(feature, "sendVstEvents") == 0)
  1344. return 1;
  1345. if (strcmp(feature, "sendVstMidiEvent") == 0)
  1346. return 1;
  1347. if (strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1348. return -1;
  1349. if (strcmp(feature, "sendVstTimeInfo") == 0)
  1350. return 1;
  1351. if (strcmp(feature, "receiveVstEvents") == 0)
  1352. return 1;
  1353. if (strcmp(feature, "receiveVstMidiEvent") == 0)
  1354. return 1;
  1355. if (strcmp(feature, "receiveVstTimeInfo") == 0)
  1356. return -1;
  1357. if (strcmp(feature, "reportConnectionChanges") == 0)
  1358. return -1;
  1359. if (strcmp(feature, "acceptIOChanges") == 0)
  1360. {
  1361. #ifndef BUILD_BRIDGE
  1362. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1363. return -1;
  1364. #endif
  1365. return 1;
  1366. }
  1367. if (strcmp(feature, "sizeWindow") == 0)
  1368. return 1;
  1369. if (strcmp(feature, "offline") == 0)
  1370. return -1;
  1371. if (strcmp(feature, "openFileSelector") == 0)
  1372. return -1;
  1373. if (strcmp(feature, "closeFileSelector") == 0)
  1374. return -1;
  1375. if (strcmp(feature, "startStopProcess") == 0)
  1376. return 1;
  1377. if (strcmp(feature, "supportShell") == 0)
  1378. return -1;
  1379. if (strcmp(feature, "shellCategory") == 0)
  1380. return -1;
  1381. // unimplemented
  1382. qWarning("VstPlugin::hostCanDo(\"%s\") - unknown feature", feature);
  1383. return 0;
  1384. }
  1385. 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)
  1386. {
  1387. #ifdef DEBUG
  1388. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1389. qDebug("VstPlugin::hostCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1390. #endif
  1391. #if 0
  1392. // Cockos VST extensions
  1393. if (/*effect &&*/ ptr && (uint32_t)opcode == 0xdeadbeef && (uint32_t)index == 0xdeadf00d)
  1394. {
  1395. const char* const func = (char*)ptr;
  1396. if (strcmp(func, "GetPlayPosition") == 0)
  1397. return 0;
  1398. if (strcmp(func, "GetPlayPosition2") == 0)
  1399. return 0;
  1400. if (strcmp(func, "GetCursorPosition") == 0)
  1401. return 0;
  1402. if (strcmp(func, "GetPlayState") == 0)
  1403. return 0;
  1404. if (strcmp(func, "SetEditCurPos") == 0)
  1405. return 0;
  1406. if (strcmp(func, "GetSetRepeat") == 0)
  1407. return 0;
  1408. if (strcmp(func, "GetProjectPath") == 0)
  1409. return 0;
  1410. if (strcmp(func, "OnPlayButton") == 0)
  1411. return 0;
  1412. if (strcmp(func, "OnStopButton") == 0)
  1413. return 0;
  1414. if (strcmp(func, "OnPauseButton") == 0)
  1415. return 0;
  1416. if (strcmp(func, "IsInRealTimeAudio") == 0)
  1417. return 0;
  1418. if (strcmp(func, "Audio_IsRunning") == 0)
  1419. return 0;
  1420. }
  1421. #endif
  1422. // Check if 'resvd1' points to this plugin, or register ourselfs if possible
  1423. VstPlugin* self = nullptr;
  1424. if (effect)
  1425. {
  1426. #ifdef VESTIGE_HEADER
  1427. if (effect->ptr1)
  1428. {
  1429. self = (VstPlugin*)effect->ptr1;
  1430. #else
  1431. if (effect->resvd1)
  1432. {
  1433. self = FromVstPtr<VstPlugin>(effect->resvd1);
  1434. #endif
  1435. if (self->unique1 != self->unique2)
  1436. self = nullptr;
  1437. }
  1438. if (self)
  1439. {
  1440. if (! self->effect)
  1441. self->effect = effect;
  1442. CARLA_ASSERT(self->effect == effect);
  1443. if (self->effect != effect)
  1444. {
  1445. qWarning("VstPlugin::hostCallback() - host pointer mismatch: %p != %p", self->effect, effect);
  1446. self = nullptr;
  1447. }
  1448. }
  1449. else if (lastVstPlugin)
  1450. {
  1451. #ifdef VESTIGE_HEADER
  1452. effect->ptr1 = lastVstPlugin;
  1453. #else
  1454. effect->resvd1 = ToVstPtr(lastVstPlugin);
  1455. #endif
  1456. self = lastVstPlugin;
  1457. }
  1458. }
  1459. intptr_t ret = 0;
  1460. switch (opcode)
  1461. {
  1462. case audioMasterAutomate:
  1463. CARLA_ASSERT(self);
  1464. if (self)
  1465. self->handleAudioMasterAutomate(index, opt);
  1466. else
  1467. qWarning("VstPlugin::hostCallback::audioMasterAutomate called without valid object");
  1468. break;
  1469. case audioMasterVersion:
  1470. ret = kVstVersion;
  1471. break;
  1472. case audioMasterCurrentId:
  1473. // TODO
  1474. // if using old sdk, return effect->uniqueID
  1475. break;
  1476. case audioMasterIdle:
  1477. CARLA_ASSERT(effect);
  1478. if (effect)
  1479. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  1480. else
  1481. qWarning("VstPlugin::hostCallback::audioMasterIdle called without valid effect");
  1482. break;
  1483. #if ! VST_FORCE_DEPRECATED
  1484. case audioMasterPinConnected:
  1485. // Deprecated in VST SDK 2.4
  1486. // TODO
  1487. break;
  1488. case audioMasterWantMidi:
  1489. // Deprecated in VST SDK 2.4
  1490. CARLA_ASSERT(self);
  1491. if (self)
  1492. self->handleAudioMasterWantMidi();
  1493. else
  1494. qWarning("VstPlugin::hostCallback::audioMasterWantMidi called without valid object");
  1495. break;
  1496. #endif
  1497. case audioMasterGetTime:
  1498. CARLA_ASSERT(self);
  1499. if (self)
  1500. {
  1501. ret = self->handleAudioMasterGetTime();
  1502. }
  1503. else
  1504. {
  1505. static VstTimeInfo_R vstTimeInfo;
  1506. memset(&vstTimeInfo, 0, sizeof(VstTimeInfo_R));
  1507. vstTimeInfo.sampleRate = 44100.0;
  1508. // Tempo
  1509. vstTimeInfo.tempo = 120.0;
  1510. vstTimeInfo.flags |= kVstTempoValid;
  1511. // Time Signature
  1512. vstTimeInfo.timeSigNumerator = 4;
  1513. vstTimeInfo.timeSigDenominator = 4;
  1514. vstTimeInfo.flags |= kVstTimeSigValid;
  1515. ret = (intptr_t)&vstTimeInfo;
  1516. }
  1517. break;
  1518. case audioMasterProcessEvents:
  1519. CARLA_ASSERT(self && ptr);
  1520. if (self)
  1521. {
  1522. if (ptr)
  1523. ret = self->handleAudioMasterProcessEvents((const VstEvents*)ptr);
  1524. else
  1525. qWarning("VstPlugin::hostCallback::audioMasterProcessEvents called with invalid pointer");
  1526. }
  1527. else
  1528. qWarning("VstPlugin::hostCallback::audioMasterProcessEvents called without valid object");
  1529. break;
  1530. #if ! VST_FORCE_DEPRECATED
  1531. case audioMasterSetTime:
  1532. // Deprecated in VST SDK 2.4
  1533. break;
  1534. case audioMasterTempoAt:
  1535. // Deprecated in VST SDK 2.4
  1536. CARLA_ASSERT(self);
  1537. if (self)
  1538. ret = self->handleAudioMasterTempoAt();
  1539. else
  1540. qWarning("VstPlugin::hostCallback::audioMasterTempoAt called without valid object");
  1541. if (ret == 0)
  1542. ret = 120 * 10000;
  1543. break;
  1544. case audioMasterGetNumAutomatableParameters:
  1545. // Deprecated in VST SDK 2.4
  1546. #ifdef BUILD_BRIDGE
  1547. ret = MAX_PARAMETERS;
  1548. #else
  1549. ret = carlaOptions.maxParameters;
  1550. #endif
  1551. if (effect && ret > effect->numParams)
  1552. ret = effect->numParams;
  1553. break;
  1554. case audioMasterGetParameterQuantization:
  1555. // Deprecated in VST SDK 2.4
  1556. ret = 1; // full single float precision
  1557. break;
  1558. #endif
  1559. case audioMasterIOChanged:
  1560. CARLA_ASSERT(self);
  1561. if (self)
  1562. ret = self->handleAudioMasterIOChanged();
  1563. else
  1564. qWarning("VstPlugin::hostCallback::audioMasterIOChanged called without valid object");
  1565. break;
  1566. case audioMasterNeedIdle:
  1567. // Deprecated in VST SDK 2.4
  1568. CARLA_ASSERT(self);
  1569. if (self)
  1570. self->handleAudioMasterNeedIdle();
  1571. else
  1572. qWarning("VstPlugin::hostCallback::audioMasterNeedIdle called without valid object");
  1573. break;
  1574. case audioMasterSizeWindow:
  1575. CARLA_ASSERT(self);
  1576. if (self)
  1577. {
  1578. if (index > 0 && value > 0)
  1579. ret = self->handleAdioMasterSizeWindow(index, value);
  1580. else
  1581. qWarning("VstPlugin::hostCallback::audioMasterSizeWindow called with invalid size");
  1582. }
  1583. else
  1584. qWarning("VstPlugin::hostCallback::audioMasterSizeWindow called without valid object");
  1585. break;
  1586. case audioMasterGetSampleRate:
  1587. CARLA_ASSERT(self);
  1588. if (self)
  1589. ret = self->handleAudioMasterGetSampleRate();
  1590. else
  1591. qWarning("VstPlugin::hostCallback::audioMasterGetSampleRate called without valid object");
  1592. if (ret == 0)
  1593. ret = 44100;
  1594. break;
  1595. case audioMasterGetBlockSize:
  1596. CARLA_ASSERT(self);
  1597. if (self)
  1598. ret = self->handleAudioMasterGetBlockSize();
  1599. else
  1600. qWarning("VstPlugin::hostCallback::audioMasterGetBlockSize called without valid object");
  1601. if (ret == 0)
  1602. #ifndef BUILD_BRIDGE
  1603. ret = carlaOptions.processHighPrecision ? 8 : 512;
  1604. #else
  1605. ret = 512;
  1606. #endif
  1607. break;
  1608. case audioMasterGetInputLatency:
  1609. ret = 0;
  1610. break;
  1611. case audioMasterGetOutputLatency:
  1612. ret = 0;
  1613. break;
  1614. #if ! VST_FORCE_DEPRECATED
  1615. case audioMasterGetPreviousPlug:
  1616. // Deprecated in VST SDK 2.4
  1617. // TODO
  1618. break;
  1619. case audioMasterGetNextPlug:
  1620. // Deprecated in VST SDK 2.4
  1621. // TODO
  1622. break;
  1623. case audioMasterWillReplaceOrAccumulate:
  1624. // Deprecated in VST SDK 2.4
  1625. ret = 1; // replace
  1626. break;
  1627. #endif
  1628. case audioMasterGetCurrentProcessLevel:
  1629. if (self)
  1630. {
  1631. ret = self->handleAudioMasterGetCurrentProcessLevel();
  1632. }
  1633. else
  1634. {
  1635. qWarning("VstPlugin::hostCallback::audioMasterGetCurrentProcessLevel called without valid object");
  1636. ret = kVstProcessLevelUnknown;
  1637. }
  1638. break;
  1639. case audioMasterGetAutomationState:
  1640. ret = kVstAutomationReadWrite;
  1641. break;
  1642. case audioMasterOfflineStart:
  1643. case audioMasterOfflineRead:
  1644. case audioMasterOfflineWrite:
  1645. case audioMasterOfflineGetCurrentPass:
  1646. case audioMasterOfflineGetCurrentMetaPass:
  1647. // TODO
  1648. break;
  1649. #if ! VST_FORCE_DEPRECATED
  1650. case audioMasterSetOutputSampleRate:
  1651. // Deprecated in VST SDK 2.4
  1652. break;
  1653. case audioMasterGetOutputSpeakerArrangement:
  1654. // Deprecated in VST SDK 2.4
  1655. // TODO
  1656. break;
  1657. #endif
  1658. case audioMasterGetVendorString:
  1659. CARLA_ASSERT(ptr);
  1660. if (ptr)
  1661. {
  1662. strcpy((char*)ptr, "Cadence");
  1663. ret = 1;
  1664. }
  1665. else
  1666. qWarning("VstPlugin::hostCallback::audioMasterGetVendorString called with invalid pointer");
  1667. break;
  1668. case audioMasterGetProductString:
  1669. CARLA_ASSERT(ptr);
  1670. if (ptr)
  1671. {
  1672. strcpy((char*)ptr, "Carla");
  1673. ret = 1;
  1674. }
  1675. else
  1676. qWarning("VstPlugin::hostCallback::audioMasterGetProductString called with invalid pointer");
  1677. break;
  1678. case audioMasterGetVendorVersion:
  1679. ret = 0x050; // 0.5.0
  1680. break;
  1681. case audioMasterVendorSpecific:
  1682. // TODO - cockos extensions
  1683. break;
  1684. #if ! VST_FORCE_DEPRECATED
  1685. case audioMasterSetIcon:
  1686. // Deprecated in VST SDK 2.4
  1687. break;
  1688. #endif
  1689. case audioMasterCanDo:
  1690. CARLA_ASSERT(ptr);
  1691. if (ptr)
  1692. ret = hostCanDo((const char*)ptr);
  1693. else
  1694. qWarning("VstPlugin::hostCallback::audioMasterCanDo called with invalid pointer");
  1695. break;
  1696. case audioMasterGetLanguage:
  1697. ret = kVstLangEnglish;
  1698. break;
  1699. #if ! VST_FORCE_DEPRECATED
  1700. case audioMasterOpenWindow:
  1701. case audioMasterCloseWindow:
  1702. // Deprecated in VST SDK 2.4
  1703. // TODO
  1704. break;
  1705. #endif
  1706. case audioMasterGetDirectory:
  1707. // TODO
  1708. //if (ptr)
  1709. // strcpy((char*)ptr, "stuff");
  1710. //else
  1711. // qWarning("VstPlugin::hostCallback::audioMasterGetDirectory called with invalid pointer");
  1712. break;
  1713. case audioMasterUpdateDisplay:
  1714. CARLA_ASSERT(effect);
  1715. if (self)
  1716. self->handleAudioMasterUpdateDisplay();
  1717. if (effect)
  1718. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  1719. ret = 1;
  1720. break;
  1721. case audioMasterBeginEdit:
  1722. case audioMasterEndEdit:
  1723. // TODO
  1724. break;
  1725. case audioMasterOpenFileSelector:
  1726. case audioMasterCloseFileSelector:
  1727. // TODO
  1728. break;
  1729. #if ! VST_FORCE_DEPRECATED
  1730. case audioMasterEditFile:
  1731. // Deprecated in VST SDK 2.4
  1732. // TODO
  1733. break;
  1734. case audioMasterGetChunkFile:
  1735. // Deprecated in VST SDK 2.4
  1736. // TODO
  1737. break;
  1738. case audioMasterGetInputSpeakerArrangement:
  1739. // Deprecated in VST SDK 2.4
  1740. // TODO
  1741. break;
  1742. #endif
  1743. default:
  1744. #ifdef DEBUG
  1745. qDebug("VstPlugin::hostCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1746. #endif
  1747. break;
  1748. }
  1749. return ret;
  1750. }
  1751. // -------------------------------------------------------------------
  1752. bool init(const char* const filename, const char* const name, const char* const label)
  1753. {
  1754. // ---------------------------------------------------------------
  1755. // open DLL
  1756. if (! libOpen(filename))
  1757. {
  1758. setLastError(libError(filename));
  1759. return false;
  1760. }
  1761. // ---------------------------------------------------------------
  1762. // get DLL main entry
  1763. VST_Function vstFn = (VST_Function)libSymbol("VSTPluginMain");
  1764. if (! vstFn)
  1765. {
  1766. vstFn = (VST_Function)libSymbol("main");
  1767. if (! vstFn)
  1768. {
  1769. setLastError("Could not find the VST main entry in the plugin library");
  1770. return false;
  1771. }
  1772. }
  1773. // ---------------------------------------------------------------
  1774. // initialize plugin (part 1)
  1775. lastVstPlugin = this;
  1776. effect = vstFn(hostCallback);
  1777. lastVstPlugin = nullptr;
  1778. if ((! effect) || effect->magic != kEffectMagic)
  1779. {
  1780. setLastError("Plugin failed to initialize");
  1781. return false;
  1782. }
  1783. #ifdef VESTIGE_HEADER
  1784. effect->ptr1 = this;
  1785. #else
  1786. effect->resvd1 = ToVstPtr(this);
  1787. #endif
  1788. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  1789. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1790. // ---------------------------------------------------------------
  1791. // get info
  1792. m_filename = strdup(filename);
  1793. if (name)
  1794. {
  1795. m_name = x_engine->getUniquePluginName(name);
  1796. }
  1797. else
  1798. {
  1799. char strBuf[STR_MAX] = { 0 };
  1800. effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
  1801. if (strBuf[0] != 0)
  1802. m_name = x_engine->getUniquePluginName(strBuf);
  1803. else
  1804. m_name = x_engine->getUniquePluginName(label);
  1805. }
  1806. // ---------------------------------------------------------------
  1807. // register client
  1808. x_client = x_engine->addClient(this);
  1809. if (! x_client->isOk())
  1810. {
  1811. setLastError("Failed to register plugin client");
  1812. return false;
  1813. }
  1814. // ---------------------------------------------------------------
  1815. // initialize plugin (part 2)
  1816. #if ! VST_FORCE_DEPRECATED
  1817. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, x_engine->getBufferSize(), nullptr, x_engine->getSampleRate());
  1818. #endif
  1819. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, x_engine->getSampleRate());
  1820. effect->dispatcher(effect, effSetBlockSize, 0, x_engine->getBufferSize(), nullptr, 0.0f);
  1821. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1822. #if ! VST_FORCE_DEPRECATED
  1823. // dummy pre-start to catch possible wantEvents() call on old plugins
  1824. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1825. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1826. #endif
  1827. // special checks
  1828. if ((uintptr_t)effect->dispatcher(effect, effCanDo, 0, 0, (void*)"hasCockosExtensions", 0.0f) == 0xbeef0000)
  1829. {
  1830. qDebug("Plugin has Cockos extensions!");
  1831. m_hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1832. }
  1833. if (effect->dispatcher(effect, effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1834. m_hints |= PLUGIN_USES_OLD_VSTSDK;
  1835. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != effect->process)
  1836. m_hints |= PLUGIN_CAN_PROCESS_REPLACING;
  1837. // ---------------------------------------------------------------
  1838. // gui stuff
  1839. if (effect->flags & effFlagsHasEditor)
  1840. {
  1841. m_hints |= PLUGIN_HAS_GUI;
  1842. #if defined(Q_OS_LINUX) && ! defined(BUILD_BRIDGE)
  1843. if (carlaOptions.bridge_vstx11 && carlaOptions.preferUiBridges && ! (effect->flags & effFlagsProgramChunks))
  1844. {
  1845. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_VST_GUI);
  1846. osc.thread->setOscData(carlaOptions.bridge_vstx11, label);
  1847. gui.type = GUI_EXTERNAL_OSC;
  1848. }
  1849. else
  1850. #endif
  1851. {
  1852. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  1853. #if defined(Q_OS_WIN)
  1854. gui.type = GUI_INTERNAL_HWND;
  1855. #elif defined(Q_OS_MACOS)
  1856. gui.type = GUI_INTERNAL_COCOA;
  1857. #elif defined(Q_OS_LINUX)
  1858. gui.type = GUI_INTERNAL_X11;
  1859. #else
  1860. m_hints &= ~PLUGIN_HAS_GUI;
  1861. #endif
  1862. }
  1863. }
  1864. return true;
  1865. }
  1866. private:
  1867. int unique1;
  1868. AEffect* effect;
  1869. struct {
  1870. int32_t numEvents;
  1871. intptr_t reserved;
  1872. VstEvent* data[MAX_MIDI_EVENTS*2];
  1873. } events;
  1874. VstMidiEvent midiEvents[MAX_MIDI_EVENTS*2];
  1875. uint32_t vstTimeOffset;
  1876. VstTimeInfo_R vstTimeInfo;
  1877. struct {
  1878. GuiType type;
  1879. bool visible;
  1880. int width;
  1881. int height;
  1882. } gui;
  1883. bool isProcessing;
  1884. bool needIdle;
  1885. static VstPlugin* lastVstPlugin;
  1886. int unique2;
  1887. };
  1888. VstPlugin* VstPlugin::lastVstPlugin = nullptr;
  1889. /**@}*/
  1890. CARLA_BACKEND_END_NAMESPACE
  1891. #else // WANT_VST
  1892. # warning Building without VST support
  1893. #endif
  1894. CARLA_BACKEND_START_NAMESPACE
  1895. CarlaPlugin* CarlaPlugin::newVST(const initializer& init)
  1896. {
  1897. qDebug("CarlaPlugin::newVST(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1898. #ifdef WANT_VST
  1899. short id = init.engine->getNewPluginId();
  1900. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  1901. {
  1902. setLastError("Maximum number of plugins reached");
  1903. return nullptr;
  1904. }
  1905. VstPlugin* const plugin = new VstPlugin(init.engine, id);
  1906. if (! plugin->init(init.filename, init.name, init.label))
  1907. {
  1908. delete plugin;
  1909. return nullptr;
  1910. }
  1911. plugin->reload();
  1912. # ifndef BUILD_BRIDGE
  1913. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1914. {
  1915. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  1916. {
  1917. setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  1918. delete plugin;
  1919. return nullptr;
  1920. }
  1921. }
  1922. # endif
  1923. plugin->registerToOscControl();
  1924. return plugin;
  1925. #else
  1926. setLastError("VST support not available");
  1927. return nullptr;
  1928. #endif
  1929. }
  1930. CARLA_BACKEND_END_NAMESPACE