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.

1779 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.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, 0, 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, 0, 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, 0, left);
  643. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, 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, 0, 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.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->midiData[0] = cin_channel + extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  725. midiEvent->midiData[1] = extMidiNotes[i].note;
  726. midiEvent->midiData[2] = extMidiNotes[i].velo;
  727. extMidiNotes[i].channel = -1;
  728. midiEventCount += 1;
  729. }
  730. engineMidiUnlock();
  731. } // End of MIDI Input (External)
  732. CARLA_PROCESS_CONTINUE_CHECK;
  733. // --------------------------------------------------------------------------------------------------------
  734. // MIDI Input (System)
  735. if (midi.portMin && m_active && m_activeBefore)
  736. {
  737. const CarlaEngineMidiEvent* minEvent;
  738. uint32_t time, nEvents = midi.portMin->getEventCount();
  739. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  740. {
  741. minEvent = midi.portMin->getEvent(i);
  742. if (! minEvent)
  743. continue;
  744. time = minEvent->time - framesOffset;
  745. if (time >= frames)
  746. continue;
  747. uint8_t status = minEvent->data[0];
  748. uint8_t channel = status & 0x0F;
  749. // Fix bad note-off
  750. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  751. status -= 0x10;
  752. VstMidiEvent* const midiEvent = &midiEvents[midiEventCount];
  753. memset(midiEvent, 0, sizeof(VstMidiEvent));
  754. midiEvent->type = kVstMidiType;
  755. midiEvent->byteSize = sizeof(VstMidiEvent);
  756. midiEvent->deltaFrames = minEvent->time;
  757. if (MIDI_IS_STATUS_NOTE_OFF(status))
  758. {
  759. uint8_t note = minEvent->data[1];
  760. midiEvent->midiData[0] = status;
  761. midiEvent->midiData[1] = note;
  762. if (channel == cin_channel)
  763. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  764. }
  765. else if (MIDI_IS_STATUS_NOTE_ON(status))
  766. {
  767. uint8_t note = minEvent->data[1];
  768. uint8_t velo = minEvent->data[2];
  769. midiEvent->midiData[0] = status;
  770. midiEvent->midiData[1] = note;
  771. midiEvent->midiData[2] = velo;
  772. if (channel == cin_channel)
  773. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  774. }
  775. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  776. {
  777. uint8_t note = minEvent->data[1];
  778. uint8_t pressure = minEvent->data[2];
  779. midiEvent->midiData[0] = status;
  780. midiEvent->midiData[1] = note;
  781. midiEvent->midiData[2] = pressure;
  782. }
  783. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  784. {
  785. uint8_t pressure = minEvent->data[1];
  786. midiEvent->midiData[0] = status;
  787. midiEvent->midiData[1] = pressure;
  788. }
  789. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  790. {
  791. uint8_t lsb = minEvent->data[1];
  792. uint8_t msb = minEvent->data[2];
  793. midiEvent->midiData[0] = status;
  794. midiEvent->midiData[1] = lsb;
  795. midiEvent->midiData[2] = msb;
  796. }
  797. else
  798. continue;
  799. midiEventCount += 1;
  800. }
  801. } // End of MIDI Input (System)
  802. CARLA_PROCESS_CONTINUE_CHECK;
  803. // --------------------------------------------------------------------------------------------------------
  804. // Plugin processing
  805. if (m_active)
  806. {
  807. if (! m_activeBefore)
  808. {
  809. if (midi.portMin && cin_channel >= 0 && cin_channel < 16)
  810. {
  811. memset(&midiEvents[0], 0, sizeof(VstMidiEvent));
  812. midiEvents[0].type = kVstMidiType;
  813. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  814. midiEvents[0].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + cin_channel;
  815. midiEvents[0].midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  816. memset(&midiEvents[1], 0, sizeof(VstMidiEvent));
  817. midiEvents[1].type = kVstMidiType;
  818. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  819. midiEvents[1].midiData[0] = MIDI_STATUS_CONTROL_CHANGE + cin_channel;
  820. midiEvents[1].midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  821. midiEventCount = 2;
  822. }
  823. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  824. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  825. }
  826. if (midiEventCount > 0)
  827. {
  828. events.numEvents = midiEventCount;
  829. events.reserved = 0;
  830. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  831. }
  832. // FIXME - make this a global option
  833. // don't process if not needed
  834. //if ((effect->flags & effFlagsNoSoundInStop) > 0 && ains_peak_tmp[0] == 0 && ains_peak_tmp[1] == 0 && midi_event_count == 0 && ! midi.port_mout)
  835. //{
  836. if (effect->flags & effFlagsCanReplacing)
  837. {
  838. effect->processReplacing(effect, inBuffer, outBuffer, frames);
  839. }
  840. else
  841. {
  842. for (i=0; i < aout.count; i++)
  843. memset(outBuffer[i], 0, sizeof(float)*frames);
  844. #if ! VST_FORCE_DEPRECATED
  845. effect->process(effect, inBuffer, outBuffer, frames);
  846. #endif
  847. }
  848. //}
  849. }
  850. else
  851. {
  852. if (m_activeBefore)
  853. {
  854. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  855. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  856. }
  857. }
  858. CARLA_PROCESS_CONTINUE_CHECK;
  859. // --------------------------------------------------------------------------------------------------------
  860. // Post-processing (dry/wet, volume and balance)
  861. if (m_active)
  862. {
  863. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_drywet != 1.0;
  864. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_vol != 1.0;
  865. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_bal_left != -1.0 || x_bal_right != 1.0);
  866. double bal_rangeL, bal_rangeR;
  867. float oldBufLeft[do_balance ? frames : 0];
  868. for (i=0; i < aout.count; i++)
  869. {
  870. // Dry/Wet and Volume
  871. if (do_drywet || do_volume)
  872. {
  873. for (k=0; k < frames; k++)
  874. {
  875. if (do_drywet)
  876. {
  877. if (aout.count == 1)
  878. outBuffer[i][k] = (outBuffer[i][k]*x_drywet)+(inBuffer[0][k]*(1.0-x_drywet));
  879. else
  880. outBuffer[i][k] = (outBuffer[i][k]*x_drywet)+(inBuffer[i][k]*(1.0-x_drywet));
  881. }
  882. if (do_volume)
  883. outBuffer[i][k] *= x_vol;
  884. }
  885. }
  886. // Balance
  887. if (do_balance)
  888. {
  889. if (i%2 == 0)
  890. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  891. bal_rangeL = (x_bal_left+1.0)/2;
  892. bal_rangeR = (x_bal_right+1.0)/2;
  893. for (k=0; k < frames; k++)
  894. {
  895. if (i%2 == 0)
  896. {
  897. // left output
  898. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  899. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  900. }
  901. else
  902. {
  903. // right
  904. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  905. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  906. }
  907. }
  908. }
  909. // Output VU
  910. for (k=0; i < 2 && k < frames; k++)
  911. {
  912. if (abs(outBuffer[i][k]) > aouts_peak_tmp[i])
  913. aouts_peak_tmp[i] = abs(outBuffer[i][k]);
  914. }
  915. }
  916. }
  917. else
  918. {
  919. // disable any output sound if not active
  920. for (i=0; i < aout.count; i++)
  921. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  922. aouts_peak_tmp[0] = 0.0;
  923. aouts_peak_tmp[1] = 0.0;
  924. } // End of Post-processing
  925. CARLA_PROCESS_CONTINUE_CHECK;
  926. // --------------------------------------------------------------------------------------------------------
  927. // MIDI Output
  928. if (midi.portMout && m_active)
  929. {
  930. uint8_t data[4];
  931. for (int32_t i = midiEventCount; i < events.numEvents; i++)
  932. {
  933. data[0] = midiEvents[i].midiData[0];
  934. data[1] = midiEvents[i].midiData[1];
  935. data[2] = midiEvents[i].midiData[2];
  936. data[3] = midiEvents[i].midiData[3];
  937. // Fix bad note-off
  938. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  939. data[0] -= 0x10;
  940. midi.portMout->writeEvent(midiEvents[i].deltaFrames, data, 3);
  941. }
  942. } // End of MIDI Output
  943. CARLA_PROCESS_CONTINUE_CHECK;
  944. // --------------------------------------------------------------------------------------------------------
  945. // Peak Values
  946. x_engine->setInputPeak(m_id, 0, ains_peak_tmp[0]);
  947. x_engine->setInputPeak(m_id, 1, ains_peak_tmp[1]);
  948. x_engine->setOutputPeak(m_id, 0, aouts_peak_tmp[0]);
  949. x_engine->setOutputPeak(m_id, 1, aouts_peak_tmp[1]);
  950. m_activeBefore = m_active;
  951. }
  952. void bufferSizeChanged(uint32_t newBufferSize)
  953. {
  954. if (m_active)
  955. {
  956. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  957. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  958. }
  959. #if ! VST_FORCE_DEPRECATED
  960. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, newBufferSize, nullptr, x_engine->getSampleRate());
  961. #endif
  962. effect->dispatcher(effect, effSetBlockSize, 0, newBufferSize, nullptr, 0.0f);
  963. if (m_active)
  964. {
  965. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  966. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  967. }
  968. }
  969. // -------------------------------------------------------------------
  970. static intptr_t VstHostCallback(AEffect* effect, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
  971. {
  972. #ifdef DEBUG
  973. qDebug("VstHostCallback(%p, opcode: %s, index: %i, value: " P_INTPTR ", opt: %f", effect, VstMasterOpcode2str(opcode), index, value, opt);
  974. #endif
  975. // Check if 'resvd1' points to this plugin
  976. VstPlugin* self = nullptr;
  977. #ifdef VESTIGE_HEADER
  978. if (effect && effect->ptr1)
  979. {
  980. self = (VstPlugin*)effect->ptr1;
  981. #else
  982. if (effect && effect->resvd1)
  983. {
  984. self = (VstPlugin*)getPointer(effect->resvd1);
  985. #endif
  986. if (self->unique1 != self->unique2)
  987. self = nullptr;
  988. }
  989. switch (opcode)
  990. {
  991. case audioMasterAutomate:
  992. if (self)
  993. {
  994. if (self->x_engine->isOnAudioThread() && ! self->x_engine->isOffline())
  995. {
  996. self->setParameterValue(index, opt, false, false, false);
  997. self->postponeEvent(PluginPostEventParameterChange, index, 0, opt);
  998. }
  999. else
  1000. self->setParameterValue(index, opt, false, true, true);
  1001. }
  1002. break;
  1003. case audioMasterVersion:
  1004. return kVstVersion;
  1005. case audioMasterCurrentId:
  1006. return 0; // TODO
  1007. case audioMasterIdle:
  1008. if (effect)
  1009. effect->dispatcher(effect, effEditIdle, 0, 0, nullptr, 0.0f);
  1010. break;
  1011. #if ! VST_FORCE_DEPRECATED
  1012. case audioMasterPinConnected:
  1013. // Deprecated in VST SDK 2.4
  1014. // TODO
  1015. break;
  1016. case audioMasterWantMidi:
  1017. // Deprecated in VST SDK 2.4
  1018. if (self)
  1019. self->m_wantsMidi = true;
  1020. break;
  1021. #endif
  1022. case audioMasterGetTime:
  1023. {
  1024. static VstTimeInfo_R vstTimeInfo;
  1025. memset(&vstTimeInfo, 0, sizeof(VstTimeInfo_R));
  1026. const CarlaTimeInfo* const timeInfo = self->x_engine->getTimeInfo();
  1027. vstTimeInfo.flags |= kVstTransportChanged;
  1028. if (timeInfo->playing)
  1029. vstTimeInfo.flags |= kVstTransportPlaying;
  1030. vstTimeInfo.samplePos = timeInfo->frame;
  1031. vstTimeInfo.sampleRate = self->x_engine->getSampleRate();
  1032. vstTimeInfo.nanoSeconds = timeInfo->time;
  1033. vstTimeInfo.flags |= kVstNanosValid;
  1034. if (timeInfo->valid & CarlaEngineTimeBBT)
  1035. {
  1036. double ppqBar = double(timeInfo->bbt.bar) * timeInfo->bbt.beats_per_bar - timeInfo->bbt.beats_per_bar;
  1037. double ppqBeat = double(timeInfo->bbt.beat) - 1.0;
  1038. double ppqTick = double(timeInfo->bbt.tick) / timeInfo->bbt.ticks_per_beat;
  1039. // Bars
  1040. vstTimeInfo.barStartPos = ppqBar + ppqBeat;
  1041. vstTimeInfo.flags |= kVstBarsValid;
  1042. // PPQ Pos
  1043. vstTimeInfo.ppqPos = ppqBar + ppqBeat + ppqTick;
  1044. vstTimeInfo.flags |= kVstPpqPosValid;
  1045. // Tempo
  1046. vstTimeInfo.tempo = timeInfo->bbt.beats_per_minute;
  1047. vstTimeInfo.flags |= kVstTempoValid;
  1048. // Time Signature
  1049. vstTimeInfo.timeSigNumerator = timeInfo->bbt.beats_per_bar;
  1050. vstTimeInfo.timeSigDenominator = timeInfo->bbt.beat_type;
  1051. vstTimeInfo.flags |= kVstTimeSigValid;
  1052. }
  1053. return (intptr_t)&vstTimeInfo;
  1054. }
  1055. case audioMasterProcessEvents:
  1056. if (self && ptr && self->midi.portMout)
  1057. {
  1058. if (! self->x_engine->isOnAudioThread())
  1059. {
  1060. qDebug("VstHostCallback:audioMasterProcessEvents - Received MIDI Out events outside audio thread, ignoring");
  1061. return 0;
  1062. }
  1063. int32_t i;
  1064. const VstEvents* const events = (VstEvents*)ptr;
  1065. for (i=0; i < events->numEvents && self->events.numEvents < MAX_MIDI_EVENTS*2; i++)
  1066. {
  1067. const VstMidiEvent* const midiEvent = (VstMidiEvent*)events->events[i];
  1068. if (midiEvent && midiEvent->type == kVstMidiType)
  1069. memcpy(&self->midiEvents[self->events.numEvents++], midiEvent, sizeof(VstMidiEvent));
  1070. }
  1071. }
  1072. else
  1073. qDebug("VstHostCallback:audioMasterProcessEvents - Some MIDI Out events were ignored");
  1074. break;
  1075. #if ! VST_FORCE_DEPRECATED
  1076. case audioMasterSetTime:
  1077. // Deprecated in VST SDK 2.4
  1078. break;
  1079. case audioMasterTempoAt:
  1080. {
  1081. // Deprecated in VST SDK 2.4
  1082. const CarlaTimeInfo* const timeInfo = self->x_engine->getTimeInfo();
  1083. if (timeInfo->valid & CarlaEngineTimeBBT)
  1084. return timeInfo->bbt.beats_per_minute * 10000;
  1085. return 120 * 10000;
  1086. }
  1087. case audioMasterGetNumAutomatableParameters:
  1088. // Deprecated in VST SDK 2.4
  1089. #ifdef BUILD_BRIDGE
  1090. return MAX_PARAMETERS;
  1091. #else
  1092. return carlaOptions.max_parameters;
  1093. #endif
  1094. case audioMasterGetParameterQuantization:
  1095. // Deprecated in VST SDK 2.4
  1096. // TODO
  1097. break;
  1098. #endif
  1099. case audioMasterIOChanged:
  1100. if (self && self->m_enabled)
  1101. {
  1102. // TESTING
  1103. qWarning("audioMasterIOChanged called!");
  1104. //carla_proc_lock();
  1105. //self->m_enabled = false;
  1106. //carla_proc_unlock();
  1107. //if (self->m_active)
  1108. //{
  1109. // self->effect->dispatcher(self->effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1110. // self->effect->dispatcher(self->effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1111. //}
  1112. //self->reload();
  1113. //if (self->m_active)
  1114. //{
  1115. // self->effect->dispatcher(self->effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1116. // self->effect->dispatcher(self->effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1117. //}
  1118. //callback_action(CALLBACK_RELOAD_ALL, self->m_id, 0, 0, 0.0);
  1119. }
  1120. return 1;
  1121. case audioMasterNeedIdle:
  1122. // Deprecated in VST SDK 2.4
  1123. // TODO
  1124. break;
  1125. case audioMasterSizeWindow:
  1126. if (self)
  1127. {
  1128. self->gui.width = index;
  1129. self->gui.height = value;
  1130. self->x_engine->callback(CALLBACK_RESIZE_GUI, self->m_id, index, value, 0.0);
  1131. }
  1132. return 1;
  1133. case audioMasterGetSampleRate:
  1134. return self->x_engine->getSampleRate();
  1135. case audioMasterGetBlockSize:
  1136. return self->x_engine->getBufferSize();
  1137. case audioMasterGetInputLatency:
  1138. // TODO
  1139. return 0;
  1140. case audioMasterGetOutputLatency:
  1141. // TODO
  1142. return 0;
  1143. #if ! VST_FORCE_DEPRECATED
  1144. case audioMasterGetPreviousPlug:
  1145. // Deprecated in VST SDK 2.4
  1146. // TODO
  1147. break;
  1148. case audioMasterGetNextPlug:
  1149. // Deprecated in VST SDK 2.4
  1150. // TODO
  1151. break;
  1152. case audioMasterWillReplaceOrAccumulate:
  1153. // Deprecated in VST SDK 2.4
  1154. break;
  1155. #endif
  1156. case audioMasterGetCurrentProcessLevel:
  1157. if (self->x_engine->isOffline())
  1158. return kVstProcessLevelOffline;
  1159. if (self->x_engine->isOnAudioThread())
  1160. return kVstProcessLevelRealtime;
  1161. return kVstProcessLevelUser;
  1162. case audioMasterGetAutomationState:
  1163. return kVstAutomationReadWrite;
  1164. case audioMasterOfflineStart:
  1165. case audioMasterOfflineRead:
  1166. case audioMasterOfflineWrite:
  1167. case audioMasterOfflineGetCurrentPass:
  1168. case audioMasterOfflineGetCurrentMetaPass:
  1169. // TODO
  1170. break;
  1171. #if ! VST_FORCE_DEPRECATED
  1172. case audioMasterSetOutputSampleRate:
  1173. // Deprecated in VST SDK 2.4
  1174. break;
  1175. #ifdef VESTIGE_HEADER
  1176. case audioMasterGetSpeakerArrangement:
  1177. #else
  1178. case audioMasterGetOutputSpeakerArrangement:
  1179. #endif
  1180. // Deprecated in VST SDK 2.4
  1181. // TODO
  1182. break;
  1183. #endif
  1184. case audioMasterGetVendorString:
  1185. strcpy((char*)ptr, "falkTX");
  1186. break;
  1187. case audioMasterGetProductString:
  1188. strcpy((char*)ptr, "Carla");
  1189. break;
  1190. case audioMasterGetVendorVersion:
  1191. return 0x05; // 0.5
  1192. case audioMasterVendorSpecific:
  1193. // TODO - cockos extensions
  1194. break;
  1195. #if ! VST_FORCE_DEPRECATED
  1196. case audioMasterSetIcon:
  1197. // Deprecated in VST SDK 2.4
  1198. break;
  1199. #endif
  1200. case audioMasterCanDo:
  1201. #ifdef DEBUG
  1202. qDebug("VstHostCallback:audioMasterCanDo - %s", (char*)ptr);
  1203. #endif
  1204. if (strcmp((char*)ptr, "supplyIdle") == 0)
  1205. return 1;
  1206. if (strcmp((char*)ptr, "sendVstEvents") == 0)
  1207. return 1;
  1208. if (strcmp((char*)ptr, "sendVstMidiEvent") == 0)
  1209. return 1;
  1210. if (strcmp((char*)ptr, "sendVstMidiEventFlagIsRealtime") == 0)
  1211. return -1;
  1212. if (strcmp((char*)ptr, "sendVstTimeInfo") == 0)
  1213. return 1;
  1214. if (strcmp((char*)ptr, "receiveVstEvents") == 0)
  1215. return 1;
  1216. if (strcmp((char*)ptr, "receiveVstMidiEvent") == 0)
  1217. return 1;
  1218. if (strcmp((char*)ptr, "receiveVstTimeInfo") == 0)
  1219. return -1;
  1220. if (strcmp((char*)ptr, "reportConnectionChanges") == 0)
  1221. return -1;
  1222. if (strcmp((char*)ptr, "acceptIOChanges") == 0)
  1223. return 1;
  1224. if (strcmp((char*)ptr, "sizeWindow") == 0)
  1225. return 1;
  1226. if (strcmp((char*)ptr, "offline") == 0)
  1227. return -1;
  1228. if (strcmp((char*)ptr, "openFileSelector") == 0)
  1229. return -1;
  1230. if (strcmp((char*)ptr, "closeFileSelector") == 0)
  1231. return -1;
  1232. if (strcmp((char*)ptr, "startStopProcess") == 0)
  1233. return 1;
  1234. if (strcmp((char*)ptr, "supportShell") == 0)
  1235. return -1;
  1236. if (strcmp((char*)ptr, "shellCategory") == 0)
  1237. return -1;
  1238. // unimplemented
  1239. qWarning("VstHostCallback:audioMasterCanDo - Got unknown feature request '%s'", (char*)ptr);
  1240. return 0;
  1241. case audioMasterGetLanguage:
  1242. return kVstLangEnglish;
  1243. #if ! VST_FORCE_DEPRECATED
  1244. case audioMasterOpenWindow:
  1245. case audioMasterCloseWindow:
  1246. // Deprecated in VST SDK 2.4
  1247. // TODO
  1248. break;
  1249. #endif
  1250. case audioMasterGetDirectory:
  1251. // TODO
  1252. break;
  1253. case audioMasterUpdateDisplay:
  1254. if (self)
  1255. {
  1256. // Update current program name
  1257. if (self->prog.count > 0 && self->prog.current >= 0)
  1258. {
  1259. char strBuf[STR_MAX] = { 0 };
  1260. int32_t i = self->prog.current;
  1261. self->effect->dispatcher(self->effect, effGetProgramName, 0, 0, strBuf, 0.0f);
  1262. if (*strBuf == 0)
  1263. return 0;
  1264. if (self->prog.names[i] && strcmp(strBuf, self->prog.names[i]) == 0)
  1265. return 0;
  1266. if (self->prog.names[i])
  1267. free((void*)self->prog.names[i]);
  1268. self->prog.names[i] = strdup(strBuf);
  1269. }
  1270. // Tell backend to update
  1271. self->x_engine->callback(CALLBACK_UPDATE, self->m_id, 0, 0, 0.0);
  1272. }
  1273. break;
  1274. case audioMasterBeginEdit:
  1275. case audioMasterEndEdit:
  1276. // TODO
  1277. break;
  1278. case audioMasterOpenFileSelector:
  1279. case audioMasterCloseFileSelector:
  1280. // TODO
  1281. break;
  1282. #if ! VST_FORCE_DEPRECATED
  1283. case audioMasterEditFile:
  1284. // Deprecated in VST SDK 2.4
  1285. // TODO
  1286. break;
  1287. case audioMasterGetChunkFile:
  1288. // Deprecated in VST SDK 2.4
  1289. // TODO
  1290. break;
  1291. case audioMasterGetInputSpeakerArrangement:
  1292. // Deprecated in VST SDK 2.4
  1293. // TODO
  1294. break;
  1295. #endif
  1296. default:
  1297. #ifdef DEBUG
  1298. qDebug("VstHostCallback(%p, opcode: %s, index: %i, value: " P_INTPTR ", opt: %f", effect, VstMasterOpcode2str(opcode), index, value, opt);
  1299. #endif
  1300. break;
  1301. }
  1302. return 0;
  1303. }
  1304. // -------------------------------------------------------------------
  1305. bool init(const char* filename, const char* const name, const char* label)
  1306. {
  1307. // ---------------------------------------------------------------
  1308. // open DLL
  1309. if (! libOpen(filename))
  1310. {
  1311. setLastError(libError(filename));
  1312. return false;
  1313. }
  1314. // ---------------------------------------------------------------
  1315. // get DLL main entry
  1316. VST_Function vstfn = (VST_Function)libSymbol("VSTPluginMain");
  1317. if (! vstfn)
  1318. {
  1319. vstfn = (VST_Function)libSymbol("main");
  1320. if (! vstfn)
  1321. {
  1322. setLastError("Could not find the VST main entry in the plugin library");
  1323. return false;
  1324. }
  1325. }
  1326. // ---------------------------------------------------------------
  1327. // initialize plugin
  1328. effect = vstfn(VstHostCallback);
  1329. if (! effect || effect->magic != kEffectMagic)
  1330. {
  1331. setLastError("Plugin failed to initialize");
  1332. return false;
  1333. }
  1334. // ---------------------------------------------------------------
  1335. // get info
  1336. m_filename = strdup(filename);
  1337. if (name)
  1338. {
  1339. m_name = x_engine->getUniqueName(name);
  1340. }
  1341. else
  1342. {
  1343. char strBuf[STR_MAX] = { 0 };
  1344. effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
  1345. if (strBuf[0] != 0)
  1346. m_name = x_engine->getUniqueName(strBuf);
  1347. else
  1348. m_name = x_engine->getUniqueName(label);
  1349. }
  1350. // ---------------------------------------------------------------
  1351. // initialize VST stuff
  1352. m_oldSDK = (effect->dispatcher(effect, effGetVstVersion, 0, 0, nullptr, 0.0f) < kVstVersion);
  1353. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  1354. #if ! VST_FORCE_DEPRECATED
  1355. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, x_engine->getBufferSize(), nullptr, x_engine->getSampleRate());
  1356. #endif
  1357. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, x_engine->getSampleRate());
  1358. effect->dispatcher(effect, effSetBlockSize, 0, x_engine->getBufferSize(), nullptr, 0.0f);
  1359. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1360. #ifdef VESTIGE_HEADER
  1361. effect->ptr1 = this;
  1362. #else
  1363. effect->resvd1 = (intptr_t)this;
  1364. #endif
  1365. #if ! VST_FORCE_DEPRECATED
  1366. // dummy pre-start to catch possible wantEvents() call on old plugins
  1367. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1368. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1369. #endif
  1370. // ---------------------------------------------------------------
  1371. // register client
  1372. x_client = x_engine->addClient(this);
  1373. if (! x_client->isOk())
  1374. {
  1375. setLastError("Failed to register plugin client");
  1376. return false;
  1377. }
  1378. // ---------------------------------------------------------------
  1379. // gui stuff
  1380. if (effect->flags & effFlagsHasEditor)
  1381. m_hints |= PLUGIN_HAS_GUI;
  1382. return true;
  1383. }
  1384. private:
  1385. int unique1;
  1386. AEffect* effect;
  1387. struct {
  1388. int32_t numEvents;
  1389. intptr_t reserved;
  1390. VstEvent* data[MAX_MIDI_EVENTS*2];
  1391. } events;
  1392. VstMidiEvent midiEvents[MAX_MIDI_EVENTS*2];
  1393. bool m_oldSDK;
  1394. bool m_wantsMidi;
  1395. struct {
  1396. bool visible;
  1397. int width;
  1398. int height;
  1399. } gui;
  1400. int unique2;
  1401. };
  1402. CarlaPlugin* CarlaPlugin::newVST(const initializer& init)
  1403. {
  1404. qDebug("CarlaPlugin::newVST(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1405. short id = init.engine->getNewPluginId();
  1406. if (id < 0)
  1407. {
  1408. setLastError("Maximum number of plugins reached");
  1409. return nullptr;
  1410. }
  1411. VstPlugin* const plugin = new VstPlugin(init.engine, id);
  1412. if (! plugin->init(init.filename, init.name, init.label))
  1413. {
  1414. delete plugin;
  1415. return nullptr;
  1416. }
  1417. plugin->reload();
  1418. #ifndef BUILD_BRIDGE
  1419. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  1420. {
  1421. const uint32_t ins = plugin->audioInCount();
  1422. const uint32_t outs = plugin->audioOutCount();
  1423. const bool stereoInput = ins == 0 || ins == 2;
  1424. const bool stereoOutput = outs == 0 || outs == 2;
  1425. if (! (stereoInput || stereoOutput))
  1426. {
  1427. setLastError("Carla's rack mode can only work with Stereo VST plugins, sorry!");
  1428. delete plugin;
  1429. return nullptr;
  1430. }
  1431. }
  1432. #endif
  1433. plugin->registerToOsc();
  1434. return plugin;
  1435. }
  1436. /**@}*/
  1437. CARLA_BACKEND_END_NAMESPACE