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.

2484 lines
79KB

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