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.

1837 lines
59KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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. #include "carla_vst.h"
  19. #ifndef __WINE__
  20. #include <QtGui/QDialog>
  21. #endif
  22. CARLA_BACKEND_START_NAMESPACE
  23. #if 0
  24. } /* adjust editor indent */
  25. #endif
  26. /*!
  27. * @defgroup CarlaBackendVstPlugin Carla Backend VST Plugin
  28. *
  29. * The Carla Backend VST Plugin.
  30. * @{
  31. */
  32. /*!
  33. * @defgroup PluginHints Plugin Hints
  34. * @{
  35. */
  36. const unsigned int PLUGIN_CAN_PROCESS_REPLACING = 0x100; //!< VST Plugin cas use processReplacing()
  37. const unsigned int PLUGIN_HAS_COCKOS_EXTENSIONS = 0x200; //!< VST Plugin has Cockos extensions
  38. const unsigned int PLUGIN_USES_OLD_VSTSDK = 0x400; //!< VST Plugin uses an old VST SDK
  39. const unsigned int PLUGIN_WANTS_MIDI_INPUT = 0x800; //!< VST Plugin wants MIDI input
  40. /**@}*/
  41. class VstPlugin : public CarlaPlugin
  42. {
  43. public:
  44. VstPlugin(CarlaEngine* const engine, unsigned short id) : CarlaPlugin(engine, id)
  45. {
  46. qDebug("VstPlugin::VstPlugin()");
  47. m_type = PLUGIN_VST;
  48. effect = nullptr;
  49. events.numEvents = 0;
  50. events.reserved = 0;
  51. gui.visible = false;
  52. gui.width = 0;
  53. gui.height = 0;
  54. memset(midiEvents, 0, sizeof(VstMidiEvent)*MAX_MIDI_EVENTS*2);
  55. for (unsigned short i=0; i < MAX_MIDI_EVENTS*2; i++)
  56. events.data[i] = (VstEvent*)&midiEvents[i];
  57. // make plugin valid
  58. unique1 = unique2 = rand();
  59. }
  60. ~VstPlugin()
  61. {
  62. qDebug("VstPlugin::~VstPlugin()");
  63. // plugin is no longer valid
  64. unique1 = 0;
  65. unique2 = 1;
  66. if (effect)
  67. {
  68. // close UI
  69. if (m_hints & PLUGIN_HAS_GUI)
  70. effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
  71. if (m_activeBefore)
  72. {
  73. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  74. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  75. }
  76. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  77. }
  78. }
  79. // -------------------------------------------------------------------
  80. // Information (base)
  81. PluginCategory category()
  82. {
  83. intptr_t VstCategory = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
  84. switch (VstCategory)
  85. {
  86. case kPlugCategSynth:
  87. return PLUGIN_CATEGORY_SYNTH;
  88. case kPlugCategAnalysis:
  89. return PLUGIN_CATEGORY_UTILITY;
  90. case kPlugCategMastering:
  91. return PLUGIN_CATEGORY_DYNAMICS;
  92. case kPlugCategRoomFx:
  93. return PLUGIN_CATEGORY_DELAY;
  94. case kPlugCategRestoration:
  95. return PLUGIN_CATEGORY_UTILITY;
  96. case kPlugCategGenerator:
  97. return PLUGIN_CATEGORY_SYNTH;
  98. }
  99. if (effect->flags & effFlagsIsSynth)
  100. return PLUGIN_CATEGORY_SYNTH;
  101. return getPluginCategoryFromName(m_name);
  102. }
  103. long uniqueId()
  104. {
  105. return effect->uniqueID;
  106. }
  107. // -------------------------------------------------------------------
  108. // Information (current data)
  109. int32_t chunkData(void** const dataPtr)
  110. {
  111. Q_ASSERT(dataPtr);
  112. return effect->dispatcher(effect, effGetChunk, 0 /* bank */, 0, dataPtr, 0.0f);
  113. }
  114. // -------------------------------------------------------------------
  115. // Information (per-plugin data)
  116. double getParameterValue(uint32_t parameterId)
  117. {
  118. Q_ASSERT(parameterId < param.count);
  119. return effect->getParameter(effect, parameterId);
  120. }
  121. void getLabel(char* const strBuf)
  122. {
  123. effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f);
  124. }
  125. void getMaker(char* const strBuf)
  126. {
  127. effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
  128. }
  129. void getCopyright(char* const strBuf)
  130. {
  131. effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
  132. }
  133. void getRealName(char* const strBuf)
  134. {
  135. effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
  136. }
  137. void getParameterName(uint32_t parameterId, char* const strBuf)
  138. {
  139. Q_ASSERT(parameterId < param.count);
  140. effect->dispatcher(effect, effGetParamName, parameterId, 0, strBuf, 0.0f);
  141. }
  142. void getParameterUnit(uint32_t parameterId, char* const strBuf)
  143. {
  144. Q_ASSERT(parameterId < param.count);
  145. effect->dispatcher(effect, effGetParamLabel, parameterId, 0, strBuf, 0.0f);
  146. }
  147. void getParameterText(uint32_t parameterId, char* const strBuf)
  148. {
  149. Q_ASSERT(parameterId < param.count);
  150. effect->dispatcher(effect, effGetParamDisplay, parameterId, 0, strBuf, 0.0f);
  151. if (*strBuf == 0)
  152. sprintf(strBuf, "%f", getParameterValue(parameterId));
  153. }
  154. void getGuiInfo(GuiType* type, bool* resizable)
  155. {
  156. if (effect->flags & effFlagsHasEditor)
  157. {
  158. #if defined(Q_OS_WIN)
  159. *type = GUI_INTERNAL_HWND;
  160. #elif defined(Q_OS_MACOS)
  161. *type = GUI_INTERNAL_COCOA;
  162. #else
  163. *type = GUI_INTERNAL_X11;
  164. #endif
  165. }
  166. else
  167. *type = GUI_NONE;
  168. *resizable = false;
  169. }
  170. // -------------------------------------------------------------------
  171. // Set data (plugin-specific stuff)
  172. void setParameterValue(uint32_t parameterId, double value, bool sendGui, bool sendOsc, bool sendCallback)
  173. {
  174. Q_ASSERT(parameterId < param.count);
  175. effect->setParameter(effect, parameterId, fixParameterValue(value, param.ranges[parameterId]));
  176. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  177. }
  178. void setChunkData(const char* const stringData)
  179. {
  180. Q_ASSERT(stringData);
  181. static QByteArray chunk;
  182. chunk = QByteArray::fromBase64(stringData);
  183. if (x_engine->isOffline())
  184. {
  185. const CarlaEngine::ScopedLocker m(x_engine);
  186. effect->dispatcher(effect, effSetChunk, 0 /* bank */, chunk.size(), chunk.data(), 0.0f);
  187. }
  188. else
  189. {
  190. const CarlaPlugin::ScopedDisabler m(this);
  191. effect->dispatcher(effect, effSetChunk, 0 /* bank */, chunk.size(), chunk.data(), 0.0f);
  192. }
  193. }
  194. void setProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  195. {
  196. Q_ASSERT(index < (int32_t)prog.count);
  197. if (index >= 0)
  198. {
  199. if (x_engine->isOffline())
  200. {
  201. const CarlaEngine::ScopedLocker m(x_engine, block);
  202. effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
  203. }
  204. else
  205. {
  206. const ScopedDisabler m(this, block);
  207. effect->dispatcher(effect, effSetProgram, 0, index, nullptr, 0.0f);
  208. }
  209. }
  210. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, block);
  211. }
  212. // -------------------------------------------------------------------
  213. // Set gui stuff
  214. void setGuiData(int data, GuiDataHandle handle)
  215. {
  216. #ifdef __WINE__
  217. if (effect->dispatcher(effect, effEditOpen, 0, data, handle, 0.0f) == 1)
  218. #else
  219. QDialog* dialog = handle;
  220. if (effect->dispatcher(effect, effEditOpen, 0, data, (void*)dialog->winId(), 0.0f) == 1)
  221. #endif
  222. {
  223. ERect* vst_rect;
  224. if (effect->dispatcher(effect, effEditGetRect, 0, 0, &vst_rect, 0.0f))
  225. {
  226. int width = vst_rect->right - vst_rect->left;
  227. int height = vst_rect->bottom - vst_rect->top;
  228. if (width <= 0 || height <= 0)
  229. {
  230. qCritical("Failed to get proper Plugin Window size");
  231. return;
  232. }
  233. gui.width = width;
  234. gui.height = height;
  235. }
  236. else
  237. qCritical("Failed to get Plugin Window size");
  238. }
  239. else
  240. {
  241. // failed to open UI
  242. m_hints &= ~PLUGIN_HAS_GUI;
  243. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  244. effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0.0f);
  245. }
  246. }
  247. void showGui(bool yesNo)
  248. {
  249. gui.visible = yesNo;
  250. if (gui.visible && gui.width > 0 && gui.height > 0)
  251. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, gui.width, gui.height, 0.0);
  252. }
  253. void idleGui()
  254. {
  255. //effect->dispatcher(effect, effIdle, 0, 0, nullptr, 0.0f);
  256. // FIXME
  257. if (gui.visible)
  258. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  259. CarlaPlugin::idleGui();
  260. }
  261. // -------------------------------------------------------------------
  262. // Plugin state
  263. void reload()
  264. {
  265. qDebug("VstPlugin::reload() - start");
  266. // Safely disable plugin for reload
  267. const ScopedDisabler m(this);
  268. if (x_client->isActive())
  269. x_client->deactivate();
  270. // Remove client ports
  271. removeClientPorts();
  272. // Delete old data
  273. deleteBuffers();
  274. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  275. aIns = aOuts = mIns = mOuts = params = 0;
  276. aIns = effect->numInputs;
  277. aOuts = effect->numOutputs;
  278. params = effect->numParams;
  279. if (VstPluginCanDo(effect, "receiveVstEvents") || VstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) > 0 || (m_hints & PLUGIN_WANTS_MIDI_INPUT))
  280. mIns = 1;
  281. if (VstPluginCanDo(effect, "sendVstEvents") || VstPluginCanDo(effect, "sendVstMidiEvent"))
  282. mOuts = 1;
  283. if (aIns > 0)
  284. {
  285. aIn.ports = new CarlaEngineAudioPort*[aIns];
  286. aIn.rindexes = new uint32_t[aIns];
  287. }
  288. if (aOuts > 0)
  289. {
  290. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  291. aOut.rindexes = new uint32_t[aOuts];
  292. }
  293. if (params > 0)
  294. {
  295. param.data = new ParameterData[params];
  296. param.ranges = new ParameterRanges[params];
  297. }
  298. const int portNameSize = CarlaEngine::maxPortNameSize() - 1;
  299. char portName[portNameSize];
  300. bool needsCin = (aOuts > 0 || params > 0);
  301. for (j=0; j < aIns; j++)
  302. {
  303. #ifndef BUILD_BRIDGE
  304. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  305. sprintf(portName, "%s:input_%02i", m_name, j+1);
  306. else
  307. #endif
  308. sprintf(portName, "input_%02i", j+1);
  309. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  310. aIn.rindexes[j] = j;
  311. }
  312. for (j=0; j < aOuts; j++)
  313. {
  314. #ifndef BUILD_BRIDGE
  315. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  316. sprintf(portName, "%s:output_%02i", m_name, j+1);
  317. else
  318. #endif
  319. sprintf(portName, "output_%02i", j+1);
  320. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  321. aOut.rindexes[j] = j;
  322. }
  323. for (j=0; j<params; j++)
  324. {
  325. param.data[j].type = PARAMETER_INPUT;
  326. param.data[j].index = j;
  327. param.data[j].rindex = j;
  328. param.data[j].hints = 0;
  329. param.data[j].midiChannel = 0;
  330. param.data[j].midiCC = -1;
  331. double min, max, def, step, step_small, step_large;
  332. VstParameterProperties prop;
  333. prop.flags = 0;
  334. if (effect->dispatcher(effect, effGetParameterProperties, j, 0, &prop, 0))
  335. {
  336. double range[2] = {0,1};
  337. if ((m_hints & PLUGIN_HAS_COCKOS_EXTENSIONS) > 0 && effect->dispatcher(effect, effVendorSpecific, 0xdeadbef0, j, range, 0.0) >= 0xbeef)
  338. {
  339. min = range[0];
  340. max = range[1];
  341. }
  342. else if (prop.flags & kVstParameterUsesIntegerMinMax)
  343. {
  344. min = prop.minInteger;
  345. max = prop.maxInteger;
  346. }
  347. else
  348. {
  349. min = 0.0;
  350. max = 1.0;
  351. }
  352. if (min > max)
  353. max = min;
  354. else if (max < min)
  355. min = max;
  356. if (max - min == 0.0)
  357. {
  358. qWarning("Broken plugin parameter: max - min == 0");
  359. max = min + 0.1;
  360. }
  361. if ((m_hints & PLUGIN_HAS_COCKOS_EXTENSIONS) > 0 && effect->dispatcher(effect, effVendorSpecific, kVstParameterUsesIntStep, j, nullptr, 0.0f) >= 0xbeef)
  362. {
  363. step = 1.0;
  364. step_small = 1.0;
  365. step_large = 10.0;
  366. }
  367. else if (prop.flags & kVstParameterIsSwitch)
  368. {
  369. step = max - min;
  370. step_small = step;
  371. step_large = step;
  372. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  373. }
  374. else if (prop.flags & kVstParameterUsesIntStep)
  375. {
  376. step = prop.stepInteger;
  377. step_small = prop.stepInteger;
  378. step_large = prop.largeStepInteger;
  379. param.data[j].hints |= PARAMETER_IS_INTEGER;
  380. }
  381. else if (prop.flags & kVstParameterUsesFloatStep)
  382. {
  383. step = prop.stepFloat;
  384. step_small = prop.smallStepFloat;
  385. step_large = prop.largeStepFloat;
  386. }
  387. else
  388. {
  389. double range = max - min;
  390. step = range/100.0;
  391. step_small = range/1000.0;
  392. step_large = range/10.0;
  393. }
  394. if (prop.flags & kVstParameterCanRamp)
  395. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  396. }
  397. else
  398. {
  399. min = 0.0;
  400. max = 1.0;
  401. step = 0.001;
  402. step_small = 0.0001;
  403. step_large = 0.1;
  404. }
  405. // no such thing as VST default parameters
  406. def = effect->getParameter(effect, j);
  407. if (def < min)
  408. def = min;
  409. else if (def > max)
  410. def = max;
  411. param.ranges[j].min = min;
  412. param.ranges[j].max = max;
  413. param.ranges[j].def = def;
  414. param.ranges[j].step = step;
  415. param.ranges[j].stepSmall = step_small;
  416. param.ranges[j].stepLarge = step_large;
  417. param.data[j].hints |= PARAMETER_IS_ENABLED;
  418. #ifndef BUILD_BRIDGE
  419. param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  420. #endif
  421. if ((m_hints & PLUGIN_USES_OLD_VSTSDK) > 0 || effect->dispatcher(effect, effCanBeAutomated, j, 0, nullptr, 0.0f) == 1)
  422. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  423. }
  424. if (needsCin)
  425. {
  426. #ifndef BUILD_BRIDGE
  427. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  428. {
  429. strcpy(portName, m_name);
  430. strcat(portName, ":control-in");
  431. }
  432. else
  433. #endif
  434. strcpy(portName, "control-in");
  435. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  436. }
  437. if (mIns == 1)
  438. {
  439. #ifndef BUILD_BRIDGE
  440. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  441. {
  442. strcpy(portName, m_name);
  443. strcat(portName, ":midi-in");
  444. }
  445. else
  446. #endif
  447. strcpy(portName, "midi-in");
  448. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  449. }
  450. if (mOuts == 1)
  451. {
  452. #ifndef BUILD_BRIDGE
  453. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  454. {
  455. strcpy(portName, m_name);
  456. strcat(portName, ":midi-out");
  457. }
  458. else
  459. #endif
  460. strcpy(portName, "midi-out");
  461. midi.portMout = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  462. }
  463. aIn.count = aIns;
  464. aOut.count = aOuts;
  465. param.count = params;
  466. // plugin checks
  467. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  468. intptr_t VstCategory = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
  469. if (VstCategory == kPlugCategSynth || VstCategory == kPlugCategGenerator)
  470. m_hints |= PLUGIN_IS_SYNTH;
  471. if (effect->flags & effFlagsProgramChunks)
  472. m_hints |= PLUGIN_USES_CHUNKS;
  473. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  474. m_hints |= PLUGIN_CAN_DRYWET;
  475. if (aOuts > 0)
  476. m_hints |= PLUGIN_CAN_VOLUME;
  477. if (aOuts >= 2 && aOuts%2 == 0)
  478. m_hints |= PLUGIN_CAN_BALANCE;
  479. reloadPrograms(true);
  480. x_client->activate();
  481. qDebug("VstPlugin::reload() - end");
  482. }
  483. void reloadPrograms(bool init)
  484. {
  485. qDebug("VstPlugin::reloadPrograms(%s)", bool2str(init));
  486. uint32_t i, oldCount = prog.count;
  487. // Delete old programs
  488. if (prog.count > 0)
  489. {
  490. for (uint32_t i=0; i < prog.count; i++)
  491. free((void*)prog.names[i]);
  492. delete[] prog.names;
  493. }
  494. prog.count = 0;
  495. prog.names = nullptr;
  496. // Query new programs
  497. prog.count = effect->numPrograms;
  498. if (prog.count > 0)
  499. prog.names = new const char* [prog.count];
  500. // Update names
  501. for (i=0; i < prog.count; i++)
  502. {
  503. char strBuf[STR_MAX] = { 0 };
  504. if (effect->dispatcher(effect, effGetProgramNameIndexed, i, 0, strBuf, 0.0f) != 1)
  505. {
  506. // program will be [re-]changed later
  507. effect->dispatcher(effect, effSetProgram, 0, i, nullptr, 0.0f);
  508. effect->dispatcher(effect, effGetProgramName, 0, 0, strBuf, 0.0f);
  509. }
  510. prog.names[i] = strdup(strBuf);
  511. }
  512. #ifndef BUILD_BRIDGE
  513. // Update OSC Names
  514. //osc_global_send_set_program_count(m_id, prog.count);
  515. //for (i=0; i < prog.count; i++)
  516. // osc_global_send_set_program_name(m_id, i, prog.names[i]);
  517. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  518. #endif
  519. if (init)
  520. {
  521. if (prog.count > 0)
  522. setProgram(0, false, false, false, true);
  523. }
  524. else
  525. {
  526. x_engine->callback(CALLBACK_UPDATE, m_id, 0, 0, 0.0);
  527. // Check if current program is invalid
  528. bool programChanged = false;
  529. if (prog.count == oldCount+1)
  530. {
  531. // one program added, probably created by user
  532. prog.current = oldCount;
  533. programChanged = true;
  534. }
  535. else if (prog.current >= (int32_t)prog.count)
  536. {
  537. // current program > count
  538. prog.current = 0;
  539. programChanged = true;
  540. }
  541. else if (prog.current < 0 && prog.count > 0)
  542. {
  543. // programs exist now, but not before
  544. prog.current = 0;
  545. programChanged = true;
  546. }
  547. else if (prog.current >= 0 && prog.count == 0)
  548. {
  549. // programs existed before, but not anymore
  550. prog.current = -1;
  551. programChanged = true;
  552. }
  553. if (programChanged)
  554. {
  555. setProgram(prog.current, true, true, true, true);
  556. }
  557. else
  558. {
  559. // Program was changed during update, re-set it
  560. if (prog.current >= 0)
  561. effect->dispatcher(effect, effSetProgram, 0, prog.current, nullptr, 0.0f);
  562. }
  563. }
  564. }
  565. // -------------------------------------------------------------------
  566. // Plugin processing
  567. void process(float** inBuffer, float** outBuffer, uint32_t frames, uint32_t framesOffset)
  568. {
  569. uint32_t i, k;
  570. uint32_t midiEventCount = 0;
  571. double ains_peak_tmp[2] = { 0.0 };
  572. double aouts_peak_tmp[2] = { 0.0 };
  573. // reset MIDI
  574. events.numEvents = 0;
  575. midiEvents[0].type = 0;
  576. CARLA_PROCESS_CONTINUE_CHECK;
  577. // --------------------------------------------------------------------------------------------------------
  578. // Input VU
  579. if (aIn.count > 0)
  580. {
  581. if (aIn.count == 1)
  582. {
  583. for (k=0; k < frames; k++)
  584. {
  585. if (abs(inBuffer[0][k]) > ains_peak_tmp[0])
  586. ains_peak_tmp[0] = abs(inBuffer[0][k]);
  587. }
  588. }
  589. else if (aIn.count >= 1)
  590. {
  591. for (k=0; k < frames; k++)
  592. {
  593. if (abs(inBuffer[0][k]) > ains_peak_tmp[0])
  594. ains_peak_tmp[0] = abs(inBuffer[0][k]);
  595. if (abs(inBuffer[1][k]) > ains_peak_tmp[1])
  596. ains_peak_tmp[1] = abs(inBuffer[1][k]);
  597. }
  598. }
  599. }
  600. CARLA_PROCESS_CONTINUE_CHECK;
  601. // --------------------------------------------------------------------------------------------------------
  602. // Parameters Input [Automation]
  603. if (param.portCin && m_active && m_activeBefore)
  604. {
  605. bool allNotesOffSent = false;
  606. const CarlaEngineControlEvent* cinEvent;
  607. uint32_t time, nEvents = param.portCin->getEventCount();
  608. for (i=0; i < nEvents; i++)
  609. {
  610. cinEvent = param.portCin->getEvent(i);
  611. if (! cinEvent)
  612. continue;
  613. time = cinEvent->time - framesOffset;
  614. if (time >= frames)
  615. continue;
  616. // Control change
  617. switch (cinEvent->type)
  618. {
  619. case CarlaEngineEventNull:
  620. break;
  621. case CarlaEngineEventControlChange:
  622. {
  623. double value;
  624. // Control backend stuff
  625. if (cinEvent->channel == m_ctrlInChannel)
  626. {
  627. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  628. {
  629. value = cinEvent->value;
  630. setDryWet(value, false, false);
  631. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  632. continue;
  633. }
  634. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  635. {
  636. value = cinEvent->value*127/100;
  637. setVolume(value, false, false);
  638. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  639. continue;
  640. }
  641. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  642. {
  643. double left, right;
  644. value = cinEvent->value/0.5 - 1.0;
  645. if (value < 0)
  646. {
  647. left = -1.0;
  648. right = (value*2)+1.0;
  649. }
  650. else if (value > 0)
  651. {
  652. left = (value*2)-1.0;
  653. right = 1.0;
  654. }
  655. else
  656. {
  657. left = -1.0;
  658. right = 1.0;
  659. }
  660. setBalanceLeft(left, false, false);
  661. setBalanceRight(right, false, false);
  662. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  663. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  664. continue;
  665. }
  666. }
  667. // Control plugin parameters
  668. for (k=0; k < param.count; k++)
  669. {
  670. if (param.data[k].midiChannel != cinEvent->channel)
  671. continue;
  672. if (param.data[k].midiCC != cinEvent->controller)
  673. continue;
  674. if (param.data[k].type != PARAMETER_INPUT)
  675. continue;
  676. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  677. {
  678. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  679. {
  680. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  681. }
  682. else
  683. {
  684. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  685. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  686. value = rint(value);
  687. }
  688. setParameterValue(k, value, false, false, false);
  689. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  690. }
  691. }
  692. break;
  693. }
  694. case CarlaEngineEventMidiBankChange:
  695. break;
  696. case CarlaEngineEventMidiProgramChange:
  697. if (cinEvent->channel == m_ctrlInChannel)
  698. {
  699. uint32_t progId = rint(cinEvent->value);
  700. if (progId < prog.count)
  701. {
  702. setProgram(progId, false, false, false, false);
  703. postponeEvent(PluginPostEventMidiProgramChange, progId, 0, 0.0);
  704. }
  705. }
  706. break;
  707. case CarlaEngineEventAllSoundOff:
  708. if (cinEvent->channel == m_ctrlInChannel)
  709. {
  710. if (midi.portMin && ! allNotesOffSent)
  711. sendMidiAllNotesOff();
  712. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  713. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  714. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  715. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  716. allNotesOffSent = true;
  717. }
  718. break;
  719. case CarlaEngineEventAllNotesOff:
  720. if (cinEvent->channel == m_ctrlInChannel)
  721. {
  722. if (midi.portMin && ! allNotesOffSent)
  723. sendMidiAllNotesOff();
  724. allNotesOffSent = true;
  725. }
  726. break;
  727. }
  728. }
  729. } // End of Parameters Input
  730. CARLA_PROCESS_CONTINUE_CHECK;
  731. // --------------------------------------------------------------------------------------------------------
  732. // MIDI Input (External)
  733. if (midi.portMin && m_ctrlInChannel >= 0 && m_ctrlInChannel < 16 && m_active && m_activeBefore)
  734. {
  735. engineMidiLock();
  736. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  737. {
  738. if (extMidiNotes[i].channel < 0)
  739. break;
  740. VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
  741. memset(midiEvent, 0, sizeof(VstMidiEvent));
  742. midiEvent->type = kVstMidiType;
  743. midiEvent->byteSize = sizeof(VstMidiEvent);
  744. midiEvent->midiData[0] = m_ctrlInChannel + extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  745. midiEvent->midiData[1] = extMidiNotes[i].note;
  746. midiEvent->midiData[2] = extMidiNotes[i].velo;
  747. extMidiNotes[i].channel = -1;
  748. midiEventCount += 1;
  749. }
  750. engineMidiUnlock();
  751. } // End of MIDI Input (External)
  752. CARLA_PROCESS_CONTINUE_CHECK;
  753. // --------------------------------------------------------------------------------------------------------
  754. // MIDI Input (System)
  755. if (midi.portMin && m_active && m_activeBefore)
  756. {
  757. const CarlaEngineMidiEvent* minEvent;
  758. uint32_t time, nEvents = midi.portMin->getEventCount();
  759. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  760. {
  761. minEvent = midi.portMin->getEvent(i);
  762. if (! minEvent)
  763. continue;
  764. time = minEvent->time - framesOffset;
  765. if (time >= frames)
  766. continue;
  767. uint8_t status = minEvent->data[0];
  768. uint8_t channel = status & 0x0F;
  769. // Fix bad note-off
  770. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  771. status -= 0x10;
  772. VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
  773. memset(midiEvent, 0, sizeof(VstMidiEvent));
  774. midiEvent->type = kVstMidiType;
  775. midiEvent->byteSize = sizeof(VstMidiEvent);
  776. midiEvent->deltaFrames = minEvent->time;
  777. if (MIDI_IS_STATUS_NOTE_OFF(status))
  778. {
  779. uint8_t note = minEvent->data[1];
  780. midiEvent->midiData[0] = status;
  781. midiEvent->midiData[1] = note;
  782. if (channel == m_ctrlInChannel)
  783. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  784. }
  785. else if (MIDI_IS_STATUS_NOTE_ON(status))
  786. {
  787. uint8_t note = minEvent->data[1];
  788. uint8_t velo = minEvent->data[2];
  789. midiEvent->midiData[0] = status;
  790. midiEvent->midiData[1] = note;
  791. midiEvent->midiData[2] = velo;
  792. if (channel == m_ctrlInChannel)
  793. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  794. }
  795. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  796. {
  797. uint8_t note = minEvent->data[1];
  798. uint8_t pressure = minEvent->data[2];
  799. midiEvent->midiData[0] = status;
  800. midiEvent->midiData[1] = note;
  801. midiEvent->midiData[2] = pressure;
  802. }
  803. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  804. {
  805. uint8_t pressure = minEvent->data[1];
  806. midiEvent->midiData[0] = status;
  807. midiEvent->midiData[1] = pressure;
  808. }
  809. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  810. {
  811. uint8_t lsb = minEvent->data[1];
  812. uint8_t msb = minEvent->data[2];
  813. midiEvent->midiData[0] = status;
  814. midiEvent->midiData[1] = lsb;
  815. midiEvent->midiData[2] = msb;
  816. }
  817. else
  818. continue;
  819. midiEventCount += 1;
  820. }
  821. } // End of MIDI Input (System)
  822. CARLA_PROCESS_CONTINUE_CHECK;
  823. // --------------------------------------------------------------------------------------------------------
  824. // Plugin processing
  825. if (m_active)
  826. {
  827. if (! m_activeBefore)
  828. {
  829. if (midi.portMin && m_ctrlInChannel >= 0 && m_ctrlInChannel < 16)
  830. {
  831. memset(&midiEvents[0], 0, sizeof(VstMidiEvent));
  832. midiEvents[0].type = kVstMidiType;
  833. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  834. midiEvents[0].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + m_ctrlInChannel;
  835. midiEvents[0].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  836. memset(&midiEvents[1], 0, sizeof(VstMidiEvent));
  837. midiEvents[1].type = kVstMidiType;
  838. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  839. midiEvents[1].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + m_ctrlInChannel;
  840. midiEvents[1].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  841. midiEventCount = 2;
  842. }
  843. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  844. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  845. }
  846. if (midiEventCount > 0)
  847. {
  848. events.numEvents = midiEventCount;
  849. events.reserved = 0;
  850. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  851. }
  852. // FIXME - make this a global option
  853. // don't process if not needed
  854. //if ((effect->flags & effFlagsNoSoundInStop) > 0 && ains_peak_tmp[0] == 0 && ains_peak_tmp[1] == 0 && midi_event_count == 0 && ! midi.port_mout)
  855. //{
  856. if (m_hints & PLUGIN_CAN_PROCESS_REPLACING)
  857. {
  858. effect->processReplacing(effect, inBuffer, outBuffer, frames);
  859. }
  860. else
  861. {
  862. for (i=0; i < aOut.count; i++)
  863. memset(outBuffer[i], 0, sizeof(float)*frames);
  864. #if ! VST_FORCE_DEPRECATED
  865. effect->process(effect, inBuffer, outBuffer, frames);
  866. #endif
  867. }
  868. //}
  869. }
  870. else
  871. {
  872. if (m_activeBefore)
  873. {
  874. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  875. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  876. }
  877. }
  878. CARLA_PROCESS_CONTINUE_CHECK;
  879. // --------------------------------------------------------------------------------------------------------
  880. // Post-processing (dry/wet, volume and balance)
  881. if (m_active)
  882. {
  883. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  884. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  885. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  886. double bal_rangeL, bal_rangeR;
  887. float oldBufLeft[do_balance ? frames : 0];
  888. for (i=0; i < aOut.count; i++)
  889. {
  890. // Dry/Wet
  891. if (do_drywet || do_volume)
  892. {
  893. for (k=0; k < frames; k++)
  894. {
  895. if (aOut.count == 1)
  896. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  897. else
  898. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  899. }
  900. }
  901. // Balance
  902. if (do_balance)
  903. {
  904. if (i%2 == 0)
  905. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  906. bal_rangeL = (x_balanceLeft+1.0)/2;
  907. bal_rangeR = (x_balanceRight+1.0)/2;
  908. for (k=0; k < frames; k++)
  909. {
  910. if (i%2 == 0)
  911. {
  912. // left output
  913. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  914. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  915. }
  916. else
  917. {
  918. // right
  919. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  920. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  921. }
  922. }
  923. }
  924. // Volume
  925. if (do_volume)
  926. {
  927. for (k=0; k < frames; k++)
  928. outBuffer[i][k] *= x_volume;
  929. }
  930. // Output VU
  931. for (k=0; i < 2 && k < frames; k++)
  932. {
  933. if (abs(outBuffer[i][k]) > aouts_peak_tmp[i])
  934. aouts_peak_tmp[i] = abs(outBuffer[i][k]);
  935. }
  936. }
  937. }
  938. else
  939. {
  940. // disable any output sound if not active
  941. for (i=0; i < aOut.count; i++)
  942. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  943. aouts_peak_tmp[0] = 0.0;
  944. aouts_peak_tmp[1] = 0.0;
  945. } // End of Post-processing
  946. CARLA_PROCESS_CONTINUE_CHECK;
  947. // --------------------------------------------------------------------------------------------------------
  948. // MIDI Output
  949. if (midi.portMout && m_active)
  950. {
  951. uint8_t data[4];
  952. for (int32_t i = midiEventCount; i < events.numEvents; i++)
  953. {
  954. data[0] = midiEvents[i].midiData[0];
  955. data[1] = midiEvents[i].midiData[1];
  956. data[2] = midiEvents[i].midiData[2];
  957. data[3] = midiEvents[i].midiData[3];
  958. // Fix bad note-off
  959. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  960. data[0] -= 0x10;
  961. midi.portMout->writeEvent(midiEvents[i].deltaFrames, data, 3);
  962. }
  963. } // End of MIDI Output
  964. CARLA_PROCESS_CONTINUE_CHECK;
  965. // --------------------------------------------------------------------------------------------------------
  966. // Peak Values
  967. x_engine->setInputPeak(m_id, 0, ains_peak_tmp[0]);
  968. x_engine->setInputPeak(m_id, 1, ains_peak_tmp[1]);
  969. x_engine->setOutputPeak(m_id, 0, aouts_peak_tmp[0]);
  970. x_engine->setOutputPeak(m_id, 1, aouts_peak_tmp[1]);
  971. m_activeBefore = m_active;
  972. }
  973. void bufferSizeChanged(uint32_t newBufferSize)
  974. {
  975. if (m_active)
  976. {
  977. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  978. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  979. }
  980. #if ! VST_FORCE_DEPRECATED
  981. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, x_engine->getSampleRate());
  982. #endif
  983. effect->dispatcher(effect, effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
  984. if (m_active)
  985. {
  986. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  987. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  988. }
  989. }
  990. // -------------------------------------------------------------------
  991. static intptr_t VstHostCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  992. {
  993. #ifdef DEBUG
  994. qDebug("VstHostCallback(%p, opcode: %s, index: %i, value: " P_INTPTR ", opt: %f", effect, VstMasterOpcode2str(opcode), index, value, opt);
  995. #endif
  996. // Cockos VST extensions
  997. if ((uint32_t)opcode == 0xdeadbeef && (uint32_t)index == 0xdeadf00d && ptr)
  998. {
  999. const char* const func = (char*)ptr;
  1000. if (strcmp(func, "GetPlayPosition") == 0)
  1001. return 0;
  1002. if (strcmp(func, "GetPlayPosition2") == 0)
  1003. return 0;
  1004. if (strcmp(func, "GetCursorPosition") == 0)
  1005. return 0;
  1006. if (strcmp(func, "GetPlayState") == 0)
  1007. return 0;
  1008. if (strcmp(func, "SetEditCurPos") == 0)
  1009. return 0;
  1010. if (strcmp(func, "GetSetRepeat") == 0)
  1011. return 0;
  1012. if (strcmp(func, "GetProjectPath") == 0)
  1013. return 0;
  1014. if (strcmp(func, "OnPlayButton") == 0)
  1015. return 0;
  1016. if (strcmp(func, "OnStopButton") == 0)
  1017. return 0;
  1018. if (strcmp(func, "OnPauseButton") == 0)
  1019. return 0;
  1020. if (strcmp(func, "IsInRealTimeAudio") == 0)
  1021. return 0;
  1022. if (strcmp(func, "Audio_IsRunning") == 0)
  1023. return 0;
  1024. }
  1025. // Check if 'resvd1' points to this plugin
  1026. VstPlugin* self = nullptr;
  1027. #ifdef VESTIGE_HEADER
  1028. if (effect && effect->ptr1)
  1029. {
  1030. self = (VstPlugin*)effect->ptr1;
  1031. #else
  1032. if (effect && effect->resvd1)
  1033. {
  1034. self = (VstPlugin*)getPointer(effect->resvd1);
  1035. #endif
  1036. if (self->unique1 != self->unique2)
  1037. self = nullptr;
  1038. }
  1039. switch (opcode)
  1040. {
  1041. case audioMasterAutomate:
  1042. if (self)
  1043. {
  1044. if (self->x_engine->isOnAudioThread() && ! self->x_engine->isOffline())
  1045. {
  1046. self->setParameterValue(index, opt, false, false, false);
  1047. self->postponeEvent(PluginPostEventParameterChange, index, 0, opt);
  1048. }
  1049. else
  1050. self->setParameterValue(index, opt, false, true, true);
  1051. }
  1052. break;
  1053. case audioMasterVersion:
  1054. return kVstVersion;
  1055. case audioMasterCurrentId:
  1056. return 0; // TODO
  1057. case audioMasterIdle:
  1058. if (effect)
  1059. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  1060. break;
  1061. #if ! VST_FORCE_DEPRECATED
  1062. case audioMasterPinConnected:
  1063. // Deprecated in VST SDK 2.4
  1064. // TODO
  1065. break;
  1066. case audioMasterWantMidi:
  1067. // Deprecated in VST SDK 2.4
  1068. if (self)
  1069. self->m_hints |= PLUGIN_WANTS_MIDI_INPUT;
  1070. break;
  1071. #endif
  1072. case audioMasterGetTime:
  1073. {
  1074. static VstTimeInfo_R vstTimeInfo;
  1075. memset(&vstTimeInfo, 0, sizeof(VstTimeInfo_R));
  1076. const CarlaTimeInfo* const timeInfo = self->x_engine->getTimeInfo();
  1077. vstTimeInfo.flags |= kVstTransportChanged;
  1078. if (timeInfo->playing)
  1079. vstTimeInfo.flags |= kVstTransportPlaying;
  1080. vstTimeInfo.samplePos = timeInfo->frame;
  1081. vstTimeInfo.sampleRate = self->x_engine->getSampleRate();
  1082. vstTimeInfo.nanoSeconds = timeInfo->time;
  1083. vstTimeInfo.flags |= kVstNanosValid;
  1084. if (timeInfo->valid & CarlaEngineTimeBBT)
  1085. {
  1086. double ppqBar = double(timeInfo->bbt.bar) * timeInfo->bbt.beats_per_bar - timeInfo->bbt.beats_per_bar;
  1087. double ppqBeat = double(timeInfo->bbt.beat) - 1.0;
  1088. double ppqTick = double(timeInfo->bbt.tick) / timeInfo->bbt.ticks_per_beat;
  1089. // Bars
  1090. vstTimeInfo.barStartPos = ppqBar + ppqBeat;
  1091. vstTimeInfo.flags |= kVstBarsValid;
  1092. // PPQ Pos
  1093. vstTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  1094. vstTimeInfo.flags |= kVstPpqPosValid;
  1095. // Tempo
  1096. vstTimeInfo.tempo = timeInfo->bbt.beats_per_minute;
  1097. vstTimeInfo.flags |= kVstTempoValid;
  1098. // Time Signature
  1099. vstTimeInfo.timeSigNumerator = timeInfo->bbt.beats_per_bar;
  1100. vstTimeInfo.timeSigDenominator = timeInfo->bbt.beat_type;
  1101. vstTimeInfo.flags |= kVstTimeSigValid;
  1102. }
  1103. return (intptr_t)&vstTimeInfo;
  1104. }
  1105. case audioMasterProcessEvents:
  1106. if (self && ptr && self->midi.portMout)
  1107. {
  1108. if (! self->x_engine->isOnAudioThread())
  1109. {
  1110. qDebug("VstHostCallback:audioMasterProcessEvents - Received MIDI Out events outside audio thread, ignoring");
  1111. return 0;
  1112. }
  1113. int32_t i;
  1114. const VstEvents* const events = (VstEvents*)ptr;
  1115. for (i=0; i < events->numEvents && self->events.numEvents < MAX_MIDI_EVENTS*2; i++)
  1116. {
  1117. const VstMidiEvent* const midiEvent = (VstMidiEvent*)events->events[i];
  1118. if (midiEvent && midiEvent->type == kVstMidiType)
  1119. memcpy(&self->midiEvents[self->events.numEvents++], midiEvent, sizeof(VstMidiEvent));
  1120. }
  1121. }
  1122. else
  1123. qDebug("VstHostCallback:audioMasterProcessEvents - Some MIDI Out events were ignored");
  1124. break;
  1125. #if ! VST_FORCE_DEPRECATED
  1126. case audioMasterSetTime:
  1127. // Deprecated in VST SDK 2.4
  1128. break;
  1129. case audioMasterTempoAt:
  1130. {
  1131. // Deprecated in VST SDK 2.4
  1132. const CarlaTimeInfo* const timeInfo = self->x_engine->getTimeInfo();
  1133. if (timeInfo->valid & CarlaEngineTimeBBT)
  1134. return timeInfo->bbt.beats_per_minute * 10000;
  1135. return 120 * 10000;
  1136. }
  1137. case audioMasterGetNumAutomatableParameters:
  1138. // Deprecated in VST SDK 2.4
  1139. #ifdef BUILD_BRIDGE
  1140. return MAX_PARAMETERS;
  1141. #else
  1142. return carlaOptions.maxParameters;
  1143. #endif
  1144. case audioMasterGetParameterQuantization:
  1145. // Deprecated in VST SDK 2.4
  1146. // TODO
  1147. break;
  1148. #endif
  1149. case audioMasterIOChanged:
  1150. if (self && self->m_enabled)
  1151. {
  1152. // TESTING
  1153. qWarning("audioMasterIOChanged called!");
  1154. //carla_proc_lock();
  1155. //self->m_enabled = false;
  1156. //carla_proc_unlock();
  1157. //if (self->m_active)
  1158. //{
  1159. // self->effect->dispatcher(self->effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1160. // self->effect->dispatcher(self->effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1161. //}
  1162. //self->reload();
  1163. //if (self->m_active)
  1164. //{
  1165. // self->effect->dispatcher(self->effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1166. // self->effect->dispatcher(self->effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1167. //}
  1168. //callback_action(CALLBACK_RELOAD_ALL, self->m_id, 0, 0, 0.0);
  1169. }
  1170. return 1;
  1171. case audioMasterNeedIdle:
  1172. // Deprecated in VST SDK 2.4
  1173. // TODO
  1174. break;
  1175. case audioMasterSizeWindow:
  1176. if (self)
  1177. {
  1178. self->gui.width = index;
  1179. self->gui.height = value;
  1180. self->x_engine->callback(CALLBACK_RESIZE_GUI, self->m_id, index, value, 0.0);
  1181. }
  1182. return 1;
  1183. case audioMasterGetSampleRate:
  1184. return self->x_engine->getSampleRate();
  1185. case audioMasterGetBlockSize:
  1186. return self->x_engine->getBufferSize();
  1187. case audioMasterGetInputLatency:
  1188. // TODO
  1189. return 0;
  1190. case audioMasterGetOutputLatency:
  1191. // TODO
  1192. return 0;
  1193. #if ! VST_FORCE_DEPRECATED
  1194. case audioMasterGetPreviousPlug:
  1195. // Deprecated in VST SDK 2.4
  1196. // TODO
  1197. break;
  1198. case audioMasterGetNextPlug:
  1199. // Deprecated in VST SDK 2.4
  1200. // TODO
  1201. break;
  1202. case audioMasterWillReplaceOrAccumulate:
  1203. // Deprecated in VST SDK 2.4
  1204. break;
  1205. #endif
  1206. case audioMasterGetCurrentProcessLevel:
  1207. if (self->x_engine->isOffline())
  1208. return kVstProcessLevelOffline;
  1209. if (self->x_engine->isOnAudioThread())
  1210. return kVstProcessLevelRealtime;
  1211. return kVstProcessLevelUser;
  1212. case audioMasterGetAutomationState:
  1213. return kVstAutomationReadWrite;
  1214. case audioMasterOfflineStart:
  1215. case audioMasterOfflineRead:
  1216. case audioMasterOfflineWrite:
  1217. case audioMasterOfflineGetCurrentPass:
  1218. case audioMasterOfflineGetCurrentMetaPass:
  1219. // TODO
  1220. break;
  1221. #if ! VST_FORCE_DEPRECATED
  1222. case audioMasterSetOutputSampleRate:
  1223. // Deprecated in VST SDK 2.4
  1224. break;
  1225. #ifdef VESTIGE_HEADER
  1226. case audioMasterGetSpeakerArrangement:
  1227. #else
  1228. case audioMasterGetOutputSpeakerArrangement:
  1229. #endif
  1230. // Deprecated in VST SDK 2.4
  1231. // TODO
  1232. break;
  1233. #endif
  1234. case audioMasterGetVendorString:
  1235. strcpy((char*)ptr, "falkTX");
  1236. break;
  1237. case audioMasterGetProductString:
  1238. strcpy((char*)ptr, "Carla");
  1239. break;
  1240. case audioMasterGetVendorVersion:
  1241. return 0x05; // 0.5
  1242. case audioMasterVendorSpecific:
  1243. // TODO - cockos extensions
  1244. break;
  1245. #if ! VST_FORCE_DEPRECATED
  1246. case audioMasterSetIcon:
  1247. // Deprecated in VST SDK 2.4
  1248. break;
  1249. #endif
  1250. case audioMasterCanDo:
  1251. #ifdef DEBUG
  1252. qDebug("VstHostCallback:audioMasterCanDo - %s", (char*)ptr);
  1253. #endif
  1254. if (strcmp((char*)ptr, "supplyIdle") == 0)
  1255. return 1;
  1256. if (strcmp((char*)ptr, "sendVstEvents") == 0)
  1257. return 1;
  1258. if (strcmp((char*)ptr, "sendVstMidiEvent") == 0)
  1259. return 1;
  1260. if (strcmp((char*)ptr, "sendVstMidiEventFlagIsRealtime") == 0)
  1261. return -1;
  1262. if (strcmp((char*)ptr, "sendVstTimeInfo") == 0)
  1263. return 1;
  1264. if (strcmp((char*)ptr, "receiveVstEvents") == 0)
  1265. return 1;
  1266. if (strcmp((char*)ptr, "receiveVstMidiEvent") == 0)
  1267. return 1;
  1268. if (strcmp((char*)ptr, "receiveVstTimeInfo") == 0)
  1269. return -1;
  1270. if (strcmp((char*)ptr, "reportConnectionChanges") == 0)
  1271. return -1;
  1272. if (strcmp((char*)ptr, "acceptIOChanges") == 0)
  1273. return 1;
  1274. if (strcmp((char*)ptr, "sizeWindow") == 0)
  1275. return 1;
  1276. if (strcmp((char*)ptr, "offline") == 0)
  1277. return -1;
  1278. if (strcmp((char*)ptr, "openFileSelector") == 0)
  1279. return -1;
  1280. if (strcmp((char*)ptr, "closeFileSelector") == 0)
  1281. return -1;
  1282. if (strcmp((char*)ptr, "startStopProcess") == 0)
  1283. return 1;
  1284. if (strcmp((char*)ptr, "supportShell") == 0)
  1285. return -1;
  1286. if (strcmp((char*)ptr, "shellCategory") == 0)
  1287. return -1;
  1288. // unimplemented
  1289. qWarning("VstHostCallback:audioMasterCanDo - Got unknown feature request '%s'", (char*)ptr);
  1290. return 0;
  1291. case audioMasterGetLanguage:
  1292. return kVstLangEnglish;
  1293. #if ! VST_FORCE_DEPRECATED
  1294. case audioMasterOpenWindow:
  1295. case audioMasterCloseWindow:
  1296. // Deprecated in VST SDK 2.4
  1297. // TODO
  1298. break;
  1299. #endif
  1300. case audioMasterGetDirectory:
  1301. // TODO
  1302. break;
  1303. case audioMasterUpdateDisplay:
  1304. if (self)
  1305. {
  1306. // Update current program name
  1307. if (self->prog.count > 0 && self->prog.current >= 0)
  1308. {
  1309. char strBuf[STR_MAX] = { 0 };
  1310. int32_t i = self->prog.current;
  1311. self->effect->dispatcher(self->effect, effGetProgramName, 0, 0, strBuf, 0.0f);
  1312. if (*strBuf == 0)
  1313. return 0;
  1314. if (self->prog.names[i] && strcmp(strBuf, self->prog.names[i]) == 0)
  1315. return 0;
  1316. if (self->prog.names[i])
  1317. free((void*)self->prog.names[i]);
  1318. self->prog.names[i] = strdup(strBuf);
  1319. }
  1320. // Tell backend to update
  1321. self->x_engine->callback(CALLBACK_UPDATE, self->m_id, 0, 0, 0.0);
  1322. }
  1323. break;
  1324. case audioMasterBeginEdit:
  1325. case audioMasterEndEdit:
  1326. // TODO
  1327. break;
  1328. case audioMasterOpenFileSelector:
  1329. case audioMasterCloseFileSelector:
  1330. // TODO
  1331. break;
  1332. #if ! VST_FORCE_DEPRECATED
  1333. case audioMasterEditFile:
  1334. // Deprecated in VST SDK 2.4
  1335. // TODO
  1336. break;
  1337. case audioMasterGetChunkFile:
  1338. // Deprecated in VST SDK 2.4
  1339. // TODO
  1340. break;
  1341. case audioMasterGetInputSpeakerArrangement:
  1342. // Deprecated in VST SDK 2.4
  1343. // TODO
  1344. break;
  1345. #endif
  1346. default:
  1347. #ifdef DEBUG
  1348. qDebug("VstHostCallback(%p, opcode: %s, index: %i, value: " P_INTPTR ", opt: %f", effect, VstMasterOpcode2str(opcode), index, value, opt);
  1349. #endif
  1350. break;
  1351. }
  1352. return 0;
  1353. }
  1354. // -------------------------------------------------------------------
  1355. bool init(const char* filename, const char* const name, const char* label)
  1356. {
  1357. // ---------------------------------------------------------------
  1358. // open DLL
  1359. if (! libOpen(filename))
  1360. {
  1361. setLastError(libError(filename));
  1362. return false;
  1363. }
  1364. // ---------------------------------------------------------------
  1365. // get DLL main entry
  1366. VST_Function vstfn = (VST_Function)libSymbol("VSTPluginMain");
  1367. if (! vstfn)
  1368. {
  1369. vstfn = (VST_Function)libSymbol("main");
  1370. if (! vstfn)
  1371. {
  1372. setLastError("Could not find the VST main entry in the plugin library");
  1373. return false;
  1374. }
  1375. }
  1376. // ---------------------------------------------------------------
  1377. // initialize plugin
  1378. effect = vstfn(VstHostCallback);
  1379. if (! effect || effect->magic != kEffectMagic)
  1380. {
  1381. setLastError("Plugin failed to initialize");
  1382. return false;
  1383. }
  1384. // ---------------------------------------------------------------
  1385. // get info
  1386. m_filename = strdup(filename);
  1387. if (name)
  1388. {
  1389. m_name = x_engine->getUniqueName(name);
  1390. }
  1391. else
  1392. {
  1393. char strBuf[STR_MAX] = { 0 };
  1394. effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
  1395. if (strBuf[0] != 0)
  1396. m_name = x_engine->getUniqueName(strBuf);
  1397. else
  1398. m_name = x_engine->getUniqueName(label);
  1399. }
  1400. // ---------------------------------------------------------------
  1401. // initialize VST stuff
  1402. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  1403. #if ! VST_FORCE_DEPRECATED
  1404. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, x_engine->getBufferSize(), nullptr, x_engine->getSampleRate());
  1405. #endif
  1406. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, x_engine->getSampleRate());
  1407. effect->dispatcher(effect, effSetBlockSize, 0, x_engine->getBufferSize(), nullptr, 0.0f);
  1408. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1409. #ifdef VESTIGE_HEADER
  1410. effect->ptr1 = this;
  1411. #else
  1412. effect->resvd1 = (intptr_t)this;
  1413. #endif
  1414. #if ! VST_FORCE_DEPRECATED
  1415. // dummy pre-start to catch possible wantEvents() call on old plugins
  1416. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1417. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1418. #endif
  1419. // special checks
  1420. if (effect->dispatcher(effect, effCanDo, 0, 0, (void*)"hasCockosExtensions", 0.0f) == 0xbeef0000)
  1421. m_hints |= PLUGIN_HAS_COCKOS_EXTENSIONS;
  1422. if (effect->dispatcher(effect, effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion)
  1423. m_hints |= PLUGIN_USES_OLD_VSTSDK;
  1424. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != effect->process)
  1425. m_hints |= PLUGIN_CAN_PROCESS_REPLACING;
  1426. // ---------------------------------------------------------------
  1427. // register client
  1428. x_client = x_engine->addClient(this);
  1429. if (! x_client->isOk())
  1430. {
  1431. setLastError("Failed to register plugin client");
  1432. return false;
  1433. }
  1434. // ---------------------------------------------------------------
  1435. // gui stuff
  1436. if (effect->flags & effFlagsHasEditor)
  1437. m_hints |= PLUGIN_HAS_GUI;
  1438. return true;
  1439. }
  1440. private:
  1441. int unique1;
  1442. AEffect* effect;
  1443. struct {
  1444. int32_t numEvents;
  1445. intptr_t reserved;
  1446. VstEvent* data[MAX_MIDI_EVENTS*2];
  1447. } events;
  1448. VstMidiEvent midiEvents[MAX_MIDI_EVENTS*2];
  1449. struct {
  1450. bool visible;
  1451. int width;
  1452. int height;
  1453. } gui;
  1454. int unique2;
  1455. };
  1456. CarlaPlugin* CarlaPlugin::newVST(const initializer& init)
  1457. {
  1458. qDebug("CarlaPlugin::newVST(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1459. short id = init.engine->getNewPluginId();
  1460. if (id < 0 || id > MAX_PLUGINS)
  1461. {
  1462. setLastError("Maximum number of plugins reached");
  1463. return nullptr;
  1464. }
  1465. VstPlugin* const plugin = new VstPlugin(init.engine, id);
  1466. if (! plugin->init(init.filename, init.name, init.label))
  1467. {
  1468. delete plugin;
  1469. return nullptr;
  1470. }
  1471. plugin->reload();
  1472. #ifndef BUILD_BRIDGE
  1473. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1474. {
  1475. const uint32_t ins = plugin->audioInCount();
  1476. const uint32_t outs = plugin->audioOutCount();
  1477. const bool stereoInput = ins == 0 || ins == 2;
  1478. const bool stereoOutput = outs == 0 || outs == 2;
  1479. if (! (stereoInput || stereoOutput))
  1480. {
  1481. setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  1482. delete plugin;
  1483. return nullptr;
  1484. }
  1485. }
  1486. #endif
  1487. plugin->registerToOsc();
  1488. return plugin;
  1489. }
  1490. /**@}*/
  1491. CARLA_BACKEND_END_NAMESPACE