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.

1776 lines
56KB

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