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.

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