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.

1926 lines
51KB

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