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.

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