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.

2225 lines
61KB

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