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.

2458 lines
79KB

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