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.

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