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.

2141 lines
58KB

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