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.

2228 lines
60KB

  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 || strcmp(key, CARLA_BRIDGE_MSG_SAVE_NOW) == 0 || strcmp(key, CARLA_BRIDGE_MSG_SET_CHUNK) == 0 || strcmp(key, CARLA_BRIDGE_MSG_SET_CUSTOM) == 0)
  936. saveData = false;
  937. break;
  938. default:
  939. break;
  940. }
  941. if (saveData)
  942. {
  943. // Check if we already have this key
  944. for (size_t i=0; i < custom.size(); i++)
  945. {
  946. if (strcmp(custom[i].key, key) == 0)
  947. {
  948. free((void*)custom[i].value);
  949. custom[i].value = strdup(value);
  950. return;
  951. }
  952. }
  953. // Otherwise store it
  954. CustomData newData;
  955. newData.type = type;
  956. newData.key = strdup(key);
  957. newData.value = strdup(value);
  958. custom.push_back(newData);
  959. }
  960. }
  961. /*!
  962. * Set the complete chunk data as \a stringData.\n
  963. * \a stringData must a base64 encoded string of binary data.
  964. *
  965. * \see chunkData()
  966. *
  967. * \note Make sure to verify the plugin supports chunks before calling this function!
  968. */
  969. virtual void setChunkData(const char* const stringData)
  970. {
  971. Q_ASSERT(stringData);
  972. }
  973. /*!
  974. * Change the current plugin program to \a index.
  975. *
  976. * If \a index is negative the plugin's program will be considered unset.\n
  977. * The plugin's default parameter values will be updated when this function is called.
  978. *
  979. * \param index New program index to use
  980. * \param sendGui Send message change to plugin's custom GUI, if any
  981. * \param sendOsc Send message change over OSC
  982. * \param sendCallback Send message change to registered callback
  983. * \param block Block the audio callback
  984. */
  985. virtual void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  986. {
  987. Q_ASSERT(index >= -1 && index < (int32_t)prog.count);
  988. if (index < -1)
  989. index = -1;
  990. else if (index > (int32_t)prog.count)
  991. return;
  992. prog.current = index;
  993. if (sendGui && index >= 0)
  994. uiProgramChange(index);
  995. #ifndef BUILD_BRIDGE
  996. if (sendOsc)
  997. {
  998. x_engine->osc_send_set_program(m_id, index);
  999. if (m_hints & PLUGIN_IS_BRIDGE)
  1000. osc_send_program(&osc.data, index);
  1001. }
  1002. #else
  1003. Q_UNUSED(sendOsc);
  1004. #endif
  1005. // Change default parameter values
  1006. if (index >= 0)
  1007. {
  1008. for (uint32_t i=0; i < param.count; i++)
  1009. {
  1010. param.ranges[i].def = getParameterValue(i);
  1011. #ifndef BUILD_BRIDGE
  1012. if (sendOsc)
  1013. x_engine->osc_send_set_default_value(m_id, i, param.ranges[i].def);
  1014. #endif
  1015. }
  1016. }
  1017. if (sendCallback)
  1018. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, index, 0, 0.0);
  1019. Q_UNUSED(block);
  1020. }
  1021. /*!
  1022. * Change the current MIDI plugin program to \a index.
  1023. *
  1024. * If \a index is negative the plugin's program will be considered unset.\n
  1025. * The plugin's default parameter values will be updated when this function is called.
  1026. *
  1027. * \param index New program index to use
  1028. * \param sendGui Send message change to plugin's custom GUI, if any
  1029. * \param sendOsc Send message change over OSC
  1030. * \param sendCallback Send message change to registered callback
  1031. * \param block Block the audio callback
  1032. */
  1033. virtual void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  1034. {
  1035. Q_ASSERT(index >= -1 && index < (int32_t)midiprog.count);
  1036. if (index < -1)
  1037. index = -1;
  1038. else if (index > (int32_t)midiprog.count)
  1039. return;
  1040. midiprog.current = index;
  1041. if (sendGui && index >= 0)
  1042. uiMidiProgramChange(index);
  1043. #ifndef BUILD_BRIDGE
  1044. if (sendOsc)
  1045. {
  1046. x_engine->osc_send_set_midi_program(m_id, index);
  1047. if (m_hints & PLUGIN_IS_BRIDGE)
  1048. osc_send_midi_program(&osc.data, index);
  1049. }
  1050. #else
  1051. Q_UNUSED(sendOsc);
  1052. #endif
  1053. // Change default parameter values (sound banks never change defaults)
  1054. if (index >= 0 && m_type != PLUGIN_GIG && m_type != PLUGIN_SF2 && m_type != PLUGIN_SFZ)
  1055. {
  1056. for (uint32_t i=0; i < param.count; i++)
  1057. {
  1058. param.ranges[i].def = getParameterValue(i);
  1059. #ifndef BUILD_BRIDGE
  1060. if (sendOsc)
  1061. x_engine->osc_send_set_default_value(m_id, i, param.ranges[i].def);
  1062. #endif
  1063. }
  1064. }
  1065. if (sendCallback)
  1066. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, index, 0, 0.0);
  1067. Q_UNUSED(block);
  1068. }
  1069. /*!
  1070. * This is an overloaded call to setMidiProgram().\n
  1071. * It changes the current MIDI program using \a bank and \a program values instead of index.
  1072. */
  1073. void setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  1074. {
  1075. Q_ASSERT(program < 128);
  1076. for (uint32_t i=0; i < midiprog.count; i++)
  1077. {
  1078. if (midiprog.data[i].bank == bank && midiprog.data[i].program == program)
  1079. return setMidiProgram(i, sendGui, sendOsc, sendCallback, block);
  1080. }
  1081. }
  1082. // -------------------------------------------------------------------
  1083. // Set gui stuff
  1084. /*!
  1085. * Set the plugin's custom GUI data.\n
  1086. * Parameters change between plugin types.
  1087. *
  1088. * \note This function must be always called from the main thread.
  1089. */
  1090. virtual void setGuiData(const int data, const GuiDataHandle handle)
  1091. {
  1092. Q_UNUSED(data);
  1093. Q_UNUSED(handle);
  1094. }
  1095. /*!
  1096. * Show (or hide) the plugin's custom GUI according to \a yesNo.
  1097. *
  1098. * \note This function must be always called from the main thread.
  1099. */
  1100. virtual void showGui(const bool yesNo)
  1101. {
  1102. Q_UNUSED(yesNo);
  1103. }
  1104. /*!
  1105. * Idle the plugin's custom GUI.
  1106. *
  1107. * \note This function must be always called from the main thread.
  1108. */
  1109. virtual void idleGui()
  1110. {
  1111. if (! m_enabled)
  1112. return;
  1113. if (m_hints & PLUGIN_USES_SINGLE_THREAD)
  1114. {
  1115. // Process postponed events
  1116. postEventsRun();
  1117. // Update parameter outputs
  1118. for (uint32_t i=0; i < param.count; i++)
  1119. {
  1120. if (param.data[i].type == PARAMETER_OUTPUT)
  1121. uiParameterChange(i, getParameterValue(i));
  1122. }
  1123. }
  1124. }
  1125. // -------------------------------------------------------------------
  1126. // Plugin state
  1127. /*!
  1128. * Reload the plugin's entire state (including programs).\n
  1129. * The plugin will be disabled during this call.
  1130. */
  1131. virtual void reload()
  1132. {
  1133. }
  1134. /*!
  1135. * Reload the plugin's programs state.
  1136. */
  1137. virtual void reloadPrograms(const bool init)
  1138. {
  1139. Q_UNUSED(init);
  1140. }
  1141. /*!
  1142. * Tell the plugin to prepare for save.
  1143. */
  1144. virtual void prepareForSave()
  1145. {
  1146. }
  1147. // -------------------------------------------------------------------
  1148. // Plugin processing
  1149. /*!
  1150. * Plugin process callback.
  1151. */
  1152. virtual void process(float* const* const inBuffer, float* const* const outBuffer, const uint32_t frames, const uint32_t framesOffset = 0)
  1153. {
  1154. Q_UNUSED(inBuffer);
  1155. Q_UNUSED(outBuffer);
  1156. Q_UNUSED(frames);
  1157. Q_UNUSED(framesOffset);
  1158. }
  1159. #ifdef CARLA_ENGINE_JACK
  1160. /*!
  1161. * Plugin process callback, JACK helper version.
  1162. */
  1163. void process_jack(const uint32_t nframes)
  1164. {
  1165. float* inBuffer[aIn.count];
  1166. float* outBuffer[aOut.count];
  1167. for (uint32_t i=0; i < aIn.count; i++)
  1168. inBuffer[i] = aIn.ports[i]->getJackAudioBuffer(nframes);
  1169. for (uint32_t i=0; i < aOut.count; i++)
  1170. outBuffer[i] = aOut.ports[i]->getJackAudioBuffer(nframes);
  1171. #ifndef BUILD_BRIDGE
  1172. if (carlaOptions.processHighPrecision)
  1173. {
  1174. float* inBuffer2[aIn.count];
  1175. float* outBuffer2[aOut.count];
  1176. for (uint32_t i=0, j; i < nframes; i += 8)
  1177. {
  1178. for (j=0; j < aIn.count; j++)
  1179. inBuffer2[j] = inBuffer[j] + i;
  1180. for (j=0; j < aOut.count; j++)
  1181. outBuffer2[j] = outBuffer[j] + i;
  1182. process(inBuffer2, outBuffer2, 8, i);
  1183. }
  1184. }
  1185. else
  1186. #endif
  1187. process(inBuffer, outBuffer, nframes);
  1188. }
  1189. #endif
  1190. /*!
  1191. * Tell the plugin the current buffer size has changed.
  1192. */
  1193. virtual void bufferSizeChanged(const uint32_t newBufferSize)
  1194. {
  1195. Q_UNUSED(newBufferSize);
  1196. }
  1197. // -------------------------------------------------------------------
  1198. // OSC stuff
  1199. /*!
  1200. * Register this plugin to the engine's OSC controller.
  1201. */
  1202. void registerToOsc()
  1203. {
  1204. #ifndef BUILD_BRIDGE
  1205. if (! x_engine->isOscControllerRegisted())
  1206. return;
  1207. x_engine->osc_send_add_plugin(m_id, m_name);
  1208. #endif
  1209. // Base data
  1210. {
  1211. char bufName[STR_MAX] = { 0 };
  1212. char bufLabel[STR_MAX] = { 0 };
  1213. char bufMaker[STR_MAX] = { 0 };
  1214. char bufCopyright[STR_MAX] = { 0 };
  1215. getRealName(bufName);
  1216. getLabel(bufLabel);
  1217. getMaker(bufMaker);
  1218. getCopyright(bufCopyright);
  1219. #ifdef BUILD_BRIDGE
  1220. osc_send_bridge_plugin_info(category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1221. #else
  1222. x_engine->osc_send_set_plugin_data(m_id, m_type, category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1223. #endif
  1224. }
  1225. // Base count
  1226. {
  1227. uint32_t cIns, cOuts, cTotals;
  1228. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  1229. #ifdef BUILD_BRIDGE
  1230. osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  1231. osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  1232. osc_send_bridge_param_count(cIns, cOuts, cTotals);
  1233. #else
  1234. x_engine->osc_send_set_plugin_ports(m_id, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  1235. #endif
  1236. }
  1237. // Internal Parameters
  1238. {
  1239. #ifndef BUILD_BRIDGE
  1240. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1241. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_DRYWET, x_dryWet);
  1242. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_VOLUME, x_volume);
  1243. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, x_balanceLeft);
  1244. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, x_balanceRight);
  1245. #endif
  1246. }
  1247. // Plugin Parameters
  1248. if (param.count > 0 && param.count < carlaOptions.maxParameters)
  1249. {
  1250. char bufName[STR_MAX], bufUnit[STR_MAX];
  1251. for (uint32_t i=0; i < param.count; i++)
  1252. {
  1253. getParameterName(i, bufName);
  1254. getParameterUnit(i, bufUnit);
  1255. #ifdef BUILD_BRIDGE
  1256. osc_send_bridge_param_info(i, bufName, bufUnit);
  1257. 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);
  1258. 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);
  1259. setParameterValue(i, param.ranges[i].def, false, false, true); // FIXME?
  1260. #else
  1261. x_engine->osc_send_set_parameter_data(m_id, i, param.data[i].type, param.data[i].hints, bufName, bufUnit, getParameterValue(i));
  1262. 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);
  1263. #endif
  1264. }
  1265. }
  1266. // Programs
  1267. {
  1268. #ifdef BUILD_BRIDGE
  1269. osc_send_bridge_program_count(prog.count);
  1270. for (uint32_t i=0; i < prog.count; i++)
  1271. osc_send_bridge_program_info(i, prog.names[i]);
  1272. osc_send_program(prog.current);
  1273. #else
  1274. x_engine->osc_send_set_program_count(m_id, prog.count);
  1275. for (uint32_t i=0; i < prog.count; i++)
  1276. x_engine->osc_send_set_program_name(m_id, i, prog.names[i]);
  1277. x_engine->osc_send_set_program(m_id, prog.current);
  1278. #endif
  1279. }
  1280. // MIDI Programs
  1281. {
  1282. #ifdef BUILD_BRIDGE
  1283. osc_send_bridge_midi_program_count(midiprog.count);
  1284. for (uint32_t i=0; i < midiprog.count; i++)
  1285. osc_send_bridge_midi_program_info(i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1286. osc_send_midi_program(midiprog.current);
  1287. #else
  1288. x_engine->osc_send_set_midi_program_count(m_id, midiprog.count);
  1289. for (uint32_t i=0; i < midiprog.count; i++)
  1290. x_engine->osc_send_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1291. x_engine->osc_send_set_midi_program(m_id, midiprog.current);
  1292. #endif
  1293. }
  1294. }
  1295. #ifndef BUILD_BRIDGE
  1296. /*!
  1297. * Update the plugin's internal OSC data according to \a source and \a url.\n
  1298. * This is used for OSC-GUI bridges.
  1299. */
  1300. void updateOscData(const lo_address source, const char* const url)
  1301. {
  1302. const char* host;
  1303. const char* port;
  1304. osc_clear_data(&osc.data);
  1305. host = lo_address_get_hostname(source);
  1306. port = lo_address_get_port(source);
  1307. osc.data.source = lo_address_new(host, port);
  1308. host = lo_url_get_hostname(url);
  1309. port = lo_url_get_port(url);
  1310. osc.data.path = lo_url_get_path(url);
  1311. osc.data.target = lo_address_new(host, port);
  1312. free((void*)host);
  1313. free((void*)port);
  1314. osc_send_sample_rate(&osc.data, x_engine->getSampleRate());
  1315. for (size_t i=0; i < custom.size(); i++)
  1316. {
  1317. if (m_type == PLUGIN_LV2)
  1318. osc_send_lv2_event_transfer(&osc.data, getCustomDataTypeString(custom[i].type), custom[i].key, custom[i].value);
  1319. else if (custom[i].type == CUSTOM_DATA_STRING)
  1320. osc_send_configure(&osc.data, custom[i].key, custom[i].value);
  1321. // FIXME
  1322. }
  1323. if (prog.current >= 0)
  1324. osc_send_program(&osc.data, prog.current);
  1325. if (midiprog.current >= 0)
  1326. {
  1327. if (m_type == PLUGIN_DSSI)
  1328. osc_send_program(&osc.data, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  1329. else
  1330. osc_send_midi_program(&osc.data, midiprog.current);
  1331. }
  1332. for (uint32_t i=0; i < param.count; i++)
  1333. osc_send_control(&osc.data, param.data[i].rindex, getParameterValue(i));
  1334. if (m_hints & PLUGIN_IS_BRIDGE)
  1335. {
  1336. osc_send_control(&osc.data, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1337. osc_send_control(&osc.data, PARAMETER_DRYWET, x_dryWet);
  1338. osc_send_control(&osc.data, PARAMETER_VOLUME, x_volume);
  1339. osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, x_balanceLeft);
  1340. osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, x_balanceRight);
  1341. }
  1342. }
  1343. /*!
  1344. * Clear the plugin's internal OSC data.
  1345. */
  1346. void clearOscData()
  1347. {
  1348. osc_clear_data(&osc.data);
  1349. }
  1350. /*!
  1351. * Show the plugin's OSC based GUI.\n
  1352. * This is a handy function that waits for the GUI to respond and automatically asks it to show itself.
  1353. */
  1354. bool showOscGui()
  1355. {
  1356. // wait for UI 'update' call
  1357. for (uint i=0; i < carlaOptions.oscUiTimeout; i++)
  1358. {
  1359. if (osc.data.target)
  1360. {
  1361. osc_send_show(&osc.data);
  1362. return true;
  1363. }
  1364. else
  1365. carla_msleep(100);
  1366. }
  1367. return false;
  1368. }
  1369. #endif
  1370. // -------------------------------------------------------------------
  1371. // MIDI events
  1372. /*!
  1373. * Send a single midi note to be processed in the next audio callback.\n
  1374. * A note with 0 velocity means note-off.
  1375. */
  1376. void sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1377. {
  1378. Q_ASSERT(channel < 16);
  1379. Q_ASSERT(note < 128);
  1380. Q_ASSERT(velo < 128);
  1381. engineMidiLock();
  1382. for (unsigned short i=0; i < MAX_MIDI_EVENTS; i++)
  1383. {
  1384. if (extMidiNotes[i].channel < 0)
  1385. {
  1386. extMidiNotes[i].channel = channel;
  1387. extMidiNotes[i].note = note;
  1388. extMidiNotes[i].velo = velo;
  1389. break;
  1390. }
  1391. }
  1392. engineMidiUnlock();
  1393. if (sendGui)
  1394. {
  1395. if (note)
  1396. uiNoteOn(channel, note, velo);
  1397. else
  1398. uiNoteOff(channel, note);
  1399. }
  1400. #ifndef BUILD_BRIDGE
  1401. if (sendOsc)
  1402. {
  1403. if (velo)
  1404. x_engine->osc_send_note_on(m_id, channel, note, velo);
  1405. else
  1406. x_engine->osc_send_note_off(m_id, channel, note);
  1407. if (m_hints & PLUGIN_IS_BRIDGE)
  1408. {
  1409. uint8_t midiData[4] = { 0 };
  1410. midiData[1] = (velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + channel;
  1411. midiData[2] = note;
  1412. midiData[3] = velo;
  1413. osc_send_midi(&osc.data, midiData);
  1414. }
  1415. }
  1416. #else
  1417. Q_UNUSED(sendOsc);
  1418. #endif
  1419. if (sendCallback)
  1420. x_engine->callback(velo ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, m_id, note, velo, 0.0);
  1421. }
  1422. /*!
  1423. * Send all midi notes off for the next audio callback.\n
  1424. * This doesn't send the actual MIDI All-Notes-Off event, but 128 note-offs instead.
  1425. */
  1426. void sendMidiAllNotesOff()
  1427. {
  1428. engineMidiLock();
  1429. postEvents.mutex.lock();
  1430. unsigned short postPad = 0;
  1431. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1432. {
  1433. if (postEvents.data[i].type == PluginPostEventNull)
  1434. {
  1435. postPad = i;
  1436. break;
  1437. }
  1438. }
  1439. if (postPad == MAX_POST_EVENTS - 1)
  1440. {
  1441. qWarning("post-events buffer full, making room for all notes off now");
  1442. postPad -= 128;
  1443. }
  1444. for (unsigned short i=0; i < 128; i++)
  1445. {
  1446. extMidiNotes[i].channel = m_ctrlInChannel;
  1447. extMidiNotes[i].note = i;
  1448. extMidiNotes[i].velo = 0;
  1449. postEvents.data[i + postPad].type = PluginPostEventNoteOff;
  1450. postEvents.data[i + postPad].value1 = i;
  1451. postEvents.data[i + postPad].value2 = 0;
  1452. postEvents.data[i + postPad].value3 = 0.0;
  1453. }
  1454. postEvents.mutex.unlock();
  1455. engineMidiUnlock();
  1456. }
  1457. // -------------------------------------------------------------------
  1458. // Post-poned events
  1459. /*!
  1460. * Post pone an event of type \a type.\n
  1461. * The event will be processed later, but as soon as possible.
  1462. */
  1463. void postponeEvent(const PluginPostEventType type, const int32_t value1, const int32_t value2, const double value3)
  1464. {
  1465. postEvents.mutex.lock();
  1466. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1467. {
  1468. if (postEvents.data[i].type == PluginPostEventNull)
  1469. {
  1470. postEvents.data[i].type = type;
  1471. postEvents.data[i].value1 = value1;
  1472. postEvents.data[i].value2 = value2;
  1473. postEvents.data[i].value3 = value3;
  1474. break;
  1475. }
  1476. }
  1477. postEvents.mutex.unlock();
  1478. }
  1479. /*!
  1480. * Process all the post-poned events.
  1481. * This function will only be called from the main thread if PLUGIN_USES_SINGLE_THREAD is set.
  1482. */
  1483. void postEventsRun()
  1484. {
  1485. PluginPostEvent newPostEvents[MAX_POST_EVENTS];
  1486. // Make a safe copy of events, and clear them
  1487. postEvents.mutex.lock();
  1488. memcpy(newPostEvents, postEvents.data, sizeof(PluginPostEvent)*MAX_POST_EVENTS);
  1489. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1490. postEvents.data[i].type = PluginPostEventNull;
  1491. postEvents.mutex.unlock();
  1492. // Handle events now
  1493. for (uint32_t i=0; i < MAX_POST_EVENTS; i++)
  1494. {
  1495. const PluginPostEvent* const event = &newPostEvents[i];
  1496. switch (event->type)
  1497. {
  1498. case PluginPostEventNull:
  1499. break;
  1500. case PluginPostEventDebug:
  1501. x_engine->callback(CALLBACK_DEBUG, m_id, event->value1, event->value2, event->value3);
  1502. break;
  1503. case PluginPostEventParameterChange:
  1504. // Update UI
  1505. if (event->value1 >= 0)
  1506. uiParameterChange(event->value1, event->value3);
  1507. // Update OSC control client
  1508. x_engine->osc_send_set_parameter_value(m_id, event->value1, event->value3);
  1509. // Update Host
  1510. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, event->value1, 0, event->value3);
  1511. break;
  1512. case PluginPostEventProgramChange:
  1513. // Update UI
  1514. uiProgramChange(event->value1);
  1515. // Update OSC control client
  1516. x_engine->osc_send_set_program(m_id, event->value1);
  1517. for (uint32_t j=0; j < param.count; j++)
  1518. x_engine->osc_send_set_default_value(m_id, j, param.ranges[j].def);
  1519. // Update Host
  1520. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1521. break;
  1522. case PluginPostEventMidiProgramChange:
  1523. // Update UI
  1524. uiMidiProgramChange(event->value1);
  1525. // Update OSC control client
  1526. x_engine->osc_send_set_midi_program(m_id, event->value1);
  1527. for (uint32_t j=0; j < param.count; j++)
  1528. x_engine->osc_send_set_default_value(m_id, j, param.ranges[j].def);
  1529. // Update Host
  1530. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1531. break;
  1532. case PluginPostEventNoteOn:
  1533. // Update UI
  1534. uiNoteOn(event->value1, event->value2, rint(event->value3));
  1535. // Update OSC control client
  1536. x_engine->osc_send_note_on(m_id, event->value1, event->value2, event->value3);
  1537. // Update Host
  1538. x_engine->callback(CALLBACK_NOTE_ON, m_id, event->value1, event->value2, event->value3);
  1539. break;
  1540. case PluginPostEventNoteOff:
  1541. // Update UI
  1542. uiNoteOff(event->value1, event->value2);
  1543. // Update OSC control client
  1544. x_engine->osc_send_note_off(m_id, event->value1, event->value2);
  1545. // Update Host
  1546. x_engine->callback(CALLBACK_NOTE_OFF, m_id, event->value1, event->value2, 0.0);
  1547. break;
  1548. }
  1549. }
  1550. }
  1551. /*!
  1552. * Tell the UI a parameter has changed.
  1553. */
  1554. virtual void uiParameterChange(const uint32_t index, const double value)
  1555. {
  1556. Q_ASSERT(index < param.count);
  1557. Q_UNUSED(index);
  1558. Q_UNUSED(value);
  1559. }
  1560. /*!
  1561. * Tell the UI the current program has changed.
  1562. */
  1563. virtual void uiProgramChange(const uint32_t index)
  1564. {
  1565. Q_ASSERT(index < prog.count);
  1566. Q_UNUSED(index);
  1567. }
  1568. /*!
  1569. * Tell the UI the current midi program has changed.
  1570. */
  1571. virtual void uiMidiProgramChange(const uint32_t index)
  1572. {
  1573. Q_ASSERT(index < midiprog.count);
  1574. Q_UNUSED(index);
  1575. }
  1576. /*!
  1577. * Tell the UI a note has been pressed.
  1578. */
  1579. virtual void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1580. {
  1581. Q_UNUSED(channel);
  1582. Q_UNUSED(note);
  1583. Q_UNUSED(velo);
  1584. }
  1585. /*!
  1586. * Tell the UI a note has been released.
  1587. */
  1588. virtual void uiNoteOff(const uint8_t channel, const uint8_t note)
  1589. {
  1590. Q_UNUSED(channel);
  1591. Q_UNUSED(note);
  1592. }
  1593. // -------------------------------------------------------------------
  1594. // Cleanup
  1595. /*!
  1596. * Clear the engine client ports of the plugin.
  1597. */
  1598. virtual void removeClientPorts()
  1599. {
  1600. qDebug("CarlaPlugin::removeClientPorts() - start");
  1601. for (uint32_t i=0; i < aIn.count; i++)
  1602. {
  1603. delete aIn.ports[i];
  1604. aIn.ports[i] = nullptr;
  1605. }
  1606. for (uint32_t i=0; i < aOut.count; i++)
  1607. {
  1608. delete aOut.ports[i];
  1609. aOut.ports[i] = nullptr;
  1610. }
  1611. if (midi.portMin)
  1612. {
  1613. delete midi.portMin;
  1614. midi.portMin = nullptr;
  1615. }
  1616. if (midi.portMout)
  1617. {
  1618. delete midi.portMout;
  1619. midi.portMout = nullptr;
  1620. }
  1621. if (param.portCin)
  1622. {
  1623. delete param.portCin;
  1624. param.portCin = nullptr;
  1625. }
  1626. if (param.portCout)
  1627. {
  1628. delete param.portCout;
  1629. param.portCout = nullptr;
  1630. }
  1631. qDebug("CarlaPlugin::removeClientPorts() - end");
  1632. }
  1633. /*!
  1634. * Initializes all RT buffers of the plugin.
  1635. */
  1636. virtual void initBuffers()
  1637. {
  1638. uint32_t i;
  1639. for (i=0; i < aIn.count; i++)
  1640. {
  1641. if (aIn.ports[i])
  1642. aIn.ports[i]->initBuffer(x_engine);
  1643. }
  1644. for (i=0; i < aOut.count; i++)
  1645. {
  1646. if (aOut.ports[i])
  1647. aOut.ports[i]->initBuffer(x_engine);
  1648. }
  1649. if (param.portCin)
  1650. param.portCin->initBuffer(x_engine);
  1651. if (param.portCout)
  1652. param.portCout->initBuffer(x_engine);
  1653. if (midi.portMin)
  1654. midi.portMin->initBuffer(x_engine);
  1655. if (midi.portMout)
  1656. midi.portMout->initBuffer(x_engine);
  1657. }
  1658. /*!
  1659. * Delete all temporary buffers of the plugin.
  1660. */
  1661. virtual void deleteBuffers()
  1662. {
  1663. qDebug("CarlaPlugin::deleteBuffers() - start");
  1664. if (aIn.count > 0)
  1665. {
  1666. delete[] aIn.ports;
  1667. delete[] aIn.rindexes;
  1668. }
  1669. if (aOut.count > 0)
  1670. {
  1671. delete[] aOut.ports;
  1672. delete[] aOut.rindexes;
  1673. }
  1674. if (param.count > 0)
  1675. {
  1676. delete[] param.data;
  1677. delete[] param.ranges;
  1678. }
  1679. aIn.count = 0;
  1680. aIn.ports = nullptr;
  1681. aIn.rindexes = nullptr;
  1682. aOut.count = 0;
  1683. aOut.ports = nullptr;
  1684. aOut.rindexes = nullptr;
  1685. param.count = 0;
  1686. param.data = nullptr;
  1687. param.ranges = nullptr;
  1688. param.portCin = nullptr;
  1689. param.portCout = nullptr;
  1690. qDebug("CarlaPlugin::deleteBuffers() - end");
  1691. }
  1692. // -------------------------------------------------------------------
  1693. // Library functions
  1694. /*!
  1695. * Open the DLL \a filename.
  1696. */
  1697. bool libOpen(const char* const filename)
  1698. {
  1699. m_lib = lib_open(filename);
  1700. return bool(m_lib);
  1701. }
  1702. /*!
  1703. * Close the DLL previously loaded in libOpen().
  1704. */
  1705. bool libClose()
  1706. {
  1707. if (m_lib)
  1708. return lib_close(m_lib);
  1709. return false;
  1710. }
  1711. /*!
  1712. * Get the symbol entry \a symbol of the currently loaded DLL.
  1713. */
  1714. void* libSymbol(const char* const symbol)
  1715. {
  1716. if (m_lib)
  1717. return lib_symbol(m_lib, symbol);
  1718. return nullptr;
  1719. }
  1720. /*!
  1721. * Get the last DLL related error.
  1722. */
  1723. const char* libError(const char* const filename)
  1724. {
  1725. return lib_error(filename);
  1726. }
  1727. // -------------------------------------------------------------------
  1728. // Locks
  1729. void engineProcessLock()
  1730. {
  1731. x_engine->processLock();
  1732. }
  1733. void engineProcessUnlock()
  1734. {
  1735. x_engine->processUnlock();
  1736. }
  1737. void engineMidiLock()
  1738. {
  1739. x_engine->midiLock();
  1740. }
  1741. void engineMidiUnlock()
  1742. {
  1743. x_engine->midiUnlock();
  1744. }
  1745. // -------------------------------------------------------------------
  1746. // Plugin initializers
  1747. struct initializer {
  1748. CarlaEngine* const engine;
  1749. const char* const filename;
  1750. const char* const name;
  1751. const char* const label;
  1752. };
  1753. static CarlaPlugin* newLADSPA(const initializer& init, const void* const extra);
  1754. static CarlaPlugin* newDSSI(const initializer& init, const void* const extra);
  1755. static CarlaPlugin* newLV2(const initializer& init);
  1756. static CarlaPlugin* newVST(const initializer& init);
  1757. static CarlaPlugin* newGIG(const initializer& init);
  1758. static CarlaPlugin* newSF2(const initializer& init);
  1759. static CarlaPlugin* newSFZ(const initializer& init);
  1760. #ifndef BUILD_BRIDGE
  1761. static CarlaPlugin* newBridge(const initializer& init, const BinaryType btype, const PluginType ptype);
  1762. #endif
  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 ScopedDisabler
  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. ScopedDisabler(CarlaPlugin* const plugin, const 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. ~ScopedDisabler()
  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. protected:
  1814. unsigned short m_id;
  1815. CarlaEngine* const x_engine;
  1816. CarlaEngineClient* x_client;
  1817. double x_dryWet, x_volume;
  1818. double x_balanceLeft, x_balanceRight;
  1819. PluginType m_type;
  1820. unsigned int m_hints;
  1821. bool m_active;
  1822. bool m_activeBefore;
  1823. bool m_enabled;
  1824. void* m_lib;
  1825. const char* m_name;
  1826. const char* m_filename;
  1827. int8_t m_ctrlInChannel;
  1828. // -------------------------------------------------------------------
  1829. // Storage Data
  1830. PluginAudioData aIn;
  1831. PluginAudioData aOut;
  1832. PluginMidiData midi;
  1833. PluginParameterData param;
  1834. PluginProgramData prog;
  1835. PluginMidiProgramData midiprog;
  1836. std::vector<CustomData> custom;
  1837. // -------------------------------------------------------------------
  1838. // Extra
  1839. #ifndef BUILD_BRIDGE
  1840. struct {
  1841. CarlaOscData data;
  1842. CarlaPluginThread* thread;
  1843. } osc;
  1844. #endif
  1845. struct {
  1846. QMutex mutex;
  1847. PluginPostEvent data[MAX_POST_EVENTS];
  1848. } postEvents;
  1849. ExternalMidiNote extMidiNotes[MAX_MIDI_EVENTS];
  1850. // -------------------------------------------------------------------
  1851. // Utilities
  1852. static double fixParameterValue(double& value, const ParameterRanges& ranges)
  1853. {
  1854. if (value < ranges.min)
  1855. value = ranges.min;
  1856. else if (value > ranges.max)
  1857. value = ranges.max;
  1858. return value;
  1859. }
  1860. static float fixParameterValue(float& value, const ParameterRanges& ranges)
  1861. {
  1862. if (value < ranges.min)
  1863. value = ranges.min;
  1864. else if (value > ranges.max)
  1865. value = ranges.max;
  1866. return value;
  1867. }
  1868. static double abs(const double& value)
  1869. {
  1870. return (value < 0.0) ? -value : value;
  1871. }
  1872. };
  1873. /**@}*/
  1874. CARLA_BACKEND_END_NAMESPACE
  1875. #endif // CARLA_PLUGIN_H