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.

2426 lines
76KB

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