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.

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