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.

2390 lines
77KB

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