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.

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