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.

2250 lines
61KB

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