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.

2449 lines
76KB

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