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.

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