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.

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