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.

2428 lines
76KB

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