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.

1738 lines
44KB

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