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.

2256 lines
61KB

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