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.

2297 lines
73KB

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