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.

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