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.

2369 lines
77KB

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