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.

2355 lines
73KB

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