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.

2244 lines
61KB

  1. /*
  2. * Carla Backend
  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. #ifndef CARLA_PLUGIN_H
  18. #define CARLA_PLUGIN_H
  19. #include "carla_engine.h"
  20. #include "carla_midi.h"
  21. #include "carla_shared.h"
  22. #include "carla_lib_includes.h"
  23. #ifdef BUILD_BRIDGE
  24. # include "carla_bridge_osc.h"
  25. #endif
  26. // common includes
  27. #include <cmath>
  28. #include <cstdio>
  29. #include <cstdlib>
  30. #include <vector>
  31. #include <QtGui/QDialog>
  32. CARLA_BACKEND_START_NAMESPACE
  33. /*!
  34. * @defgroup CarlaBackendPlugin Carla Backend Plugin
  35. *
  36. * The Carla Backend Plugin.
  37. * @{
  38. */
  39. #define CARLA_PROCESS_CONTINUE_CHECK if (! m_enabled) { x_engine->callback(CALLBACK_DEBUG, m_id, m_enabled, 0, 0.0); return; }
  40. const unsigned short MAX_MIDI_EVENTS = 512;
  41. const unsigned short MAX_POST_EVENTS = 152;
  42. #ifndef BUILD_BRIDGE
  43. enum PluginBridgeInfoType {
  44. PluginBridgeAudioCount,
  45. PluginBridgeMidiCount,
  46. PluginBridgeParameterCount,
  47. PluginBridgeProgramCount,
  48. PluginBridgeMidiProgramCount,
  49. PluginBridgePluginInfo,
  50. PluginBridgeParameterInfo,
  51. PluginBridgeParameterData,
  52. PluginBridgeParameterRanges,
  53. PluginBridgeProgramInfo,
  54. PluginBridgeMidiProgramInfo,
  55. PluginBridgeConfigure,
  56. PluginBridgeSetParameterValue,
  57. PluginBridgeSetDefaultValue,
  58. PluginBridgeSetProgram,
  59. PluginBridgeSetMidiProgram,
  60. PluginBridgeSetCustomData,
  61. PluginBridgeSetChunkData,
  62. PluginBridgeUpdateNow
  63. };
  64. #endif
  65. enum PluginPostEventType {
  66. PluginPostEventNull,
  67. PluginPostEventDebug,
  68. PluginPostEventParameterChange, // param, N, value
  69. PluginPostEventProgramChange, // index
  70. PluginPostEventMidiProgramChange, // index
  71. PluginPostEventNoteOn, // channel, note, velo
  72. PluginPostEventNoteOff, // channel, note
  73. PluginPostEventCustom
  74. };
  75. struct PluginAudioData {
  76. uint32_t count;
  77. uint32_t* rindexes;
  78. CarlaEngineAudioPort** ports;
  79. PluginAudioData()
  80. : count(0),
  81. rindexes(nullptr),
  82. ports(nullptr) {}
  83. };
  84. struct PluginMidiData {
  85. CarlaEngineMidiPort* portMin;
  86. CarlaEngineMidiPort* portMout;
  87. PluginMidiData()
  88. : portMin(nullptr),
  89. portMout(nullptr) {}
  90. };
  91. struct PluginParameterData {
  92. uint32_t count;
  93. ParameterData* data;
  94. ParameterRanges* ranges;
  95. CarlaEngineControlPort* portCin;
  96. CarlaEngineControlPort* portCout;
  97. PluginParameterData()
  98. : count(0),
  99. data(nullptr),
  100. ranges(nullptr),
  101. portCin(nullptr),
  102. portCout(nullptr) {}
  103. };
  104. struct PluginProgramData {
  105. uint32_t count;
  106. int32_t current;
  107. const char** names;
  108. PluginProgramData()
  109. : count(0),
  110. current(-1),
  111. names(nullptr) {}
  112. };
  113. struct PluginMidiProgramData {
  114. uint32_t count;
  115. int32_t current;
  116. midi_program_t* data;
  117. PluginMidiProgramData()
  118. : count(0),
  119. current(-1),
  120. data(nullptr) {}
  121. };
  122. struct PluginPostEvent {
  123. PluginPostEventType type;
  124. int32_t value1;
  125. int32_t value2;
  126. double value3;
  127. const void* cdata;
  128. PluginPostEvent()
  129. : type(PluginPostEventNull),
  130. value1(-1),
  131. value2(-1),
  132. value3(0.0),
  133. cdata(nullptr) {}
  134. };
  135. struct ExternalMidiNote {
  136. int8_t channel; // invalid = -1
  137. uint8_t note;
  138. uint8_t velo;
  139. ExternalMidiNote()
  140. : channel(-1),
  141. note(0),
  142. velo(0) {}
  143. };
  144. // fallback data
  145. static ParameterData paramDataNull;
  146. static ParameterRanges paramRangesNull;
  147. static midi_program_t midiProgramNull;
  148. static CustomData customDataNull;
  149. /*!
  150. * \class CarlaPlugin
  151. *
  152. * \brief Carla Backend base plugin class
  153. *
  154. * This is the base class for all available plugin types available in Carla Backend.\n
  155. * All virtual calls are implemented in this class as fallback, so it's safe to only override needed calls.
  156. *
  157. * \see PluginType
  158. */
  159. class CarlaPlugin
  160. {
  161. public:
  162. /*!
  163. * This is the constructor of the base plugin class.
  164. *
  165. * \param engine The engine which this plugin belongs to, must not be null
  166. * \param id The 'id' of this plugin, must between 0 and CarlaEngine::maxPluginNumber()
  167. */
  168. CarlaPlugin(CarlaEngine* const engine, const unsigned short id)
  169. : m_id(id),
  170. x_engine(engine),
  171. x_client(nullptr),
  172. x_dryWet(1.0),
  173. x_volume(1.0),
  174. x_balanceLeft(-1.0),
  175. x_balanceRight(1.0)
  176. {
  177. Q_ASSERT(engine);
  178. Q_ASSERT(id < CarlaEngine::maxPluginNumber());
  179. qDebug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  180. m_type = PLUGIN_NONE;
  181. m_hints = 0;
  182. m_active = false;
  183. m_activeBefore = false;
  184. m_enabled = false;
  185. m_lib = nullptr;
  186. m_name = nullptr;
  187. m_filename = nullptr;
  188. #ifndef BUILD_BRIDGE
  189. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  190. m_ctrlInChannel = m_id;
  191. else
  192. #endif
  193. m_ctrlInChannel = 0;
  194. #ifndef BUILD_BRIDGE
  195. osc.data.path = nullptr;
  196. osc.data.source = nullptr;
  197. osc.data.target = nullptr;
  198. osc.thread = nullptr;
  199. #endif
  200. }
  201. /*!
  202. * This is the de-constructor of the base plugin class.
  203. */
  204. virtual ~CarlaPlugin()
  205. {
  206. qDebug("CarlaPlugin::~CarlaPlugin()");
  207. // Remove client and ports
  208. if (x_client)
  209. {
  210. if (x_client->isActive())
  211. x_client->deactivate();
  212. removeClientPorts();
  213. delete x_client;
  214. }
  215. // Delete data
  216. deleteBuffers();
  217. // Unload DLL
  218. libClose();
  219. if (m_name)
  220. free((void*)m_name);
  221. if (m_filename)
  222. free((void*)m_filename);
  223. if (prog.count > 0)
  224. {
  225. for (uint32_t i=0; i < prog.count; i++)
  226. {
  227. if (prog.names[i])
  228. free((void*)prog.names[i]);
  229. }
  230. delete[] prog.names;
  231. }
  232. if (midiprog.count > 0)
  233. {
  234. for (uint32_t i=0; i < midiprog.count; i++)
  235. {
  236. if (midiprog.data[i].name)
  237. free((void*)midiprog.data[i].name);
  238. }
  239. delete[] midiprog.data;
  240. }
  241. if (custom.size() > 0)
  242. {
  243. for (size_t i=0; i < custom.size(); i++)
  244. {
  245. if (custom[i].key)
  246. free((void*)custom[i].key);
  247. if (custom[i].value)
  248. free((void*)custom[i].value);
  249. }
  250. custom.clear();
  251. }
  252. }
  253. // -------------------------------------------------------------------
  254. // Information (base)
  255. /*!
  256. * Get the plugin's type (ie, a subclass of CarlaPlugin).
  257. *
  258. * \note Plugin bridges will return their respective plugin type, there is no plugin type such as "bridge".\n
  259. * To check if a plugin is a bridge use:
  260. * \code
  261. * if (m_hints & PLUGIN_IS_BRIDGE)
  262. * ...
  263. * \endcode
  264. */
  265. PluginType type() const
  266. {
  267. return m_type;
  268. }
  269. /*!
  270. * Get the plugin's id (as passed in the constructor).
  271. *
  272. * \see setId()
  273. */
  274. unsigned short id() const
  275. {
  276. return m_id;
  277. }
  278. /*!
  279. * Get the plugin's hints.
  280. *
  281. * \see PluginHints
  282. */
  283. unsigned int hints() const
  284. {
  285. return m_hints;
  286. }
  287. /*!
  288. * Check if the plugin is enabled.
  289. *
  290. * \see setEnabled()
  291. */
  292. bool enabled() const
  293. {
  294. return m_enabled;
  295. }
  296. /*!
  297. * Get the plugin's internal name.\n
  298. * This name is unique within all plugins in an engine.
  299. *
  300. * \see getRealName()
  301. */
  302. const char* name() const
  303. {
  304. return m_name;
  305. }
  306. /*!
  307. * Get the currently loaded DLL filename for this plugin.\n
  308. * (Sound kits return their exact filename).
  309. */
  310. const char* filename() const
  311. {
  312. return m_filename;
  313. }
  314. /*!
  315. * Get the plugin's category (delay, filter, synth, etc).
  316. */
  317. virtual PluginCategory category()
  318. {
  319. return PLUGIN_CATEGORY_NONE;
  320. }
  321. /*!
  322. * Get the plugin's native unique Id.\n
  323. * May return 0 on plugin types that don't support Ids.
  324. */
  325. virtual long uniqueId()
  326. {
  327. return 0;
  328. }
  329. // -------------------------------------------------------------------
  330. // Information (count)
  331. /*!
  332. * Get the number of audio inputs.
  333. */
  334. virtual uint32_t audioInCount()
  335. {
  336. return aIn.count;
  337. }
  338. /*!
  339. * Get the number of audio outputs.
  340. */
  341. virtual uint32_t audioOutCount()
  342. {
  343. return aOut.count;
  344. }
  345. /*!
  346. * Get the number of MIDI inputs.
  347. */
  348. virtual uint32_t midiInCount()
  349. {
  350. return midi.portMin ? 1 : 0;
  351. }
  352. /*!
  353. * Get the number of MIDI outputs.
  354. */
  355. virtual uint32_t midiOutCount()
  356. {
  357. return midi.portMout ? 1 : 0;
  358. }
  359. /*!
  360. * Get the number of parameters.\n
  361. * To know the number of parameter inputs and outputs separately use getParameterCountInfo() instead.
  362. */
  363. uint32_t parameterCount() const
  364. {
  365. return param.count;
  366. }
  367. /*!
  368. * Get the number of scalepoints for parameter \a parameterId.
  369. */
  370. virtual uint32_t parameterScalePointCount(const uint32_t parameterId)
  371. {
  372. Q_ASSERT(parameterId < param.count);
  373. return 0;
  374. }
  375. /*!
  376. * Get the number of programs.
  377. */
  378. uint32_t programCount() const
  379. {
  380. return prog.count;
  381. }
  382. /*!
  383. * Get the number of MIDI programs.
  384. */
  385. uint32_t midiProgramCount() const
  386. {
  387. return midiprog.count;
  388. }
  389. /*!
  390. * Get the number of custom data sets.
  391. */
  392. size_t customDataCount() const
  393. {
  394. return custom.size();
  395. }
  396. // -------------------------------------------------------------------
  397. // Information (current data)
  398. /*!
  399. * Get the current program number (-1 if unset).
  400. *
  401. * \see setProgram()
  402. */
  403. int32_t currentProgram() const
  404. {
  405. return prog.current;
  406. }
  407. /*!
  408. * Get the current MIDI program number (-1 if unset).
  409. *
  410. * \see setMidiProgram()
  411. * \see setMidiProgramById()
  412. */
  413. int32_t currentMidiProgram() const
  414. {
  415. return midiprog.current;
  416. }
  417. /*!
  418. * Get the parameter data of \a parameterId.
  419. */
  420. const ParameterData* parameterData(const uint32_t parameterId) const
  421. {
  422. Q_ASSERT(parameterId < param.count);
  423. if (parameterId < param.count)
  424. return &param.data[parameterId];
  425. return &paramDataNull;
  426. }
  427. /*!
  428. * Get the parameter ranges of \a parameterId.
  429. */
  430. const ParameterRanges* parameterRanges(const uint32_t parameterId) const
  431. {
  432. Q_ASSERT(parameterId < param.count);
  433. if (parameterId < param.count)
  434. return &param.ranges[parameterId];
  435. return &paramRangesNull;
  436. }
  437. /*!
  438. * Check if parameter \a parameterId is of output type.
  439. */
  440. bool parameterIsOutput(const uint32_t parameterId) const
  441. {
  442. Q_ASSERT(parameterId < param.count);
  443. if (parameterId < param.count)
  444. return (param.data[parameterId].type == PARAMETER_OUTPUT);
  445. return false;
  446. }
  447. /*!
  448. * Get the MIDI program at \a index.
  449. *
  450. * \see getMidiProgramName()
  451. */
  452. const midi_program_t* midiProgramData(const uint32_t index) const
  453. {
  454. Q_ASSERT(index < midiprog.count);
  455. if (index < midiprog.count)
  456. return &midiprog.data[index];
  457. return &midiProgramNull;
  458. }
  459. /*!
  460. * Get the custom data set at \a index.
  461. *
  462. * \see setCustomData()
  463. */
  464. const CustomData* customData(const size_t index) const
  465. {
  466. Q_ASSERT(index < custom.size());
  467. if (index < custom.size())
  468. return &custom[index];
  469. return &customDataNull;
  470. }
  471. /*!
  472. * Get the complete plugin chunk data into \a dataPtr.
  473. *
  474. * \return The size of the chunk or 0 if invalid.
  475. *
  476. * \note Make sure to verify the plugin supports chunks before calling this function!
  477. *
  478. * \see setChunkData()
  479. */
  480. virtual int32_t chunkData(void** const dataPtr)
  481. {
  482. Q_ASSERT(dataPtr);
  483. return 0;
  484. }
  485. // -------------------------------------------------------------------
  486. // Information (per-plugin data)
  487. /*!
  488. * Get the current parameter value of \a parameterId.
  489. */
  490. virtual double getParameterValue(const uint32_t parameterId)
  491. {
  492. Q_ASSERT(parameterId < param.count);
  493. return 0.0;
  494. }
  495. /*!
  496. * Get the scalepoint \a scalePointId value of the parameter \a parameterId.
  497. */
  498. virtual double getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  499. {
  500. Q_ASSERT(parameterId < param.count);
  501. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  502. return 0.0;
  503. }
  504. /*!
  505. * Get the plugin's label (URI for PLUGIN_LV2).
  506. */
  507. virtual void getLabel(char* const strBuf)
  508. {
  509. *strBuf = 0;
  510. }
  511. /*!
  512. * Get the plugin's maker.
  513. */
  514. virtual void getMaker(char* const strBuf)
  515. {
  516. *strBuf = 0;
  517. }
  518. /*!
  519. * Get the plugin's copyright/license.
  520. */
  521. virtual void getCopyright(char* const strBuf)
  522. {
  523. *strBuf = 0;
  524. }
  525. /*!
  526. * Get the plugin's (real) name.
  527. *
  528. * \see name()
  529. */
  530. virtual void getRealName(char* const strBuf)
  531. {
  532. *strBuf = 0;;
  533. }
  534. /*!
  535. * Get the name of the parameter \a parameterId.
  536. */
  537. virtual void getParameterName(const uint32_t parameterId, char* const strBuf)
  538. {
  539. Q_ASSERT(parameterId < param.count);
  540. *strBuf = 0;
  541. }
  542. /*!
  543. * Get the symbol of the parameter \a parameterId.
  544. */
  545. virtual void getParameterSymbol(const uint32_t parameterId, char* const strBuf)
  546. {
  547. Q_ASSERT(parameterId < param.count);
  548. *strBuf = 0;
  549. }
  550. /*!
  551. * Get the custom text of the parameter \a parameterId.
  552. */
  553. virtual void getParameterText(const uint32_t parameterId, char* const strBuf)
  554. {
  555. Q_ASSERT(parameterId < param.count);
  556. *strBuf = 0;
  557. }
  558. /*!
  559. * Get the unit of the parameter \a parameterId.
  560. */
  561. virtual void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  562. {
  563. Q_ASSERT(parameterId < param.count);
  564. *strBuf = 0;
  565. }
  566. /*!
  567. * Get the scalepoint \a scalePointId label of the parameter \a parameterId.
  568. */
  569. virtual void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  570. {
  571. Q_ASSERT(parameterId < param.count);
  572. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  573. *strBuf = 0;
  574. }
  575. /*!
  576. * Get the name of the program at \a index.
  577. */
  578. void getProgramName(const uint32_t index, char* const strBuf)
  579. {
  580. Q_ASSERT(index < prog.count);
  581. if (index < prog.count && prog.names[index])
  582. strncpy(strBuf, prog.names[index], STR_MAX);
  583. else
  584. *strBuf = 0;
  585. }
  586. /*!
  587. * Get the name of the MIDI program at \a index.
  588. *
  589. * \see getMidiProgramInfo()
  590. */
  591. void getMidiProgramName(const uint32_t index, char* const strBuf)
  592. {
  593. Q_ASSERT(index < midiprog.count);
  594. if (index < midiprog.count && midiprog.data[index].name)
  595. strncpy(strBuf, midiprog.data[index].name, STR_MAX);
  596. else
  597. *strBuf = 0;
  598. }
  599. /*!
  600. * Get information about the plugin's parameter count.\n
  601. * This is used to check how many input, output and total parameters are available.\n
  602. *
  603. * \note Some parameters might not be input or output (ie, invalid).
  604. *
  605. * \see parameterCount()
  606. */
  607. void getParameterCountInfo(uint32_t* const ins, uint32_t* const outs, uint32_t* const total)
  608. {
  609. *ins = 0;
  610. *outs = 0;
  611. *total = param.count;
  612. for (uint32_t i=0; i < param.count; i++)
  613. {
  614. if (param.data[i].type == PARAMETER_INPUT)
  615. *ins += 1;
  616. else if (param.data[i].type == PARAMETER_OUTPUT)
  617. *outs += 1;
  618. }
  619. }
  620. /*!
  621. * Get information about the plugin's custom GUI, if provided.
  622. */
  623. virtual void getGuiInfo(GuiType* const type, bool* const resizable)
  624. {
  625. *type = GUI_NONE;
  626. *resizable = false;
  627. }
  628. // -------------------------------------------------------------------
  629. // Set data (internal stuff)
  630. #ifndef BUILD_BRIDGE
  631. /*!
  632. * Set the plugin's id to \a id.
  633. *
  634. * \see id()
  635. */
  636. void setId(const unsigned short id)
  637. {
  638. m_id = id;
  639. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  640. m_ctrlInChannel = id;
  641. }
  642. #endif
  643. /*!
  644. * Enable or disable the plugin according to \a yesNo.
  645. *
  646. * When a plugin is disabled, it will never be processed or managed in any way.\n
  647. * To 'bypass' a plugin use setActive() instead.
  648. *
  649. * \see enabled()
  650. */
  651. void setEnabled(const bool yesNo)
  652. {
  653. m_enabled = yesNo;
  654. }
  655. /*!
  656. * Set plugin as active according to \a active.
  657. *
  658. * \param sendOsc Send message change over OSC
  659. * \param sendCallback Send message change to registered callback
  660. */
  661. void setActive(const bool active, const bool sendOsc, const bool sendCallback)
  662. {
  663. m_active = active;
  664. double value = active ? 1.0 : 0.0;
  665. #ifndef BUILD_BRIDGE
  666. if (sendOsc)
  667. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_ACTIVE, value);
  668. #else
  669. Q_UNUSED(sendOsc);
  670. #endif
  671. if (sendCallback)
  672. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_ACTIVE, 0, value);
  673. #ifndef BUILD_BRIDGE
  674. else if (m_hints & PLUGIN_IS_BRIDGE)
  675. osc_send_control(&osc.data, PARAMETER_ACTIVE, value);
  676. #endif
  677. }
  678. /*!
  679. * Set the plugin's dry/wet signal value to \a value.\n
  680. * \a value must be between 0.0 and 1.0.
  681. *
  682. * \param sendOsc Send message change over OSC
  683. * \param sendCallback Send message change to registered callback
  684. */
  685. void setDryWet(double value, const bool sendOsc, const bool sendCallback)
  686. {
  687. Q_ASSERT(value >= 0.0 && value <= 1.0);
  688. if (value < 0.0)
  689. value = 0.0;
  690. else if (value > 1.0)
  691. value = 1.0;
  692. x_dryWet = value;
  693. #ifndef BUILD_BRIDGE
  694. if (sendOsc)
  695. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_DRYWET, value);
  696. #else
  697. Q_UNUSED(sendOsc);
  698. #endif
  699. if (sendCallback)
  700. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_DRYWET, 0, value);
  701. #ifndef BUILD_BRIDGE
  702. else if (m_hints & PLUGIN_IS_BRIDGE)
  703. osc_send_control(&osc.data, PARAMETER_DRYWET, value);
  704. #endif
  705. }
  706. /*!
  707. * Set the plugin's output volume to \a value.\n
  708. * \a value must be between 0.0 and 1.27.
  709. *
  710. * \param sendOsc Send message change over OSC
  711. * \param sendCallback Send message change to registered callback
  712. */
  713. void setVolume(double value, const bool sendOsc, const bool sendCallback)
  714. {
  715. Q_ASSERT(value >= 0.0 && value <= 1.27);
  716. if (value < 0.0)
  717. value = 0.0;
  718. else if (value > 1.27)
  719. value = 1.27;
  720. x_volume = value;
  721. #ifndef BUILD_BRIDGE
  722. if (sendOsc)
  723. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_VOLUME, value);
  724. #else
  725. Q_UNUSED(sendOsc);
  726. #endif
  727. if (sendCallback)
  728. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_VOLUME, 0, value);
  729. #ifndef BUILD_BRIDGE
  730. else if (m_hints & PLUGIN_IS_BRIDGE)
  731. osc_send_control(&osc.data, PARAMETER_VOLUME, value);
  732. #endif
  733. }
  734. /*!
  735. * Set the plugin's output left balance value to \a value.\n
  736. * \a value must be between -1.0 and 1.0.
  737. *
  738. * \param sendOsc Send message change over OSC
  739. * \param sendCallback Send message change to registered callback
  740. */
  741. void setBalanceLeft(double value, const bool sendOsc, const bool sendCallback)
  742. {
  743. Q_ASSERT(value >= -1.0 && value <= 1.0);
  744. if (value < -1.0)
  745. value = -1.0;
  746. else if (value > 1.0)
  747. value = 1.0;
  748. x_balanceLeft = value;
  749. #ifndef BUILD_BRIDGE
  750. if (sendOsc)
  751. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, value);
  752. #else
  753. Q_UNUSED(sendOsc);
  754. #endif
  755. if (sendCallback)
  756. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_BALANCE_LEFT, 0, value);
  757. #ifndef BUILD_BRIDGE
  758. else if (m_hints & PLUGIN_IS_BRIDGE)
  759. osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, value);
  760. #endif
  761. }
  762. /*!
  763. * Set the plugin's output right balance value to \a value.\n
  764. * \a value must be between -1.0 and 1.0.
  765. *
  766. * \param sendOsc Send message change over OSC
  767. * \param sendCallback Send message change to registered callback
  768. */
  769. void setBalanceRight(double value, const bool sendOsc, const bool sendCallback)
  770. {
  771. Q_ASSERT(value >= -1.0 && value <= 1.0);
  772. if (value < -1.0)
  773. value = -1.0;
  774. else if (value > 1.0)
  775. value = 1.0;
  776. x_balanceRight = value;
  777. #ifndef BUILD_BRIDGE
  778. if (sendOsc)
  779. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, value);
  780. #else
  781. Q_UNUSED(sendOsc);
  782. #endif
  783. if (sendCallback)
  784. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_BALANCE_RIGHT, 0, value);
  785. #ifndef BUILD_BRIDGE
  786. else if (m_hints & PLUGIN_IS_BRIDGE)
  787. osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, value);
  788. #endif
  789. }
  790. #ifndef BUILD_BRIDGE
  791. /*!
  792. * BridgePlugin call used to set internal data.
  793. */
  794. virtual int setOscBridgeInfo(const PluginBridgeInfoType type, const int argc, const lo_arg* const* const argv, const char* const types)
  795. {
  796. return 1;
  797. Q_UNUSED(type);
  798. Q_UNUSED(argc);
  799. Q_UNUSED(argv);
  800. Q_UNUSED(types);
  801. }
  802. #endif
  803. // -------------------------------------------------------------------
  804. // Set data (plugin-specific stuff)
  805. /*!
  806. * Set a plugin's parameter value.
  807. *
  808. * \param parameterId The parameter to change
  809. * \param value The new parameter value, must be within the parameter's range
  810. * \param sendGui Send message change to plugin's custom GUI, if any
  811. * \param sendOsc Send message change over OSC
  812. * \param sendCallback Send message change to registered callback
  813. *
  814. * \see getParameterValue()
  815. */
  816. virtual void setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  817. {
  818. Q_ASSERT(parameterId < param.count);
  819. if (sendGui)
  820. uiParameterChange(parameterId, value);
  821. #ifndef BUILD_BRIDGE
  822. if (sendOsc)
  823. x_engine->osc_send_control_set_parameter_value(m_id, parameterId, value);
  824. #else
  825. Q_UNUSED(sendOsc);
  826. #endif
  827. if (sendCallback)
  828. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, parameterId, 0, value);
  829. }
  830. /*!
  831. * Set a plugin's parameter value, including internal parameters.\n
  832. * \a rindex can be negative to allow internal parameters change (as defined in InternalParametersIndex).
  833. *
  834. * \see setParameterValue()
  835. * \see setActive()
  836. * \see setDryWet()
  837. * \see setVolume()
  838. * \see setBalanceLeft()
  839. * \see setBalanceRight()
  840. */
  841. void setParameterValueByRIndex(const int32_t rindex, const double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  842. {
  843. Q_ASSERT(rindex >= PARAMETER_BALANCE_RIGHT && rindex != PARAMETER_NULL);
  844. if (rindex == PARAMETER_ACTIVE)
  845. return setActive(value > 0.0, sendOsc, sendCallback);
  846. if (rindex == PARAMETER_DRYWET)
  847. return setDryWet(value, sendOsc, sendCallback);
  848. if (rindex == PARAMETER_VOLUME)
  849. return setVolume(value, sendOsc, sendCallback);
  850. if (rindex == PARAMETER_BALANCE_LEFT)
  851. return setBalanceLeft(value, sendOsc, sendCallback);
  852. if (rindex == PARAMETER_BALANCE_RIGHT)
  853. return setBalanceRight(value, sendOsc, sendCallback);
  854. for (uint32_t i=0; i < param.count; i++)
  855. {
  856. if (param.data[i].rindex == rindex)
  857. return setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  858. }
  859. }
  860. /*!
  861. * Set parameter's \a parameterId MIDI channel to \a channel.\n
  862. * \a channel must be between 0 and 15.
  863. */
  864. void setParameterMidiChannel(const uint32_t parameterId, uint8_t channel, const bool sendOsc, const bool sendCallback)
  865. {
  866. Q_ASSERT(parameterId < param.count && channel < 16);
  867. if (channel >= 16)
  868. channel = 16;
  869. param.data[parameterId].midiChannel = channel;
  870. #ifndef BUILD_BRIDGE
  871. if (sendOsc)
  872. x_engine->osc_send_control_set_parameter_midi_channel(m_id, parameterId, channel);
  873. #else
  874. Q_UNUSED(sendOsc);
  875. #endif
  876. if (sendCallback)
  877. x_engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, m_id, parameterId, channel, 0.0);
  878. }
  879. /*!
  880. * Set parameter's \a parameterId MIDI CC to \a cc.\n
  881. * \a cc must be between 0 and 95 (0x5F), or -1 for invalid.
  882. */
  883. void setParameterMidiCC(const uint32_t parameterId, int16_t cc, const bool sendOsc, const bool sendCallback)
  884. {
  885. Q_ASSERT(parameterId < param.count && cc >= -1);
  886. if (cc < -1 || cc > 0x5F)
  887. cc = -1;
  888. param.data[parameterId].midiCC = cc;
  889. #ifndef BUILD_BRIDGE
  890. if (sendOsc)
  891. x_engine->osc_send_control_set_parameter_midi_cc(m_id, parameterId, cc);
  892. #else
  893. Q_UNUSED(sendOsc);
  894. #endif
  895. if (sendCallback)
  896. x_engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, m_id, parameterId, cc, 0.0);
  897. }
  898. /*!
  899. * Add a custom data set.\n
  900. * If \a key already exists, its current value will be swapped with \a value.
  901. *
  902. * \param type Type of data used in \a value.
  903. * \param key A key identifing this data set.
  904. * \param value The value of the data set, of type \a type.
  905. * \param sendGui Send message change to plugin's custom GUI, if any
  906. *
  907. * \see customData()
  908. */
  909. virtual void setCustomData(const CustomDataType type, const char* const key, const char* const value, const bool sendGui)
  910. {
  911. Q_ASSERT(type != CUSTOM_DATA_INVALID);
  912. Q_ASSERT(key);
  913. Q_ASSERT(value);
  914. if (type == CUSTOM_DATA_INVALID)
  915. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - type is invalid", CustomDataType2str(type), key, value, bool2str(sendGui));
  916. if (! key)
  917. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - key is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  918. if (! value)
  919. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - value is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  920. bool saveData = true;
  921. switch (type)
  922. {
  923. case CUSTOM_DATA_INVALID:
  924. saveData = false;
  925. break;
  926. case CUSTOM_DATA_STRING:
  927. // Ignore some keys
  928. if (strncmp(key, "OSC:", 4) == 0 || strcmp(key, "guiVisible") == 0)
  929. saveData = false;
  930. 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)
  931. saveData = false;
  932. break;
  933. default:
  934. break;
  935. }
  936. if (saveData)
  937. {
  938. // Check if we already have this key
  939. for (size_t i=0; i < custom.size(); i++)
  940. {
  941. if (strcmp(custom[i].key, key) == 0)
  942. {
  943. free((void*)custom[i].value);
  944. custom[i].value = strdup(value);
  945. return;
  946. }
  947. }
  948. // Otherwise store it
  949. CustomData newData;
  950. newData.type = type;
  951. newData.key = strdup(key);
  952. newData.value = strdup(value);
  953. custom.push_back(newData);
  954. }
  955. }
  956. /*!
  957. * Set the complete chunk data as \a stringData.\n
  958. * \a stringData must a base64 encoded string of binary data.
  959. *
  960. * \see chunkData()
  961. *
  962. * \note Make sure to verify the plugin supports chunks before calling this function!
  963. */
  964. virtual void setChunkData(const char* const stringData)
  965. {
  966. Q_ASSERT(stringData);
  967. }
  968. /*!
  969. * Change the current plugin program to \a index.
  970. *
  971. * If \a index is negative the plugin's program will be considered unset.\n
  972. * The plugin's default parameter values will be updated when this function is called.
  973. *
  974. * \param index New program index to use
  975. * \param sendGui Send message change to plugin's custom GUI, if any
  976. * \param sendOsc Send message change over OSC
  977. * \param sendCallback Send message change to registered callback
  978. * \param block Block the audio callback
  979. */
  980. virtual void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  981. {
  982. Q_ASSERT(index >= -1 && index < (int32_t)prog.count);
  983. if (index < -1)
  984. index = -1;
  985. else if (index > (int32_t)prog.count)
  986. return;
  987. prog.current = index;
  988. if (sendGui && index >= 0)
  989. uiProgramChange(index);
  990. #ifndef BUILD_BRIDGE
  991. if (sendOsc)
  992. x_engine->osc_send_control_set_program(m_id, index);
  993. #else
  994. Q_UNUSED(sendOsc);
  995. #endif
  996. // Change default parameter values
  997. if (index >= 0)
  998. {
  999. for (uint32_t i=0; i < param.count; i++)
  1000. {
  1001. param.ranges[i].def = getParameterValue(i);
  1002. #ifndef BUILD_BRIDGE
  1003. if (sendOsc)
  1004. x_engine->osc_send_control_set_default_value(m_id, i, param.ranges[i].def);
  1005. #endif
  1006. }
  1007. }
  1008. if (sendCallback)
  1009. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, index, 0, 0.0);
  1010. Q_UNUSED(block);
  1011. }
  1012. /*!
  1013. * Change the current MIDI plugin program to \a index.
  1014. *
  1015. * If \a index is negative the plugin's program will be considered unset.\n
  1016. * The plugin's default parameter values will be updated when this function is called.
  1017. *
  1018. * \param index New program index to use
  1019. * \param sendGui Send message change to plugin's custom GUI, if any
  1020. * \param sendOsc Send message change over OSC
  1021. * \param sendCallback Send message change to registered callback
  1022. * \param block Block the audio callback
  1023. */
  1024. virtual void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  1025. {
  1026. Q_ASSERT(index >= -1 && index < (int32_t)midiprog.count);
  1027. if (index < -1)
  1028. index = -1;
  1029. else if (index > (int32_t)midiprog.count)
  1030. return;
  1031. midiprog.current = index;
  1032. if (sendGui && index >= 0)
  1033. uiMidiProgramChange(index);
  1034. #ifndef BUILD_BRIDGE
  1035. if (sendOsc)
  1036. x_engine->osc_send_control_set_midi_program(m_id, index);
  1037. #else
  1038. Q_UNUSED(sendOsc);
  1039. #endif
  1040. // Change default parameter values (sound banks never change defaults)
  1041. if (index >= 0 && m_type != PLUGIN_GIG && m_type != PLUGIN_SF2 && m_type != PLUGIN_SFZ)
  1042. {
  1043. for (uint32_t i=0; i < param.count; i++)
  1044. {
  1045. param.ranges[i].def = getParameterValue(i);
  1046. #ifndef BUILD_BRIDGE
  1047. if (sendOsc)
  1048. x_engine->osc_send_control_set_default_value(m_id, i, param.ranges[i].def);
  1049. #endif
  1050. }
  1051. }
  1052. if (sendCallback)
  1053. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, index, 0, 0.0);
  1054. Q_UNUSED(block);
  1055. }
  1056. /*!
  1057. * This is an overloaded call to setMidiProgram().\n
  1058. * It changes the current MIDI program using \a bank and \a program values instead of index.
  1059. */
  1060. void setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  1061. {
  1062. Q_ASSERT(program < 128);
  1063. for (uint32_t i=0; i < midiprog.count; i++)
  1064. {
  1065. if (midiprog.data[i].bank == bank && midiprog.data[i].program == program)
  1066. return setMidiProgram(i, sendGui, sendOsc, sendCallback, block);
  1067. }
  1068. }
  1069. // -------------------------------------------------------------------
  1070. // Set gui stuff
  1071. /*!
  1072. * Set the plugin's custom GUI data.\n
  1073. * Parameters change between plugin types.
  1074. *
  1075. * \note This function must be always called from the main thread.
  1076. */
  1077. virtual void setGuiData(QDialog* const dialog)
  1078. {
  1079. Q_UNUSED(dialog);
  1080. }
  1081. /*!
  1082. * Show (or hide) the plugin's custom GUI according to \a yesNo.
  1083. *
  1084. * \note This function must be always called from the main thread.
  1085. */
  1086. virtual void showGui(const bool yesNo)
  1087. {
  1088. Q_UNUSED(yesNo);
  1089. }
  1090. /*!
  1091. * Idle the plugin's custom GUI.
  1092. *
  1093. * \note This function must be always called from the main thread.
  1094. */
  1095. virtual void idleGui()
  1096. {
  1097. if (! m_enabled)
  1098. return;
  1099. if (m_hints & PLUGIN_USES_SINGLE_THREAD)
  1100. {
  1101. // Process postponed events
  1102. postEventsRun();
  1103. // Update parameter outputs
  1104. for (uint32_t i=0; i < param.count; i++)
  1105. {
  1106. if (param.data[i].type == PARAMETER_OUTPUT)
  1107. uiParameterChange(i, getParameterValue(i));
  1108. }
  1109. }
  1110. }
  1111. // -------------------------------------------------------------------
  1112. // Plugin state
  1113. /*!
  1114. * Reload the plugin's entire state (including programs).\n
  1115. * The plugin will be disabled during this call.
  1116. */
  1117. virtual void reload()
  1118. {
  1119. }
  1120. /*!
  1121. * Reload the plugin's programs state.
  1122. */
  1123. virtual void reloadPrograms(const bool init)
  1124. {
  1125. Q_UNUSED(init);
  1126. }
  1127. /*!
  1128. * Tell the plugin to prepare for save.
  1129. */
  1130. virtual void prepareForSave()
  1131. {
  1132. }
  1133. // -------------------------------------------------------------------
  1134. // Plugin processing
  1135. /*!
  1136. * Plugin process callback.
  1137. */
  1138. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset = 0)
  1139. {
  1140. Q_UNUSED(inBuffer);
  1141. Q_UNUSED(outBuffer);
  1142. Q_UNUSED(frames);
  1143. Q_UNUSED(framesOffset);
  1144. }
  1145. #ifdef CARLA_ENGINE_JACK
  1146. /*!
  1147. * Plugin process callback, JACK helper version.
  1148. */
  1149. void process_jack(const uint32_t nframes)
  1150. {
  1151. float* inBuffer[aIn.count];
  1152. float* outBuffer[aOut.count];
  1153. for (uint32_t i=0; i < aIn.count; i++)
  1154. inBuffer[i] = aIn.ports[i]->getJackAudioBuffer(nframes);
  1155. for (uint32_t i=0; i < aOut.count; i++)
  1156. outBuffer[i] = aOut.ports[i]->getJackAudioBuffer(nframes);
  1157. #ifndef BUILD_BRIDGE
  1158. if (carlaOptions.processHighPrecision)
  1159. {
  1160. float* inBuffer2[aIn.count];
  1161. float* outBuffer2[aOut.count];
  1162. for (uint32_t i=0, j; i < nframes; i += 8)
  1163. {
  1164. for (j=0; j < aIn.count; j++)
  1165. inBuffer2[j] = inBuffer[j] + i;
  1166. for (j=0; j < aOut.count; j++)
  1167. outBuffer2[j] = outBuffer[j] + i;
  1168. process(inBuffer2, outBuffer2, 8, i);
  1169. }
  1170. }
  1171. else
  1172. #endif
  1173. process(inBuffer, outBuffer, nframes);
  1174. }
  1175. #endif
  1176. /*!
  1177. * Tell the plugin the current buffer size has changed.
  1178. */
  1179. virtual void bufferSizeChanged(const uint32_t newBufferSize)
  1180. {
  1181. Q_UNUSED(newBufferSize);
  1182. }
  1183. // -------------------------------------------------------------------
  1184. // OSC stuff
  1185. /*!
  1186. * Register this plugin to the engine's OSC controller.
  1187. */
  1188. void registerToOsc()
  1189. {
  1190. #ifndef BUILD_BRIDGE
  1191. if (! x_engine->isOscControllerRegisted())
  1192. return;
  1193. x_engine->osc_send_control_add_plugin(m_id, m_name);
  1194. #endif
  1195. // Base data
  1196. {
  1197. char bufName[STR_MAX] = { 0 };
  1198. char bufLabel[STR_MAX] = { 0 };
  1199. char bufMaker[STR_MAX] = { 0 };
  1200. char bufCopyright[STR_MAX] = { 0 };
  1201. getRealName(bufName);
  1202. getLabel(bufLabel);
  1203. getMaker(bufMaker);
  1204. getCopyright(bufCopyright);
  1205. #ifdef BUILD_BRIDGE
  1206. x_engine->osc_send_bridge_plugin_info(category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1207. #else
  1208. x_engine->osc_send_control_set_plugin_data(m_id, m_type, category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1209. #endif
  1210. }
  1211. // Base count
  1212. {
  1213. uint32_t cIns, cOuts, cTotals;
  1214. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  1215. #ifdef BUILD_BRIDGE
  1216. x_engine->osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  1217. x_engine->osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  1218. x_engine->osc_send_bridge_parameter_count(cIns, cOuts, cTotals);
  1219. #else
  1220. x_engine->osc_send_control_set_plugin_ports(m_id, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  1221. #endif
  1222. }
  1223. // Internal Parameters
  1224. {
  1225. #ifndef BUILD_BRIDGE
  1226. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1227. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_DRYWET, x_dryWet);
  1228. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_VOLUME, x_volume);
  1229. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, x_balanceLeft);
  1230. x_engine->osc_send_control_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, x_balanceRight);
  1231. #endif
  1232. }
  1233. // Plugin Parameters
  1234. #ifdef BUILD_BRIDGE
  1235. uint32_t maxParameters = MAX_PARAMETERS;
  1236. #else
  1237. uint32_t maxParameters = carlaOptions.maxParameters;
  1238. #endif
  1239. if (param.count > 0 && param.count < maxParameters)
  1240. {
  1241. char bufName[STR_MAX], bufUnit[STR_MAX];
  1242. for (uint32_t i=0; i < param.count; i++)
  1243. {
  1244. getParameterName(i, bufName);
  1245. getParameterUnit(i, bufUnit);
  1246. #ifdef BUILD_BRIDGE
  1247. x_engine->osc_send_bridge_parameter_info(i, bufName, bufUnit);
  1248. 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);
  1249. 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);
  1250. x_engine->osc_send_bridge_set_parameter_value(i, getParameterValue(i));
  1251. #else
  1252. x_engine->osc_send_control_set_parameter_data(m_id, i, param.data[i].type, param.data[i].hints, bufName, bufUnit, getParameterValue(i));
  1253. 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);
  1254. x_engine->osc_send_control_set_parameter_value(m_id, i, getParameterValue(i));
  1255. #endif
  1256. }
  1257. }
  1258. // Programs
  1259. if (prog.count > 0)
  1260. {
  1261. #ifdef BUILD_BRIDGE
  1262. x_engine->osc_send_bridge_program_count(prog.count);
  1263. for (uint32_t i=0; i < prog.count; i++)
  1264. x_engine->osc_send_bridge_program_info(i, prog.names[i]);
  1265. x_engine->osc_send_bridge_set_program(prog.current);
  1266. #else
  1267. x_engine->osc_send_control_set_program_count(m_id, prog.count);
  1268. for (uint32_t i=0; i < prog.count; i++)
  1269. x_engine->osc_send_control_set_program_name(m_id, i, prog.names[i]);
  1270. x_engine->osc_send_control_set_program(m_id, prog.current);
  1271. #endif
  1272. }
  1273. // MIDI Programs
  1274. if (midiprog.count > 0)
  1275. {
  1276. #ifdef BUILD_BRIDGE
  1277. x_engine->osc_send_bridge_midi_program_count(midiprog.count);
  1278. for (uint32_t i=0; i < midiprog.count; i++)
  1279. x_engine->osc_send_bridge_midi_program_info(i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1280. x_engine->osc_send_bridge_set_midi_program(prog.current);
  1281. #else
  1282. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  1283. for (uint32_t i=0; i < midiprog.count; i++)
  1284. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1285. x_engine->osc_send_control_set_midi_program(m_id, midiprog.current);
  1286. #endif
  1287. }
  1288. }
  1289. #ifndef BUILD_BRIDGE
  1290. /*!
  1291. * Update the plugin's internal OSC data according to \a source and \a url.\n
  1292. * This is used for OSC-GUI bridges.
  1293. */
  1294. void updateOscData(const lo_address source, const char* const url)
  1295. {
  1296. const char* host;
  1297. const char* port;
  1298. osc_clear_data(&osc.data);
  1299. host = lo_address_get_hostname(source);
  1300. port = lo_address_get_port(source);
  1301. osc.data.source = lo_address_new(host, port);
  1302. host = lo_url_get_hostname(url);
  1303. port = lo_url_get_port(url);
  1304. osc.data.path = lo_url_get_path(url);
  1305. osc.data.target = lo_address_new(host, port);
  1306. free((void*)host);
  1307. free((void*)port);
  1308. if (m_hints & PLUGIN_IS_BRIDGE)
  1309. return;
  1310. osc_send_sample_rate(&osc.data, x_engine->getSampleRate());
  1311. for (size_t i=0; i < custom.size(); i++)
  1312. {
  1313. // TODO
  1314. //if (m_type == PLUGIN_LV2)
  1315. //osc_send_lv2_transfer_event(&osc.data, getCustomDataTypeString(custom[i].type), /*custom[i].key,*/ custom[i].value);
  1316. //else
  1317. if (custom[i].type == CUSTOM_DATA_STRING)
  1318. osc_send_configure(&osc.data, custom[i].key, custom[i].value);
  1319. }
  1320. if (prog.current >= 0)
  1321. osc_send_program(&osc.data, prog.current);
  1322. if (midiprog.current >= 0)
  1323. {
  1324. if (m_type == PLUGIN_DSSI)
  1325. osc_send_program(&osc.data, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  1326. else
  1327. osc_send_midi_program(&osc.data, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  1328. }
  1329. for (uint32_t i=0; i < param.count; i++)
  1330. osc_send_control(&osc.data, param.data[i].rindex, getParameterValue(i));
  1331. // if (m_hints & PLUGIN_IS_BRIDGE)
  1332. // {
  1333. // osc_send_control(&osc.data, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1334. // osc_send_control(&osc.data, PARAMETER_DRYWET, x_dryWet);
  1335. // osc_send_control(&osc.data, PARAMETER_VOLUME, x_volume);
  1336. // osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, x_balanceLeft);
  1337. // osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, x_balanceRight);
  1338. // }
  1339. }
  1340. /*!
  1341. * Clear the plugin's internal OSC data.
  1342. */
  1343. void clearOscData()
  1344. {
  1345. osc_clear_data(&osc.data);
  1346. }
  1347. /*!
  1348. * Show the plugin's OSC based GUI.\n
  1349. * This is a handy function that waits for the GUI to respond and automatically asks it to show itself.
  1350. */
  1351. bool showOscGui()
  1352. {
  1353. // wait for UI 'update' call
  1354. for (uint i=0; i < carlaOptions.oscUiTimeout; i++)
  1355. {
  1356. if (osc.data.target)
  1357. {
  1358. osc_send_show(&osc.data);
  1359. return true;
  1360. }
  1361. else
  1362. carla_msleep(100);
  1363. }
  1364. return false;
  1365. }
  1366. #endif
  1367. // -------------------------------------------------------------------
  1368. // MIDI events
  1369. /*!
  1370. * Send a single midi note to be processed in the next audio callback.\n
  1371. * A note with 0 velocity means note-off.
  1372. */
  1373. void sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1374. {
  1375. Q_ASSERT(channel < 16);
  1376. Q_ASSERT(note < 128);
  1377. Q_ASSERT(velo < 128);
  1378. engineMidiLock();
  1379. for (unsigned short i=0; i < MAX_MIDI_EVENTS; i++)
  1380. {
  1381. if (extMidiNotes[i].channel < 0)
  1382. {
  1383. extMidiNotes[i].channel = channel;
  1384. extMidiNotes[i].note = note;
  1385. extMidiNotes[i].velo = velo;
  1386. break;
  1387. }
  1388. }
  1389. engineMidiUnlock();
  1390. if (sendGui)
  1391. {
  1392. if (velo > 0)
  1393. uiNoteOn(channel, note, velo);
  1394. else
  1395. uiNoteOff(channel, note);
  1396. }
  1397. #ifndef BUILD_BRIDGE
  1398. if (sendOsc)
  1399. {
  1400. if (velo > 0)
  1401. x_engine->osc_send_control_note_on(m_id, channel, note, velo);
  1402. else
  1403. x_engine->osc_send_control_note_off(m_id, channel, note);
  1404. }
  1405. #else
  1406. Q_UNUSED(sendOsc);
  1407. #endif
  1408. if (sendCallback)
  1409. x_engine->callback(velo ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, m_id, note, velo, 0.0);
  1410. }
  1411. /*!
  1412. * Send all midi notes off for the next audio callback.\n
  1413. * This doesn't send the actual MIDI All-Notes-Off event, but 128 note-offs instead.
  1414. */
  1415. void sendMidiAllNotesOff()
  1416. {
  1417. engineMidiLock();
  1418. postEvents.mutex.lock();
  1419. unsigned short postPad = 0;
  1420. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1421. {
  1422. if (postEvents.data[i].type == PluginPostEventNull)
  1423. {
  1424. postPad = i;
  1425. break;
  1426. }
  1427. }
  1428. if (postPad == MAX_POST_EVENTS - 1)
  1429. {
  1430. qWarning("post-events buffer full, making room for all notes off now");
  1431. postPad -= 128;
  1432. }
  1433. for (unsigned short i=0; i < 128; i++)
  1434. {
  1435. extMidiNotes[i].channel = m_ctrlInChannel;
  1436. extMidiNotes[i].note = i;
  1437. extMidiNotes[i].velo = 0;
  1438. postEvents.data[i + postPad].type = PluginPostEventNoteOff;
  1439. postEvents.data[i + postPad].value1 = i;
  1440. postEvents.data[i + postPad].value2 = 0;
  1441. postEvents.data[i + postPad].value3 = 0.0;
  1442. }
  1443. postEvents.mutex.unlock();
  1444. engineMidiUnlock();
  1445. }
  1446. // -------------------------------------------------------------------
  1447. // Post-poned events
  1448. /*!
  1449. * Post pone an event of type \a type.\n
  1450. * The event will be processed later, but as soon as possible.
  1451. */
  1452. void postponeEvent(const PluginPostEventType type, const int32_t value1, const int32_t value2, const double value3, const void* const cdata = nullptr)
  1453. {
  1454. postEvents.mutex.lock();
  1455. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1456. {
  1457. if (postEvents.data[i].type == PluginPostEventNull)
  1458. {
  1459. postEvents.data[i].type = type;
  1460. postEvents.data[i].value1 = value1;
  1461. postEvents.data[i].value2 = value2;
  1462. postEvents.data[i].value3 = value3;
  1463. postEvents.data[i].cdata = cdata;
  1464. break;
  1465. }
  1466. }
  1467. postEvents.mutex.unlock();
  1468. }
  1469. /*!
  1470. * Process all the post-poned events.
  1471. * This function will only be called from the main thread if PLUGIN_USES_SINGLE_THREAD is set.
  1472. */
  1473. void postEventsRun()
  1474. {
  1475. PluginPostEvent newPostEvents[MAX_POST_EVENTS];
  1476. // Make a safe copy of events, and clear them
  1477. postEvents.mutex.lock();
  1478. memcpy(newPostEvents, postEvents.data, sizeof(PluginPostEvent)*MAX_POST_EVENTS);
  1479. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1480. postEvents.data[i].type = PluginPostEventNull;
  1481. postEvents.mutex.unlock();
  1482. // Handle events now
  1483. for (uint32_t i=0; i < MAX_POST_EVENTS; i++)
  1484. {
  1485. const PluginPostEvent* const event = &newPostEvents[i];
  1486. switch (event->type)
  1487. {
  1488. case PluginPostEventNull:
  1489. break;
  1490. case PluginPostEventDebug:
  1491. x_engine->callback(CALLBACK_DEBUG, m_id, event->value1, event->value2, event->value3);
  1492. break;
  1493. case PluginPostEventParameterChange:
  1494. // Update UI
  1495. if (event->value1 >= 0)
  1496. uiParameterChange(event->value1, event->value3);
  1497. #ifndef BUILD_BRIDGE
  1498. // Update OSC control client
  1499. x_engine->osc_send_control_set_parameter_value(m_id, event->value1, event->value3);
  1500. #endif
  1501. // Update Host
  1502. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, event->value1, 0, event->value3);
  1503. break;
  1504. case PluginPostEventProgramChange:
  1505. // Update UI
  1506. if (event->value1 >= 0)
  1507. uiProgramChange(event->value1);
  1508. #ifndef BUILD_BRIDGE
  1509. // Update OSC control client
  1510. x_engine->osc_send_control_set_program(m_id, event->value1);
  1511. for (uint32_t j=0; j < param.count; j++)
  1512. x_engine->osc_send_control_set_default_value(m_id, j, param.ranges[j].def);
  1513. #endif
  1514. // Update Host
  1515. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1516. break;
  1517. case PluginPostEventMidiProgramChange:
  1518. // Update UI
  1519. if (event->value1 >= 0)
  1520. uiMidiProgramChange(event->value1);
  1521. #ifndef BUILD_BRIDGE
  1522. // Update OSC control client
  1523. x_engine->osc_send_control_set_midi_program(m_id, event->value1);
  1524. for (uint32_t j=0; j < param.count; j++)
  1525. x_engine->osc_send_control_set_default_value(m_id, j, param.ranges[j].def);
  1526. #endif
  1527. // Update Host
  1528. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1529. break;
  1530. case PluginPostEventNoteOn:
  1531. // Update UI
  1532. uiNoteOn(event->value1, event->value2, rint(event->value3));
  1533. #ifndef BUILD_BRIDGE
  1534. // Update OSC control client
  1535. x_engine->osc_send_control_note_on(m_id, event->value1, event->value2, rint(event->value3));
  1536. #endif
  1537. // Update Host
  1538. x_engine->callback(CALLBACK_NOTE_ON, m_id, event->value1, event->value2, event->value3);
  1539. break;
  1540. case PluginPostEventNoteOff:
  1541. // Update UI
  1542. uiNoteOff(event->value1, event->value2);
  1543. #ifndef BUILD_BRIDGE
  1544. // Update OSC control client
  1545. x_engine->osc_send_control_note_off(m_id, event->value1, event->value2);
  1546. #endif
  1547. // Update Host
  1548. x_engine->callback(CALLBACK_NOTE_OFF, m_id, event->value1, event->value2, 0.0);
  1549. break;
  1550. case PluginPostEventCustom:
  1551. // Handle custom event
  1552. postEventHandleCustom(event->value1, event->value2, event->value3, event->cdata);
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. /*!
  1558. * Handle custom post event.\n
  1559. * Implementation depends on plugin type.
  1560. */
  1561. virtual void postEventHandleCustom(const int32_t value1, const int32_t value2, const double value3, const void* const cdata)
  1562. {
  1563. Q_UNUSED(value1);
  1564. Q_UNUSED(value2);
  1565. Q_UNUSED(value3);
  1566. Q_UNUSED(cdata);
  1567. }
  1568. /*!
  1569. * Tell the UI a parameter has changed.
  1570. */
  1571. virtual void uiParameterChange(const uint32_t index, const double value)
  1572. {
  1573. Q_ASSERT(index < param.count);
  1574. Q_UNUSED(index);
  1575. Q_UNUSED(value);
  1576. }
  1577. /*!
  1578. * Tell the UI the current program has changed.
  1579. */
  1580. virtual void uiProgramChange(const uint32_t index)
  1581. {
  1582. Q_ASSERT(index < prog.count);
  1583. Q_UNUSED(index);
  1584. }
  1585. /*!
  1586. * Tell the UI the current midi program has changed.
  1587. */
  1588. virtual void uiMidiProgramChange(const uint32_t index)
  1589. {
  1590. Q_ASSERT(index < midiprog.count);
  1591. Q_UNUSED(index);
  1592. }
  1593. /*!
  1594. * Tell the UI a note has been pressed.
  1595. */
  1596. virtual void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1597. {
  1598. Q_ASSERT(channel < 16);
  1599. Q_ASSERT(note < 128);
  1600. Q_ASSERT(velo > 0 && velo < 128);
  1601. Q_UNUSED(channel);
  1602. Q_UNUSED(note);
  1603. Q_UNUSED(velo);
  1604. }
  1605. /*!
  1606. * Tell the UI a note has been released.
  1607. */
  1608. virtual void uiNoteOff(const uint8_t channel, const uint8_t note)
  1609. {
  1610. Q_ASSERT(channel < 16);
  1611. Q_ASSERT(note < 128);
  1612. Q_UNUSED(channel);
  1613. Q_UNUSED(note);
  1614. }
  1615. // -------------------------------------------------------------------
  1616. // Cleanup
  1617. /*!
  1618. * Clear the engine client ports of the plugin.
  1619. */
  1620. virtual void removeClientPorts()
  1621. {
  1622. qDebug("CarlaPlugin::removeClientPorts() - start");
  1623. for (uint32_t i=0; i < aIn.count; i++)
  1624. {
  1625. delete aIn.ports[i];
  1626. aIn.ports[i] = nullptr;
  1627. }
  1628. for (uint32_t i=0; i < aOut.count; i++)
  1629. {
  1630. delete aOut.ports[i];
  1631. aOut.ports[i] = nullptr;
  1632. }
  1633. if (midi.portMin)
  1634. {
  1635. delete midi.portMin;
  1636. midi.portMin = nullptr;
  1637. }
  1638. if (midi.portMout)
  1639. {
  1640. delete midi.portMout;
  1641. midi.portMout = nullptr;
  1642. }
  1643. if (param.portCin)
  1644. {
  1645. delete param.portCin;
  1646. param.portCin = nullptr;
  1647. }
  1648. if (param.portCout)
  1649. {
  1650. delete param.portCout;
  1651. param.portCout = nullptr;
  1652. }
  1653. qDebug("CarlaPlugin::removeClientPorts() - end");
  1654. }
  1655. /*!
  1656. * Initializes all RT buffers of the plugin.
  1657. */
  1658. virtual void initBuffers()
  1659. {
  1660. uint32_t i;
  1661. for (i=0; i < aIn.count; i++)
  1662. {
  1663. if (aIn.ports[i])
  1664. aIn.ports[i]->initBuffer(x_engine);
  1665. }
  1666. for (i=0; i < aOut.count; i++)
  1667. {
  1668. if (aOut.ports[i])
  1669. aOut.ports[i]->initBuffer(x_engine);
  1670. }
  1671. if (param.portCin)
  1672. param.portCin->initBuffer(x_engine);
  1673. if (param.portCout)
  1674. param.portCout->initBuffer(x_engine);
  1675. if (midi.portMin)
  1676. midi.portMin->initBuffer(x_engine);
  1677. if (midi.portMout)
  1678. midi.portMout->initBuffer(x_engine);
  1679. }
  1680. /*!
  1681. * Delete all temporary buffers of the plugin.
  1682. */
  1683. virtual void deleteBuffers()
  1684. {
  1685. qDebug("CarlaPlugin::deleteBuffers() - start");
  1686. if (aIn.count > 0)
  1687. {
  1688. delete[] aIn.ports;
  1689. delete[] aIn.rindexes;
  1690. }
  1691. if (aOut.count > 0)
  1692. {
  1693. delete[] aOut.ports;
  1694. delete[] aOut.rindexes;
  1695. }
  1696. if (param.count > 0)
  1697. {
  1698. delete[] param.data;
  1699. delete[] param.ranges;
  1700. }
  1701. aIn.count = 0;
  1702. aIn.ports = nullptr;
  1703. aIn.rindexes = nullptr;
  1704. aOut.count = 0;
  1705. aOut.ports = nullptr;
  1706. aOut.rindexes = nullptr;
  1707. param.count = 0;
  1708. param.data = nullptr;
  1709. param.ranges = nullptr;
  1710. param.portCin = nullptr;
  1711. param.portCout = nullptr;
  1712. qDebug("CarlaPlugin::deleteBuffers() - end");
  1713. }
  1714. // -------------------------------------------------------------------
  1715. // Library functions
  1716. /*!
  1717. * Open the DLL \a filename.
  1718. */
  1719. bool libOpen(const char* const filename)
  1720. {
  1721. m_lib = lib_open(filename);
  1722. return bool(m_lib);
  1723. }
  1724. /*!
  1725. * Close the DLL previously loaded in libOpen().
  1726. */
  1727. bool libClose()
  1728. {
  1729. if (m_lib)
  1730. return lib_close(m_lib);
  1731. return false;
  1732. }
  1733. /*!
  1734. * Get the symbol entry \a symbol of the currently loaded DLL.
  1735. */
  1736. void* libSymbol(const char* const symbol)
  1737. {
  1738. if (m_lib)
  1739. return lib_symbol(m_lib, symbol);
  1740. return nullptr;
  1741. }
  1742. /*!
  1743. * Get the last DLL related error.
  1744. */
  1745. const char* libError(const char* const filename)
  1746. {
  1747. return lib_error(filename);
  1748. }
  1749. // -------------------------------------------------------------------
  1750. // Locks
  1751. void engineProcessLock()
  1752. {
  1753. x_engine->processLock();
  1754. }
  1755. void engineProcessUnlock()
  1756. {
  1757. x_engine->processUnlock();
  1758. }
  1759. void engineMidiLock()
  1760. {
  1761. x_engine->midiLock();
  1762. }
  1763. void engineMidiUnlock()
  1764. {
  1765. x_engine->midiUnlock();
  1766. }
  1767. // -------------------------------------------------------------------
  1768. // Plugin initializers
  1769. struct initializer {
  1770. CarlaEngine* const engine;
  1771. const char* const filename;
  1772. const char* const name;
  1773. const char* const label;
  1774. };
  1775. static CarlaPlugin* newLADSPA(const initializer& init, const void* const extra);
  1776. static CarlaPlugin* newDSSI(const initializer& init, const void* const extra);
  1777. static CarlaPlugin* newLV2(const initializer& init);
  1778. static CarlaPlugin* newVST(const initializer& init);
  1779. static CarlaPlugin* newGIG(const initializer& init);
  1780. static CarlaPlugin* newSF2(const initializer& init);
  1781. static CarlaPlugin* newSFZ(const initializer& init);
  1782. #ifndef BUILD_BRIDGE
  1783. static CarlaPlugin* newBridge(const initializer& init, const BinaryType btype, const PluginType ptype);
  1784. #endif
  1785. // -------------------------------------------------------------------
  1786. /*!
  1787. * \class CarlaPluginScopedDisabler
  1788. *
  1789. * \brief Carla plugin scoped disabler
  1790. *
  1791. * This is a handy class that temporarily disables a plugin during a function scope.\n
  1792. * It should be used when the plugin needs reload or state change, something like this:
  1793. * \code
  1794. * {
  1795. * const CarlaPluginScopedDisabler m(plugin);
  1796. * plugin->setChunkData(data);
  1797. * }
  1798. * \endcode
  1799. */
  1800. class ScopedDisabler
  1801. {
  1802. public:
  1803. /*!
  1804. * Disable plugin \a plugin if \a disable is true.
  1805. * The plugin is re-enabled in the deconstructor of this class if \a disable is true.
  1806. *
  1807. * \param plugin The plugin to disable
  1808. * \param disable Wherever to disable the plugin or not, true by default
  1809. */
  1810. ScopedDisabler(CarlaPlugin* const plugin, const bool disable = true) :
  1811. m_plugin(plugin),
  1812. m_disable(disable)
  1813. {
  1814. if (m_disable)
  1815. {
  1816. m_plugin->engineProcessLock();
  1817. m_plugin->setEnabled(false);
  1818. m_plugin->engineProcessUnlock();
  1819. }
  1820. }
  1821. ~ScopedDisabler()
  1822. {
  1823. if (m_disable)
  1824. {
  1825. m_plugin->engineProcessLock();
  1826. m_plugin->setEnabled(true);
  1827. m_plugin->engineProcessUnlock();
  1828. }
  1829. }
  1830. private:
  1831. CarlaPlugin* const m_plugin;
  1832. const bool m_disable;
  1833. };
  1834. // -------------------------------------------------------------------
  1835. protected:
  1836. unsigned short m_id;
  1837. CarlaEngine* const x_engine;
  1838. CarlaEngineClient* x_client;
  1839. double x_dryWet, x_volume;
  1840. double x_balanceLeft, x_balanceRight;
  1841. PluginType m_type;
  1842. unsigned int m_hints;
  1843. bool m_active;
  1844. bool m_activeBefore;
  1845. bool m_enabled;
  1846. void* m_lib;
  1847. const char* m_name;
  1848. const char* m_filename;
  1849. int8_t m_ctrlInChannel;
  1850. // -------------------------------------------------------------------
  1851. // Storage Data
  1852. PluginAudioData aIn;
  1853. PluginAudioData aOut;
  1854. PluginMidiData midi;
  1855. PluginParameterData param;
  1856. PluginProgramData prog;
  1857. PluginMidiProgramData midiprog;
  1858. std::vector<CustomData> custom;
  1859. // -------------------------------------------------------------------
  1860. // Extra
  1861. #ifndef BUILD_BRIDGE
  1862. struct {
  1863. CarlaOscData data;
  1864. CarlaPluginThread* thread;
  1865. } osc;
  1866. #endif
  1867. struct {
  1868. QMutex mutex;
  1869. PluginPostEvent data[MAX_POST_EVENTS];
  1870. } postEvents;
  1871. ExternalMidiNote extMidiNotes[MAX_MIDI_EVENTS];
  1872. // -------------------------------------------------------------------
  1873. // Utilities
  1874. static double fixParameterValue(double& value, const ParameterRanges& ranges)
  1875. {
  1876. if (value < ranges.min)
  1877. value = ranges.min;
  1878. else if (value > ranges.max)
  1879. value = ranges.max;
  1880. return value;
  1881. }
  1882. static float fixParameterValue(float& value, const ParameterRanges& ranges)
  1883. {
  1884. if (value < ranges.min)
  1885. value = ranges.min;
  1886. else if (value > ranges.max)
  1887. value = ranges.max;
  1888. return value;
  1889. }
  1890. static double abs(const double& value)
  1891. {
  1892. return (value < 0.0) ? -value : value;
  1893. }
  1894. };
  1895. /**@}*/
  1896. CARLA_BACKEND_END_NAMESPACE
  1897. #endif // CARLA_PLUGIN_H