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.

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