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.

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