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.

2462 lines
77KB

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