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.

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