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.

2246 lines
62KB

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