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.

2422 lines
78KB

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