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.

2186 lines
59KB

  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. * Get the MIDI program at \a index.
  433. *
  434. * \see getMidiProgramName()
  435. */
  436. const midi_program_t* midiProgramData(uint32_t index) const
  437. {
  438. Q_ASSERT(index < midiprog.count);
  439. return &midiprog.data[index];
  440. }
  441. /*!
  442. * Get the custom data set at \a index.
  443. *
  444. * \see setCustomData()
  445. */
  446. const CustomData* customData(uint32_t index) const
  447. {
  448. Q_ASSERT(index < custom.size());
  449. return &custom[index];
  450. }
  451. /*!
  452. * Get the complete plugin chunk data.
  453. *
  454. * \param dataPtr TODO
  455. * \return The size of the chunk.
  456. *
  457. * \note Make sure to verify the plugin supports chunks before calling this function!
  458. *
  459. * \see setChunkData()
  460. */
  461. virtual int32_t chunkData(void** const dataPtr)
  462. {
  463. Q_ASSERT(dataPtr);
  464. return 0;
  465. }
  466. #ifndef BUILD_BRIDGE
  467. /*!
  468. * Get the plugin's internal OSC data.
  469. */
  470. //const OscData* oscData() const
  471. //{
  472. // return &osc.data;
  473. //}
  474. #endif
  475. // -------------------------------------------------------------------
  476. // Information (per-plugin data)
  477. /*!
  478. * Get the parameter value of \a parameterId.
  479. */
  480. virtual double getParameterValue(uint32_t parameterId)
  481. {
  482. Q_ASSERT(parameterId < param.count);
  483. return 0.0;
  484. }
  485. /*!
  486. * Get the scalepoint \a scalePointId value of the parameter \a parameterId.
  487. */
  488. virtual double getParameterScalePointValue(uint32_t parameterId, uint32_t scalePointId)
  489. {
  490. Q_ASSERT(parameterId < param.count);
  491. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  492. return 0.0;
  493. }
  494. /*!
  495. * Get the plugin's label (URI for PLUGIN_LV2).
  496. */
  497. virtual void getLabel(char* const strBuf)
  498. {
  499. *strBuf = 0;
  500. }
  501. /*!
  502. * Get the plugin's maker.
  503. */
  504. virtual void getMaker(char* const strBuf)
  505. {
  506. *strBuf = 0;
  507. }
  508. /*!
  509. * Get the plugin's copyright/license.
  510. */
  511. virtual void getCopyright(char* const strBuf)
  512. {
  513. *strBuf = 0;
  514. }
  515. /*!
  516. * Get the plugin's (real) name.
  517. *
  518. * \see name()
  519. */
  520. virtual void getRealName(char* const strBuf)
  521. {
  522. *strBuf = 0;;
  523. }
  524. /*!
  525. * Get the name of the parameter \a parameterId.
  526. */
  527. virtual void getParameterName(uint32_t parameterId, char* const strBuf)
  528. {
  529. Q_ASSERT(parameterId < param.count);
  530. *strBuf = 0;
  531. }
  532. /*!
  533. * Get the symbol of the parameter \a parameterId.
  534. */
  535. virtual void getParameterSymbol(uint32_t parameterId, char* const strBuf)
  536. {
  537. Q_ASSERT(parameterId < param.count);
  538. *strBuf = 0;
  539. }
  540. /*!
  541. * Get the custom text of the parameter \a parameterId.
  542. */
  543. virtual void getParameterText(uint32_t parameterId, char* const strBuf)
  544. {
  545. Q_ASSERT(parameterId < param.count);
  546. *strBuf = 0;
  547. }
  548. /*!
  549. * Get the unit of the parameter \a parameterId.
  550. */
  551. virtual void getParameterUnit(uint32_t parameterId, char* const strBuf)
  552. {
  553. Q_ASSERT(parameterId < param.count);
  554. *strBuf = 0;
  555. }
  556. /*!
  557. * Get the scalepoint \a scalePointId label of the parameter \a parameterId.
  558. */
  559. virtual void getParameterScalePointLabel(uint32_t parameterId, uint32_t scalePointId, char* const strBuf)
  560. {
  561. Q_ASSERT(parameterId < param.count);
  562. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  563. *strBuf = 0;
  564. }
  565. /*!
  566. * Get the name of the program at \a index.
  567. */
  568. void getProgramName(uint32_t index, char* const strBuf)
  569. {
  570. Q_ASSERT(index < prog.count);
  571. strncpy(strBuf, prog.names[index], STR_MAX);
  572. }
  573. /*!
  574. * Get the name of the MIDI program at \a index.
  575. *
  576. * \see getMidiProgramInfo()
  577. */
  578. void getMidiProgramName(uint32_t index, char* const strBuf)
  579. {
  580. Q_ASSERT(index < midiprog.count);
  581. strncpy(strBuf, midiprog.data[index].name, STR_MAX);
  582. }
  583. /*!
  584. * Get information about the plugin's parameter count.\n
  585. * This is used to check how many input, output and total parameters are available.\n
  586. *
  587. * \note Some parameters might not be input or output (ie, invalid).
  588. *
  589. * \see parameterCount()
  590. */
  591. void getParameterCountInfo(uint32_t* ins, uint32_t* outs, uint32_t* total)
  592. {
  593. uint32_t _ins = 0;
  594. uint32_t _outs = 0;
  595. uint32_t _total = param.count;
  596. for (uint32_t i=0; i < param.count; i++)
  597. {
  598. if (param.data[i].type == PARAMETER_INPUT)
  599. _ins += 1;
  600. else if (param.data[i].type == PARAMETER_OUTPUT)
  601. _outs += 1;
  602. }
  603. *ins = _ins;
  604. *outs = _outs;
  605. *total = _total;
  606. }
  607. /*!
  608. * Get information about the plugin's custom GUI, if provided.
  609. */
  610. virtual void getGuiInfo(GuiType* type, bool* resizable)
  611. {
  612. *type = GUI_NONE;
  613. *resizable = false;
  614. }
  615. // -------------------------------------------------------------------
  616. // Set data (internal stuff)
  617. /*!
  618. * Set the plugin id to \a yesNo.
  619. *
  620. * \see id()
  621. */
  622. void setId(unsigned short id)
  623. {
  624. m_id = id;
  625. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  626. cin_channel = id;
  627. }
  628. /*!
  629. * Enable or disable the plugin according to \a yesNo.
  630. *
  631. * When a plugin is disabled, it will never be processed or managed in any way.\n
  632. * If you want to "bypass" a plugin, use setActive() instead.
  633. *
  634. * \see enabled()
  635. */
  636. void setEnabled(bool yesNo)
  637. {
  638. m_enabled = yesNo;
  639. }
  640. /*!
  641. * Set plugin as active according to \a active.
  642. *
  643. * \param sendOsc Send message change over OSC
  644. * \param sendCallback Send message change to registered callback
  645. */
  646. void setActive(bool active, bool sendOsc, bool sendCallback)
  647. {
  648. m_active = active;
  649. double value = active ? 1.0 : 0.0;
  650. #ifndef BUILD_BRIDGE
  651. if (sendOsc)
  652. {
  653. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_ACTIVE, value);
  654. if (m_hints & PLUGIN_IS_BRIDGE)
  655. osc_send_control(&osc.data, PARAMETER_ACTIVE, value);
  656. }
  657. #else
  658. Q_UNUSED(sendOsc);
  659. #endif
  660. if (sendCallback)
  661. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_ACTIVE, 0, value);
  662. }
  663. /*!
  664. * Set the plugin's dry/wet signal value to \a value.\n
  665. * \a value must be between 0.0 and 1.0.
  666. *
  667. * \param sendOsc Send message change over OSC
  668. * \param sendCallback Send message change to registered callback
  669. */
  670. void setDryWet(double value, bool sendOsc, bool sendCallback)
  671. {
  672. if (value < 0.0)
  673. value = 0.0;
  674. else if (value > 1.0)
  675. value = 1.0;
  676. x_drywet = value;
  677. #ifndef BUILD_BRIDGE
  678. if (sendOsc)
  679. {
  680. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_DRYWET, value);
  681. if (m_hints & PLUGIN_IS_BRIDGE)
  682. osc_send_control(&osc.data, PARAMETER_DRYWET, value);
  683. }
  684. #else
  685. Q_UNUSED(sendOsc);
  686. #endif
  687. if (sendCallback)
  688. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_DRYWET, 0, value);
  689. }
  690. /*!
  691. * Set the plugin's output volume to \a value.\n
  692. * \a value must be between 0.0 and 1.27.
  693. *
  694. * \param sendOsc Send message change over OSC
  695. * \param sendCallback Send message change to registered callback
  696. */
  697. void setVolume(double value, bool sendOsc, bool sendCallback)
  698. {
  699. if (value < 0.0)
  700. value = 0.0;
  701. else if (value > 1.27)
  702. value = 1.27;
  703. x_vol = value;
  704. #ifndef BUILD_BRIDGE
  705. if (sendOsc)
  706. {
  707. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_VOLUME, value);
  708. if (m_hints & PLUGIN_IS_BRIDGE)
  709. osc_send_control(&osc.data, PARAMETER_VOLUME, value);
  710. }
  711. #else
  712. Q_UNUSED(sendOsc);
  713. #endif
  714. if (sendCallback)
  715. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_VOLUME, 0, value);
  716. }
  717. /*!
  718. * Set the plugin's output balance-left value to \a value.\n
  719. * \a value must be between -1.0 and 1.0.
  720. *
  721. * \param sendOsc Send message change over OSC
  722. * \param sendCallback Send message change to registered callback
  723. */
  724. void setBalanceLeft(double value, bool sendOsc, bool sendCallback)
  725. {
  726. if (value < -1.0)
  727. value = -1.0;
  728. else if (value > 1.0)
  729. value = 1.0;
  730. x_bal_left = value;
  731. #ifndef BUILD_BRIDGE
  732. if (sendOsc)
  733. {
  734. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, value);
  735. if (m_hints & PLUGIN_IS_BRIDGE)
  736. osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, value);
  737. }
  738. #else
  739. Q_UNUSED(sendOsc);
  740. #endif
  741. if (sendCallback)
  742. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_BALANCE_LEFT, 0, value);
  743. }
  744. /*!
  745. * Set the plugin's output balance-right value to \a value.\n
  746. * \a value must be between -1.0 and 1.0.
  747. *
  748. * \param sendOsc Send message change over OSC
  749. * \param sendCallback Send message change to registered callback
  750. */
  751. void setBalanceRight(double value, bool sendOsc, bool sendCallback)
  752. {
  753. if (value < -1.0)
  754. value = -1.0;
  755. else if (value > 1.0)
  756. value = 1.0;
  757. x_bal_right = value;
  758. #ifndef BUILD_BRIDGE
  759. if (sendOsc)
  760. {
  761. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, value);
  762. if (m_hints & PLUGIN_IS_BRIDGE)
  763. osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, value);
  764. }
  765. #else
  766. Q_UNUSED(sendOsc);
  767. #endif
  768. if (sendCallback)
  769. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, PARAMETER_BALANCE_RIGHT, 0, value);
  770. }
  771. #ifndef BUILD_BRIDGE
  772. /*!
  773. * TODO
  774. */
  775. virtual int setOscBridgeInfo(PluginBridgeInfoType type, const lo_arg* const* const argv)
  776. {
  777. return 1;
  778. Q_UNUSED(type);
  779. Q_UNUSED(argv);
  780. }
  781. #endif
  782. // -------------------------------------------------------------------
  783. // Set data (plugin-specific stuff)
  784. /*!
  785. * Set a plugin's parameter value.\n
  786. * \a value must be within the parameter's range.
  787. *
  788. * \param parameterId The parameter to change
  789. * \param value The new parameter value
  790. * \param sendGui Send message change to plugin's custom GUI, if any
  791. * \param sendOsc Send message change over OSC
  792. * \param sendCallback Send message change to registered callback
  793. *
  794. * \see getParameterValue()
  795. */
  796. virtual void setParameterValue(uint32_t parameterId, double value, bool sendGui, bool sendOsc, bool sendCallback)
  797. {
  798. Q_ASSERT(parameterId < param.count);
  799. #ifndef BUILD_BRIDGE
  800. if (sendOsc)
  801. {
  802. x_engine->osc_send_set_parameter_value(m_id, parameterId, value);
  803. if (m_hints & PLUGIN_IS_BRIDGE)
  804. osc_send_control(&osc.data, parameterId, value);
  805. }
  806. #else
  807. Q_UNUSED(sendOsc);
  808. #endif
  809. if (sendCallback)
  810. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, parameterId, 0, value);
  811. Q_UNUSED(sendGui);
  812. }
  813. /*!
  814. * Set a plugin's parameter value, including internal parameters.\n
  815. * \a rindex can be negative to allow internal parameters change (as defined in InternalParametersIndex).
  816. *
  817. * \see setActive()
  818. * \see setDryWet()
  819. * \see setVolume()
  820. * \see setBalanceLeft()
  821. * \see setBalanceRight()
  822. * \see setParameterValue()
  823. */
  824. void setParameterValueByRIndex(int32_t rindex, double value, bool sendGui, bool sendOsc, bool sendCallback)
  825. {
  826. if (rindex == PARAMETER_ACTIVE)
  827. return setActive(value > 0.0, sendOsc, sendCallback);
  828. if (rindex == PARAMETER_DRYWET)
  829. return setDryWet(value, sendOsc, sendCallback);
  830. if (rindex == PARAMETER_VOLUME)
  831. return setVolume(value, sendOsc, sendCallback);
  832. if (rindex == PARAMETER_BALANCE_LEFT)
  833. return setBalanceLeft(value, sendOsc, sendCallback);
  834. if (rindex == PARAMETER_BALANCE_RIGHT)
  835. return setBalanceRight(value, sendOsc, sendCallback);
  836. for (uint32_t i=0; i < param.count; i++)
  837. {
  838. if (param.data[i].rindex == rindex)
  839. return setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  840. }
  841. }
  842. /*!
  843. * Set parameter's \a parameterId MIDI channel to \a channel.\n
  844. * \a channel must be between 0 and 15.
  845. */
  846. void setParameterMidiChannel(uint32_t parameterId, uint8_t channel, bool sendOsc, bool sendCallback)
  847. {
  848. Q_ASSERT(parameterId < param.count && channel < 16);
  849. param.data[parameterId].midiChannel = channel;
  850. #ifndef BUILD_BRIDGE
  851. if (sendOsc)
  852. x_engine->osc_send_set_parameter_midi_channel(m_id, parameterId, channel);
  853. #else
  854. Q_UNUSED(sendOsc);
  855. #endif
  856. if (sendCallback)
  857. x_engine->callback(CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, m_id, parameterId, channel, 0.0);
  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, bool sendOsc, bool sendCallback)
  864. {
  865. Q_ASSERT(parameterId < param.count);
  866. param.data[parameterId].midiCC = cc;
  867. #ifndef BUILD_BRIDGE
  868. if (sendOsc)
  869. x_engine->osc_send_set_parameter_midi_cc(m_id, parameterId, cc);
  870. #else
  871. Q_UNUSED(sendOsc);
  872. #endif
  873. if (sendCallback)
  874. x_engine->callback(CALLBACK_PARAMETER_MIDI_CC_CHANGED, m_id, parameterId, cc, 0.0);
  875. }
  876. /*!
  877. * Add a custom data set.\n
  878. * If \a key already exists, its value will be swapped with \a value.
  879. *
  880. * \param type Type of data used in \a value.
  881. * \param key A key identifing this data set.
  882. * \param value The value of the data set, of type \a type.
  883. *
  884. * \see customData()
  885. */
  886. virtual void setCustomData(CustomDataType type, const char* const key, const char* const value, bool sendGui)
  887. {
  888. Q_ASSERT(key);
  889. Q_ASSERT(value);
  890. if (! key)
  891. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - key is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  892. if (! value)
  893. return qCritical("CarlaPlugin::setCustomData(%s, \"%s\", \"%s\", %s) - value is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  894. bool saveData = true;
  895. switch (type)
  896. {
  897. case CUSTOM_DATA_INVALID:
  898. saveData = false;
  899. break;
  900. case CUSTOM_DATA_STRING:
  901. // Ignore some keys
  902. if (strncmp(key, "OSC:", 4) == 0 || strcmp(key, "guiVisible") || strcmp(key, "CarlaBridgeSaveNow") == 0)
  903. saveData = false;
  904. break;
  905. default:
  906. break;
  907. }
  908. if (saveData)
  909. {
  910. // Check if we already have this key
  911. for (size_t i=0; i < custom.size(); i++)
  912. {
  913. if (strcmp(custom[i].key, key) == 0)
  914. {
  915. free((void*)custom[i].value);
  916. custom[i].value = strdup(value);
  917. return;
  918. }
  919. }
  920. // False if we get here, so store it
  921. CustomData newData;
  922. newData.type = type;
  923. newData.key = strdup(key);
  924. newData.value = strdup(value);
  925. custom.push_back(newData);
  926. }
  927. }
  928. /*!
  929. * Set the complete chunk data as \a stringData.
  930. * \a stringData must a base64 encoded string of binary data.
  931. *
  932. * \see chunkData()
  933. *
  934. * \note Make sure to verify the plugin supports chunks before calling this function!
  935. */
  936. virtual void setChunkData(const char* const stringData)
  937. {
  938. Q_ASSERT(stringData);
  939. }
  940. /*!
  941. * Change the current plugin program to \a index.
  942. *
  943. * If \a index is negative the plugin's program will be considered unset.
  944. * The plugin's default parameter values will be updated when this function is called.
  945. *
  946. * \param index New program index to use
  947. * \param sendGui Send message change to plugin's custom GUI, if any
  948. * \param sendOsc Send message change over OSC
  949. * \param sendCallback Send message change to registered callback
  950. * \param block Block the audio callback
  951. */
  952. virtual void setProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  953. {
  954. Q_ASSERT(index < (int32_t)prog.count);
  955. if (index < -1)
  956. index = -1;
  957. prog.current = index;
  958. #ifndef BUILD_BRIDGE
  959. if (sendOsc)
  960. {
  961. x_engine->osc_send_set_program(m_id, prog.current);
  962. if (m_hints & PLUGIN_IS_BRIDGE)
  963. osc_send_program(&osc.data, prog.current);
  964. }
  965. #else
  966. Q_UNUSED(sendOsc);
  967. #endif
  968. // Change default parameter values
  969. if (index >= 0)
  970. {
  971. for (uint32_t i=0; i < param.count; i++)
  972. {
  973. param.ranges[i].def = getParameterValue(i);
  974. #ifndef BUILD_BRIDGE
  975. if (sendOsc)
  976. x_engine->osc_send_set_default_value(m_id, i, param.ranges[i].def);
  977. #endif
  978. }
  979. }
  980. if (sendCallback)
  981. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, prog.current, 0, 0.0);
  982. Q_UNUSED(sendGui);
  983. Q_UNUSED(block);
  984. }
  985. /*!
  986. * Change the current MIDI plugin program to \a index.
  987. *
  988. * If \a index is negative the plugin's program will be considered unset.
  989. * The plugin's default parameter values will be updated when this function is called.
  990. *
  991. * \param index New program index to use
  992. * \param sendGui Send message change to plugin's custom GUI, if any
  993. * \param sendOsc Send message change over OSC
  994. * \param sendCallback Send message change to registered callback
  995. * \param block Block the audio callback
  996. */
  997. virtual void setMidiProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  998. {
  999. Q_ASSERT(index < (int32_t)midiprog.count);
  1000. if (index < -1)
  1001. index = -1;
  1002. midiprog.current = index;
  1003. #ifndef BUILD_BRIDGE
  1004. if (sendOsc)
  1005. {
  1006. x_engine->osc_send_set_midi_program(m_id, midiprog.current);
  1007. if (m_hints & PLUGIN_IS_BRIDGE)
  1008. osc_send_midi_program(&osc.data, midiprog.current);
  1009. }
  1010. #else
  1011. Q_UNUSED(sendOsc);
  1012. #endif
  1013. // Change default parameter values (sound banks never change defaults)
  1014. if (index >= 0 && m_type != PLUGIN_GIG && m_type != PLUGIN_SF2 && m_type != PLUGIN_SFZ)
  1015. {
  1016. for (uint32_t i=0; i < param.count; i++)
  1017. {
  1018. param.ranges[i].def = getParameterValue(i);
  1019. #ifndef BUILD_BRIDGE
  1020. if (sendOsc)
  1021. x_engine->osc_send_set_default_value(m_id, i, param.ranges[i].def);
  1022. #endif
  1023. }
  1024. }
  1025. if (sendCallback)
  1026. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, midiprog.current, 0, 0.0);
  1027. Q_UNUSED(sendGui);
  1028. Q_UNUSED(block);
  1029. }
  1030. /*!
  1031. * This is an overloaded call to setMidiProgram().\n
  1032. * It changes the current MIDI program using \a bank and \a program values instead of index.
  1033. */
  1034. void setMidiProgramById(uint32_t bank, uint32_t program, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  1035. {
  1036. for (uint32_t i=0; i < midiprog.count; i++)
  1037. {
  1038. if (midiprog.data[i].bank == bank && midiprog.data[i].program == program)
  1039. return setMidiProgram(i, sendGui, sendOsc, sendCallback, block);
  1040. }
  1041. }
  1042. // -------------------------------------------------------------------
  1043. // Set gui stuff
  1044. /*!
  1045. * Set plugin's custom GUI stuff.\n
  1046. * Parameters change between plugin types.
  1047. *
  1048. * \note This function must be always called from the main thread.
  1049. */
  1050. virtual void setGuiData(int data, GuiDataHandle handle)
  1051. {
  1052. Q_UNUSED(data);
  1053. Q_UNUSED(handle);
  1054. }
  1055. /*!
  1056. * Show (or hide) a plugin's custom GUI according to \a yesNo.
  1057. *
  1058. * \note This function must be always called from the main thread.
  1059. */
  1060. virtual void showGui(bool yesNo)
  1061. {
  1062. Q_UNUSED(yesNo);
  1063. }
  1064. /*!
  1065. * Idle plugin's custom GUI.
  1066. *
  1067. * \note This function must be always called from the main thread.
  1068. */
  1069. virtual void idleGui()
  1070. {
  1071. m_needsParamUpdate = false;
  1072. m_needsProgUpdate = false;
  1073. if (! m_enabled)
  1074. return;
  1075. // -------------------------------------------------------
  1076. // Process postponed events
  1077. postEventsRun();
  1078. // -------------------------------------------------------
  1079. // Update parameters (OSC)
  1080. updateOscParameterOutputs();
  1081. // -------------------------------------------------------
  1082. // Send peak values (OSC)
  1083. if (x_engine->isOscControllerRegisted())
  1084. {
  1085. if (audioInCount() > 0)
  1086. {
  1087. x_engine->osc_send_set_input_peak_value(m_id, 1, x_engine->getInputPeak(m_id, 0));
  1088. x_engine->osc_send_set_input_peak_value(m_id, 2, x_engine->getInputPeak(m_id, 1));
  1089. }
  1090. if (audioOutCount() > 0)
  1091. {
  1092. x_engine->osc_send_set_output_peak_value(m_id, 1, x_engine->getOutputPeak(m_id, 0));
  1093. x_engine->osc_send_set_output_peak_value(m_id, 2, x_engine->getOutputPeak(m_id, 1));
  1094. }
  1095. }
  1096. }
  1097. // -------------------------------------------------------------------
  1098. // Plugin state
  1099. /*!
  1100. * Reload the plugin's entire state (including programs).\n
  1101. * The plugin will be disabled during this call.
  1102. */
  1103. virtual void reload()
  1104. {
  1105. }
  1106. /*!
  1107. * Reload the plugin's programs state.
  1108. */
  1109. virtual void reloadPrograms(bool init)
  1110. {
  1111. Q_UNUSED(init);
  1112. }
  1113. /*!
  1114. * Tell the plugin to prepare for save.
  1115. */
  1116. virtual void prepareForSave()
  1117. {
  1118. }
  1119. // -------------------------------------------------------------------
  1120. // Plugin processing
  1121. /*!
  1122. * Plugin process callback.
  1123. */
  1124. virtual void process(float** inBuffer, float** outBuffer, uint32_t frames, uint32_t framesOffset = 0)
  1125. {
  1126. Q_UNUSED(inBuffer);
  1127. Q_UNUSED(outBuffer);
  1128. Q_UNUSED(frames);
  1129. Q_UNUSED(framesOffset);
  1130. }
  1131. #ifdef CARLA_ENGINE_JACK
  1132. void process_jack(uint32_t nframes)
  1133. {
  1134. float* inBuffer[ain.count];
  1135. float* outBuffer[aout.count];
  1136. for (uint32_t i=0; i < ain.count; i++)
  1137. inBuffer[i] = ain.ports[i]->getJackAudioBuffer(nframes);
  1138. for (uint32_t i=0; i < aout.count; i++)
  1139. outBuffer[i] = aout.ports[i]->getJackAudioBuffer(nframes);
  1140. #ifndef BUILD_BRIDGE
  1141. if (carlaOptions.proccess_hp)
  1142. {
  1143. float* inBuffer2[ain.count];
  1144. float* outBuffer2[aout.count];
  1145. for (uint32_t i=0, j; i < nframes; i += 8)
  1146. {
  1147. for (j=0; j < ain.count; j++)
  1148. inBuffer2[j] = inBuffer[j] + i;
  1149. for (j=0; j < aout.count; j++)
  1150. outBuffer2[j] = outBuffer[j] + i;
  1151. process(inBuffer2, outBuffer2, 8, i);
  1152. }
  1153. }
  1154. else
  1155. #endif
  1156. process(inBuffer, outBuffer, nframes);
  1157. }
  1158. #endif
  1159. /*!
  1160. * Tell the plugin the current buffer size has changed.
  1161. */
  1162. virtual void bufferSizeChanged(uint32_t newBufferSize)
  1163. {
  1164. Q_UNUSED(newBufferSize);
  1165. }
  1166. // -------------------------------------------------------------------
  1167. // OSC stuff
  1168. /*!
  1169. * Register this plugin to the global OSC controller.
  1170. */
  1171. void registerToOsc()
  1172. {
  1173. #ifndef BUILD_BRIDGE
  1174. if (! x_engine->isOscControllerRegisted())
  1175. return;
  1176. x_engine->osc_send_add_plugin(m_id, m_name);
  1177. #endif
  1178. // Base data
  1179. {
  1180. char bufName[STR_MAX] = { 0 };
  1181. char bufLabel[STR_MAX] = { 0 };
  1182. char bufMaker[STR_MAX] = { 0 };
  1183. char bufCopyright[STR_MAX] = { 0 };
  1184. getRealName(bufName);
  1185. getLabel(bufLabel);
  1186. getMaker(bufMaker);
  1187. getCopyright(bufCopyright);
  1188. #ifdef BUILD_BRIDGE
  1189. osc_send_bridge_plugin_info(category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1190. #else
  1191. x_engine->osc_send_set_plugin_data(m_id, m_type, category(), m_hints, bufName, bufLabel, bufMaker, bufCopyright, uniqueId());
  1192. #endif
  1193. }
  1194. // Base count
  1195. {
  1196. uint32_t cIns, cOuts, cTotals;
  1197. getParameterCountInfo(&cIns, &cOuts, &cTotals);
  1198. #ifdef BUILD_BRIDGE
  1199. osc_send_bridge_audio_count(audioInCount(), audioOutCount(), audioInCount() + audioOutCount());
  1200. osc_send_bridge_midi_count(midiInCount(), midiOutCount(), midiInCount() + midiOutCount());
  1201. osc_send_bridge_param_count(cIns, cOuts, cTotals);
  1202. #else
  1203. x_engine->osc_send_set_plugin_ports(m_id, audioInCount(), audioOutCount(), midiInCount(), midiOutCount(), cIns, cOuts, cTotals);
  1204. #endif
  1205. }
  1206. // Internal Parameters
  1207. {
  1208. #ifndef BUILD_BRIDGE
  1209. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1210. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_DRYWET, x_drywet);
  1211. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_VOLUME, x_vol);
  1212. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_LEFT, x_bal_left);
  1213. x_engine->osc_send_set_parameter_value(m_id, PARAMETER_BALANCE_RIGHT, x_bal_right);
  1214. #endif
  1215. }
  1216. // Plugin Parameters
  1217. if (param.count > 0 && param.count < carlaOptions.max_parameters)
  1218. {
  1219. char bufName[STR_MAX], bufUnit[STR_MAX];
  1220. for (uint32_t i=0; i < param.count; i++)
  1221. {
  1222. getParameterName(i, bufName);
  1223. getParameterUnit(i, bufUnit);
  1224. #ifdef BUILD_BRIDGE
  1225. osc_send_bridge_param_info(i, bufName, bufUnit);
  1226. 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);
  1227. 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);
  1228. setParameterValue(i, param.ranges[i].def, false, false, true); // FIXME?
  1229. #else
  1230. x_engine->osc_send_set_parameter_data(m_id, i, param.data[i].type, param.data[i].hints, bufName, bufUnit, getParameterValue(i));
  1231. 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);
  1232. #endif
  1233. }
  1234. }
  1235. // Programs
  1236. {
  1237. #ifdef BUILD_BRIDGE
  1238. osc_send_bridge_program_count(prog.count);
  1239. for (uint32_t i=0; i < prog.count; i++)
  1240. osc_send_bridge_program_info(i, prog.names[i]);
  1241. //if (prog.current >= 0)
  1242. osc_send_program(prog.current);
  1243. #else
  1244. x_engine->osc_send_set_program_count(m_id, prog.count);
  1245. for (uint32_t i=0; i < prog.count; i++)
  1246. x_engine->osc_send_set_program_name(m_id, i, prog.names[i]);
  1247. x_engine->osc_send_set_program(m_id, prog.current);
  1248. #endif
  1249. }
  1250. // MIDI Programs
  1251. {
  1252. #ifdef BUILD_BRIDGE
  1253. osc_send_bridge_midi_program_count(midiprog.count);
  1254. for (uint32_t i=0; i < midiprog.count; i++)
  1255. osc_send_bridge_midi_program_info(i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1256. //if (midiprog.current >= 0 /*&& midiprog.count > 0*/)
  1257. //osc_send_midi_program(midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program, false);
  1258. osc_send_midi_program(midiprog.current);
  1259. #else
  1260. x_engine->osc_send_set_midi_program_count(m_id, midiprog.count);
  1261. for (uint32_t i=0; i < midiprog.count; i++)
  1262. x_engine->osc_send_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1263. x_engine->osc_send_set_midi_program(m_id, midiprog.current);
  1264. #endif
  1265. }
  1266. }
  1267. #ifndef BUILD_BRIDGE
  1268. /*!
  1269. * Update the plugin's internal OSC data according to \a source and \a url.\n
  1270. * This is used for OSC-GUI bridges.
  1271. */
  1272. void updateOscData(lo_address source, const char* url)
  1273. {
  1274. const char* host;
  1275. const char* port;
  1276. osc_clear_data(&osc.data);
  1277. host = lo_address_get_hostname(source);
  1278. port = lo_address_get_port(source);
  1279. osc.data.source = lo_address_new(host, port);
  1280. host = lo_url_get_hostname(url);
  1281. port = lo_url_get_port(url);
  1282. osc.data.path = lo_url_get_path(url);
  1283. osc.data.target = lo_address_new(host, port);
  1284. free((void*)host);
  1285. free((void*)port);
  1286. // TODO
  1287. //osc_send_sample_rate(%osc.data, get_sample_rate());
  1288. for (size_t i=0; i < custom.size(); i++)
  1289. {
  1290. if (m_type == PLUGIN_LV2)
  1291. osc_send_lv2_event_transfer(&osc.data, getCustomDataTypeString(custom[i].type), custom[i].key, custom[i].value);
  1292. else if (custom[i].type == CUSTOM_DATA_STRING)
  1293. osc_send_configure(&osc.data, custom[i].key, custom[i].value);
  1294. // FIXME
  1295. }
  1296. if (prog.current >= 0)
  1297. osc_send_program(&osc.data, prog.current);
  1298. if (midiprog.current >= 0)
  1299. {
  1300. //int32_t id = midiprog.current;
  1301. //osc_send_midi_program(&osc.data, midiprog.data[id].bank, midiprog.data[id].program, (m_type == PLUGIN_DSSI));
  1302. osc_send_midi_program(&osc.data, midiprog.current);
  1303. }
  1304. for (uint32_t i=0; i < param.count; i++)
  1305. osc_send_control(&osc.data, param.data[i].rindex, getParameterValue(i));
  1306. if (m_hints & PLUGIN_IS_BRIDGE)
  1307. {
  1308. osc_send_control(&osc.data, PARAMETER_ACTIVE, m_active ? 1.0 : 0.0);
  1309. osc_send_control(&osc.data, PARAMETER_DRYWET, x_drywet);
  1310. osc_send_control(&osc.data, PARAMETER_VOLUME, x_vol);
  1311. osc_send_control(&osc.data, PARAMETER_BALANCE_LEFT, x_bal_left);
  1312. osc_send_control(&osc.data, PARAMETER_BALANCE_RIGHT, x_bal_right);
  1313. }
  1314. }
  1315. /*!
  1316. * TODO
  1317. */
  1318. void updateOscParameterOutputs()
  1319. {
  1320. // Check if it needs update
  1321. bool updatePortsGui = (osc.data.target && (m_hints & PLUGIN_IS_BRIDGE) == 0);
  1322. if (! (x_engine->isOscControllerRegisted() || updatePortsGui))
  1323. return;
  1324. // Update
  1325. double value;
  1326. for (uint32_t i=0; i < param.count; i++)
  1327. {
  1328. if (param.data[i].type == PARAMETER_OUTPUT /*&& (paramData->hints & PARAMETER_IS_AUTOMABLE) > 0*/)
  1329. {
  1330. value = getParameterValue(i);
  1331. if (updatePortsGui)
  1332. osc_send_control(&osc.data, param.data[i].rindex, value);
  1333. x_engine->osc_send_set_parameter_value(m_id, i, value);
  1334. }
  1335. }
  1336. }
  1337. /*!
  1338. * Clear the plugin's internal OSC data.
  1339. */
  1340. void clearOscData()
  1341. {
  1342. osc_clear_data(&osc.data);
  1343. }
  1344. /*!
  1345. * Show the plugin's OSC based GUI.\n
  1346. * This is a handy function that waits for the GUI to respond and automatically asks it to show itself.
  1347. */
  1348. bool showOscGui()
  1349. {
  1350. // wait for UI 'update' call
  1351. for (int i=0; i < carlaOptions.osc_gui_timeout; i++)
  1352. {
  1353. if (osc.data.target)
  1354. {
  1355. osc_send_show(&osc.data);
  1356. return true;
  1357. }
  1358. else
  1359. carla_msleep(100);
  1360. }
  1361. return false;
  1362. }
  1363. #endif
  1364. // -------------------------------------------------------------------
  1365. // MIDI events
  1366. /*!
  1367. * Send a single midi note to be processed in the next audio callback.\n
  1368. * A note with 0 velocity means note-off.
  1369. */
  1370. virtual void sendMidiSingleNote(uint8_t channel, uint8_t note, uint8_t velo, bool sendGui, bool sendOsc, bool sendCallback)
  1371. {
  1372. engineMidiLock();
  1373. for (unsigned short i=0; i<MAX_MIDI_EVENTS; i++)
  1374. {
  1375. if (extMidiNotes[i].channel < 0)
  1376. {
  1377. extMidiNotes[i].channel = channel;
  1378. extMidiNotes[i].note = note;
  1379. extMidiNotes[i].velo = velo;
  1380. break;
  1381. }
  1382. }
  1383. engineMidiUnlock();
  1384. #ifndef BUILD_BRIDGE
  1385. if (sendOsc)
  1386. {
  1387. if (velo)
  1388. x_engine->osc_send_note_on(m_id, channel, note, velo);
  1389. else
  1390. x_engine->osc_send_note_off(m_id, channel, note);
  1391. if (m_hints & PLUGIN_IS_BRIDGE)
  1392. {
  1393. uint8_t midiData[4] = { 0 };
  1394. midiData[1] = (velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + channel;
  1395. midiData[2] = note;
  1396. midiData[3] = velo;
  1397. osc_send_midi(&osc.data, midiData);
  1398. }
  1399. }
  1400. #else
  1401. Q_UNUSED(sendOsc);
  1402. #endif
  1403. if (sendCallback)
  1404. x_engine->callback(velo ? CALLBACK_NOTE_ON : CALLBACK_NOTE_OFF, m_id, note, velo, 0.0);
  1405. Q_UNUSED(sendGui);
  1406. }
  1407. /*!
  1408. * Send all midi notes off for the next audio callback.\n
  1409. * This doesn't send the actual MIDI All-Notes-Off event, but 128 note-offs instead.
  1410. */
  1411. void sendMidiAllNotesOff()
  1412. {
  1413. engineMidiLock();
  1414. postEvents.mutex.lock();
  1415. unsigned short postPad = 0;
  1416. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1417. {
  1418. if (postEvents.data[i].type == PluginPostEventNull)
  1419. {
  1420. postPad = i;
  1421. break;
  1422. }
  1423. }
  1424. if (postPad == MAX_POST_EVENTS - 1)
  1425. {
  1426. qWarning("post-events buffer full, making room for all notes off now");
  1427. postPad -= 128;
  1428. }
  1429. for (unsigned short i=0; i < 128; i++)
  1430. {
  1431. extMidiNotes[i].channel = 0; // FIXME
  1432. extMidiNotes[i].note = i;
  1433. extMidiNotes[i].velo = 0;
  1434. postEvents.data[i + postPad].type = PluginPostEventNoteOff;
  1435. postEvents.data[i + postPad].value1 = i;
  1436. postEvents.data[i + postPad].value2 = 0;
  1437. postEvents.data[i + postPad].value3 = 0.0;
  1438. }
  1439. postEvents.mutex.unlock();
  1440. engineMidiUnlock();
  1441. }
  1442. // -------------------------------------------------------------------
  1443. // Post-poned events
  1444. /*!
  1445. * Post pone an event of type \a type.\n
  1446. * The event will be processed later in a high-priority thread (but not the main one).
  1447. */
  1448. void postponeEvent(PluginPostEventType type, int32_t value1, int32_t value2, double value3)
  1449. {
  1450. postEvents.mutex.lock();
  1451. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1452. {
  1453. if (postEvents.data[i].type == PluginPostEventNull)
  1454. {
  1455. postEvents.data[i].type = type;
  1456. postEvents.data[i].value1 = value1;
  1457. postEvents.data[i].value2 = value2;
  1458. postEvents.data[i].value3 = value3;
  1459. break;
  1460. }
  1461. }
  1462. postEvents.mutex.unlock();
  1463. }
  1464. /*!
  1465. * TODO
  1466. */
  1467. void postEventsRun()
  1468. {
  1469. static PluginPostEvent newPostEvents[MAX_POST_EVENTS];
  1470. // Make a safe copy of events, and clear them
  1471. postEvents.mutex.lock();
  1472. memcpy(newPostEvents, postEvents.data, sizeof(PluginPostEvent)*MAX_POST_EVENTS);
  1473. for (unsigned short i=0; i < MAX_POST_EVENTS; i++)
  1474. postEvents.data[i].type = PluginPostEventNull;
  1475. postEvents.mutex.unlock();
  1476. // Handle events now
  1477. for (uint32_t i=0; i < MAX_POST_EVENTS; i++)
  1478. {
  1479. const PluginPostEvent* const event = &newPostEvents[i];
  1480. switch (event->type)
  1481. {
  1482. case PluginPostEventNull:
  1483. return;
  1484. case PluginPostEventDebug:
  1485. x_engine->callback(CALLBACK_DEBUG, m_id, event->value1, event->value2, event->value3);
  1486. break;
  1487. case PluginPostEventParameterChange:
  1488. // Update OSC based UIs
  1489. m_needsParamUpdate = true;
  1490. osc_send_control(&osc.data, event->value1, event->value3);
  1491. // Update OSC control client
  1492. x_engine->osc_send_set_parameter_value(m_id, event->value1, event->value3);
  1493. // Update Host
  1494. x_engine->callback(CALLBACK_PARAMETER_VALUE_CHANGED, m_id, event->value1, 0, event->value3);
  1495. break;
  1496. case PluginPostEventProgramChange:
  1497. // Update OSC based UIs
  1498. m_needsProgUpdate = true;
  1499. osc_send_program(&osc.data, event->value1);
  1500. // Update OSC control client
  1501. x_engine->osc_send_set_program(m_id, event->value1);
  1502. for (uint32_t j=0; j < param.count; j++)
  1503. x_engine->osc_send_set_default_value(m_id, j, param.ranges[j].def);
  1504. // Update Host
  1505. x_engine->callback(CALLBACK_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1506. break;
  1507. case PluginPostEventMidiProgramChange:
  1508. // Update OSC based UIs
  1509. m_needsProgUpdate = true;
  1510. if (m_type == PLUGIN_DSSI)
  1511. {
  1512. if (event->value1 >= 0 && event->value1 < (int32_t)midiprog.count)
  1513. osc_send_program(&osc.data, midiprog.data[event->value1].bank, midiprog.data[event->value1].program);
  1514. }
  1515. else
  1516. osc_send_midi_program(&osc.data, event->value1);
  1517. // Update OSC control client
  1518. x_engine->osc_send_set_midi_program(m_id, event->value1);
  1519. for (uint32_t j=0; j < param.count; j++)
  1520. x_engine->osc_send_set_default_value(m_id, j, param.ranges[j].def);
  1521. // Update Host
  1522. x_engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, m_id, event->value1, 0, 0.0);
  1523. //}
  1524. break;
  1525. case PluginPostEventNoteOn:
  1526. // Update OSC based UIs
  1527. if (true)
  1528. {
  1529. uint8_t midiData[4] = { 0 };
  1530. midiData[1] = MIDI_STATUS_NOTE_ON + event->value1;
  1531. midiData[2] = event->value2;
  1532. midiData[3] = rint(event->value3);
  1533. osc_send_midi(&osc.data, midiData);
  1534. }
  1535. // Update OSC control client
  1536. x_engine->osc_send_note_on(m_id, event->value1, event->value2, event->value3);
  1537. // Update Host
  1538. x_engine->callback(CALLBACK_NOTE_ON, m_id, event->value1, event->value2, event->value3);
  1539. break;
  1540. case PluginPostEventNoteOff:
  1541. // Update OSC based UIs
  1542. if (true)
  1543. {
  1544. uint8_t midiData[4] = { 0 };
  1545. midiData[1] = MIDI_STATUS_NOTE_OFF + event->value1;
  1546. midiData[2] = event->value2;
  1547. osc_send_midi(&osc.data, midiData);
  1548. }
  1549. // Update OSC control client
  1550. x_engine->osc_send_note_off(m_id, event->value1, event->value2);
  1551. // Update Host
  1552. x_engine->callback(CALLBACK_NOTE_OFF, m_id, event->value1, event->value2, 0.0);
  1553. break;
  1554. }
  1555. }
  1556. }
  1557. // -------------------------------------------------------------------
  1558. // Cleanup
  1559. /*!
  1560. * Clear the engine client ports of the plugin.
  1561. */
  1562. virtual void removeClientPorts()
  1563. {
  1564. qDebug("CarlaPlugin::removeClientPorts() - start");
  1565. for (uint32_t i=0; i < ain.count; i++)
  1566. {
  1567. delete ain.ports[i];
  1568. ain.ports[i] = nullptr;
  1569. }
  1570. for (uint32_t i=0; i < aout.count; i++)
  1571. {
  1572. delete aout.ports[i];
  1573. aout.ports[i] = nullptr;
  1574. }
  1575. if (midi.portMin)
  1576. {
  1577. delete midi.portMin;
  1578. midi.portMin = nullptr;
  1579. }
  1580. if (midi.portMout)
  1581. {
  1582. delete midi.portMout;
  1583. midi.portMout = nullptr;
  1584. }
  1585. if (param.portCin)
  1586. {
  1587. delete param.portCin;
  1588. param.portCin = nullptr;
  1589. }
  1590. if (param.portCout)
  1591. {
  1592. delete param.portCout;
  1593. param.portCout = nullptr;
  1594. }
  1595. qDebug("CarlaPlugin::removeClientPorts() - end");
  1596. }
  1597. /*!
  1598. * Initializes all RT buffers of the plugin.
  1599. */
  1600. virtual void initBuffers()
  1601. {
  1602. uint32_t i;
  1603. for (i=0; i < ain.count; i++)
  1604. {
  1605. if (ain.ports[i])
  1606. ain.ports[i]->initBuffer(x_engine);
  1607. }
  1608. for (i=0; i < aout.count; i++)
  1609. {
  1610. if (aout.ports[i])
  1611. aout.ports[i]->initBuffer(x_engine);
  1612. }
  1613. if (param.portCin)
  1614. param.portCin->initBuffer(x_engine);
  1615. if (param.portCout)
  1616. param.portCout->initBuffer(x_engine);
  1617. if (midi.portMin)
  1618. midi.portMin->initBuffer(x_engine);
  1619. if (midi.portMout)
  1620. midi.portMout->initBuffer(x_engine);
  1621. }
  1622. /*!
  1623. * Delete all temporary buffers of the plugin.
  1624. */
  1625. virtual void deleteBuffers()
  1626. {
  1627. qDebug("CarlaPlugin::deleteBuffers() - start");
  1628. if (ain.count > 0)
  1629. {
  1630. delete[] ain.ports;
  1631. delete[] ain.rindexes;
  1632. }
  1633. if (aout.count > 0)
  1634. {
  1635. delete[] aout.ports;
  1636. delete[] aout.rindexes;
  1637. }
  1638. if (param.count > 0)
  1639. {
  1640. delete[] param.data;
  1641. delete[] param.ranges;
  1642. }
  1643. ain.count = 0;
  1644. ain.ports = nullptr;
  1645. ain.rindexes = nullptr;
  1646. aout.count = 0;
  1647. aout.ports = nullptr;
  1648. aout.rindexes = nullptr;
  1649. param.count = 0;
  1650. param.data = nullptr;
  1651. param.ranges = nullptr;
  1652. param.portCin = nullptr;
  1653. param.portCout = nullptr;
  1654. qDebug("CarlaPlugin::deleteBuffers() - end");
  1655. }
  1656. // -------------------------------------------------------------------
  1657. // Library functions
  1658. /*!
  1659. * Open the DLL \a filename.
  1660. */
  1661. bool libOpen(const char* filename)
  1662. {
  1663. m_lib = lib_open(filename);
  1664. return bool(m_lib);
  1665. }
  1666. /*!
  1667. * Close the DLL previously loaded in libOpen().
  1668. */
  1669. bool libClose()
  1670. {
  1671. if (m_lib)
  1672. return lib_close(m_lib);
  1673. return false;
  1674. }
  1675. /*!
  1676. * Get the symbol entry \a symbol of the currently loaded DLL.
  1677. */
  1678. void* libSymbol(const char* symbol)
  1679. {
  1680. if (m_lib)
  1681. return lib_symbol(m_lib, symbol);
  1682. return nullptr;
  1683. }
  1684. /*!
  1685. * Get the last DLL related error.
  1686. */
  1687. const char* libError(const char* filename)
  1688. {
  1689. return lib_error(filename);
  1690. }
  1691. // -------------------------------------------------------------------
  1692. // Locks
  1693. void engineProcessLock()
  1694. {
  1695. x_engine->processLock();
  1696. }
  1697. void engineProcessUnlock()
  1698. {
  1699. x_engine->processUnlock();
  1700. }
  1701. void engineMidiLock()
  1702. {
  1703. x_engine->midiLock();
  1704. }
  1705. void engineMidiUnlock()
  1706. {
  1707. x_engine->midiUnlock();
  1708. }
  1709. // -------------------------------------------------------------------
  1710. // Plugin initializers
  1711. struct initializer {
  1712. CarlaEngine* const engine;
  1713. const char* const filename;
  1714. const char* const name;
  1715. const char* const label;
  1716. };
  1717. static CarlaPlugin* newLADSPA(const initializer& init, const void* const extra);
  1718. static CarlaPlugin* newDSSI(const initializer& init, const void* const extra);
  1719. static CarlaPlugin* newLV2(const initializer& init);
  1720. static CarlaPlugin* newVST(const initializer& init);
  1721. static CarlaPlugin* newGIG(const initializer& init);
  1722. static CarlaPlugin* newSF2(const initializer& init);
  1723. static CarlaPlugin* newSFZ(const initializer& init);
  1724. #ifndef BUILD_BRIDGE
  1725. static CarlaPlugin* newBridge(const initializer& init, BinaryType btype, PluginType ptype);
  1726. #endif
  1727. // -------------------------------------------------------------------
  1728. protected:
  1729. // static
  1730. unsigned short m_id;
  1731. CarlaEngine* const x_engine;
  1732. // non-static
  1733. PluginType m_type;
  1734. unsigned int m_hints;
  1735. bool m_active;
  1736. bool m_activeBefore;
  1737. bool m_enabled;
  1738. void* m_lib;
  1739. const char* m_name;
  1740. const char* m_filename;
  1741. int8_t cin_channel;
  1742. double x_drywet, x_vol, x_bal_left, x_bal_right;
  1743. CarlaEngineClient* x_client;
  1744. bool m_needsParamUpdate;
  1745. bool m_needsProgUpdate;
  1746. // -------------------------------------------------------------------
  1747. // Storage Data
  1748. PluginAudioData ain;
  1749. PluginAudioData aout;
  1750. PluginMidiData midi;
  1751. PluginParameterData param;
  1752. PluginProgramData prog;
  1753. PluginMidiProgramData midiprog;
  1754. std::vector<CustomData> custom;
  1755. // -------------------------------------------------------------------
  1756. // Extra
  1757. #ifndef BUILD_BRIDGE
  1758. struct {
  1759. CarlaOscData data;
  1760. CarlaPluginThread* thread;
  1761. } osc;
  1762. #endif
  1763. struct {
  1764. QMutex mutex;
  1765. PluginPostEvent data[MAX_POST_EVENTS];
  1766. } postEvents;
  1767. ExternalMidiNote extMidiNotes[MAX_MIDI_EVENTS];
  1768. // -------------------------------------------------------------------
  1769. // Utilities
  1770. static double fixParameterValue(double& value, const ParameterRanges& ranges)
  1771. {
  1772. if (value < ranges.min)
  1773. value = ranges.min;
  1774. else if (value > ranges.max)
  1775. value = ranges.max;
  1776. return value;
  1777. }
  1778. static float fixParameterValue(float& value, const ParameterRanges& ranges)
  1779. {
  1780. if (value < ranges.min)
  1781. value = ranges.min;
  1782. else if (value > ranges.max)
  1783. value = ranges.max;
  1784. return value;
  1785. }
  1786. static double abs(const double& value)
  1787. {
  1788. return (value < 0.0) ? -value : value;
  1789. }
  1790. };
  1791. /*!
  1792. * \class CarlaPluginScopedDisabler
  1793. *
  1794. * \brief Carla plugin scoped disabler
  1795. *
  1796. * This is a handy class that temporarily disables a plugin during a function scope.\n
  1797. * It should be used when the plugin needs reload or state change, something like this:
  1798. * \code
  1799. * {
  1800. * const CarlaPluginScopedDisabler m(plugin);
  1801. * plugin->setChunkData(data);
  1802. * }
  1803. * \endcode
  1804. */
  1805. class CarlaPluginScopedDisabler
  1806. {
  1807. public:
  1808. /*!
  1809. * Disable plugin \a plugin if \a disable is true.
  1810. * The plugin is re-enabled in the deconstructor of this class if \a disable is true.
  1811. *
  1812. * \param plugin The plugin to disable
  1813. * \param disable Wherever to disable the plugin or not, true by default
  1814. */
  1815. CarlaPluginScopedDisabler(CarlaPlugin* const plugin, bool disable = true) :
  1816. m_plugin(plugin),
  1817. m_disable(disable)
  1818. {
  1819. if (m_disable)
  1820. {
  1821. m_plugin->engineProcessLock();
  1822. m_plugin->setEnabled(false);
  1823. m_plugin->engineProcessUnlock();
  1824. }
  1825. }
  1826. ~CarlaPluginScopedDisabler()
  1827. {
  1828. if (m_disable)
  1829. {
  1830. m_plugin->engineProcessLock();
  1831. m_plugin->setEnabled(true);
  1832. m_plugin->engineProcessUnlock();
  1833. }
  1834. }
  1835. private:
  1836. CarlaPlugin* const m_plugin;
  1837. const bool m_disable;
  1838. };
  1839. /**@}*/
  1840. CARLA_BACKEND_END_NAMESPACE
  1841. #endif // CARLA_PLUGIN_H