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.

2547 lines
81KB

  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 (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1047. {
  1048. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1049. continue;
  1050. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1051. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1052. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1053. fMidiEvents[fMidiEventCount].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1054. fMidiEvents[fMidiEventCount].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1055. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1056. fMidiEventCount += 1;
  1057. }
  1058. break;
  1059. case kEngineControlEventTypeAllNotesOff:
  1060. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1061. {
  1062. if (event.channel == kData->ctrlChannel && ! allNotesOffSent)
  1063. {
  1064. allNotesOffSent = true;
  1065. sendMidiAllNotesOffToCallback();
  1066. }
  1067. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1068. continue;
  1069. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1070. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1071. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1072. fMidiEvents[fMidiEventCount].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1073. fMidiEvents[fMidiEventCount].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1074. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1075. fMidiEventCount += 1;
  1076. }
  1077. break;
  1078. }
  1079. break;
  1080. }
  1081. case kEngineEventTypeMidi:
  1082. {
  1083. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1084. continue;
  1085. const EngineMidiEvent& midiEvent(event.midi);
  1086. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1087. uint8_t channel = event.channel;
  1088. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1089. continue;
  1090. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1091. continue;
  1092. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1093. continue;
  1094. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1095. continue;
  1096. // Fix bad note-off
  1097. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1098. status -= 0x10;
  1099. carla_zeroStruct<VstMidiEvent>(fMidiEvents[fMidiEventCount]);
  1100. fMidiEvents[fMidiEventCount].type = kVstMidiType;
  1101. fMidiEvents[fMidiEventCount].byteSize = sizeof(VstMidiEvent);
  1102. fMidiEvents[fMidiEventCount].midiData[0] = status + channel;
  1103. fMidiEvents[fMidiEventCount].midiData[1] = midiEvent.data[1];
  1104. fMidiEvents[fMidiEventCount].midiData[2] = midiEvent.data[2];
  1105. fMidiEvents[fMidiEventCount].deltaFrames = sampleAccurate ? startTime : time;
  1106. fMidiEventCount += 1;
  1107. if (status == MIDI_STATUS_NOTE_ON)
  1108. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1109. else if (status == MIDI_STATUS_NOTE_OFF)
  1110. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. kData->postRtEvents.trySplice();
  1116. if (frames > timeOffset)
  1117. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1118. } // End of Event Input and Processing
  1119. // --------------------------------------------------------------------------------------------------------
  1120. // Plugin processing (no events)
  1121. else
  1122. {
  1123. processSingle(inBuffer, outBuffer, frames, 0);
  1124. } // End of Plugin processing (no events)
  1125. CARLA_PROCESS_CONTINUE_CHECK;
  1126. // --------------------------------------------------------------------------------------------------------
  1127. // MIDI Output
  1128. if (kData->event.portOut != nullptr)
  1129. {
  1130. // reverse lookup MIDI events
  1131. for (k = (MAX_MIDI_EVENTS*2)-1; k >= fMidiEventCount; --k)
  1132. {
  1133. if (fMidiEvents[k].type == 0)
  1134. break;
  1135. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].midiData);
  1136. uint8_t midiData[3] = { 0 };
  1137. midiData[0] = fMidiEvents[k].midiData[0];
  1138. midiData[1] = fMidiEvents[k].midiData[1];
  1139. midiData[2] = fMidiEvents[k].midiData[2];
  1140. kData->event.portOut->writeMidiEvent(fMidiEvents[k].deltaFrames, channel, 0, midiData, 3);
  1141. }
  1142. } // End of Control and MIDI Output
  1143. }
  1144. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1145. {
  1146. CARLA_ASSERT(frames > 0);
  1147. if (frames == 0)
  1148. return false;
  1149. if (kData->audioIn.count > 0)
  1150. {
  1151. CARLA_ASSERT(inBuffer != nullptr);
  1152. if (inBuffer == nullptr)
  1153. return false;
  1154. }
  1155. if (kData->audioOut.count > 0)
  1156. {
  1157. CARLA_ASSERT(outBuffer != nullptr);
  1158. if (outBuffer == nullptr)
  1159. return false;
  1160. }
  1161. uint32_t i, k;
  1162. // --------------------------------------------------------------------------------------------------------
  1163. // Try lock, silence otherwise
  1164. if (kData->engine->isOffline())
  1165. {
  1166. kData->singleMutex.lock();
  1167. }
  1168. else if (! kData->singleMutex.tryLock())
  1169. {
  1170. for (i=0; i < kData->audioOut.count; ++i)
  1171. {
  1172. for (k=0; k < frames; ++k)
  1173. outBuffer[i][k+timeOffset] = 0.0f;
  1174. }
  1175. return false;
  1176. }
  1177. // --------------------------------------------------------------------------------------------------------
  1178. // Set audio buffers
  1179. float* vstInBuffer[kData->audioIn.count];
  1180. float* vstOutBuffer[kData->audioOut.count];
  1181. for (i=0; i < kData->audioIn.count; ++i)
  1182. vstInBuffer[i] = inBuffer[i]+timeOffset;
  1183. for (i=0; i < kData->audioOut.count; ++i)
  1184. vstOutBuffer[i] = outBuffer[i]+timeOffset;
  1185. // --------------------------------------------------------------------------------------------------------
  1186. // Set MIDI events
  1187. if (fMidiEventCount > 0)
  1188. {
  1189. fEvents.numEvents = fMidiEventCount;
  1190. fEvents.reserved = 0;
  1191. dispatcher(effProcessEvents, 0, 0, &fEvents, 0.0f);
  1192. }
  1193. // --------------------------------------------------------------------------------------------------------
  1194. // Run plugin
  1195. fIsProcessing = true;
  1196. if (fHints & PLUGIN_CAN_PROCESS_REPLACING)
  1197. {
  1198. fEffect->processReplacing(fEffect,
  1199. (kData->audioIn.count > 0) ? vstInBuffer : nullptr,
  1200. (kData->audioOut.count > 0) ? vstOutBuffer : nullptr,
  1201. frames);
  1202. }
  1203. else
  1204. {
  1205. for (i=0; i < kData->audioOut.count; ++i)
  1206. carla_zeroFloat(vstOutBuffer[i], frames);
  1207. #if ! VST_FORCE_DEPRECATED
  1208. fEffect->process(fEffect,
  1209. (kData->audioIn.count > 0) ? vstInBuffer : nullptr,
  1210. (kData->audioOut.count > 0) ? vstOutBuffer : nullptr,
  1211. frames);
  1212. #endif
  1213. }
  1214. fIsProcessing = false;
  1215. fTimeInfo.samplePos += frames;
  1216. #ifndef BUILD_BRIDGE
  1217. // --------------------------------------------------------------------------------------------------------
  1218. // Post-processing (dry/wet, volume and balance)
  1219. {
  1220. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) != 0 && kData->postProc.volume != 1.0f;
  1221. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1222. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1223. bool isPair;
  1224. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1225. for (i=0; i < kData->audioOut.count; ++i)
  1226. {
  1227. // Dry/Wet
  1228. if (doDryWet)
  1229. {
  1230. for (k=0; k < frames; ++k)
  1231. {
  1232. bufValue = inBuffer[(kData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  1233. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1234. }
  1235. }
  1236. // Balance
  1237. if (doBalance)
  1238. {
  1239. isPair = (i % 2 == 0);
  1240. if (isPair)
  1241. {
  1242. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1243. carla_copyFloat(oldBufLeft, outBuffer[i]+timeOffset, frames);
  1244. }
  1245. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1246. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1247. for (k=0; k < frames; ++k)
  1248. {
  1249. if (isPair)
  1250. {
  1251. // left
  1252. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  1253. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  1254. }
  1255. else
  1256. {
  1257. // right
  1258. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  1259. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  1260. }
  1261. }
  1262. }
  1263. // Volume
  1264. if (doVolume)
  1265. {
  1266. for (k=0; k < frames; ++k)
  1267. outBuffer[i][k+timeOffset] *= kData->postProc.volume;
  1268. }
  1269. }
  1270. } // End of Post-processing
  1271. #endif
  1272. // --------------------------------------------------------------------------------------------------------
  1273. kData->singleMutex.unlock();
  1274. return true;
  1275. }
  1276. void bufferSizeChanged(const uint32_t newBufferSize) override
  1277. {
  1278. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1279. carla_debug("VstPlugin::bufferSizeChanged(%i)", newBufferSize);
  1280. if (kData->active)
  1281. deactivate();
  1282. #if ! VST_FORCE_DEPRECATED
  1283. dispatcher(effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, kData->engine->getSampleRate());
  1284. #endif
  1285. dispatcher(effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
  1286. if (kData->active)
  1287. activate();
  1288. }
  1289. void sampleRateChanged(const double newSampleRate) override
  1290. {
  1291. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1292. carla_debug("VstPlugin::sampleRateChanged(%g)", newSampleRate);
  1293. if (kData->active)
  1294. deactivate();
  1295. #if ! VST_FORCE_DEPRECATED
  1296. dispatcher(effSetBlockSizeAndSampleRate, 0, kData->engine->getBufferSize(), nullptr, newSampleRate);
  1297. #endif
  1298. dispatcher(effSetSampleRate, 0, 0, nullptr, newSampleRate);
  1299. if (kData->active)
  1300. activate();
  1301. }
  1302. // -------------------------------------------------------------------
  1303. // Plugin buffers
  1304. // nothing
  1305. // -------------------------------------------------------------------
  1306. // Post-poned UI Stuff
  1307. void uiParameterChange(const uint32_t index, const float value) override
  1308. {
  1309. CARLA_ASSERT(index < kData->param.count);
  1310. if (index >= kData->param.count)
  1311. return;
  1312. if (! fGui.isOsc)
  1313. return;
  1314. if (kData->osc.data.target == nullptr)
  1315. return;
  1316. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1317. }
  1318. void uiProgramChange(const uint32_t index) override
  1319. {
  1320. CARLA_ASSERT(index < kData->prog.count);
  1321. if (index >= kData->prog.count)
  1322. return;
  1323. if (! fGui.isOsc)
  1324. return;
  1325. if (kData->osc.data.target == nullptr)
  1326. return;
  1327. osc_send_program(&kData->osc.data, index);
  1328. }
  1329. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1330. {
  1331. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1332. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1333. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1334. if (channel >= MAX_MIDI_CHANNELS)
  1335. return;
  1336. if (note >= MAX_MIDI_NOTE)
  1337. return;
  1338. if (velo >= MAX_MIDI_VALUE)
  1339. return;
  1340. if (! fGui.isOsc)
  1341. return;
  1342. if (kData->osc.data.target == nullptr)
  1343. return;
  1344. uint8_t midiData[4] = { 0 };
  1345. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1346. midiData[2] = note;
  1347. midiData[3] = velo;
  1348. osc_send_midi(&kData->osc.data, midiData);
  1349. }
  1350. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1351. {
  1352. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1353. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1354. if (channel >= MAX_MIDI_CHANNELS)
  1355. return;
  1356. if (note >= MAX_MIDI_NOTE)
  1357. return;
  1358. if (! fGui.isOsc)
  1359. return;
  1360. if (kData->osc.data.target == nullptr)
  1361. return;
  1362. uint8_t midiData[4] = { 0 };
  1363. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1364. midiData[2] = note;
  1365. osc_send_midi(&kData->osc.data, midiData);
  1366. }
  1367. // -------------------------------------------------------------------
  1368. protected:
  1369. void guiClosedCallback() override
  1370. {
  1371. showGui(false);
  1372. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1373. }
  1374. intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1375. {
  1376. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1377. if (opcode != effEditIdle && opcode != effProcessEvents)
  1378. carla_debug("VstPlugin::dispatcher(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstEffectOpcode2str(opcode), index, value, ptr, opt);
  1379. #endif
  1380. CARLA_ASSERT(fEffect != nullptr);
  1381. return (fEffect != nullptr) ? fEffect->dispatcher(fEffect, opcode, index, value, ptr, opt) : 0;
  1382. }
  1383. intptr_t handleAudioMasterCallback(const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1384. {
  1385. #if 0
  1386. // Cockos VST extensions
  1387. if (ptr != nullptr && static_cast<uint32_t>(opcode) == 0xdeadbeef && static_cast<uint32_t>(index) == 0xdeadf00d)
  1388. {
  1389. const char* const func = (char*)ptr;
  1390. if (std::strcmp(func, "GetPlayPosition") == 0)
  1391. return 0;
  1392. if (std::strcmp(func, "GetPlayPosition2") == 0)
  1393. return 0;
  1394. if (std::strcmp(func, "GetCursorPosition") == 0)
  1395. return 0;
  1396. if (std::strcmp(func, "GetPlayState") == 0)
  1397. return 0;
  1398. if (std::strcmp(func, "SetEditCurPos") == 0)
  1399. return 0;
  1400. if (std::strcmp(func, "GetSetRepeat") == 0)
  1401. return 0;
  1402. if (std::strcmp(func, "GetProjectPath") == 0)
  1403. return 0;
  1404. if (std::strcmp(func, "OnPlayButton") == 0)
  1405. return 0;
  1406. if (std::strcmp(func, "OnStopButton") == 0)
  1407. return 0;
  1408. if (std::strcmp(func, "OnPauseButton") == 0)
  1409. return 0;
  1410. if (std::strcmp(func, "IsInRealTimeAudio") == 0)
  1411. return 0;
  1412. if (std::strcmp(func, "Audio_IsRunning") == 0)
  1413. return 0;
  1414. }
  1415. #endif
  1416. intptr_t ret = 0;
  1417. switch (opcode)
  1418. {
  1419. case audioMasterAutomate:
  1420. if (! fEnabled)
  1421. break;
  1422. // plugins should never do this:
  1423. CARLA_SAFE_ASSERT_INT(index < static_cast<int32_t>(kData->param.count), index);
  1424. if (index < 0 || index >= static_cast<int32_t>(kData->param.count))
  1425. break;
  1426. if (fGui.isVisible && ! fIsProcessing)
  1427. {
  1428. // Called from GUI
  1429. setParameterValue(index, opt, false, true, true);
  1430. }
  1431. else if (fIsProcessing)
  1432. {
  1433. // Called from engine
  1434. const float fixedValue(kData->param.fixValue(index, opt));
  1435. if (kData->engine->isOffline())
  1436. {
  1437. CarlaPlugin::setParameterValue(index, fixedValue, true, true, true);
  1438. }
  1439. else
  1440. {
  1441. CarlaPlugin::setParameterValue(index, fixedValue, false, false, false);
  1442. postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, fixedValue);
  1443. }
  1444. }
  1445. else
  1446. {
  1447. carla_stdout("audioMasterAutomate called from unknown source");
  1448. }
  1449. break;
  1450. case audioMasterCurrentId:
  1451. // TODO
  1452. // if using old sdk, return effect->uniqueID
  1453. break;
  1454. case audioMasterIdle:
  1455. if (fGui.isVisible)
  1456. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1457. break;
  1458. #if ! VST_FORCE_DEPRECATED
  1459. case audioMasterPinConnected:
  1460. // Deprecated in VST SDK 2.4
  1461. // TODO
  1462. break;
  1463. case audioMasterWantMidi:
  1464. // Deprecated in VST SDK 2.4
  1465. fHints |= PLUGIN_WANTS_MIDI_INPUT;
  1466. break;
  1467. #endif
  1468. case audioMasterGetTime:
  1469. #ifdef VESTIGE_HEADER
  1470. ret = getAddressFromPointer(&fTimeInfo);
  1471. #else
  1472. ret = ToVstPtr<VstTimeInfo_R>(&fTimeInfo);
  1473. #endif
  1474. break;
  1475. case audioMasterProcessEvents:
  1476. CARLA_ASSERT(fEnabled);
  1477. CARLA_ASSERT(fIsProcessing);
  1478. CARLA_ASSERT(kData->event.portOut != nullptr);
  1479. CARLA_ASSERT(ptr != nullptr);
  1480. if (! fEnabled)
  1481. return 0;
  1482. if (! fIsProcessing)
  1483. return 0;
  1484. if (kData->event.portOut == nullptr)
  1485. return 0;
  1486. if (ptr == nullptr)
  1487. return 0;
  1488. if (! fIsProcessing)
  1489. {
  1490. carla_stderr2("audioMasterProcessEvents(%p) - received MIDI out events outside audio thread, ignoring", ptr);
  1491. return 0;
  1492. }
  1493. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1494. return 0;
  1495. {
  1496. const VstEvents* const vstEvents((const VstEvents*)ptr);
  1497. for (int32_t i=0; i < vstEvents->numEvents && i < MAX_MIDI_EVENTS*2; ++i)
  1498. {
  1499. if (vstEvents->events[i] == nullptr)
  1500. break;
  1501. const VstMidiEvent* const vstMidiEvent = (const VstMidiEvent*)vstEvents->events[i];
  1502. if (vstMidiEvent->type != kVstMidiType)
  1503. continue;
  1504. // reverse-find first free event, and put it there
  1505. for (uint32_t j=(MAX_MIDI_EVENTS*2)-1; j >= fMidiEventCount; --j)
  1506. {
  1507. if (fMidiEvents[j].type == 0)
  1508. {
  1509. std::memcpy(&fMidiEvents[j], vstMidiEvent, sizeof(VstMidiEvent));
  1510. break;
  1511. }
  1512. }
  1513. }
  1514. }
  1515. ret = 1;
  1516. break;
  1517. #if ! VST_FORCE_DEPRECATED
  1518. case audioMasterSetTime:
  1519. // Deprecated in VST SDK 2.4
  1520. break;
  1521. case audioMasterTempoAt:
  1522. // Deprecated in VST SDK 2.4
  1523. CARLA_ASSERT(fIsProcessing);
  1524. ret = fTimeInfo.tempo * 10000;
  1525. break;
  1526. case audioMasterGetNumAutomatableParameters:
  1527. // Deprecated in VST SDK 2.4
  1528. ret = carla_min<intptr_t>(0, fEffect->numParams, kData->engine->getOptions().maxParameters);
  1529. break;
  1530. case audioMasterGetParameterQuantization:
  1531. // Deprecated in VST SDK 2.4
  1532. ret = 1; // full single float precision
  1533. break;
  1534. #endif
  1535. #if 0
  1536. case audioMasterIOChanged:
  1537. CARLA_ASSERT(fEnabled);
  1538. // TESTING
  1539. if (! fEnabled)
  1540. {
  1541. ret = 1;
  1542. break;
  1543. }
  1544. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1545. {
  1546. carla_stderr2("VstPlugin::handleAudioMasterIOChanged() - plugin asked IO change, but it's not supported in rack mode");
  1547. return 0;
  1548. }
  1549. engineProcessLock();
  1550. m_enabled = false;
  1551. engineProcessUnlock();
  1552. if (m_active)
  1553. {
  1554. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1555. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1556. }
  1557. reload();
  1558. if (m_active)
  1559. {
  1560. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1561. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1562. }
  1563. x_engine->callback(CALLBACK_RELOAD_ALL, m_id, 0, 0, 0.0, nullptr);
  1564. ret = 1;
  1565. break;
  1566. #endif
  1567. case audioMasterNeedIdle:
  1568. // Deprecated in VST SDK 2.4
  1569. fNeedIdle = true;
  1570. ret = 1;
  1571. break;
  1572. case audioMasterSizeWindow:
  1573. if (kData->gui != nullptr)
  1574. {
  1575. CARLA_SAFE_ASSERT(fGui.isVisible);
  1576. if (fGui.isVisible)
  1577. kData->gui->setSize(index, value);
  1578. ret = 1;
  1579. }
  1580. break;
  1581. case audioMasterGetSampleRate:
  1582. ret = kData->engine->getSampleRate();
  1583. break;
  1584. case audioMasterGetBlockSize:
  1585. ret = kData->engine->getBufferSize();
  1586. break;
  1587. case audioMasterGetInputLatency:
  1588. ret = 0;
  1589. break;
  1590. case audioMasterGetOutputLatency:
  1591. ret = 0;
  1592. break;
  1593. #if ! VST_FORCE_DEPRECATED
  1594. case audioMasterGetPreviousPlug:
  1595. // Deprecated in VST SDK 2.4
  1596. // TODO
  1597. break;
  1598. case audioMasterGetNextPlug:
  1599. // Deprecated in VST SDK 2.4
  1600. // TODO
  1601. break;
  1602. case audioMasterWillReplaceOrAccumulate:
  1603. // Deprecated in VST SDK 2.4
  1604. ret = 1; // replace
  1605. break;
  1606. #endif
  1607. case audioMasterGetCurrentProcessLevel:
  1608. if (kData->engine->isOffline())
  1609. ret = kVstProcessLevelOffline;
  1610. else if (fIsProcessing)
  1611. ret = kVstProcessLevelRealtime;
  1612. else
  1613. ret = kVstProcessLevelUser;
  1614. break;
  1615. case audioMasterGetAutomationState:
  1616. ret = kData->active ? kVstAutomationReadWrite : kVstAutomationOff;
  1617. break;
  1618. case audioMasterOfflineStart:
  1619. case audioMasterOfflineRead:
  1620. case audioMasterOfflineWrite:
  1621. case audioMasterOfflineGetCurrentPass:
  1622. case audioMasterOfflineGetCurrentMetaPass:
  1623. // TODO
  1624. break;
  1625. #if ! VST_FORCE_DEPRECATED
  1626. case audioMasterSetOutputSampleRate:
  1627. // Deprecated in VST SDK 2.4
  1628. break;
  1629. case audioMasterGetOutputSpeakerArrangement:
  1630. // Deprecated in VST SDK 2.4
  1631. // TODO
  1632. break;
  1633. #endif
  1634. case audioMasterVendorSpecific:
  1635. // TODO - cockos extensions
  1636. break;
  1637. #if ! VST_FORCE_DEPRECATED
  1638. case audioMasterSetIcon:
  1639. // Deprecated in VST SDK 2.4
  1640. break;
  1641. #endif
  1642. #if ! VST_FORCE_DEPRECATED
  1643. case audioMasterOpenWindow:
  1644. case audioMasterCloseWindow:
  1645. // Deprecated in VST SDK 2.4
  1646. // TODO
  1647. break;
  1648. #endif
  1649. case audioMasterGetDirectory:
  1650. // TODO
  1651. break;
  1652. case audioMasterUpdateDisplay:
  1653. // Idle UI if visible
  1654. if (fGui.isVisible)
  1655. dispatcher(effEditIdle, 0, 0, nullptr, 0.0f);
  1656. // Update current program
  1657. if (kData->prog.count > 0)
  1658. {
  1659. const int32_t current = dispatcher(effGetProgram, 0, 0, nullptr, 0.0f);
  1660. if (current >= 0 && current < static_cast<int32_t>(kData->prog.count))
  1661. {
  1662. char strBuf[STR_MAX+1] = { '\0' };
  1663. dispatcher(effGetProgramName, 0, 0, strBuf, 0.0f);
  1664. if (kData->prog.names[current] != nullptr)
  1665. delete[] kData->prog.names[current];
  1666. kData->prog.names[current] = carla_strdup(strBuf);
  1667. if (kData->prog.current != current)
  1668. {
  1669. kData->prog.current = current;
  1670. kData->engine->callback(CALLBACK_PROGRAM_CHANGED, fId, current, 0, 0.0f, nullptr);
  1671. }
  1672. }
  1673. }
  1674. kData->engine->callback(CALLBACK_UPDATE, fId, 0, 0, 0.0f, nullptr);
  1675. ret = 1;
  1676. break;
  1677. case audioMasterBeginEdit:
  1678. case audioMasterEndEdit:
  1679. // TODO
  1680. break;
  1681. case audioMasterOpenFileSelector:
  1682. case audioMasterCloseFileSelector:
  1683. // TODO
  1684. break;
  1685. #if ! VST_FORCE_DEPRECATED
  1686. case audioMasterEditFile:
  1687. // Deprecated in VST SDK 2.4
  1688. // TODO
  1689. break;
  1690. case audioMasterGetChunkFile:
  1691. // Deprecated in VST SDK 2.4
  1692. // TODO
  1693. break;
  1694. case audioMasterGetInputSpeakerArrangement:
  1695. // Deprecated in VST SDK 2.4
  1696. // TODO
  1697. break;
  1698. #endif
  1699. default:
  1700. carla_debug("VstPlugin::handleAudioMasterCallback(%02i:%s, %i, " P_INTPTR ", %p, %f)", opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1701. break;
  1702. }
  1703. return ret;
  1704. }
  1705. // -------------------------------------------------------------------
  1706. public:
  1707. bool init(const char* const filename, const char* const name)
  1708. {
  1709. CARLA_ASSERT(kData->engine != nullptr);
  1710. CARLA_ASSERT(kData->client == nullptr);
  1711. CARLA_ASSERT(filename != nullptr);
  1712. // ---------------------------------------------------------------
  1713. // first checks
  1714. if (kData->engine == nullptr)
  1715. {
  1716. return false;
  1717. }
  1718. if (kData->client != nullptr)
  1719. {
  1720. kData->engine->setLastError("Plugin client is already registered");
  1721. return false;
  1722. }
  1723. if (filename == nullptr)
  1724. {
  1725. kData->engine->setLastError("null filename");
  1726. return false;
  1727. }
  1728. // ---------------------------------------------------------------
  1729. // open DLL
  1730. if (! kData->libOpen(filename))
  1731. {
  1732. kData->engine->setLastError(kData->libError(filename));
  1733. return false;
  1734. }
  1735. // ---------------------------------------------------------------
  1736. // get DLL main entry
  1737. VST_Function vstFn = (VST_Function)kData->libSymbol("VSTPluginMain");
  1738. if (vstFn == nullptr)
  1739. {
  1740. vstFn = (VST_Function)kData->libSymbol("main");
  1741. if (vstFn == nullptr)
  1742. {
  1743. kData->engine->setLastError("Could not find the VST main entry in the plugin library");
  1744. return false;
  1745. }
  1746. }
  1747. // ---------------------------------------------------------------
  1748. // initialize plugin (part 1)
  1749. sLastVstPlugin = this;
  1750. fEffect = vstFn(carla_vst_audioMasterCallback);
  1751. sLastVstPlugin = nullptr;
  1752. if (fEffect == nullptr)
  1753. {
  1754. kData->engine->setLastError("Plugin failed to initialize");
  1755. return false;
  1756. }
  1757. if (fEffect->magic != kEffectMagic)
  1758. {
  1759. kData->engine->setLastError("Plugin is not valid (wrong vst effect magic code)");
  1760. return false;
  1761. }
  1762. #ifdef VESTIGE_HEADER
  1763. fEffect->ptr1 = this;
  1764. #else
  1765. fEffect->resvd1 = ToVstPtr<VstPlugin>(this);
  1766. #endif
  1767. dispatcher(effOpen, 0, 0, nullptr, 0.0f);
  1768. // ---------------------------------------------------------------
  1769. // get info
  1770. if (name != nullptr)
  1771. {
  1772. fName = kData->engine->getUniquePluginName(name);
  1773. }
  1774. else
  1775. {
  1776. char strBuf[STR_MAX+1] = { '\0' };
  1777. dispatcher(effGetEffectName, 0, 0, strBuf, 0.0f);
  1778. if (strBuf[0] != '\0')
  1779. {
  1780. fName = kData->engine->getUniquePluginName(strBuf);
  1781. }
  1782. else
  1783. {
  1784. const char* const label = std::strrchr(filename, OS_SEP)+1;
  1785. fName = kData->engine->getUniquePluginName(label);
  1786. }
  1787. }
  1788. fFilename = filename;
  1789. // ---------------------------------------------------------------
  1790. // register client
  1791. kData->client = kData->engine->addClient(this);
  1792. if (kData->client == nullptr || ! kData->client->isOk())
  1793. {
  1794. kData->engine->setLastError("Failed to register plugin client");
  1795. return false;
  1796. }
  1797. // ---------------------------------------------------------------
  1798. // initialize plugin (part 2)
  1799. #if ! VST_FORCE_DEPRECATED
  1800. dispatcher(effSetBlockSizeAndSampleRate, 0, kData->engine->getBufferSize(), nullptr, kData->engine->getSampleRate());
  1801. #endif
  1802. dispatcher(effSetSampleRate, 0, 0, nullptr, kData->engine->getSampleRate());
  1803. dispatcher(effSetBlockSize, 0, kData->engine->getBufferSize(), nullptr, 0.0f);
  1804. dispatcher(effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1805. if (dispatcher(effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1806. fHints |= PLUGIN_USES_OLD_VSTSDK;
  1807. if (static_cast<uintptr_t>(dispatcher(effCanDo, 0, 0, (void*)"hasCockosExtensions", 0.0f)) == 0xbeef0000)
  1808. fHints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1809. // ---------------------------------------------------------------
  1810. // gui stuff
  1811. if (fEffect->flags & effFlagsHasEditor)
  1812. {
  1813. const EngineOptions& engineOptions(kData->engine->getOptions());
  1814. #if defined(Q_WS_X11)
  1815. CarlaString uiBridgeBinary(engineOptions.bridge_vstX11);
  1816. #elif defined(CARLA_OS_MAC)
  1817. CarlaString uiBridgeBinary(engineOptions.bridge_vstCocoa);
  1818. #elif defined(CARLA_OS_WIN)
  1819. CarlaString uiBridgeBinary(engineOptions.bridge_vstHWND);
  1820. #else
  1821. CarlaString uiBridgeBinary;
  1822. #endif
  1823. if (engineOptions.preferUiBridges && uiBridgeBinary.isNotEmpty() && (fEffect->flags & effFlagsProgramChunks) == 0)
  1824. {
  1825. kData->osc.thread.setOscData(uiBridgeBinary, nullptr);
  1826. fGui.isOsc = true;
  1827. }
  1828. }
  1829. // ---------------------------------------------------------------
  1830. // load plugin settings
  1831. {
  1832. // set default options
  1833. fOptions = 0x0;
  1834. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1835. if (midiInCount() > 0)
  1836. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1837. if (fEffect->flags & effFlagsProgramChunks)
  1838. fOptions |= PLUGIN_OPTION_USE_CHUNKS;
  1839. if (vstPluginCanDo(fEffect, "receiveVstEvents") || vstPluginCanDo(fEffect, "receiveVstMidiEvent") || (fEffect->flags & effFlagsIsSynth) > 0 || (fHints & PLUGIN_WANTS_MIDI_INPUT))
  1840. {
  1841. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1842. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1843. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1844. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1845. }
  1846. // load settings
  1847. kData->idStr = "VST/";
  1848. kData->idStr += std::strrchr(filename, OS_SEP)+1; // FIXME!
  1849. kData->idStr += "/";
  1850. kData->idStr += CarlaString(uniqueId());
  1851. fOptions = kData->loadSettings(fOptions, availableOptions());
  1852. // ignore settings, we need this anyway
  1853. if (midiInCount() > 0)
  1854. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1855. }
  1856. return true;
  1857. }
  1858. private:
  1859. int fUnique1;
  1860. AEffect* fEffect;
  1861. void* fLastChunk;
  1862. uint32_t fMidiEventCount;
  1863. VstMidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1864. VstTimeInfo_R fTimeInfo;
  1865. struct FixedVstEvents {
  1866. int32_t numEvents;
  1867. intptr_t reserved;
  1868. VstEvent* data[MAX_MIDI_EVENTS*2];
  1869. FixedVstEvents()
  1870. : numEvents(0),
  1871. reserved(0),
  1872. data{nullptr} {}
  1873. } fEvents;
  1874. struct GuiInfo {
  1875. bool isOsc;
  1876. bool isVisible;
  1877. int lastWidth;
  1878. int lastHeight;
  1879. GuiInfo()
  1880. : isOsc(false),
  1881. isVisible(false),
  1882. lastWidth(0),
  1883. lastHeight(0) {}
  1884. } fGui;
  1885. bool fIsProcessing;
  1886. bool fNeedIdle;
  1887. int fUnique2;
  1888. static VstPlugin* sLastVstPlugin;
  1889. // -------------------------------------------------------------------
  1890. static intptr_t carla_vst_hostCanDo(const char* const feature)
  1891. {
  1892. carla_debug("carla_vst_hostCanDo(\"%s\")", feature);
  1893. if (std::strcmp(feature, "supplyIdle") == 0)
  1894. return 1;
  1895. if (std::strcmp(feature, "sendVstEvents") == 0)
  1896. return 1;
  1897. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  1898. return 1;
  1899. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  1900. return 1;
  1901. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  1902. return 1;
  1903. if (std::strcmp(feature, "receiveVstEvents") == 0)
  1904. return 1;
  1905. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  1906. return 1;
  1907. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  1908. return -1;
  1909. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  1910. return -1;
  1911. if (std::strcmp(feature, "acceptIOChanges") == 0)
  1912. return 1;
  1913. if (std::strcmp(feature, "sizeWindow") == 0)
  1914. return 1;
  1915. if (std::strcmp(feature, "offline") == 0)
  1916. return -1;
  1917. if (std::strcmp(feature, "openFileSelector") == 0)
  1918. return -1;
  1919. if (std::strcmp(feature, "closeFileSelector") == 0)
  1920. return -1;
  1921. if (std::strcmp(feature, "startStopProcess") == 0)
  1922. return 1;
  1923. if (std::strcmp(feature, "supportShell") == 0)
  1924. return -1;
  1925. if (std::strcmp(feature, "shellCategory") == 0)
  1926. return -1;
  1927. // unimplemented
  1928. carla_stderr("carla_vst_hostCanDo(\"%s\") - unknown feature", feature);
  1929. return 0;
  1930. }
  1931. static intptr_t VSTCALLBACK carla_vst_audioMasterCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1932. {
  1933. #if defined(DEBUG) && ! defined(CARLA_OS_WIN)
  1934. if (opcode != audioMasterGetTime && opcode != audioMasterProcessEvents && opcode != audioMasterGetCurrentProcessLevel && opcode != audioMasterGetOutputLatency)
  1935. carla_debug("carla_vst_audioMasterCallback(%p, %02i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  1936. #endif
  1937. switch (opcode)
  1938. {
  1939. case audioMasterVersion:
  1940. return kVstVersion;
  1941. case audioMasterGetVendorString:
  1942. CARLA_ASSERT(ptr != nullptr);
  1943. if (ptr != nullptr)
  1944. {
  1945. std::strcpy((char*)ptr, "falkTX");
  1946. return 1;
  1947. }
  1948. else
  1949. {
  1950. carla_stderr("carla_vst_audioMasterCallback() - audioMasterGetVendorString called with invalid pointer");
  1951. return 0;
  1952. }
  1953. case audioMasterGetProductString:
  1954. CARLA_ASSERT(ptr != nullptr);
  1955. if (ptr != nullptr)
  1956. {
  1957. std::strcpy((char*)ptr, "Carla");
  1958. return 1;
  1959. }
  1960. else
  1961. {
  1962. carla_stderr("carla_vst_audioMasterCallback() - audioMasterGetProductString called with invalid pointer");
  1963. return 0;
  1964. }
  1965. case audioMasterGetVendorVersion:
  1966. return 0x1000; // 1.0.0
  1967. case audioMasterCanDo:
  1968. CARLA_ASSERT(ptr != nullptr);
  1969. if (ptr != nullptr)
  1970. {
  1971. return carla_vst_hostCanDo((const char*)ptr);
  1972. }
  1973. else
  1974. {
  1975. carla_stderr("carla_vst_audioMasterCallback() - audioMasterCanDo called with invalid pointer");
  1976. return 0;
  1977. }
  1978. case audioMasterGetLanguage:
  1979. return kVstLangEnglish;
  1980. }
  1981. // Check if 'resvd1' points to us, otherwise register ourselfs if possible
  1982. VstPlugin* self = nullptr;
  1983. if (effect != nullptr)
  1984. {
  1985. #ifdef VESTIGE_HEADER
  1986. if (effect->ptr1 != nullptr)
  1987. {
  1988. self = (VstPlugin*)effect->ptr1;
  1989. #else
  1990. if (effect->resvd1 != 0)
  1991. {
  1992. self = FromVstPtr<VstPlugin>(effect->resvd1);
  1993. #endif
  1994. if (self->fUnique1 != self->fUnique2)
  1995. self = nullptr;
  1996. }
  1997. if (self != nullptr)
  1998. {
  1999. if (self->fEffect == nullptr)
  2000. self->fEffect = effect;
  2001. if (self->fEffect != effect)
  2002. {
  2003. carla_stderr2("carla_vst_audioMasterCallback() - host pointer mismatch: %p != %p", self->fEffect, effect);
  2004. self = nullptr;
  2005. }
  2006. }
  2007. else if (sLastVstPlugin != nullptr)
  2008. {
  2009. #ifdef VESTIGE_HEADER
  2010. effect->ptr1 = sLastVstPlugin;
  2011. #else
  2012. effect->resvd1 = ToVstPtr<VstPlugin>(sLastVstPlugin);
  2013. #endif
  2014. self = sLastVstPlugin;
  2015. }
  2016. }
  2017. return (self != nullptr) ? self->handleAudioMasterCallback(opcode, index, value, ptr, opt) : 0;
  2018. }
  2019. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(VstPlugin)
  2020. };
  2021. VstPlugin* VstPlugin::sLastVstPlugin = nullptr;
  2022. CARLA_BACKEND_END_NAMESPACE
  2023. #else // WANT_VST
  2024. # warning Building without VST support
  2025. #endif
  2026. CARLA_BACKEND_START_NAMESPACE
  2027. CarlaPlugin* CarlaPlugin::newVST(const Initializer& init)
  2028. {
  2029. carla_debug("CarlaPlugin::newVST({%p, \"%s\", \"%s\"})", init.engine, init.filename, init.name);
  2030. #ifdef WANT_VST
  2031. VstPlugin* const plugin(new VstPlugin(init.engine, init.id));
  2032. if (! plugin->init(init.filename, init.name))
  2033. {
  2034. delete plugin;
  2035. return nullptr;
  2036. }
  2037. plugin->reload();
  2038. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  2039. {
  2040. init.engine->setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  2041. delete plugin;
  2042. return nullptr;
  2043. }
  2044. return plugin;
  2045. #else
  2046. init.engine->setLastError("VST support not available");
  2047. return nullptr;
  2048. #endif
  2049. }
  2050. CARLA_BACKEND_END_NAMESPACE