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.

2168 lines
58KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #ifndef CARLA_PLUGIN_H
  18. #define CARLA_PLUGIN_H
  19. #include "carla_engine.h"
  20. #include "carla_midi.h"
  21. #include "carla_shared.h"
  22. #include "carla_lib_includes.h"
  23. #ifdef BUILD_BRIDGE
  24. # include "carla_bridge_osc.h"
  25. #endif
  26. // common includes
  27. #include <cmath>
  28. #include <cstdio>
  29. #include <cstdlib>
  30. #include <vector>
  31. #ifndef __WINE__
  32. # include <QtGui/QDialog>
  33. #endif
  34. CARLA_BACKEND_START_NAMESPACE
  35. /*!
  36. * @defgroup CarlaBackendPlugin Carla Backend Plugin
  37. *
  38. * The Carla Backend Plugin.
  39. * @{
  40. */
  41. #define CARLA_PROCESS_CONTINUE_CHECK if (! m_enabled) { x_engine->callback(CALLBACK_DEBUG, m_id, m_enabled, 0, 0.0); return; }
  42. #ifdef __WINE__
  43. typedef HWND GuiDataHandle;
  44. #else
  45. typedef QDialog* GuiDataHandle;
  46. #endif
  47. const unsigned short MAX_MIDI_EVENTS = 512;
  48. const unsigned short MAX_POST_EVENTS = 152;
  49. const char* const CARLA_BRIDGE_MSG_HIDE_GUI = "CarlaBridgeHideGUI"; //!< Plugin -> Host call, tells host GUI is now hidden
  50. const char* const CARLA_BRIDGE_MSG_SAVED = "CarlaBridgeSaved"; //!< Plugin -> Host call, tells host state is saved
  51. const char* const CARLA_BRIDGE_MSG_SAVE_NOW = "CarlaBridgeSaveNow"; //!< Host -> Plugin call, tells plugin to save state now
  52. const char* const CARLA_BRIDGE_MSG_SET_CHUNK = "CarlaBridgeSetChunk"; //!< Host -> Plugin call, tells plugin to set chunk in file \a value
  53. 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.
  54. #ifndef BUILD_BRIDGE
  55. enum PluginBridgeInfoType {
  56. PluginBridgeAudioCount,
  57. PluginBridgeMidiCount,
  58. PluginBridgeParameterCount,
  59. PluginBridgeProgramCount,
  60. PluginBridgeMidiProgramCount,
  61. PluginBridgePluginInfo,
  62. PluginBridgeParameterInfo,
  63. PluginBridgeParameterDataInfo,
  64. PluginBridgeParameterRangesInfo,
  65. PluginBridgeProgramInfo,
  66. PluginBridgeMidiProgramInfo,
  67. PluginBridgeCustomData,
  68. PluginBridgeChunkData,
  69. PluginBridgeUpdateNow,
  70. PluginBridgeSaved
  71. };
  72. #endif
  73. enum PluginPostEventType {
  74. PluginPostEventNull,
  75. PluginPostEventDebug,
  76. PluginPostEventParameterChange, // param, N, value
  77. PluginPostEventProgramChange, // index
  78. PluginPostEventMidiProgramChange, // index
  79. PluginPostEventNoteOn, // channel, note, velo
  80. PluginPostEventNoteOff // channel, note
  81. };
  82. struct PluginAudioData {
  83. uint32_t count;
  84. uint32_t* rindexes;
  85. CarlaEngineAudioPort** ports;
  86. PluginAudioData()
  87. : count(0),
  88. rindexes(nullptr),
  89. ports(nullptr) {}
  90. };
  91. struct PluginMidiData {
  92. CarlaEngineMidiPort* portMin;
  93. CarlaEngineMidiPort* portMout;
  94. PluginMidiData()
  95. : portMin(nullptr),
  96. portMout(nullptr) {}
  97. };
  98. struct PluginParameterData {
  99. uint32_t count;
  100. ParameterData* data;
  101. ParameterRanges* ranges;
  102. CarlaEngineControlPort* portCin;
  103. CarlaEngineControlPort* portCout;
  104. PluginParameterData()
  105. : count(0),
  106. data(nullptr),
  107. ranges(nullptr),
  108. portCin(nullptr),
  109. portCout(nullptr) {}
  110. };
  111. struct PluginProgramData {
  112. uint32_t count;
  113. int32_t current;
  114. const char** names;
  115. PluginProgramData()
  116. : count(0),
  117. current(-1),
  118. names(nullptr) {}
  119. };
  120. struct PluginMidiProgramData {
  121. uint32_t count;
  122. int32_t current;
  123. midi_program_t* data;
  124. PluginMidiProgramData()
  125. : count(0),
  126. current(-1),
  127. data(nullptr) {}
  128. };
  129. struct PluginPostEvent {
  130. PluginPostEventType type;
  131. int32_t value1;
  132. int32_t value2;
  133. double value3;
  134. PluginPostEvent()
  135. : type(PluginPostEventNull),
  136. value1(-1),
  137. value2(-1),
  138. value3(0.0) {}
  139. };
  140. struct ExternalMidiNote {
  141. int8_t channel; // invalid = -1
  142. uint8_t note;
  143. uint8_t velo;
  144. ExternalMidiNote()
  145. : channel(-1),
  146. note(0),
  147. velo(0) {}
  148. };
  149. /*!
  150. * \class CarlaPlugin
  151. *
  152. * \brief Carla Backend base plugin class
  153. *
  154. * This is the base class for all available plugin types available in Carla Backend.\n
  155. * All virtual calls are implemented in this class as fallback, so it's safe to only override needed calls.
  156. *
  157. * \see PluginType
  158. */
  159. class CarlaPlugin
  160. {
  161. public:
  162. /*!
  163. * This is the constructor of the base plugin class.
  164. *
  165. * \param id The 'id' of this plugin, must between 0 and MAX_PLUGINS
  166. */
  167. CarlaPlugin(CarlaEngine* const engine, unsigned short id) :
  168. m_id(id),
  169. x_engine(engine),
  170. x_client(nullptr)
  171. {
  172. Q_ASSERT(engine);
  173. Q_ASSERT(id < MAX_PLUGINS);
  174. qDebug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  175. m_type = PLUGIN_NONE;
  176. m_hints = 0;
  177. m_active = false;
  178. m_activeBefore = false;
  179. m_enabled = false;
  180. m_lib = nullptr;
  181. m_name = nullptr;
  182. m_filename = nullptr;
  183. #ifndef BUILD_BRIDGE
  184. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  185. cin_channel = m_id;
  186. else
  187. #endif
  188. cin_channel = 0;
  189. x_drywet = 1.0;
  190. x_vol = 1.0;
  191. x_bal_left = -1.0;
  192. x_bal_right = 1.0;
  193. #ifndef BUILD_BRIDGE
  194. osc.data.path = nullptr;
  195. osc.data.source = nullptr;
  196. osc.data.target = nullptr;
  197. osc.thread = nullptr;
  198. #endif
  199. }
  200. virtual ~CarlaPlugin()
  201. {
  202. qDebug("CarlaPlugin::~CarlaPlugin()");
  203. // Remove client and ports
  204. if (x_client)
  205. {
  206. if (x_client->isActive())
  207. x_client->deactivate();
  208. removeClientPorts();
  209. delete x_client;
  210. }
  211. // Delete data
  212. deleteBuffers();
  213. // Unload DLL
  214. libClose();
  215. if (m_name)
  216. free((void*)m_name);
  217. if (m_filename)
  218. free((void*)m_filename);
  219. if (prog.count > 0)
  220. {
  221. for (uint32_t i=0; i < prog.count; i++)
  222. {
  223. if (prog.names[i])
  224. free((void*)prog.names[i]);
  225. }
  226. delete[] prog.names;
  227. }
  228. if (midiprog.count > 0)
  229. {
  230. for (uint32_t i=0; i < midiprog.count; i++)
  231. {
  232. if (midiprog.data[i].name)
  233. free((void*)midiprog.data[i].name);
  234. }
  235. delete[] midiprog.data;
  236. }
  237. if (custom.size() > 0)
  238. {
  239. for (size_t i=0; i < custom.size(); i++)
  240. {
  241. if (custom[i].key)
  242. free((void*)custom[i].key);
  243. if (custom[i].value)
  244. free((void*)custom[i].value);
  245. }
  246. custom.clear();
  247. }
  248. }
  249. // -------------------------------------------------------------------
  250. // Information (base)
  251. /*!
  252. * Get the plugin's type (ie, a subclass of CarlaPlugin).
  253. *
  254. * \note Plugin bridges will return their respective plugin type, there is no plugin type such as "bridge".\n
  255. * To check if a plugin is a bridge use:
  256. * \code
  257. * if (hints() & PLUGIN_IS_BRIDGE)
  258. * ...
  259. * \endcode
  260. */
  261. PluginType type() const
  262. {
  263. return m_type;
  264. }
  265. /*!
  266. * Get the plugin's id (as passed in the constructor).
  267. *
  268. * \see setId()
  269. */
  270. unsigned short id() const
  271. {
  272. return m_id;
  273. }
  274. /*!
  275. * Get the plugin's hints.
  276. *
  277. * \see PluginHints
  278. */
  279. unsigned int hints() const
  280. {
  281. return m_hints;
  282. }
  283. /*!
  284. * Check if the plugin is enabled.
  285. *
  286. * \see setEnabled()
  287. */
  288. bool enabled() const
  289. {
  290. return m_enabled;
  291. }
  292. /*!
  293. * Get the plugin's internal name.\n
  294. * This name is unique within all plugins (same as getRealName() but with suffix added if needed).
  295. *
  296. * \see getRealName()
  297. */
  298. const char* name() const
  299. {
  300. return m_name;
  301. }
  302. /*!
  303. * Get the currently loaded DLL filename for this plugin.\n
  304. * (Sound kits return their exact filename).
  305. */
  306. const char* filename() const
  307. {
  308. return m_filename;
  309. }
  310. /*!
  311. * Get the plugin's category (delay, filter, synth, etc).
  312. */
  313. virtual PluginCategory category()
  314. {
  315. return PLUGIN_CATEGORY_NONE;
  316. }
  317. /*!
  318. * Get the plugin's native unique Id.\n
  319. * May return 0 on plugin types that don't support Ids.
  320. */
  321. virtual long uniqueId()
  322. {
  323. return 0;
  324. }
  325. // -------------------------------------------------------------------
  326. // Information (count)
  327. /*!
  328. * Get the number of audio inputs.
  329. */
  330. virtual uint32_t audioInCount()
  331. {
  332. return ain.count;
  333. }
  334. /*!
  335. * Get the number of audio outputs.
  336. */
  337. virtual uint32_t audioOutCount()
  338. {
  339. return aout.count;
  340. }
  341. /*!
  342. * Get the number of MIDI inputs.
  343. */
  344. virtual uint32_t midiInCount()
  345. {
  346. return midi.portMin ? 1 : 0;
  347. }
  348. /*!
  349. * Get the number of MIDI outputs.
  350. */
  351. virtual uint32_t midiOutCount()
  352. {
  353. return midi.portMout ? 1 : 0;
  354. }
  355. /*!
  356. * Get the number of parameters.\n
  357. * To know the number of parameter inputs and outputs, use getParameterCountInfo() instead.
  358. */
  359. uint32_t parameterCount() const
  360. {
  361. return param.count;
  362. }
  363. /*!
  364. * Get the number of scalepoints for parameter \a parameterId.
  365. */
  366. virtual uint32_t parameterScalePointCount(uint32_t parameterId)
  367. {
  368. Q_ASSERT(parameterId < param.count);
  369. return 0;
  370. }
  371. /*!
  372. * Get the number of programs.
  373. */
  374. uint32_t programCount() const
  375. {
  376. return prog.count;
  377. }
  378. /*!
  379. * Get the number of MIDI programs.
  380. */
  381. uint32_t midiProgramCount() const
  382. {
  383. return midiprog.count;
  384. }
  385. /*!
  386. * Get the number of custom data sets.
  387. */
  388. uint32_t customDataCount() const
  389. {
  390. return custom.size();
  391. }
  392. // -------------------------------------------------------------------
  393. // Information (current data)
  394. /*!
  395. * Get the current program number (-1 if unset).
  396. *
  397. * \see setProgram()
  398. */
  399. int32_t currentProgram() const
  400. {
  401. return prog.current;
  402. }
  403. /*!
  404. * Get the current MIDI program number (-1 if unset).
  405. *
  406. * \see setMidiProgram()
  407. * \see setMidiProgramById()
  408. */
  409. int32_t currentMidiProgram() const
  410. {
  411. return midiprog.current;
  412. }
  413. /*!
  414. * Get the parameter data of \a parameterId.
  415. */
  416. const ParameterData* parameterData(uint32_t parameterId) const
  417. {
  418. Q_ASSERT(parameterId < param.count);
  419. return &param.data[parameterId];
  420. }
  421. /*!
  422. * Get the parameter ranges of \a parameterId.
  423. */
  424. const ParameterRanges* parameterRanges(uint32_t parameterId) const
  425. {
  426. Q_ASSERT(parameterId < param.count);
  427. return &param.ranges[parameterId];
  428. }
  429. /*!
  430. * Check if a parameter is of output type.
  431. *
  432. * \see PARAMETER_OUTPUT
  433. */
  434. bool parameterIsOutput(uint32_t parameterId) const
  435. {
  436. Q_ASSERT(parameterId < param.count);
  437. return (param.data[parameterId].type == PARAMETER_OUTPUT);
  438. }
  439. /*!
  440. * Get the MIDI program at \a index.
  441. *
  442. * \see getMidiProgramName()
  443. */
  444. const midi_program_t* midiProgramData(uint32_t index) const
  445. {
  446. Q_ASSERT(index < midiprog.count);
  447. return &midiprog.data[index];
  448. }
  449. /*!
  450. * Get the custom data set at \a index.
  451. *
  452. * \see setCustomData()
  453. */
  454. const CustomData* customData(uint32_t index) const
  455. {
  456. Q_ASSERT(index < custom.size());
  457. return &custom[index];
  458. }
  459. /*!
  460. * Get the complete plugin chunk data.
  461. *
  462. * \param dataPtr TODO
  463. * \return The size of the chunk.
  464. *
  465. * \note Make sure to verify the plugin supports chunks before calling this function!
  466. *
  467. * \see setChunkData()
  468. */
  469. virtual int32_t chunkData(void** const dataPtr)
  470. {
  471. Q_ASSERT(dataPtr);
  472. return 0;
  473. }
  474. #ifndef BUILD_BRIDGE
  475. /*!
  476. * Get the plugin's internal OSC data.
  477. */
  478. //const OscData* oscData() const
  479. //{
  480. // return &osc.data;
  481. //}
  482. #endif
  483. // -------------------------------------------------------------------
  484. // Information (per-plugin data)
  485. /*!
  486. * Get the parameter value of \a parameterId.
  487. */
  488. virtual double getParameterValue(uint32_t parameterId)
  489. {
  490. Q_ASSERT(parameterId < param.count);
  491. return 0.0;
  492. }
  493. /*!
  494. * Get the scalepoint \a scalePointId value of the parameter \a parameterId.
  495. */
  496. virtual double getParameterScalePointValue(uint32_t parameterId, uint32_t scalePointId)
  497. {
  498. Q_ASSERT(parameterId < param.count);
  499. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  500. return 0.0;
  501. }
  502. /*!
  503. * Get the plugin's label (URI for PLUGIN_LV2).
  504. */
  505. virtual void getLabel(char* const strBuf)
  506. {
  507. *strBuf = 0;
  508. }
  509. /*!
  510. * Get the plugin's maker.
  511. */
  512. virtual void getMaker(char* const strBuf)
  513. {
  514. *strBuf = 0;
  515. }
  516. /*!
  517. * Get the plugin's copyright/license.
  518. */
  519. virtual void getCopyright(char* const strBuf)
  520. {
  521. *strBuf = 0;
  522. }
  523. /*!
  524. * Get the plugin's (real) name.
  525. *
  526. * \see name()
  527. */
  528. virtual void getRealName(char* const strBuf)
  529. {
  530. *strBuf = 0;;
  531. }
  532. /*!
  533. * Get the name of the parameter \a parameterId.
  534. */
  535. virtual void getParameterName(uint32_t parameterId, char* const strBuf)
  536. {
  537. Q_ASSERT(parameterId < param.count);
  538. *strBuf = 0;
  539. }
  540. /*!
  541. * Get the symbol of the parameter \a parameterId.
  542. */
  543. virtual void getParameterSymbol(uint32_t parameterId, char* const strBuf)
  544. {
  545. Q_ASSERT(parameterId < param.count);
  546. *strBuf = 0;
  547. }
  548. /*!
  549. * Get the custom text of the parameter \a parameterId.
  550. */
  551. virtual void getParameterText(uint32_t parameterId, char* const strBuf)
  552. {
  553. Q_ASSERT(parameterId < param.count);
  554. *strBuf = 0;
  555. }
  556. /*!
  557. * Get the unit of the parameter \a parameterId.
  558. */
  559. virtual void getParameterUnit(uint32_t parameterId, char* const strBuf)
  560. {
  561. Q_ASSERT(parameterId < param.count);
  562. *strBuf = 0;
  563. }
  564. /*!
  565. * Get the scalepoint \a scalePointId label of the parameter \a parameterId.
  566. */
  567. virtual void getParameterScalePointLabel(uint32_t parameterId, uint32_t scalePointId, char* const strBuf)
  568. {
  569. Q_ASSERT(parameterId < param.count);
  570. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  571. *strBuf = 0;
  572. }
  573. /*!
  574. * Get the name of the program at \a index.
  575. */
  576. void getProgramName(uint32_t index, char* const strBuf)
  577. {
  578. Q_ASSERT(index < prog.count);
  579. strncpy(strBuf, prog.names[index], STR_MAX);
  580. }
  581. /*!
  582. * Get the name of the MIDI program at \a index.
  583. *
  584. * \see getMidiProgramInfo()
  585. */
  586. void getMidiProgramName(uint32_t index, char* const strBuf)
  587. {
  588. Q_ASSERT(index < midiprog.count);
  589. strncpy(strBuf, midiprog.data[index].name, STR_MAX);
  590. }
  591. /*!
  592. * Get information about the plugin's parameter count.\n
  593. * This is used to check how many input, output and total parameters are available.\n
  594. *
  595. * \note Some parameters might not be input or output (ie, invalid).
  596. *
  597. * \see parameterCount()
  598. */
  599. void getParameterCountInfo(uint32_t* ins, uint32_t* outs, uint32_t* total)
  600. {
  601. *ins = 0;
  602. *outs = 0;
  603. *total = param.count;
  604. for (uint32_t i=0; i < param.count; i++)
  605. {
  606. if (param.data[i].type == PARAMETER_INPUT)
  607. *ins += 1;
  608. else if (param.data[i].type == PARAMETER_OUTPUT)
  609. *outs += 1;
  610. }
  611. }
  612. /*!
  613. * Get information about the plugin's custom GUI, if provided.
  614. */
  615. virtual void getGuiInfo(GuiType* type, bool* resizable)
  616. {
  617. *type = GUI_NONE;
  618. *resizable = false;
  619. }
  620. // -------------------------------------------------------------------
  621. // Set data (internal stuff)
  622. #ifndef BUILD_BRIDGE
  623. /*!
  624. * Set the plugin id to \a yesNo.
  625. *
  626. * \see id()
  627. */
  628. void setId(unsigned short id)
  629. {
  630. m_id = id;
  631. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  632. cin_channel = id;
  633. }
  634. #endif
  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.processHighPrecision)
  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.maxParameters)
  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 (uint i=0; i < carlaOptions.oscUiTimeout; 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. // -------------------------------------------------------------------
  1769. protected:
  1770. unsigned short m_id;
  1771. CarlaEngine* const x_engine;
  1772. CarlaEngineClient* x_client;
  1773. PluginType m_type;
  1774. unsigned int m_hints;
  1775. bool m_active;
  1776. bool m_activeBefore;
  1777. bool m_enabled;
  1778. void* m_lib;
  1779. const char* m_name;
  1780. const char* m_filename;
  1781. int8_t cin_channel;
  1782. double x_drywet, x_vol, x_bal_left, x_bal_right;
  1783. // -------------------------------------------------------------------
  1784. // Storage Data
  1785. PluginAudioData ain;
  1786. PluginAudioData aout;
  1787. PluginMidiData midi;
  1788. PluginParameterData param;
  1789. PluginProgramData prog;
  1790. PluginMidiProgramData midiprog;
  1791. std::vector<CustomData> custom;
  1792. // -------------------------------------------------------------------
  1793. // Extra
  1794. #ifndef BUILD_BRIDGE
  1795. struct {
  1796. CarlaOscData data;
  1797. CarlaPluginThread* thread;
  1798. } osc;
  1799. #endif
  1800. struct {
  1801. QMutex mutex;
  1802. PluginPostEvent data[MAX_POST_EVENTS];
  1803. } postEvents;
  1804. ExternalMidiNote extMidiNotes[MAX_MIDI_EVENTS];
  1805. // -------------------------------------------------------------------
  1806. // Utilities
  1807. static double fixParameterValue(double& value, const ParameterRanges& ranges)
  1808. {
  1809. if (value < ranges.min)
  1810. value = ranges.min;
  1811. else if (value > ranges.max)
  1812. value = ranges.max;
  1813. return value;
  1814. }
  1815. static float fixParameterValue(float& value, const ParameterRanges& ranges)
  1816. {
  1817. if (value < ranges.min)
  1818. value = ranges.min;
  1819. else if (value > ranges.max)
  1820. value = ranges.max;
  1821. return value;
  1822. }
  1823. static double abs(const double& value)
  1824. {
  1825. return (value < 0.0) ? -value : value;
  1826. }
  1827. };
  1828. /**@}*/
  1829. CARLA_BACKEND_END_NAMESPACE
  1830. #endif // CARLA_PLUGIN_H