Collection of tools useful for audio production
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.

2342 lines
72KB

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