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.

1684 lines
43KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.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.hpp"
  18. #include "carla_lib_utils.hpp"
  19. #include <QtGui/QtEvents>
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -------------------------------------------------------------------
  22. // fallback data
  23. static ParameterData paramDataNull;
  24. static ParameterRanges paramRangesNull;
  25. static MidiProgramData midiProgramDataNull;
  26. static CustomData customDataNull;
  27. // -------------------------------------------------------------------
  28. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const unsigned short id)
  29. : m_id(id),
  30. x_engine(engine),
  31. x_client(nullptr),
  32. x_dryWet(1.0),
  33. x_volume(1.0),
  34. x_balanceLeft(-1.0),
  35. x_balanceRight(1.0)
  36. {
  37. CARLA_ASSERT(engine);
  38. CARLA_ASSERT(id < x_engine->maxPluginNumber());
  39. qDebug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  40. m_type = PLUGIN_NONE;
  41. m_hints = 0;
  42. m_active = false;
  43. m_activeBefore = false;
  44. m_enabled = false;
  45. m_lib = nullptr;
  46. m_name = nullptr;
  47. m_filename = nullptr;
  48. // options
  49. m_ctrlInChannel = 0;
  50. m_fixedBufferSize = true;
  51. m_processHighPrecision = false;
  52. #ifndef BUILD_BRIDGE
  53. {
  54. const CarlaEngineOptions& options(x_engine->getOptions());
  55. m_processHighPrecision = options.processHighPrecision;
  56. if (options.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  57. m_ctrlInChannel = m_id;
  58. }
  59. #endif
  60. // latency
  61. m_latency = 0;
  62. m_latencyBuffers = nullptr;
  63. // Extra
  64. osc.data.path = nullptr;
  65. osc.data.source = nullptr;
  66. osc.data.target = nullptr;
  67. osc.thread = nullptr;
  68. }
  69. CarlaPlugin::~CarlaPlugin()
  70. {
  71. qDebug("CarlaPlugin::~CarlaPlugin()");
  72. // Remove client and ports
  73. if (x_client)
  74. {
  75. if (x_client->isActive())
  76. x_client->deactivate();
  77. removeClientPorts();
  78. delete x_client;
  79. }
  80. // Delete data
  81. deleteBuffers();
  82. // Unload DLL
  83. libClose();
  84. if (m_name)
  85. free((void*)m_name);
  86. if (m_filename)
  87. free((void*)m_filename);
  88. if (prog.count > 0)
  89. {
  90. for (uint32_t i=0; i < prog.count; i++)
  91. {
  92. if (prog.names[i])
  93. free((void*)prog.names[i]);
  94. }
  95. delete[] prog.names;
  96. }
  97. if (midiprog.count > 0)
  98. {
  99. for (uint32_t i=0; i < midiprog.count; i++)
  100. {
  101. if (midiprog.data[i].name)
  102. free((void*)midiprog.data[i].name);
  103. }
  104. delete[] midiprog.data;
  105. }
  106. if (custom.size() > 0)
  107. {
  108. for (size_t i=0; i < custom.size(); i++)
  109. {
  110. if (custom[i].key)
  111. free((void*)custom[i].key);
  112. if (custom[i].value)
  113. free((void*)custom[i].value);
  114. }
  115. custom.clear();
  116. }
  117. if (m_latencyBuffers)
  118. {
  119. for (uint32_t i=0; i < aIn.count; i++)
  120. delete[] m_latencyBuffers[i];
  121. delete[] m_latencyBuffers;
  122. }
  123. }
  124. // -------------------------------------------------------------------
  125. // Information (base)
  126. PluginType CarlaPlugin::type() const
  127. {
  128. return m_type;
  129. }
  130. unsigned short CarlaPlugin::id() const
  131. {
  132. return m_id;
  133. }
  134. unsigned int CarlaPlugin::hints() const
  135. {
  136. return m_hints;
  137. }
  138. bool CarlaPlugin::enabled() const
  139. {
  140. return m_enabled;
  141. }
  142. const char* CarlaPlugin::name() const
  143. {
  144. return m_name;
  145. }
  146. const char* CarlaPlugin::filename() const
  147. {
  148. return m_filename;
  149. }
  150. PluginCategory CarlaPlugin::category()
  151. {
  152. return PLUGIN_CATEGORY_NONE;
  153. }
  154. long CarlaPlugin::uniqueId()
  155. {
  156. return 0;
  157. }
  158. // -------------------------------------------------------------------
  159. // Information (count)
  160. uint32_t CarlaPlugin::audioInCount()
  161. {
  162. return aIn.count;
  163. }
  164. uint32_t CarlaPlugin::audioOutCount()
  165. {
  166. return aOut.count;
  167. }
  168. uint32_t CarlaPlugin::midiInCount()
  169. {
  170. return midi.portMin ? 1 : 0;
  171. }
  172. uint32_t CarlaPlugin::midiOutCount()
  173. {
  174. return midi.portMout ? 1 : 0;
  175. }
  176. uint32_t CarlaPlugin::parameterCount() const
  177. {
  178. return param.count;
  179. }
  180. uint32_t CarlaPlugin::parameterScalePointCount(const uint32_t parameterId)
  181. {
  182. CARLA_ASSERT(parameterId < param.count);
  183. return 0;
  184. }
  185. uint32_t CarlaPlugin::programCount() const
  186. {
  187. return prog.count;
  188. }
  189. uint32_t CarlaPlugin::midiProgramCount() const
  190. {
  191. return midiprog.count;
  192. }
  193. size_t CarlaPlugin::customDataCount() const
  194. {
  195. return custom.size();
  196. }
  197. // -------------------------------------------------------------------
  198. // Information (current data)
  199. int32_t CarlaPlugin::currentProgram() const
  200. {
  201. return prog.current;
  202. }
  203. int32_t CarlaPlugin::currentMidiProgram() const
  204. {
  205. return midiprog.current;
  206. }
  207. const ParameterData* CarlaPlugin::parameterData(const uint32_t parameterId) const
  208. {
  209. CARLA_ASSERT(parameterId < param.count);
  210. if (parameterId < param.count)
  211. return &param.data[parameterId];
  212. return &paramDataNull;
  213. }
  214. const ParameterRanges* CarlaPlugin::parameterRanges(const uint32_t parameterId) const
  215. {
  216. CARLA_ASSERT(parameterId < param.count);
  217. if (parameterId < param.count)
  218. return &param.ranges[parameterId];
  219. return &paramRangesNull;
  220. }
  221. bool CarlaPlugin::parameterIsOutput(const uint32_t parameterId) const
  222. {
  223. CARLA_ASSERT(parameterId < param.count);
  224. if (parameterId < param.count)
  225. return (param.data[parameterId].type == PARAMETER_OUTPUT);
  226. return false;
  227. }
  228. const MidiProgramData* CarlaPlugin::midiProgramData(const uint32_t index) const
  229. {
  230. CARLA_ASSERT(index < midiprog.count);
  231. if (index < midiprog.count)
  232. return &midiprog.data[index];
  233. return &midiProgramDataNull;
  234. }
  235. const CustomData* CarlaPlugin::customData(const size_t index) const
  236. {
  237. CARLA_ASSERT(index < custom.size());
  238. if (index < custom.size())
  239. return &custom[index];
  240. return &customDataNull;
  241. }
  242. int32_t CarlaPlugin::chunkData(void** const dataPtr)
  243. {
  244. CARLA_ASSERT(dataPtr);
  245. return 0;
  246. }
  247. // -------------------------------------------------------------------
  248. // Information (per-plugin data)
  249. double CarlaPlugin::getParameterValue(const uint32_t parameterId)
  250. {
  251. CARLA_ASSERT(parameterId < param.count);
  252. return 0.0;
  253. }
  254. double CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  255. {
  256. CARLA_ASSERT(parameterId < param.count);
  257. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  258. return 0.0;
  259. }
  260. void CarlaPlugin::getLabel(char* const strBuf)
  261. {
  262. *strBuf = 0;
  263. }
  264. void CarlaPlugin::getMaker(char* const strBuf)
  265. {
  266. *strBuf = 0;
  267. }
  268. void CarlaPlugin::getCopyright(char* const strBuf)
  269. {
  270. *strBuf = 0;
  271. }
  272. void CarlaPlugin::getRealName(char* const strBuf)
  273. {
  274. *strBuf = 0;;
  275. }
  276. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf)
  277. {
  278. CARLA_ASSERT(parameterId < param.count);
  279. *strBuf = 0;
  280. }
  281. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf)
  282. {
  283. CARLA_ASSERT(parameterId < param.count);
  284. *strBuf = 0;
  285. }
  286. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf)
  287. {
  288. CARLA_ASSERT(parameterId < param.count);
  289. *strBuf = 0;
  290. }
  291. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf)
  292. {
  293. CARLA_ASSERT(parameterId < param.count);
  294. *strBuf = 0;
  295. }
  296. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  297. {
  298. CARLA_ASSERT(parameterId < param.count);
  299. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  300. *strBuf = 0;
  301. }
  302. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf)
  303. {
  304. CARLA_ASSERT(index < prog.count);
  305. if (index < prog.count && prog.names[index])
  306. strncpy(strBuf, prog.names[index], STR_MAX);
  307. else
  308. *strBuf = 0;
  309. }
  310. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf)
  311. {
  312. CARLA_ASSERT(index < midiprog.count);
  313. if (index < midiprog.count && midiprog.data[index].name)
  314. strncpy(strBuf, midiprog.data[index].name, STR_MAX);
  315. else
  316. *strBuf = 0;
  317. }
  318. void CarlaPlugin::getParameterCountInfo(uint32_t* const ins, uint32_t* const outs, uint32_t* const total)
  319. {
  320. *ins = 0;
  321. *outs = 0;
  322. *total = param.count;
  323. for (uint32_t i=0; i < param.count; i++)
  324. {
  325. if (param.data[i].type == PARAMETER_INPUT)
  326. *ins += 1;
  327. else if (param.data[i].type == PARAMETER_OUTPUT)
  328. *outs += 1;
  329. }
  330. }
  331. void CarlaPlugin::getGuiInfo(GuiType* const type, bool* const resizable)
  332. {
  333. *type = GUI_NONE;
  334. *resizable = false;
  335. }
  336. // -------------------------------------------------------------------
  337. // Set data (internal stuff)
  338. #ifndef BUILD_BRIDGE
  339. void CarlaPlugin::setId(const unsigned short id)
  340. {
  341. m_id = id;
  342. if (x_engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  343. m_ctrlInChannel = id;
  344. }
  345. #endif
  346. void CarlaPlugin::setEnabled(const bool yesNo)
  347. {
  348. m_enabled = yesNo;
  349. }
  350. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback)
  351. {
  352. m_active = active;
  353. double value = active ? 1.0 : 0.0;
  354. #ifndef BUILD_BRIDGE
  355. if (sendOsc)
  356. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_ACTIVE, value);
  357. #else
  358. Q_UNUSED(sendOsc);
  359. #endif
  360. if (sendCallback)
  361. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_ACTIVE, 0, value, nullptr);
  362. #ifndef BUILD_BRIDGE
  363. else if (m_hints & PLUGIN_IS_BRIDGE)
  364. osc_send_control(&osc.data, PARAMETER_ACTIVE, value);
  365. #endif
  366. }
  367. void CarlaPlugin::setDryWet(double value, const bool sendOsc, const bool sendCallback)
  368. {
  369. CARLA_ASSERT(value >= 0.0 && value <= 1.0);
  370. if (value < 0.0)
  371. value = 0.0;
  372. else if (value > 1.0)
  373. value = 1.0;
  374. x_dryWet = value;
  375. #ifndef BUILD_BRIDGE
  376. if (sendOsc)
  377. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_DRYWET, value);
  378. #else
  379. Q_UNUSED(sendOsc);
  380. #endif
  381. if (sendCallback)
  382. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_DRYWET, 0, value, nullptr);
  383. #ifndef BUILD_BRIDGE
  384. else if (m_hints & PLUGIN_IS_BRIDGE)
  385. osc_send_control(&osc.data, PARAMETER_DRYWET, value);
  386. #endif
  387. }
  388. void CarlaPlugin::setVolume(double value, const bool sendOsc, const bool sendCallback)
  389. {
  390. CARLA_ASSERT(value >= 0.0 && value <= 1.27);
  391. if (value < 0.0)
  392. value = 0.0;
  393. else if (value > 1.27)
  394. value = 1.27;
  395. x_volume = value;
  396. #ifndef BUILD_BRIDGE
  397. if (sendOsc)
  398. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_VOLUME, value);
  399. #else
  400. Q_UNUSED(sendOsc);
  401. #endif
  402. if (sendCallback)
  403. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_VOLUME, 0, value, nullptr);
  404. #ifndef BUILD_BRIDGE
  405. else if (m_hints & PLUGIN_IS_BRIDGE)
  406. osc_send_control(&osc.data, PARAMETER_VOLUME, value);
  407. #endif
  408. }
  409. void CarlaPlugin::setBalanceLeft(double value, const bool sendOsc, const bool sendCallback)
  410. {
  411. CARLA_ASSERT(value >= -1.0 && value <= 1.0);
  412. if (value < -1.0)
  413. value = -1.0;
  414. else if (value > 1.0)
  415. value = 1.0;
  416. x_balanceLeft = value;
  417. #ifndef BUILD_BRIDGE
  418. if (sendOsc)
  419. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, value);
  420. #else
  421. Q_UNUSED(sendOsc);
  422. #endif
  423. if (sendCallback)
  424. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_BALANCE_LEFT, 0, value, nullptr);
  425. #ifndef BUILD_BRIDGE
  426. else if (m_hints & PLUGIN_IS_BRIDGE)
  427. osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, value);
  428. #endif
  429. }
  430. void CarlaPlugin::setBalanceRight(double value, const bool sendOsc, const bool sendCallback)
  431. {
  432. CARLA_ASSERT(value >= -1.0 && value <= 1.0);
  433. if (value < -1.0)
  434. value = -1.0;
  435. else if (value > 1.0)
  436. value = 1.0;
  437. x_balanceRight = value;
  438. #ifndef BUILD_BRIDGE
  439. if (sendOsc)
  440. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, value);
  441. #else
  442. Q_UNUSED(sendOsc);
  443. #endif
  444. if (sendCallback)
  445. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_BALANCE_RIGHT, 0, value, nullptr);
  446. #ifndef BUILD_BRIDGE
  447. else if (m_hints & PLUGIN_IS_BRIDGE)
  448. osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, value);
  449. #endif
  450. }
  451. #ifndef BUILD_BRIDGE
  452. int CarlaPlugin::setOscBridgeInfo(const PluginBridgeInfoType type, const int argc, const lo_arg* const* const argv, const char* const types)
  453. {
  454. return 1;
  455. Q_UNUSED(type);
  456. Q_UNUSED(argc);
  457. Q_UNUSED(argv);
  458. Q_UNUSED(types);
  459. }
  460. #endif
  461. // -------------------------------------------------------------------
  462. // Set data (plugin-specific stuff)
  463. void CarlaPlugin::setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  464. {
  465. CARLA_ASSERT(parameterId < param.count);
  466. if (sendGui)
  467. uiParameterChange(parameterId, value);
  468. #ifndef BUILD_BRIDGE
  469. if (sendOsc)
  470. x_engine->osc_send_control_set_parameter_value(m_id, parameterId, value);
  471. #else
  472. Q_UNUSED(sendOsc);
  473. #endif
  474. if (sendCallback)
  475. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, parameterId, 0, value, nullptr);
  476. }
  477. void CarlaPlugin::setParameterValueByRIndex(const int32_t rindex, const double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  478. {
  479. CARLA_ASSERT(rindex >= PARAMETER_BALANCE_RIGHT && rindex != PARAMETER_NULL);
  480. if (rindex == PARAMETER_ACTIVE)
  481. return setActive(value > 0.0, sendOsc, sendCallback);
  482. if (rindex == PARAMETER_DRYWET)
  483. return setDryWet(value, sendOsc, sendCallback);
  484. if (rindex == PARAMETER_VOLUME)
  485. return setVolume(value, sendOsc, sendCallback);
  486. if (rindex == PARAMETER_BALANCE_LEFT)
  487. return setBalanceLeft(value, sendOsc, sendCallback);
  488. if (rindex == PARAMETER_BALANCE_RIGHT)
  489. return setBalanceRight(value, sendOsc, sendCallback);
  490. for (uint32_t i=0; i < param.count; i++)
  491. {
  492. if (param.data[i].rindex == rindex)
  493. return setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  494. }
  495. }
  496. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback)
  497. {
  498. CARLA_ASSERT(parameterId < param.count);
  499. CARLA_ASSERT(channel < 16);
  500. if (channel >= 16)
  501. channel = 16;
  502. param.data[parameterId].midiChannel = channel;
  503. #ifndef BUILD_BRIDGE
  504. if (sendOsc)
  505. x_engine->osc_send_control_set_parameter_midi_channel(m_id, parameterId, channel);
  506. #else
  507. Q_UNUSED(sendOsc);
  508. #endif
  509. if (sendCallback)
  510. x_engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, m_id, parameterId, channel, 0.0, nullptr);
  511. }
  512. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback)
  513. {
  514. CARLA_ASSERT(parameterId < param.count);
  515. CARLA_ASSERT(cc >= -1);
  516. if (cc < -1 || cc > 0x5F)
  517. cc = -1;
  518. param.data[parameterId].midiCC = cc;
  519. #ifndef BUILD_BRIDGE
  520. if (sendOsc)
  521. x_engine->osc_send_control_set_parameter_midi_cc(m_id, parameterId, cc);
  522. #else
  523. Q_UNUSED(sendOsc);
  524. #endif
  525. if (sendCallback)
  526. x_engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, m_id, parameterId, cc, 0.0, nullptr);
  527. }
  528. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  529. {
  530. CARLA_ASSERT(type);
  531. CARLA_ASSERT(key);
  532. CARLA_ASSERT(value);
  533. if (! type)
  534. return qCritical("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  535. if (! key)
  536. return qCritical("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  537. if (! value)
  538. return qCritical("CarlaPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  539. bool saveData = true;
  540. if (strcmp(type, CUSTOM_DATA_STRING) == 0)
  541. {
  542. // Ignore some keys
  543. if (strncmp(key, "OSC:", 4) == 0 || strcmp(key, "guiVisible") == 0)
  544. saveData = false;
  545. else if (strcmp(key, CARLA_BRIDGE_MSG_SAVE_NOW) == 0 || strcmp(key, CARLA_BRIDGE_MSG_SET_CHUNK) == 0 || strcmp(key, CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  546. saveData = false;
  547. }
  548. if (saveData)
  549. {
  550. // Check if we already have this key
  551. for (size_t i=0; i < custom.size(); i++)
  552. {
  553. if (strcmp(custom[i].key, key) == 0)
  554. {
  555. free((void*)custom[i].value);
  556. custom[i].value = strdup(value);
  557. return;
  558. }
  559. }
  560. // Otherwise store it
  561. CustomData newData;
  562. newData.type = strdup(type);
  563. newData.key = strdup(key);
  564. newData.value = strdup(value);
  565. custom.push_back(newData);
  566. }
  567. }
  568. void CarlaPlugin::setChunkData(const char* const stringData)
  569. {
  570. CARLA_ASSERT(stringData);
  571. }
  572. void CarlaPlugin::setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  573. {
  574. CARLA_ASSERT(index >= -1 && index < (int32_t)prog.count);
  575. if (index < -1)
  576. index = -1;
  577. else if (index > (int32_t)prog.count)
  578. return;
  579. prog.current = index;
  580. if (sendGui && index >= 0)
  581. uiProgramChange(index);
  582. // Change default parameter values
  583. if (index >= 0)
  584. {
  585. for (uint32_t i=0; i < param.count; i++)
  586. {
  587. param.ranges[i].def = getParameterValue(i);
  588. fixParameterValue(param.ranges[i].def, param.ranges[i]);
  589. #ifndef BUILD_BRIDGE
  590. if (sendOsc)
  591. {
  592. x_engine->osc_send_control_set_default_value(m_id, i, param.ranges[i].def);
  593. x_engine->osc_send_control_set_parameter_value(m_id, i, param.ranges[i].def);
  594. }
  595. #endif
  596. }
  597. }
  598. #ifndef BUILD_BRIDGE
  599. if (sendOsc)
  600. x_engine->osc_send_control_set_program(m_id, index);
  601. #else
  602. Q_UNUSED(sendOsc);
  603. #endif
  604. if (sendCallback)
  605. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, index, 0, 0.0, nullptr);
  606. Q_UNUSED(block);
  607. }
  608. void CarlaPlugin::setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  609. {
  610. CARLA_ASSERT(index >= -1 && index < (int32_t)midiprog.count);
  611. if (index < -1)
  612. index = -1;
  613. else if (index > (int32_t)midiprog.count)
  614. return;
  615. midiprog.current = index;
  616. if (sendGui && index >= 0)
  617. uiMidiProgramChange(index);
  618. // Change default parameter values (sound banks never change defaults)
  619. if (index >= 0 && m_type != PLUGIN_GIG && m_type != PLUGIN_SF2 && m_type != PLUGIN_SFZ)
  620. {
  621. for (uint32_t i=0; i < param.count; i++)
  622. {
  623. param.ranges[i].def = getParameterValue(i);
  624. fixParameterValue(param.ranges[i].def, param.ranges[i]);
  625. #ifndef BUILD_BRIDGE
  626. if (sendOsc)
  627. {
  628. x_engine->osc_send_control_set_default_value(m_id, i, param.ranges[i].def);
  629. x_engine->osc_send_control_set_parameter_value(m_id, i, param.ranges[i].def);
  630. }
  631. #endif
  632. }
  633. }
  634. #ifndef BUILD_BRIDGE
  635. if (sendOsc)
  636. x_engine->osc_send_control_set_midi_program(m_id, index);
  637. #else
  638. Q_UNUSED(sendOsc);
  639. #endif
  640. if (sendCallback)
  641. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, index, 0, 0.0, nullptr);
  642. Q_UNUSED(block);
  643. }
  644. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  645. {
  646. for (uint32_t i=0; i < midiprog.count; i++)
  647. {
  648. if (midiprog.data[i].bank == bank && midiprog.data[i].program == program)
  649. return setMidiProgram(i, sendGui, sendOsc, sendCallback, block);
  650. }
  651. }
  652. // -------------------------------------------------------------------
  653. // Set gui stuff
  654. void CarlaPlugin::setGuiContainer(GuiContainer* const container)
  655. {
  656. Q_UNUSED(container);
  657. }
  658. void CarlaPlugin::showGui(const bool yesNo)
  659. {
  660. Q_UNUSED(yesNo);
  661. }
  662. void CarlaPlugin::idleGui()
  663. {
  664. if (! m_enabled)
  665. return;
  666. if (m_hints & PLUGIN_USES_SINGLE_THREAD)
  667. {
  668. // Process postponed events
  669. postEventsRun();
  670. // Update parameter outputs
  671. for (uint32_t i=0; i < param.count; i++)
  672. {
  673. if (param.data[i].type == PARAMETER_OUTPUT)
  674. uiParameterChange(i, getParameterValue(i));
  675. }
  676. }
  677. }
  678. // -------------------------------------------------------------------
  679. // Plugin state
  680. void CarlaPlugin::reload()
  681. {
  682. }
  683. void CarlaPlugin::reloadPrograms(const bool init)
  684. {
  685. Q_UNUSED(init);
  686. }
  687. void CarlaPlugin::prepareForSave()
  688. {
  689. }
  690. // -------------------------------------------------------------------
  691. // Plugin processing
  692. void CarlaPlugin::process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  693. {
  694. Q_UNUSED(inBuffer);
  695. Q_UNUSED(outBuffer);
  696. Q_UNUSED(frames);
  697. Q_UNUSED(framesOffset);
  698. }
  699. void CarlaPlugin::bufferSizeChanged(const uint32_t newBufferSize)
  700. {
  701. Q_UNUSED(newBufferSize);
  702. }
  703. void CarlaPlugin::recreateLatencyBuffers()
  704. {
  705. if (m_latencyBuffers)
  706. {
  707. for (uint32_t i=0; i < aIn.count; i++)
  708. delete[] m_latencyBuffers[i];
  709. delete[] m_latencyBuffers;
  710. }
  711. if (aIn.count > 0 && m_latency > 0)
  712. {
  713. m_latencyBuffers = new float* [aIn.count];
  714. for (uint32_t i=0; i < aIn.count; i++)
  715. m_latencyBuffers[i] = new float [m_latency];
  716. }
  717. else
  718. m_latencyBuffers = nullptr;
  719. }
  720. // -------------------------------------------------------------------
  721. // OSC stuff
  722. void CarlaPlugin::registerToOscClient()
  723. {
  724. #ifndef BUILD_BRIDGE
  725. if (! x_engine->isOscControlRegistered())
  726. return;
  727. #else
  728. if (! x_engine->isOscBridgeRegistered())
  729. return;
  730. #endif
  731. #ifndef BUILD_BRIDGE
  732. x_engine->osc_send_control_add_plugin_start(m_id, m_name);
  733. #endif
  734. // Base data
  735. {
  736. char bufName[STR_MAX] = { 0 };
  737. char bufLabel[STR_MAX] = { 0 };
  738. char bufMaker[STR_MAX] = { 0 };
  739. char bufCopyright[STR_MAX] = { 0 };
  740. getRealName(bufName);
  741. getLabel(bufLabel);
  742. getMaker(bufMaker);
  743. getCopyright(bufCopyright);
  744. #ifdef BUILD_BRIDGE
  745. x_engine->osc_send_bridge_plugin_info(category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  746. #else
  747. x_engine->osc_send_control_set_plugin_data(m_id, m_type, category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  748. #endif
  749. }
  750. // Base count
  751. {
  752. uint32_t cIns, cOuts, cTotals;
  753. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  754. #ifdef BUILD_BRIDGE
  755. x_engine->osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  756. x_engine->osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  757. x_engine->osc_send_bridge_parameter_count(cIns, cOuts, cTotals);
  758. #else
  759. x_engine->osc_send_control_set_plugin_ports(m_id, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  760. #endif
  761. }
  762. // Plugin Parameters
  763. #ifdef BUILD_BRIDGE
  764. const uint32_t maxParameters = MAX_PARAMETERS;
  765. #else
  766. const uint32_t maxParameters = x_engine->getOptions().maxParameters;
  767. #endif
  768. if (param.count > 0 && param.count < maxParameters)
  769. {
  770. char bufName[STR_MAX], bufUnit[STR_MAX];
  771. for (uint32_t i=0; i < param.count; i++)
  772. {
  773. getParameterName(i, bufName);
  774. getParameterUnit(i, bufUnit);
  775. #ifdef BUILD_BRIDGE
  776. x_engine->osc_send_bridge_parameter_info(i, bufName, bufUnit);
  777. x_engine->osc_send_bridge_parameter_data(i, param.data[i].type, param.data[i].rindex, param.data[i].hints, param.data[i].midiChannel, param.data[i].midiCC);
  778. x_engine->osc_send_bridge_parameter_ranges(i, param.ranges[i].def, param.ranges[i].min, param.ranges[i].max, param.ranges[i].step, param.ranges[i].stepSmall, param.ranges[i].stepLarge);
  779. x_engine->osc_send_bridge_set_parameter_value(i, getParameterValue(i));
  780. #else
  781. x_engine->osc_send_control_set_parameter_data(m_id, i, param.data[i].type, param.data[i].hints, bufName, bufUnit, getParameterValue(i));
  782. x_engine->osc_send_control_set_parameter_ranges(m_id, i, param.ranges[i].min, param.ranges[i].max, param.ranges[i].def, param.ranges[i].step, param.ranges[i].stepSmall, param.ranges[i].stepLarge);
  783. x_engine->osc_send_control_set_parameter_midi_cc(m_id, i, param.data[i].midiCC);
  784. x_engine->osc_send_control_set_parameter_midi_channel(m_id, i, param.data[i].midiChannel);
  785. x_engine->osc_send_control_set_parameter_value(m_id, i, getParameterValue(i));
  786. #endif
  787. }
  788. }
  789. // Programs
  790. if (prog.count > 0)
  791. {
  792. #ifdef BUILD_BRIDGE
  793. x_engine->osc_send_bridge_program_count(prog.count);
  794. for (uint32_t i=0; i < prog.count; i++)
  795. x_engine->osc_send_bridge_program_info(i, prog.names[i]);
  796. x_engine->osc_send_bridge_set_program(prog.current);
  797. #else
  798. x_engine->osc_send_control_set_program_count(m_id, prog.count);
  799. for (uint32_t i=0; i < prog.count; i++)
  800. x_engine->osc_send_control_set_program_name(m_id, i, prog.names[i]);
  801. x_engine->osc_send_control_set_program(m_id, prog.current);
  802. #endif
  803. }
  804. // MIDI Programs
  805. if (midiprog.count > 0)
  806. {
  807. #ifdef BUILD_BRIDGE
  808. x_engine->osc_send_bridge_midi_program_count(midiprog.count);
  809. for (uint32_t i=0; i < midiprog.count; i++)
  810. x_engine->osc_send_bridge_midi_program_info(i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  811. x_engine->osc_send_bridge_set_midi_program(prog.current);
  812. #else
  813. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  814. for (uint32_t i=0; i < midiprog.count; i++)
  815. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  816. x_engine->osc_send_control_set_midi_program(m_id, midiprog.current);
  817. #endif
  818. }
  819. #ifndef BUILD_BRIDGE
  820. x_engine->osc_send_control_add_plugin_end(m_id);
  821. // Internal Parameters
  822. {
  823. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  824. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_DRYWET, x_dryWet);
  825. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_VOLUME, x_volume);
  826. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, x_balanceLeft);
  827. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, x_balanceRight);
  828. }
  829. #endif
  830. }
  831. void CarlaPlugin::updateOscData(const lo_address source, const char* const url)
  832. {
  833. // FIXME - remove debug prints later
  834. qWarning("CarlaPlugin::updateOscData(%p, \"%s\")", source, url);
  835. const char* host;
  836. const char* port;
  837. const int proto = lo_address_get_protocol(source);
  838. osc.data.free();
  839. host = lo_address_get_hostname(source);
  840. port = lo_address_get_port(source);
  841. osc.data.source = lo_address_new_with_proto(proto, host, port);
  842. qWarning("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  843. host = lo_url_get_hostname(url);
  844. port = lo_url_get_port(url);
  845. osc.data.path = lo_url_get_path(url);
  846. osc.data.target = lo_address_new_with_proto(proto, host, port);
  847. qWarning("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, osc.data.path);
  848. free((void*)host);
  849. free((void*)port);
  850. if (m_hints & PLUGIN_IS_BRIDGE)
  851. return;
  852. osc_send_sample_rate(&osc.data, x_engine->getSampleRate());
  853. for (size_t i=0; i < custom.size(); i++)
  854. {
  855. // TODO
  856. //if (m_type == PLUGIN_LV2)
  857. //osc_send_lv2_transfer_event(&osc.data, getCustomDataTypeString(custom[i].type), /*custom[i].key,*/ custom[i].value);
  858. //else
  859. if (custom[i].type == CUSTOM_DATA_STRING)
  860. osc_send_configure(&osc.data, custom[i].key, custom[i].value);
  861. }
  862. if (prog.current >= 0)
  863. osc_send_program(&osc.data, prog.current);
  864. if (midiprog.current >= 0)
  865. {
  866. if (m_type == PLUGIN_DSSI)
  867. osc_send_program(&osc.data, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  868. else
  869. osc_send_midi_program(&osc.data, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  870. }
  871. for (uint32_t i=0; i < param.count; i++)
  872. osc_send_control(&osc.data, param.data[i].rindex, getParameterValue(i));
  873. qWarning("CarlaPlugin::updateOscData() - done");
  874. }
  875. void CarlaPlugin::freeOscData()
  876. {
  877. osc.data.free();
  878. }
  879. bool CarlaPlugin::waitForOscGuiShow()
  880. {
  881. qWarning("CarlaPlugin::waitForOscGuiShow()");
  882. #ifndef BUILD_BRIDGE
  883. const uint oscUiTimeout = x_engine->getOptions().oscUiTimeout;
  884. #else
  885. const uint oscUiTimeout = 40;
  886. #endif
  887. // wait for UI 'update' call
  888. for (uint i=0; i < oscUiTimeout; i++)
  889. {
  890. if (osc.data.target)
  891. {
  892. qWarning("CarlaPlugin::waitForOscGuiShow() - got response, asking UI to show itself now");
  893. osc_send_show(&osc.data);
  894. return true;
  895. }
  896. else
  897. carla_msleep(100);
  898. }
  899. qWarning("CarlaPlugin::waitForOscGuiShow() - Timeout while waiting for UI to respond (waited %u msecs)", oscUiTimeout);
  900. return false;
  901. }
  902. // -------------------------------------------------------------------
  903. // MIDI events
  904. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  905. {
  906. CARLA_ASSERT(channel < 16);
  907. CARLA_ASSERT(note < 128);
  908. CARLA_ASSERT(velo < 128);
  909. if (! m_active)
  910. return;
  911. engineMidiLock();
  912. for (unsigned short i=0; i < MAX_MIDI_EVENTS; i++)
  913. {
  914. if (extMidiNotes[i].channel < 0)
  915. {
  916. extMidiNotes[i].channel = channel;
  917. extMidiNotes[i].note = note;
  918. extMidiNotes[i].velo = velo;
  919. break;
  920. }
  921. }
  922. engineMidiUnlock();
  923. if (sendGui)
  924. {
  925. if (velo > 0)
  926. uiNoteOn(channel, note, velo);
  927. else
  928. uiNoteOff(channel, note);
  929. }
  930. #ifndef BUILD_BRIDGE
  931. if (sendOsc)
  932. {
  933. if (velo > 0)
  934. x_engine->osc_send_control_note_on(m_id, channel, note, velo);
  935. else
  936. x_engine->osc_send_control_note_off(m_id, channel, note);
  937. }
  938. #else
  939. Q_UNUSED(sendOsc);
  940. #endif
  941. if (sendCallback)
  942. x_engine->callback(velo ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, m_id, channel, note, velo, nullptr);
  943. }
  944. void CarlaPlugin::sendMidiAllNotesOff()
  945. {
  946. engineMidiLock();
  947. postEvents.mutex.lock();
  948. unsigned short postPad = 0;
  949. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  950. {
  951. if (postEvents.data[i].type == PluginPostEventNull)
  952. {
  953. postPad = i;
  954. break;
  955. }
  956. }
  957. if (postPad == MAX_POST_EVENTS - 1)
  958. {
  959. qWarning("post-events buffer full, making room for all notes off now");
  960. postPad -= 128;
  961. }
  962. for (unsigned short i=0; i < 128; i++)
  963. {
  964. extMidiNotes[i].channel = m_ctrlInChannel;
  965. extMidiNotes[i].note = i;
  966. extMidiNotes[i].velo = 0;
  967. postEvents.data[i + postPad].type = PluginPostEventNoteOff;
  968. postEvents.data[i + postPad].value1 = m_ctrlInChannel;
  969. postEvents.data[i + postPad].value2 = i;
  970. postEvents.data[i + postPad].value3 = 0.0;
  971. }
  972. postEvents.mutex.unlock();
  973. engineMidiUnlock();
  974. }
  975. // -------------------------------------------------------------------
  976. // Post-poned events
  977. void CarlaPlugin::postponeEvent(const PluginPostEventType type, const int32_t value1, const int32_t value2, const double value3, const void* const cdata)
  978. {
  979. postEvents.mutex.lock();
  980. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  981. {
  982. if (postEvents.data[i].type == PluginPostEventNull)
  983. {
  984. postEvents.data[i].type = type;
  985. postEvents.data[i].value1 = value1;
  986. postEvents.data[i].value2 = value2;
  987. postEvents.data[i].value3 = value3;
  988. postEvents.data[i].cdata = cdata;
  989. break;
  990. }
  991. }
  992. postEvents.mutex.unlock();
  993. }
  994. void CarlaPlugin::postEventsRun()
  995. {
  996. PluginPostEvent newPostEvents[MAX_POST_EVENTS];
  997. // Make a safe copy of events, and clear them
  998. postEvents.mutex.lock();
  999. memcpy(newPostEvents, postEvents.data, sizeof(PluginPostEvent)*MAX_POST_EVENTS);
  1000. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1001. postEvents.data[i].type = PluginPostEventNull;
  1002. postEvents.mutex.unlock();
  1003. // Handle events now
  1004. for (uint32_t i=0; i < MAX_POST_EVENTS; i++)
  1005. {
  1006. const PluginPostEvent* const event = &newPostEvents[i];
  1007. switch (event->type)
  1008. {
  1009. case PluginPostEventNull:
  1010. break;
  1011. case PluginPostEventDebug:
  1012. x_engine->callback(CALLBACK_DEBUG, m_id, event->value1, event->value2, event->value3, nullptr);
  1013. break;
  1014. case PluginPostEventParameterChange:
  1015. // Update UI
  1016. if (event->value1 >= 0)
  1017. uiParameterChange(event->value1, event->value3);
  1018. #ifndef BUILD_BRIDGE
  1019. // Update OSC control client
  1020. if (x_engine->isOscControlRegistered())
  1021. x_engine->osc_send_control_set_parameter_value(m_id, event->value1, event->value3);
  1022. #endif
  1023. // Update Host
  1024. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, event->value1, 0, event->value3, nullptr);
  1025. break;
  1026. case PluginPostEventProgramChange:
  1027. // Update UI
  1028. if (event->value1 >= 0)
  1029. uiProgramChange(event->value1);
  1030. #ifndef BUILD_BRIDGE
  1031. // Update OSC control client
  1032. if (x_engine->isOscControlRegistered())
  1033. {
  1034. x_engine->osc_send_control_set_program(m_id, event->value1);
  1035. for (uint32_t j=0; j < param.count; j++)
  1036. x_engine->osc_send_control_set_default_value(m_id, j, param.ranges[j].def);
  1037. }
  1038. #endif
  1039. // Update Host
  1040. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0, nullptr);
  1041. break;
  1042. case PluginPostEventMidiProgramChange:
  1043. // Update UI
  1044. if (event->value1 >= 0)
  1045. uiMidiProgramChange(event->value1);
  1046. #ifndef BUILD_BRIDGE
  1047. // Update OSC control client
  1048. if (x_engine->isOscControlRegistered())
  1049. {
  1050. x_engine->osc_send_control_set_midi_program(m_id, event->value1);
  1051. for (uint32_t j=0; j < param.count; j++)
  1052. x_engine->osc_send_control_set_default_value(m_id, j, param.ranges[j].def);
  1053. }
  1054. #endif
  1055. // Update Host
  1056. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0, nullptr);
  1057. break;
  1058. case PluginPostEventNoteOn:
  1059. // Update UI
  1060. uiNoteOn(event->value1, event->value2, rint(event->value3));
  1061. #ifndef BUILD_BRIDGE
  1062. // Update OSC control client
  1063. if (x_engine->isOscControlRegistered())
  1064. x_engine->osc_send_control_note_on(m_id, event->value1, event->value2, rint(event->value3));
  1065. #endif
  1066. // Update Host
  1067. x_engine->callback(CALLBACK_NOTE_ON, m_id, event->value1, event->value2, rint(event->value3), nullptr);
  1068. break;
  1069. case PluginPostEventNoteOff:
  1070. // Update UI
  1071. uiNoteOff(event->value1, event->value2);
  1072. #ifndef BUILD_BRIDGE
  1073. // Update OSC control client
  1074. if (x_engine->isOscControlRegistered())
  1075. x_engine->osc_send_control_note_off(m_id, event->value1, event->value2);
  1076. #endif
  1077. // Update Host
  1078. x_engine->callback(CALLBACK_NOTE_OFF, m_id, event->value1, event->value2, 0.0, nullptr);
  1079. break;
  1080. case PluginPostEventCustom:
  1081. // Handle custom event
  1082. postEventHandleCustom(event->value1, event->value2, event->value3, event->cdata);
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. void CarlaPlugin::postEventHandleCustom(const int32_t value1, const int32_t value2, const double value3, const void* const cdata)
  1088. {
  1089. Q_UNUSED(value1);
  1090. Q_UNUSED(value2);
  1091. Q_UNUSED(value3);
  1092. Q_UNUSED(cdata);
  1093. }
  1094. void CarlaPlugin::uiParameterChange(const uint32_t index, const double value)
  1095. {
  1096. CARLA_ASSERT(index < param.count);
  1097. Q_UNUSED(value);
  1098. }
  1099. void CarlaPlugin::uiProgramChange(const uint32_t index)
  1100. {
  1101. CARLA_ASSERT(index < prog.count);
  1102. }
  1103. void CarlaPlugin::uiMidiProgramChange(const uint32_t index)
  1104. {
  1105. CARLA_ASSERT(index < midiprog.count);
  1106. }
  1107. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1108. {
  1109. CARLA_ASSERT(channel < 16);
  1110. CARLA_ASSERT(note < 128);
  1111. CARLA_ASSERT(velo > 0 && velo < 128);
  1112. }
  1113. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note)
  1114. {
  1115. CARLA_ASSERT(channel < 16);
  1116. CARLA_ASSERT(note < 128);
  1117. }
  1118. // -------------------------------------------------------------------
  1119. // Cleanup
  1120. void CarlaPlugin::removeClientPorts()
  1121. {
  1122. qDebug("CarlaPlugin::removeClientPorts() - start");
  1123. for (uint32_t i=0; i < aIn.count; i++)
  1124. {
  1125. delete aIn.ports[i];
  1126. aIn.ports[i] = nullptr;
  1127. }
  1128. for (uint32_t i=0; i < aOut.count; i++)
  1129. {
  1130. delete aOut.ports[i];
  1131. aOut.ports[i] = nullptr;
  1132. }
  1133. if (midi.portMin)
  1134. {
  1135. delete midi.portMin;
  1136. midi.portMin = nullptr;
  1137. }
  1138. if (midi.portMout)
  1139. {
  1140. delete midi.portMout;
  1141. midi.portMout = nullptr;
  1142. }
  1143. if (param.portCin)
  1144. {
  1145. delete param.portCin;
  1146. param.portCin = nullptr;
  1147. }
  1148. if (param.portCout)
  1149. {
  1150. delete param.portCout;
  1151. param.portCout = nullptr;
  1152. }
  1153. qDebug("CarlaPlugin::removeClientPorts() - end");
  1154. }
  1155. void CarlaPlugin::initBuffers()
  1156. {
  1157. for (uint32_t i=0; i < aIn.count; i++)
  1158. {
  1159. if (aIn.ports[i])
  1160. aIn.ports[i]->initBuffer(x_engine);
  1161. }
  1162. for (uint32_t i=0; i < aOut.count; i++)
  1163. {
  1164. if (aOut.ports[i])
  1165. aOut.ports[i]->initBuffer(x_engine);
  1166. }
  1167. if (param.portCin)
  1168. param.portCin->initBuffer(x_engine);
  1169. if (param.portCout)
  1170. param.portCout->initBuffer(x_engine);
  1171. if (midi.portMin)
  1172. midi.portMin->initBuffer(x_engine);
  1173. if (midi.portMout)
  1174. midi.portMout->initBuffer(x_engine);
  1175. }
  1176. void CarlaPlugin::deleteBuffers()
  1177. {
  1178. qDebug("CarlaPlugin::deleteBuffers() - start");
  1179. if (aIn.count > 0)
  1180. {
  1181. delete[] aIn.ports;
  1182. delete[] aIn.rindexes;
  1183. }
  1184. if (aOut.count > 0)
  1185. {
  1186. delete[] aOut.ports;
  1187. delete[] aOut.rindexes;
  1188. }
  1189. if (param.count > 0)
  1190. {
  1191. delete[] param.data;
  1192. delete[] param.ranges;
  1193. }
  1194. aIn.count = 0;
  1195. aIn.ports = nullptr;
  1196. aIn.rindexes = nullptr;
  1197. aOut.count = 0;
  1198. aOut.ports = nullptr;
  1199. aOut.rindexes = nullptr;
  1200. param.count = 0;
  1201. param.data = nullptr;
  1202. param.ranges = nullptr;
  1203. param.portCin = nullptr;
  1204. param.portCout = nullptr;
  1205. qDebug("CarlaPlugin::deleteBuffers() - end");
  1206. }
  1207. // -------------------------------------------------------------------
  1208. // Library functions
  1209. bool CarlaPlugin::libOpen(const char* const filename)
  1210. {
  1211. m_lib = lib_open(filename);
  1212. return bool(m_lib);
  1213. }
  1214. bool CarlaPlugin::libClose()
  1215. {
  1216. if (m_lib)
  1217. return lib_close(m_lib);
  1218. return false;
  1219. }
  1220. void* CarlaPlugin::libSymbol(const char* const symbol)
  1221. {
  1222. if (m_lib)
  1223. return lib_symbol(m_lib, symbol);
  1224. return nullptr;
  1225. }
  1226. const char* CarlaPlugin::libError(const char* const filename)
  1227. {
  1228. return lib_error(filename);
  1229. }
  1230. // -------------------------------------------------------------------
  1231. // Locks
  1232. void CarlaPlugin::engineProcessLock()
  1233. {
  1234. x_engine->processLock();
  1235. }
  1236. void CarlaPlugin::engineProcessUnlock()
  1237. {
  1238. x_engine->processUnlock();
  1239. }
  1240. void CarlaPlugin::engineMidiLock()
  1241. {
  1242. x_engine->midiLock();
  1243. }
  1244. void CarlaPlugin::engineMidiUnlock()
  1245. {
  1246. x_engine->midiUnlock();
  1247. }
  1248. // -------------------------------------------------------------------
  1249. // CarlaPluginGUI
  1250. CarlaPluginGUI::CarlaPluginGUI(QWidget* const parent, Callback* const callback)
  1251. : QMainWindow(parent),
  1252. m_callback(callback)
  1253. {
  1254. qDebug("CarlaPluginGUI::CarlaPluginGUI(%p, %p", parent, callback);
  1255. CARLA_ASSERT(callback);
  1256. m_container = new GuiContainer(this);
  1257. setCentralWidget(m_container);
  1258. adjustSize();
  1259. m_container->setParent(this);
  1260. m_container->show();
  1261. m_resizable = true;
  1262. setNewSize(50, 50);
  1263. QMainWindow::setVisible(false);
  1264. }
  1265. CarlaPluginGUI::~CarlaPluginGUI()
  1266. {
  1267. qDebug("CarlaPluginGUI::~CarlaPluginGUI()");
  1268. CARLA_ASSERT(m_container);
  1269. delete m_container;
  1270. }
  1271. // -------------------------------------------------------------------
  1272. GuiContainer* CarlaPluginGUI::getContainer() const
  1273. {
  1274. return m_container;
  1275. }
  1276. WId CarlaPluginGUI::getWinId() const
  1277. {
  1278. return m_container->winId();
  1279. }
  1280. // -------------------------------------------------------------------
  1281. void CarlaPluginGUI::setNewSize(int width, int height)
  1282. {
  1283. qDebug("CarlaPluginGUI::setNewSize(%i, %i)", width, height);
  1284. if (width < 30)
  1285. width = 30;
  1286. if (height < 30)
  1287. height = 30;
  1288. if (m_resizable)
  1289. {
  1290. resize(width, height);
  1291. }
  1292. else
  1293. {
  1294. setFixedSize(width, height);
  1295. m_container->setFixedSize(width, height);
  1296. }
  1297. }
  1298. void CarlaPluginGUI::setResizable(bool resizable)
  1299. {
  1300. m_resizable = resizable;
  1301. setNewSize(width(), height());
  1302. #ifdef Q_OS_WIN
  1303. if (! resizable)
  1304. setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  1305. #endif
  1306. }
  1307. void CarlaPluginGUI::setTitle(const char* const title)
  1308. {
  1309. CARLA_ASSERT(title);
  1310. setWindowTitle(QString("%1 (GUI)").arg(title));
  1311. }
  1312. void CarlaPluginGUI::setVisible(const bool yesNo)
  1313. {
  1314. qDebug("CarlaPluginGUI::setVisible(%s)", bool2str(yesNo));
  1315. if (yesNo)
  1316. {
  1317. if (! m_geometry.isNull())
  1318. restoreGeometry(m_geometry);
  1319. }
  1320. else
  1321. m_geometry = saveGeometry();
  1322. QMainWindow::setVisible(yesNo);
  1323. }
  1324. // -------------------------------------------------------------------
  1325. void CarlaPluginGUI::hideEvent(QHideEvent* const event)
  1326. {
  1327. qDebug("CarlaPluginGUI::hideEvent(%p)", event);
  1328. CARLA_ASSERT(event);
  1329. event->accept();
  1330. close();
  1331. }
  1332. void CarlaPluginGUI::closeEvent(QCloseEvent* const event)
  1333. {
  1334. qDebug("CarlaPluginGUI::closeEvent(%p)", event);
  1335. CARLA_ASSERT(event);
  1336. if (event->spontaneous())
  1337. {
  1338. if (m_callback)
  1339. m_callback->guiClosedCallback();
  1340. QMainWindow::closeEvent(event);
  1341. return;
  1342. }
  1343. event->ignore();
  1344. }
  1345. // -------------------------------------------------------------------
  1346. CARLA_BACKEND_END_NAMESPACE