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.

2387 lines
74KB

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