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.

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