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.

2136 lines
57KB

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