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.

2406 lines
75KB

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