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.

2137 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 channel to \a channel.\n
  846. * \a channel must be between 0 and 15.
  847. */
  848. void setParameterMidiChannel(uint32_t parameterId, uint8_t channel)
  849. {
  850. assert(parameterId < param.count && channel < 16);
  851. param.data[parameterId].midiChannel = channel;
  852. #ifndef BUILD_BRIDGE
  853. x_engine->osc_send_set_parameter_midi_channel(m_id, parameterId, channel);
  854. // FIXME: send to engine
  855. //if (m_hints & PLUGIN_IS_BRIDGE)
  856. // osc_send_set_parameter_midi_channel(&osc.data, m_id, parameterId, channel);
  857. #endif
  858. }
  859. /*!
  860. * Set parameter's \a parameterId MIDI CC to \a cc.\n
  861. * \a cc must be between 0 and 15.
  862. */
  863. void setParameterMidiCC(uint32_t parameterId, int16_t cc)
  864. {
  865. assert(parameterId < param.count);
  866. param.data[parameterId].midiCC = cc;
  867. #ifndef BUILD_BRIDGE
  868. x_engine->osc_send_set_parameter_midi_cc(m_id, parameterId, cc);
  869. // FIXME: send to engine
  870. //if (m_hints & PLUGIN_IS_BRIDGE)
  871. // osc_send_set_parameter_midi_cc(&osc.data, m_id, parameterId, midi_cc);
  872. #endif
  873. }
  874. /*!
  875. * Add a custom data set.\n
  876. * If \a key already exists, its value will be swapped with \a value.
  877. *
  878. * \param type Type of data used in \a value.
  879. * \param key A key identifing this data set.
  880. * \param value The value of the data set, of type \a type.
  881. *
  882. * \see customData()
  883. */
  884. virtual void setCustomData(CustomDataType type, const char* const key, const char* const value, bool sendGui)
  885. {
  886. assert(key);
  887. assert(value);
  888. if (! key)
  889. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - key is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  890. if (! value)
  891. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - value is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  892. bool saveData = true;
  893. switch (type)
  894. {
  895. case CUSTOM_DATA_INVALID:
  896. saveData = false;
  897. break;
  898. case CUSTOM_DATA_STRING:
  899. // Ignore some keys
  900. if (strncmp(key, "OSC:", 4) == 0 || strcmp(key, "guiVisible") || strcmp(key, "CarlaBridgeSaveNow") == 0)
  901. saveData = false;
  902. break;
  903. default:
  904. break;
  905. }
  906. if (saveData)
  907. {
  908. // Check if we already have this key
  909. for (size_t i=0; i < custom.size(); i++)
  910. {
  911. if (strcmp(custom[i].key, key) == 0)
  912. {
  913. free((void*)custom[i].value);
  914. custom[i].value = strdup(value);
  915. return;
  916. }
  917. }
  918. // False if we get here, so store it
  919. CustomData newData;
  920. newData.type = type;
  921. newData.key = strdup(key);
  922. newData.value = strdup(value);
  923. custom.push_back(newData);
  924. }
  925. }
  926. /*!
  927. * Set the complete chunk data as \a stringData.
  928. * \a stringData must a base64 encoded string of binary data.
  929. *
  930. * \see chunkData()
  931. *
  932. * \note Make sure to verify the plugin supports chunks before calling this function!
  933. */
  934. virtual void setChunkData(const char* const stringData)
  935. {
  936. assert(stringData);
  937. }
  938. /*!
  939. * Change the current plugin program to \a index.
  940. *
  941. * If \a index is negative the plugin's program will be considered unset.
  942. * The plugin's default parameter values will be updated when this function is called.
  943. *
  944. * \param index New program index to use
  945. * \param sendGui Send message change to plugin's custom GUI, if any
  946. * \param sendOsc Send message change over OSC
  947. * \param sendCallback Send message change to registered callback
  948. * \param block Block the audio callback
  949. */
  950. virtual void setProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  951. {
  952. assert(index < (int32_t)prog.count);
  953. if (index < -1)
  954. index = -1;
  955. prog.current = index;
  956. #ifndef BUILD_BRIDGE
  957. if (sendOsc)
  958. {
  959. x_engine->osc_send_set_program(m_id, prog.current);
  960. if (m_hints & PLUGIN_IS_BRIDGE)
  961. osc_send_program(&osc.data, prog.current);
  962. }
  963. #else
  964. Q_UNUSED(sendOsc);
  965. #endif
  966. // Change default parameter values
  967. if (index >= 0)
  968. {
  969. for (uint32_t i=0; i < param.count; i++)
  970. {
  971. param.ranges[i].def = getParameterValue(i);
  972. #ifndef BUILD_BRIDGE
  973. if (sendOsc)
  974. x_engine->osc_send_set_default_value(m_id, i, param.ranges[i].def);
  975. #endif
  976. }
  977. }
  978. if (sendCallback)
  979. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, prog.current, 0, 0.0);
  980. Q_UNUSED(sendGui);
  981. Q_UNUSED(block);
  982. }
  983. /*!
  984. * Change the current MIDI plugin program to \a index.
  985. *
  986. * If \a index is negative the plugin's program will be considered unset.
  987. * The plugin's default parameter values will be updated when this function is called.
  988. *
  989. * \param index New program index to use
  990. * \param sendGui Send message change to plugin's custom GUI, if any
  991. * \param sendOsc Send message change over OSC
  992. * \param sendCallback Send message change to registered callback
  993. * \param block Block the audio callback
  994. */
  995. virtual void setMidiProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  996. {
  997. assert(index < (int32_t)midiprog.count);
  998. if (index < -1)
  999. index = -1;
  1000. midiprog.current = index;
  1001. #ifndef BUILD_BRIDGE
  1002. if (sendOsc)
  1003. {
  1004. x_engine->osc_send_set_midi_program(m_id, midiprog.current);
  1005. if (m_hints & PLUGIN_IS_BRIDGE)
  1006. osc_send_midi_program(&osc.data, midiprog.current);
  1007. }
  1008. #else
  1009. Q_UNUSED(sendOsc);
  1010. #endif
  1011. // Change default parameter values (sound banks never change defaults)
  1012. if (index >= 0 && m_type != PLUGIN_GIG && m_type != PLUGIN_SF2 && m_type != PLUGIN_SFZ)
  1013. {
  1014. for (uint32_t i=0; i < param.count; i++)
  1015. {
  1016. param.ranges[i].def = getParameterValue(i);
  1017. #ifndef BUILD_BRIDGE
  1018. if (sendOsc)
  1019. x_engine->osc_send_set_default_value(m_id, i, param.ranges[i].def);
  1020. #endif
  1021. }
  1022. }
  1023. if (sendCallback)
  1024. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, midiprog.current, 0, 0.0);
  1025. Q_UNUSED(sendGui);
  1026. Q_UNUSED(block);
  1027. }
  1028. /*!
  1029. * This is an overloaded call to setMidiProgram().\n
  1030. * It changes the current MIDI program using \a bank and \a program values instead of index.
  1031. */
  1032. void setMidiProgramById(uint32_t bank, uint32_t program, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  1033. {
  1034. for (uint32_t i=0; i < midiprog.count; i++)
  1035. {
  1036. if (midiprog.data[i].bank == bank && midiprog.data[i].program == program)
  1037. return setMidiProgram(i, sendGui, sendOsc, sendCallback, block);
  1038. }
  1039. }
  1040. // -------------------------------------------------------------------
  1041. // Set gui stuff
  1042. /*!
  1043. * Set plugin's custom GUI stuff.\n
  1044. * Parameters change between plugin types.
  1045. *
  1046. * \note This function must be always called from the main thread.
  1047. */
  1048. virtual void setGuiData(int data, GuiDataHandle handle)
  1049. {
  1050. Q_UNUSED(data);
  1051. Q_UNUSED(handle);
  1052. }
  1053. /*!
  1054. * Show (or hide) a plugin's custom GUI according to \a yesNo.
  1055. *
  1056. * \note This function must be always called from the main thread.
  1057. */
  1058. virtual void showGui(bool yesNo)
  1059. {
  1060. Q_UNUSED(yesNo);
  1061. }
  1062. /*!
  1063. * Idle plugin's custom GUI.
  1064. *
  1065. * \note This function must be always called from the main thread.
  1066. */
  1067. virtual void idleGui()
  1068. {
  1069. m_needsParamUpdate = false;
  1070. m_needsProgUpdate = false;
  1071. }
  1072. // -------------------------------------------------------------------
  1073. // Plugin state
  1074. /*!
  1075. * Reload the plugin's entire state (including programs).\n
  1076. * The plugin will be disabled during this call.
  1077. */
  1078. virtual void reload()
  1079. {
  1080. }
  1081. /*!
  1082. * Reload the plugin's programs state.
  1083. */
  1084. virtual void reloadPrograms(bool init)
  1085. {
  1086. Q_UNUSED(init);
  1087. }
  1088. /*!
  1089. * Tell the plugin to prepare for save.
  1090. */
  1091. virtual void prepareForSave()
  1092. {
  1093. }
  1094. // -------------------------------------------------------------------
  1095. // Plugin processing
  1096. /*!
  1097. * Plugin process callback.
  1098. */
  1099. virtual void process(float** inBuffer, float** outBuffer, uint32_t frames, uint32_t framesOffset = 0)
  1100. {
  1101. Q_UNUSED(inBuffer);
  1102. Q_UNUSED(outBuffer);
  1103. Q_UNUSED(frames);
  1104. Q_UNUSED(framesOffset);
  1105. }
  1106. #ifdef CARLA_ENGINE_JACK
  1107. void process_jack(uint32_t nframes)
  1108. {
  1109. float* inBuffer[ain.count];
  1110. float* outBuffer[aout.count];
  1111. for (uint32_t i=0; i < ain.count; i++)
  1112. inBuffer[i] = ain.ports[i]->getJackAudioBuffer(nframes);
  1113. for (uint32_t i=0; i < aout.count; i++)
  1114. outBuffer[i] = aout.ports[i]->getJackAudioBuffer(nframes);
  1115. #ifndef BUILD_BRIDGE
  1116. if (carlaOptions.proccess_hp)
  1117. {
  1118. float* inBuffer2[ain.count];
  1119. float* outBuffer2[aout.count];
  1120. for (uint32_t i=0, j; i < nframes; i += 8)
  1121. {
  1122. for (j=0; j < ain.count; j++)
  1123. inBuffer2[j] = inBuffer[j] + i;
  1124. for (j=0; j < aout.count; j++)
  1125. outBuffer2[j] = outBuffer[j] + i;
  1126. process(inBuffer2, outBuffer2, 8, i);
  1127. }
  1128. }
  1129. else
  1130. #endif
  1131. process(inBuffer, outBuffer, nframes);
  1132. }
  1133. #endif
  1134. /*!
  1135. * Tell the plugin the current buffer size has changed.
  1136. */
  1137. virtual void bufferSizeChanged(uint32_t newBufferSize)
  1138. {
  1139. Q_UNUSED(newBufferSize);
  1140. }
  1141. // -------------------------------------------------------------------
  1142. // OSC stuff
  1143. /*!
  1144. * Register this plugin to the global OSC controller.
  1145. */
  1146. void registerToOsc()
  1147. {
  1148. #ifndef BUILD_BRIDGE
  1149. if (! x_engine->isOscControllerRegisted())
  1150. return;
  1151. x_engine->osc_send_add_plugin(m_id, m_name);
  1152. #endif
  1153. // Base data
  1154. {
  1155. char bufName[STR_MAX] = { 0 };
  1156. char bufLabel[STR_MAX] = { 0 };
  1157. char bufMaker[STR_MAX] = { 0 };
  1158. char bufCopyright[STR_MAX] = { 0 };
  1159. getRealName(bufName);
  1160. getLabel(bufLabel);
  1161. getMaker(bufMaker);
  1162. getCopyright(bufCopyright);
  1163. #ifdef BUILD_BRIDGE
  1164. osc_send_bridge_plugin_info(category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1165. #else
  1166. x_engine->osc_send_set_plugin_data(m_id, m_type, category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1167. #endif
  1168. }
  1169. // Base count
  1170. {
  1171. uint32_t cIns, cOuts, cTotals;
  1172. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  1173. #ifdef BUILD_BRIDGE
  1174. osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  1175. osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  1176. osc_send_bridge_param_count(cIns, cOuts, cTotals);
  1177. #else
  1178. x_engine->osc_send_set_plugin_ports(m_id, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  1179. #endif
  1180. }
  1181. // Internal Parameters
  1182. {
  1183. #ifndef BUILD_BRIDGE
  1184. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1185. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_DRYWET, x_drywet);
  1186. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_VOLUME, x_vol);
  1187. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, x_bal_left);
  1188. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, x_bal_right);
  1189. #endif
  1190. }
  1191. // Plugin Parameters
  1192. if (param.count > 0 && param.count < carlaOptions.max_parameters)
  1193. {
  1194. char bufName[STR_MAX], bufUnit[STR_MAX];
  1195. for (uint32_t i=0; i < param.count; i++)
  1196. {
  1197. getParameterName(i, bufName);
  1198. getParameterUnit(i, bufUnit);
  1199. #ifdef BUILD_BRIDGE
  1200. osc_send_bridge_param_info(i, bufName, bufUnit);
  1201. 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);
  1202. 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);
  1203. setParameterValue(i, param.ranges[i].def, false, false, true); // FIXME?
  1204. #else
  1205. x_engine->osc_send_set_parameter_data(m_id, i, param.data[i].type, param.data[i].hints, bufName, bufUnit, getParameterValue(i));
  1206. 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);
  1207. #endif
  1208. }
  1209. }
  1210. // Programs
  1211. {
  1212. #ifdef BUILD_BRIDGE
  1213. osc_send_bridge_program_count(prog.count);
  1214. for (uint32_t i=0; i < prog.count; i++)
  1215. osc_send_bridge_program_info(i, prog.names[i]);
  1216. //if (prog.current >= 0)
  1217. osc_send_program(prog.current);
  1218. #else
  1219. x_engine->osc_send_set_program_count(m_id, prog.count);
  1220. for (uint32_t i=0; i < prog.count; i++)
  1221. x_engine->osc_send_set_program_name(m_id, i, prog.names[i]);
  1222. x_engine->osc_send_set_program(m_id, prog.current);
  1223. #endif
  1224. }
  1225. // MIDI Programs
  1226. {
  1227. #ifdef BUILD_BRIDGE
  1228. osc_send_bridge_midi_program_count(midiprog.count);
  1229. for (uint32_t i=0; i < midiprog.count; i++)
  1230. osc_send_bridge_midi_program_info(i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1231. //if (midiprog.current >= 0 /*&& midiprog.count > 0*/)
  1232. //osc_send_midi_program(midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program, false);
  1233. osc_send_midi_program(midiprog.current);
  1234. #else
  1235. x_engine->osc_send_set_midi_program_count(m_id, midiprog.count);
  1236. for (uint32_t i=0; i < midiprog.count; i++)
  1237. x_engine->osc_send_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1238. x_engine->osc_send_set_midi_program(m_id, midiprog.current);
  1239. #endif
  1240. }
  1241. }
  1242. #ifndef BUILD_BRIDGE
  1243. /*!
  1244. * Update the plugin's internal OSC data according to \a source and \a url.\n
  1245. * This is used for OSC-GUI bridges.
  1246. */
  1247. void updateOscData(lo_address source, const char* url)
  1248. {
  1249. const char* host;
  1250. const char* port;
  1251. osc_clear_data(&osc.data);
  1252. host = lo_address_get_hostname(source);
  1253. port = lo_address_get_port(source);
  1254. osc.data.source = lo_address_new(host, port);
  1255. host = lo_url_get_hostname(url);
  1256. port = lo_url_get_port(url);
  1257. osc.data.path = lo_url_get_path(url);
  1258. osc.data.target = lo_address_new(host, port);
  1259. free((void*)host);
  1260. free((void*)port);
  1261. // TODO
  1262. //osc_send_sample_rate(%osc.data, get_sample_rate());
  1263. for (size_t i=0; i < custom.size(); i++)
  1264. {
  1265. if (m_type == PLUGIN_LV2)
  1266. osc_send_lv2_event_transfer(&osc.data, getCustomDataTypeString(custom[i].type), custom[i].key, custom[i].value);
  1267. else if (custom[i].type == CUSTOM_DATA_STRING)
  1268. osc_send_configure(&osc.data, custom[i].key, custom[i].value);
  1269. // FIXME
  1270. }
  1271. if (prog.current >= 0)
  1272. osc_send_program(&osc.data, prog.current);
  1273. if (midiprog.current >= 0)
  1274. {
  1275. //int32_t id = midiprog.current;
  1276. //osc_send_midi_program(&osc.data, midiprog.data[id].bank, midiprog.data[id].program, (m_type == PLUGIN_DSSI));
  1277. osc_send_midi_program(&osc.data, midiprog.current);
  1278. }
  1279. for (uint32_t i=0; i < param.count; i++)
  1280. osc_send_control(&osc.data, param.data[i].rindex, getParameterValue(i));
  1281. if (m_hints & PLUGIN_IS_BRIDGE)
  1282. {
  1283. osc_send_control(&osc.data, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1284. osc_send_control(&osc.data, PARAMETER_DRYWET, x_drywet);
  1285. osc_send_control(&osc.data, PARAMETER_VOLUME, x_vol);
  1286. osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, x_bal_left);
  1287. osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, x_bal_right);
  1288. }
  1289. }
  1290. /*!
  1291. * TODO
  1292. */
  1293. void updateOscParameterOutputs()
  1294. {
  1295. // Check if it needs update
  1296. bool updatePortsGui = (osc.data.target && (m_hints & PLUGIN_IS_BRIDGE) == 0);
  1297. if (! (x_engine->isOscControllerRegisted() || updatePortsGui))
  1298. return;
  1299. // Update
  1300. double value;
  1301. for (uint32_t i=0; i < param.count; i++)
  1302. {
  1303. if (param.data[i].type == PARAMETER_OUTPUT /*&& (paramData->hints & PARAMETER_IS_AUTOMABLE) > 0*/)
  1304. {
  1305. value = getParameterValue(i);
  1306. if (updatePortsGui)
  1307. osc_send_control(&osc.data, param.data[i].rindex, value);
  1308. x_engine->osc_send_set_parameter_value(m_id, i, value);
  1309. }
  1310. }
  1311. }
  1312. /*!
  1313. * Clear the plugin's internal OSC data.
  1314. */
  1315. void clearOscData()
  1316. {
  1317. osc_clear_data(&osc.data);
  1318. }
  1319. /*!
  1320. * Show the plugin's OSC based GUI.\n
  1321. * This is a handy function that waits for the GUI to respond and automatically asks it to show itself.
  1322. */
  1323. bool showOscGui()
  1324. {
  1325. // wait for UI 'update' call
  1326. for (int i=0; i < carlaOptions.osc_gui_timeout; i++)
  1327. {
  1328. if (osc.data.target)
  1329. {
  1330. osc_send_show(&osc.data);
  1331. return true;
  1332. }
  1333. else
  1334. carla_msleep(100);
  1335. }
  1336. return false;
  1337. }
  1338. #endif
  1339. // -------------------------------------------------------------------
  1340. // MIDI events
  1341. /*!
  1342. * Send a single midi note to be processed in the next audio callback.\n
  1343. * A note with 0 velocity means note-off.
  1344. */
  1345. virtual void sendMidiSingleNote(uint8_t channel, uint8_t note, uint8_t velo, bool sendGui, bool sendOsc, bool sendCallback)
  1346. {
  1347. engineMidiLock();
  1348. for (unsigned short i=0; i<MAX_MIDI_EVENTS; i++)
  1349. {
  1350. if (extMidiNotes[i].channel < 0)
  1351. {
  1352. extMidiNotes[i].channel = channel;
  1353. extMidiNotes[i].note = note;
  1354. extMidiNotes[i].velo = velo;
  1355. break;
  1356. }
  1357. }
  1358. engineMidiUnlock();
  1359. #ifndef BUILD_BRIDGE
  1360. if (sendOsc)
  1361. {
  1362. if (velo)
  1363. x_engine->osc_send_note_on(m_id, channel, note, velo);
  1364. else
  1365. x_engine->osc_send_note_off(m_id, channel, note);
  1366. if (m_hints & PLUGIN_IS_BRIDGE)
  1367. {
  1368. uint8_t midiData[4] = { 0 };
  1369. midiData[1] = (velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + channel;
  1370. midiData[2] = note;
  1371. midiData[3] = velo;
  1372. osc_send_midi(&osc.data, midiData);
  1373. }
  1374. }
  1375. #else
  1376. Q_UNUSED(sendOsc);
  1377. #endif
  1378. if (sendCallback)
  1379. x_engine->callback(velo ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, m_id, note, velo, 0.0);
  1380. Q_UNUSED(sendGui);
  1381. }
  1382. /*!
  1383. * Send all midi notes off for the next audio callback.\n
  1384. * This doesn't send the actual MIDI All-Notes-Off event, but 128 note-offs instead.
  1385. */
  1386. void sendMidiAllNotesOff()
  1387. {
  1388. engineMidiLock();
  1389. postEvents.mutex.lock();
  1390. unsigned short postPad = 0;
  1391. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1392. {
  1393. if (postEvents.data[i].type == PluginPostEventNull)
  1394. {
  1395. postPad = i;
  1396. break;
  1397. }
  1398. }
  1399. if (postPad == MAX_POST_EVENTS - 1)
  1400. {
  1401. qWarning("post-events buffer full, making room for all notes off now");
  1402. postPad -= 128;
  1403. }
  1404. for (unsigned short i=0; i < 128; i++)
  1405. {
  1406. extMidiNotes[i].channel = 0; // FIXME
  1407. extMidiNotes[i].note = i;
  1408. extMidiNotes[i].velo = 0;
  1409. postEvents.data[i + postPad].type = PluginPostEventNoteOff;
  1410. postEvents.data[i + postPad].value1 = i;
  1411. postEvents.data[i + postPad].value2 = 0;
  1412. postEvents.data[i + postPad].value3 = 0.0;
  1413. }
  1414. postEvents.mutex.unlock();
  1415. engineMidiUnlock();
  1416. }
  1417. // -------------------------------------------------------------------
  1418. // Post-poned events
  1419. /*!
  1420. * Post pone an event of type \a type.\n
  1421. * The event will be processed later in a high-priority thread (but not the main one).
  1422. */
  1423. void postponeEvent(PluginPostEventType type, int32_t value1, int32_t value2, double value3)
  1424. {
  1425. postEvents.mutex.lock();
  1426. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1427. {
  1428. if (postEvents.data[i].type == PluginPostEventNull)
  1429. {
  1430. postEvents.data[i].type = type;
  1431. postEvents.data[i].value1 = value1;
  1432. postEvents.data[i].value2 = value2;
  1433. postEvents.data[i].value3 = value3;
  1434. break;
  1435. }
  1436. }
  1437. postEvents.mutex.unlock();
  1438. }
  1439. /*!
  1440. * TODO
  1441. */
  1442. void postEventsRun()
  1443. {
  1444. static PluginPostEvent newPostEvents[MAX_POST_EVENTS];
  1445. // Make a safe copy of events, and clear them
  1446. postEvents.mutex.lock();
  1447. memcpy(newPostEvents, postEvents.data, sizeof(PluginPostEvent)*MAX_POST_EVENTS);
  1448. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1449. postEvents.data[i].type = PluginPostEventNull;
  1450. postEvents.mutex.unlock();
  1451. // Handle events now
  1452. for (uint32_t i=0; i < MAX_POST_EVENTS; i++)
  1453. {
  1454. const PluginPostEvent* const event = &newPostEvents[i];
  1455. switch (event->type)
  1456. {
  1457. case PluginPostEventNull:
  1458. return;
  1459. case PluginPostEventDebug:
  1460. x_engine->callback(CALLBACK_DEBUG, m_id, event->value1, event->value2, event->value3);
  1461. break;
  1462. case PluginPostEventParameterChange:
  1463. // Update OSC based UIs
  1464. m_needsParamUpdate = true;
  1465. osc_send_control(&osc.data, event->value1, event->value3);
  1466. // Update OSC control client
  1467. x_engine->osc_send_set_parameter_value(m_id, event->value1, event->value3);
  1468. // Update Host
  1469. x_engine->callback(CALLBACK_PARAMETER_CHANGED, m_id, event->value1, 0, event->value3);
  1470. break;
  1471. case PluginPostEventProgramChange:
  1472. // Update OSC based UIs
  1473. m_needsProgUpdate = true;
  1474. osc_send_program(&osc.data, event->value1);
  1475. // Update OSC control client
  1476. x_engine->osc_send_set_program(m_id, event->value1);
  1477. for (uint32_t j=0; j < param.count; j++)
  1478. x_engine->osc_send_set_default_value(m_id, j, param.ranges[j].def);
  1479. // Update Host
  1480. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1481. break;
  1482. case PluginPostEventMidiProgramChange:
  1483. // Update OSC based UIs
  1484. m_needsProgUpdate = true;
  1485. if (m_type == PLUGIN_DSSI)
  1486. {
  1487. if (event->value1 >= 0 && event->value1 < (int32_t)midiprog.count)
  1488. osc_send_program(&osc.data, midiprog.data[event->value1].bank, midiprog.data[event->value1].program);
  1489. }
  1490. else
  1491. osc_send_midi_program(&osc.data, event->value1);
  1492. // Update OSC control client
  1493. x_engine->osc_send_set_midi_program(m_id, event->value1);
  1494. for (uint32_t j=0; j < param.count; j++)
  1495. x_engine->osc_send_set_default_value(m_id, j, param.ranges[j].def);
  1496. // Update Host
  1497. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1498. //}
  1499. break;
  1500. case PluginPostEventNoteOn:
  1501. // Update OSC based UIs
  1502. if (true)
  1503. {
  1504. uint8_t midiData[4] = { 0 };
  1505. midiData[1] = MIDI_STATUS_NOTE_ON + event->value1;
  1506. midiData[2] = event->value2;
  1507. midiData[3] = rint(event->value3);
  1508. osc_send_midi(&osc.data, midiData);
  1509. }
  1510. // Update OSC control client
  1511. x_engine->osc_send_note_on(m_id, event->value1, event->value2, event->value3);
  1512. // Update Host
  1513. x_engine->callback(CALLBACK_NOTE_ON, m_id, event->value1, event->value2, event->value3);
  1514. break;
  1515. case PluginPostEventNoteOff:
  1516. // Update OSC based UIs
  1517. if (true)
  1518. {
  1519. uint8_t midiData[4] = { 0 };
  1520. midiData[1] = MIDI_STATUS_NOTE_OFF + event->value1;
  1521. midiData[2] = event->value2;
  1522. osc_send_midi(&osc.data, midiData);
  1523. }
  1524. // Update OSC control client
  1525. x_engine->osc_send_note_off(m_id, event->value1, event->value2);
  1526. // Update Host
  1527. x_engine->callback(CALLBACK_NOTE_OFF, m_id, event->value1, event->value2, 0.0);
  1528. break;
  1529. }
  1530. }
  1531. }
  1532. // -------------------------------------------------------------------
  1533. // Cleanup
  1534. /*!
  1535. * Clear the engine client ports of the plugin.
  1536. */
  1537. virtual void removeClientPorts()
  1538. {
  1539. qDebug("CarlaPlugin::removeClientPorts() - start");
  1540. for (uint32_t i=0; i < ain.count; i++)
  1541. {
  1542. delete ain.ports[i];
  1543. ain.ports[i] = nullptr;
  1544. }
  1545. for (uint32_t i=0; i < aout.count; i++)
  1546. {
  1547. delete aout.ports[i];
  1548. aout.ports[i] = nullptr;
  1549. }
  1550. if (midi.portMin)
  1551. {
  1552. delete midi.portMin;
  1553. midi.portMin = nullptr;
  1554. }
  1555. if (midi.portMout)
  1556. {
  1557. delete midi.portMout;
  1558. midi.portMout = nullptr;
  1559. }
  1560. if (param.portCin)
  1561. {
  1562. delete param.portCin;
  1563. param.portCin = nullptr;
  1564. }
  1565. if (param.portCout)
  1566. {
  1567. delete param.portCout;
  1568. param.portCout = nullptr;
  1569. }
  1570. qDebug("CarlaPlugin::removeClientPorts() - end");
  1571. }
  1572. /*!
  1573. * Initializes all RT buffers of the plugin.
  1574. */
  1575. virtual void initBuffers()
  1576. {
  1577. uint32_t i;
  1578. for (i=0; i < ain.count; i++)
  1579. {
  1580. if (ain.ports[i])
  1581. ain.ports[i]->initBuffer(x_engine);
  1582. }
  1583. for (i=0; i < aout.count; i++)
  1584. {
  1585. if (aout.ports[i])
  1586. aout.ports[i]->initBuffer(x_engine);
  1587. }
  1588. if (param.portCin)
  1589. param.portCin->initBuffer(x_engine);
  1590. if (param.portCout)
  1591. param.portCout->initBuffer(x_engine);
  1592. if (midi.portMin)
  1593. midi.portMin->initBuffer(x_engine);
  1594. if (midi.portMout)
  1595. midi.portMout->initBuffer(x_engine);
  1596. }
  1597. /*!
  1598. * Delete all temporary buffers of the plugin.
  1599. */
  1600. virtual void deleteBuffers()
  1601. {
  1602. qDebug("CarlaPlugin::deleteBuffers() - start");
  1603. if (ain.count > 0)
  1604. {
  1605. delete[] ain.ports;
  1606. delete[] ain.rindexes;
  1607. }
  1608. if (aout.count > 0)
  1609. {
  1610. delete[] aout.ports;
  1611. delete[] aout.rindexes;
  1612. }
  1613. if (param.count > 0)
  1614. {
  1615. delete[] param.data;
  1616. delete[] param.ranges;
  1617. }
  1618. ain.count = 0;
  1619. ain.ports = nullptr;
  1620. ain.rindexes = nullptr;
  1621. aout.count = 0;
  1622. aout.ports = nullptr;
  1623. aout.rindexes = nullptr;
  1624. param.count = 0;
  1625. param.data = nullptr;
  1626. param.ranges = nullptr;
  1627. param.portCin = nullptr;
  1628. param.portCout = nullptr;
  1629. qDebug("CarlaPlugin::deleteBuffers() - end");
  1630. }
  1631. // -------------------------------------------------------------------
  1632. // Library functions
  1633. /*!
  1634. * Open the DLL \a filename.
  1635. */
  1636. bool libOpen(const char* filename)
  1637. {
  1638. m_lib = lib_open(filename);
  1639. return bool(m_lib);
  1640. }
  1641. /*!
  1642. * Close the DLL previously loaded in libOpen().
  1643. */
  1644. bool libClose()
  1645. {
  1646. if (m_lib)
  1647. return lib_close(m_lib);
  1648. return false;
  1649. }
  1650. /*!
  1651. * Get the symbol entry \a symbol of the currently loaded DLL.
  1652. */
  1653. void* libSymbol(const char* symbol)
  1654. {
  1655. if (m_lib)
  1656. return lib_symbol(m_lib, symbol);
  1657. return nullptr;
  1658. }
  1659. /*!
  1660. * Get the last DLL related error.
  1661. */
  1662. const char* libError(const char* filename)
  1663. {
  1664. return lib_error(filename);
  1665. }
  1666. // -------------------------------------------------------------------
  1667. // Locks
  1668. void engineProcessLock()
  1669. {
  1670. x_engine->processLock();
  1671. }
  1672. void engineProcessUnlock()
  1673. {
  1674. x_engine->processUnlock();
  1675. }
  1676. void engineMidiLock()
  1677. {
  1678. x_engine->midiLock();
  1679. }
  1680. void engineMidiUnlock()
  1681. {
  1682. x_engine->midiUnlock();
  1683. }
  1684. // -------------------------------------------------------------------
  1685. // Plugin initializers
  1686. struct initializer {
  1687. CarlaEngine* const engine;
  1688. const char* const filename;
  1689. const char* const name;
  1690. const char* const label;
  1691. };
  1692. static CarlaPlugin* newLADSPA(const initializer& init, const void* const extra);
  1693. static CarlaPlugin* newDSSI(const initializer& init, const void* const extra);
  1694. static CarlaPlugin* newLV2(const initializer& init);
  1695. static CarlaPlugin* newVST(const initializer& init);
  1696. static CarlaPlugin* newGIG(const initializer& init);
  1697. static CarlaPlugin* newSF2(const initializer& init);
  1698. static CarlaPlugin* newSFZ(const initializer& init);
  1699. #ifndef BUILD_BRIDGE
  1700. static CarlaPlugin* newBridge(const initializer& init, BinaryType btype, PluginType ptype);
  1701. #endif
  1702. // -------------------------------------------------------------------
  1703. protected:
  1704. // static
  1705. unsigned short m_id;
  1706. CarlaEngine* const x_engine;
  1707. // non-static
  1708. PluginType m_type;
  1709. unsigned int m_hints;
  1710. bool m_active;
  1711. bool m_activeBefore;
  1712. bool m_enabled;
  1713. void* m_lib;
  1714. const char* m_name;
  1715. const char* m_filename;
  1716. int8_t cin_channel;
  1717. double x_drywet, x_vol, x_bal_left, x_bal_right;
  1718. CarlaEngineClient* x_client;
  1719. bool m_needsParamUpdate;
  1720. bool m_needsProgUpdate;
  1721. // -------------------------------------------------------------------
  1722. // Storage Data
  1723. PluginAudioData ain;
  1724. PluginAudioData aout;
  1725. PluginMidiData midi;
  1726. PluginParameterData param;
  1727. PluginProgramData prog;
  1728. PluginMidiProgramData midiprog;
  1729. std::vector<CustomData> custom;
  1730. // -------------------------------------------------------------------
  1731. // Extra
  1732. #ifndef BUILD_BRIDGE
  1733. struct {
  1734. CarlaOscData data;
  1735. CarlaPluginThread* thread;
  1736. } osc;
  1737. #endif
  1738. struct {
  1739. QMutex mutex;
  1740. PluginPostEvent data[MAX_POST_EVENTS];
  1741. } postEvents;
  1742. ExternalMidiNote extMidiNotes[MAX_MIDI_EVENTS];
  1743. // -------------------------------------------------------------------
  1744. // Utilities
  1745. static double fixParameterValue(double& value, const ParameterRanges& ranges)
  1746. {
  1747. if (value < ranges.min)
  1748. value = ranges.min;
  1749. else if (value > ranges.max)
  1750. value = ranges.max;
  1751. return value;
  1752. }
  1753. static float fixParameterValue(float& value, const ParameterRanges& ranges)
  1754. {
  1755. if (value < ranges.min)
  1756. value = ranges.min;
  1757. else if (value > ranges.max)
  1758. value = ranges.max;
  1759. return value;
  1760. }
  1761. static double abs(const double& value)
  1762. {
  1763. return (value < 0.0) ? -value : value;
  1764. }
  1765. };
  1766. /*!
  1767. * \class CarlaPluginScopedDisabler
  1768. *
  1769. * \brief Carla plugin scoped disabler
  1770. *
  1771. * This is a handy class that temporarily disables a plugin during a function scope.\n
  1772. * It should be used when the plugin needs reload or state change, something like this:
  1773. * \code
  1774. * {
  1775. * const CarlaPluginScopedDisabler m(plugin);
  1776. * plugin->setChunkData(data);
  1777. * }
  1778. * \endcode
  1779. */
  1780. class CarlaPluginScopedDisabler
  1781. {
  1782. public:
  1783. /*!
  1784. * Disable plugin \a plugin if \a disable is true.
  1785. * The plugin is re-enabled in the deconstructor of this class if \a disable is true.
  1786. *
  1787. * \param plugin The plugin to disable
  1788. * \param disable Wherever to disable the plugin or not, true by default
  1789. */
  1790. CarlaPluginScopedDisabler(CarlaPlugin* const plugin, bool disable = true) :
  1791. m_plugin(plugin),
  1792. m_disable(disable)
  1793. {
  1794. if (m_disable)
  1795. {
  1796. m_plugin->engineProcessLock();
  1797. m_plugin->setEnabled(false);
  1798. m_plugin->engineProcessUnlock();
  1799. }
  1800. }
  1801. ~CarlaPluginScopedDisabler()
  1802. {
  1803. if (m_disable)
  1804. {
  1805. m_plugin->engineProcessLock();
  1806. m_plugin->setEnabled(true);
  1807. m_plugin->engineProcessUnlock();
  1808. }
  1809. }
  1810. private:
  1811. CarlaPlugin* const m_plugin;
  1812. const bool m_disable;
  1813. };
  1814. /**@}*/
  1815. CARLA_BACKEND_END_NAMESPACE
  1816. #endif // CARLA_PLUGIN_H