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.

2562 lines
82KB

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