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.

2573 lines
83KB

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