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.

1836 lines
58KB

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