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.

2167 lines
57KB

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