Audio plugin host https://kx.studio/carla
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.

2419 lines
76KB

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