Audio plugin host https://kx.studio/carla
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.

1384 lines
35KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_ENGINE_HPP_INCLUDED
  18. #define CARLA_ENGINE_HPP_INCLUDED
  19. #include "CarlaBackend.h"
  20. #include "CarlaPluginPtr.hpp"
  21. namespace water {
  22. class MemoryOutputStream;
  23. class XmlDocument;
  24. }
  25. CARLA_BACKEND_START_NAMESPACE
  26. // -----------------------------------------------------------------------
  27. /*!
  28. * @defgroup CarlaEngineAPI Carla Engine API
  29. *
  30. * The Carla Engine API.
  31. * @{
  32. */
  33. /*!
  34. * The type of an engine.
  35. */
  36. enum EngineType {
  37. /*!
  38. * Null engine type.
  39. */
  40. kEngineTypeNull = 0,
  41. /*!
  42. * JACK engine type.
  43. * Provides all processing modes.
  44. */
  45. kEngineTypeJack = 1,
  46. /*!
  47. * JUCE engine type, used to provide Native Audio and MIDI support.
  48. */
  49. kEngineTypeJuce = 2,
  50. /*!
  51. * RtAudio engine type, used to provide Native Audio and MIDI support.
  52. */
  53. kEngineTypeRtAudio = 3,
  54. /*!
  55. * Plugin engine type, used to export the engine as a plugin.
  56. */
  57. kEngineTypePlugin = 4,
  58. /*!
  59. * Bridge engine type, used in BridgePlugin class.
  60. */
  61. kEngineTypeBridge = 5,
  62. /*!
  63. * Dummy engine type, does not send audio or MIDI anywhere.
  64. */
  65. kEngineTypeDummy = 6
  66. };
  67. /*!
  68. * The type of an engine port.
  69. */
  70. enum EnginePortType {
  71. /*!
  72. * Null port type.
  73. */
  74. kEnginePortTypeNull = 0,
  75. /*!
  76. * Audio port type.
  77. * @see CarlaEngineAudioPort
  78. */
  79. kEnginePortTypeAudio = 1,
  80. /*!
  81. * CV port type.
  82. * @see CarlaEngineCVPort
  83. */
  84. kEnginePortTypeCV = 2,
  85. /*!
  86. * Event port type (Control or MIDI).
  87. * @see CarlaEngineEventPort
  88. */
  89. kEnginePortTypeEvent = 3
  90. };
  91. /*!
  92. * The type of an engine event.
  93. */
  94. enum EngineEventType {
  95. /*!
  96. * Null port type.
  97. */
  98. kEngineEventTypeNull = 0,
  99. /*!
  100. * Control event type.
  101. * @see EngineControlEvent
  102. */
  103. kEngineEventTypeControl = 1,
  104. /*!
  105. * MIDI event type.
  106. * @see EngineMidiEvent
  107. */
  108. kEngineEventTypeMidi = 2
  109. };
  110. /*!
  111. * The type of an engine control event.
  112. */
  113. enum EngineControlEventType {
  114. /*!
  115. * Null event type.
  116. */
  117. kEngineControlEventTypeNull = 0,
  118. /*!
  119. * Parameter event type.
  120. * @note Value uses a normalized range of 0.0f<->1.0f.
  121. */
  122. kEngineControlEventTypeParameter = 1,
  123. /*!
  124. * MIDI Bank event type.
  125. */
  126. kEngineControlEventTypeMidiBank = 2,
  127. /*!
  128. * MIDI Program change event type.
  129. */
  130. kEngineControlEventTypeMidiProgram = 3,
  131. /*!
  132. * All sound off event type.
  133. */
  134. kEngineControlEventTypeAllSoundOff = 4,
  135. /*!
  136. * All notes off event type.
  137. */
  138. kEngineControlEventTypeAllNotesOff = 5
  139. };
  140. /*!
  141. * Special value for EngineEvent channel field, indicating a non-midi parameter event.
  142. */
  143. static const uint8_t kEngineEventNonMidiChannel = 0x30;
  144. // -----------------------------------------------------------------------
  145. /*!
  146. * Engine control event.
  147. */
  148. struct CARLA_API EngineControlEvent {
  149. EngineControlEventType type; //!< Control-Event type.
  150. uint16_t param; //!< Parameter Id, midi bank or midi program.
  151. float value; //!< Parameter value, normalized to 0.0f<->1.0f.
  152. /*!
  153. * Convert this control event into MIDI data.
  154. * Returns size.
  155. */
  156. uint8_t convertToMidiData(uint8_t channel, uint8_t data[3]) const noexcept;
  157. };
  158. /*!
  159. * Engine MIDI event.
  160. */
  161. struct CARLA_API EngineMidiEvent {
  162. static const uint8_t kDataSize = 4; //!< Size of internal data
  163. uint8_t port; //!< Port offset (usually 0)
  164. uint8_t size; //!< Number of bytes used
  165. /*!
  166. * MIDI data, without channel bit.
  167. * If size > kDataSize, dataExt is used (otherwise NULL).
  168. */
  169. uint8_t data[kDataSize];
  170. const uint8_t* dataExt;
  171. };
  172. /*!
  173. * Engine event.
  174. */
  175. struct CARLA_API EngineEvent {
  176. EngineEventType type; //!< Event Type; either Control or MIDI
  177. uint32_t time; //!< Time offset in frames
  178. uint8_t channel; //!< Channel, used for MIDI-related events
  179. /*!
  180. * Event specific data.
  181. */
  182. union {
  183. EngineControlEvent ctrl;
  184. EngineMidiEvent midi;
  185. };
  186. /*!
  187. * Fill this event from MIDI data.
  188. */
  189. void fillFromMidiData(uint8_t size, const uint8_t* data, uint8_t midiPortOffset) noexcept;
  190. };
  191. // -----------------------------------------------------------------------
  192. /*!
  193. * Engine options.
  194. */
  195. struct CARLA_API EngineOptions {
  196. EngineProcessMode processMode;
  197. EngineTransportMode transportMode;
  198. const char* transportExtra;
  199. bool forceStereo;
  200. bool resetXruns;
  201. bool preferPluginBridges;
  202. bool preferUiBridges;
  203. bool uisAlwaysOnTop;
  204. uint bgColor;
  205. uint fgColor;
  206. float uiScale;
  207. uint maxParameters;
  208. uint uiBridgesTimeout;
  209. uint audioBufferSize;
  210. uint audioSampleRate;
  211. bool audioTripleBuffer;
  212. const char* audioDriver;
  213. const char* audioDevice;
  214. #ifndef BUILD_BRIDGE
  215. bool oscEnabled;
  216. int oscPortTCP;
  217. int oscPortUDP;
  218. #endif
  219. const char* pathAudio;
  220. const char* pathMIDI;
  221. const char* pathLADSPA;
  222. const char* pathDSSI;
  223. const char* pathLV2;
  224. const char* pathVST2;
  225. const char* pathVST3;
  226. const char* pathSF2;
  227. const char* pathSFZ;
  228. const char* binaryDir;
  229. const char* resourceDir;
  230. bool preventBadBehaviour;
  231. uintptr_t frontendWinId;
  232. #ifndef CARLA_OS_WIN
  233. struct Wine {
  234. const char* executable;
  235. bool autoPrefix;
  236. const char* fallbackPrefix;
  237. bool rtPrio;
  238. int baseRtPrio;
  239. int serverRtPrio;
  240. Wine() noexcept;
  241. ~Wine() noexcept;
  242. CARLA_DECLARE_NON_COPY_STRUCT(Wine)
  243. } wine;
  244. #endif
  245. #ifndef DOXYGEN
  246. EngineOptions() noexcept;
  247. ~EngineOptions() noexcept;
  248. CARLA_DECLARE_NON_COPY_STRUCT(EngineOptions)
  249. #endif
  250. };
  251. /*!
  252. * Engine BBT Time information.
  253. */
  254. struct CARLA_API EngineTimeInfoBBT {
  255. bool valid;
  256. int32_t bar; //!< current bar
  257. int32_t beat; //!< current beat-within-bar
  258. double tick; //!< current tick-within-beat
  259. double barStartTick;
  260. float beatsPerBar; //!< time signature "numerator"
  261. float beatType; //!< time signature "denominator"
  262. double ticksPerBeat;
  263. double beatsPerMinute;
  264. /*!
  265. * Clear.
  266. */
  267. void clear() noexcept;
  268. #ifndef DOXYGEN
  269. EngineTimeInfoBBT() noexcept;
  270. EngineTimeInfoBBT(const EngineTimeInfoBBT&) noexcept;
  271. #endif
  272. };
  273. /*!
  274. * Engine Time information.
  275. */
  276. struct CARLA_API EngineTimeInfo {
  277. bool playing;
  278. uint64_t frame;
  279. uint64_t usecs;
  280. EngineTimeInfoBBT bbt;
  281. /*!
  282. * Clear.
  283. */
  284. void clear() noexcept;
  285. #ifndef DOXYGEN
  286. EngineTimeInfo() noexcept;
  287. EngineTimeInfo(const EngineTimeInfo&) noexcept;
  288. EngineTimeInfo& operator=(const EngineTimeInfo&) noexcept;
  289. // fast comparison, doesn't check all values
  290. bool compareIgnoringRollingFrames(const EngineTimeInfo& timeInfo, uint32_t maxFrames) const noexcept;
  291. // quick operator, doesn't check all values
  292. bool operator==(const EngineTimeInfo& timeInfo) const noexcept;
  293. bool operator!=(const EngineTimeInfo& timeInfo) const noexcept;
  294. #endif
  295. };
  296. // -----------------------------------------------------------------------
  297. /*!
  298. * Carla Engine port (Abstract).
  299. * This is the base class for all Carla Engine ports.
  300. */
  301. class CARLA_API CarlaEnginePort
  302. {
  303. protected:
  304. /*!
  305. * The constructor, protected.
  306. * All constructor parameters are constant and will never change in the lifetime of the port.
  307. */
  308. CarlaEnginePort(const CarlaEngineClient& client, bool isInputPort, uint32_t indexOffset) noexcept;
  309. public:
  310. /*!
  311. * The destructor.
  312. */
  313. virtual ~CarlaEnginePort() noexcept;
  314. /*!
  315. * Get the type of the port, as provided by the respective subclasses.
  316. */
  317. virtual EnginePortType getType() const noexcept = 0;
  318. /*!
  319. * Initialize the port's internal buffer.
  320. */
  321. virtual void initBuffer() noexcept = 0;
  322. /*!
  323. * Check if this port is an input.
  324. */
  325. inline bool isInput() const noexcept
  326. {
  327. return kIsInput;
  328. }
  329. /*!
  330. * Get the index offset as passed in the constructor.
  331. */
  332. inline uint32_t getIndexOffset() const noexcept
  333. {
  334. return kIndexOffset;
  335. }
  336. /*!
  337. * Get this ports' engine client.
  338. */
  339. inline const CarlaEngineClient& getEngineClient() const noexcept
  340. {
  341. return kClient;
  342. }
  343. /*!
  344. * Set a meta-data property on this port.
  345. */
  346. virtual void setMetaData(const char* key, const char* value, const char* type);
  347. #ifndef DOXYGEN
  348. protected:
  349. const CarlaEngineClient& kClient;
  350. const bool kIsInput;
  351. const uint32_t kIndexOffset;
  352. CARLA_DECLARE_NON_COPY_CLASS(CarlaEnginePort)
  353. #endif
  354. };
  355. /*!
  356. * Carla Engine Audio port.
  357. */
  358. class CARLA_API CarlaEngineAudioPort : public CarlaEnginePort
  359. {
  360. public:
  361. /*!
  362. * The constructor.
  363. * All constructor parameters are constant and will never change in the lifetime of the port.
  364. */
  365. CarlaEngineAudioPort(const CarlaEngineClient& client, bool isInputPort, uint32_t indexOffset) noexcept;
  366. /*!
  367. * The destructor.
  368. */
  369. ~CarlaEngineAudioPort() noexcept override;
  370. /*!
  371. * Get the type of the port, in this case kEnginePortTypeAudio.
  372. */
  373. inline EnginePortType getType() const noexcept final
  374. {
  375. return kEnginePortTypeAudio;
  376. }
  377. /*!
  378. * Initialize the port's internal buffer.
  379. */
  380. void initBuffer() noexcept override;
  381. /*!
  382. * Direct access to the port's audio buffer.
  383. * May be null.
  384. */
  385. inline float* getBuffer() const noexcept
  386. {
  387. return fBuffer;
  388. }
  389. #ifndef DOXYGEN
  390. protected:
  391. float* fBuffer;
  392. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineAudioPort)
  393. #endif
  394. };
  395. /*!
  396. * Carla Engine CV port.
  397. */
  398. class CARLA_API CarlaEngineCVPort : public CarlaEnginePort
  399. {
  400. public:
  401. /*!
  402. * The constructor.
  403. * All constructor parameters are constant and will never change in the lifetime of the port.
  404. */
  405. CarlaEngineCVPort(const CarlaEngineClient& client, bool isInputPort, uint32_t indexOffset) noexcept;
  406. /*!
  407. * The destructor.
  408. */
  409. ~CarlaEngineCVPort() noexcept override;
  410. /*!
  411. * Get the type of the port, in this case kEnginePortTypeCV.
  412. */
  413. inline EnginePortType getType() const noexcept final
  414. {
  415. return kEnginePortTypeCV;
  416. }
  417. /*!
  418. * Initialize the port's internal buffer.
  419. */
  420. void initBuffer() noexcept override;
  421. /*!
  422. * Direct access to the port's CV buffer.
  423. * May be null.
  424. */
  425. inline float* getBuffer() const noexcept
  426. {
  427. return fBuffer;
  428. }
  429. /*!
  430. * Get min/max range for this CV port.
  431. */
  432. inline void getRange(float& min, float& max) const noexcept
  433. {
  434. min = fMinimum;
  435. max = fMaximum;
  436. }
  437. /*!
  438. * Set min/max range for this CV port.
  439. */
  440. void setRange(float min, float max) noexcept;
  441. #ifndef DOXYGEN
  442. protected:
  443. float* fBuffer;
  444. float fMinimum, fMaximum;
  445. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineCVPort)
  446. #endif
  447. };
  448. /*!
  449. * Carla Engine Event port.
  450. */
  451. class CARLA_API CarlaEngineEventPort : public CarlaEnginePort
  452. {
  453. public:
  454. /*!
  455. * The constructor.
  456. * All constructor parameters are constant and will never change in the lifetime of the port.
  457. */
  458. CarlaEngineEventPort(const CarlaEngineClient& client, bool isInputPort, uint32_t indexOffset) noexcept;
  459. /*!
  460. * The destructor.
  461. */
  462. ~CarlaEngineEventPort() noexcept override;
  463. /*!
  464. * Get the type of the port, in this case kEnginePortTypeEvent.
  465. */
  466. inline EnginePortType getType() const noexcept final
  467. {
  468. return kEnginePortTypeEvent;
  469. }
  470. /*!
  471. * Initialize the port's internal buffer for @a engine.
  472. */
  473. void initBuffer() noexcept override;
  474. /*!
  475. * Get the number of events present in the buffer.
  476. * @note You must only call this for input ports.
  477. */
  478. virtual uint32_t getEventCount() const noexcept;
  479. /*!
  480. * Get the event at @a index.
  481. * @note You must only call this for input ports.
  482. */
  483. virtual const EngineEvent& getEvent(uint32_t index) const noexcept;
  484. /*!
  485. * Get the event at @a index, faster unchecked version.
  486. */
  487. virtual const EngineEvent& getEventUnchecked(uint32_t index) const noexcept;
  488. /*!
  489. * Write a control event into the buffer.
  490. * @note You must only call this for output ports.
  491. */
  492. bool writeControlEvent(uint32_t time, uint8_t channel, const EngineControlEvent& ctrl) noexcept;
  493. /*!
  494. * Write a control event into the buffer.
  495. * Arguments are the same as in the EngineControlEvent struct.
  496. * @note You must only call this for output ports.
  497. */
  498. virtual bool writeControlEvent(uint32_t time, uint8_t channel, EngineControlEventType type, uint16_t param, float value = 0.0f) noexcept;
  499. /*!
  500. * Write a MIDI event into the buffer.
  501. * @note You must only call this for output ports.
  502. */
  503. bool writeMidiEvent(uint32_t time, uint8_t size, const uint8_t* data) noexcept;
  504. /*!
  505. * Write a MIDI event into the buffer.
  506. * @note You must only call this for output ports.
  507. */
  508. bool writeMidiEvent(uint32_t time, uint8_t channel, const EngineMidiEvent& midi) noexcept;
  509. /*!
  510. * Write a MIDI event into the buffer.
  511. * Arguments are the same as in the EngineMidiEvent struct.
  512. * @note You must only call this for output ports.
  513. */
  514. virtual bool writeMidiEvent(uint32_t time, uint8_t channel, uint8_t size, const uint8_t* data) noexcept;
  515. #ifndef DOXYGEN
  516. protected:
  517. const EngineProcessMode kProcessMode;
  518. EngineEvent* fBuffer;
  519. friend class CarlaPluginInstance;
  520. friend class CarlaEngineCVSourcePorts;
  521. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineEventPort)
  522. #endif
  523. };
  524. // -----------------------------------------------------------------------
  525. /*!
  526. * Carla Engine Meta CV Port.
  527. * FIXME needs a better name...
  528. */
  529. class CARLA_API CarlaEngineCVSourcePorts
  530. {
  531. public:
  532. /*!
  533. * The destructor.
  534. */
  535. virtual ~CarlaEngineCVSourcePorts();
  536. /*!
  537. * Add a CV port as a source of events.
  538. */
  539. virtual bool addCVSource(CarlaEngineCVPort* port, uint32_t portIndexOffset);
  540. /*!
  541. * Remove a CV port as a source of events.
  542. */
  543. virtual bool removeCVSource(uint32_t portIndexOffset);
  544. /*!
  545. * Get events and add them to an event port.
  546. * FIXME needs a better name...
  547. */
  548. virtual void initPortBuffers(const float* const* buffers, uint32_t frames,
  549. bool sampleAccurate, CarlaEngineEventPort* eventPort);
  550. /*!
  551. * Set value range for a CV port.
  552. */
  553. bool setCVSourceRange(uint32_t portIndexOffset, float minimum, float maximum);
  554. /*!
  555. * Destroy all ports.
  556. */
  557. void cleanup();
  558. #ifndef DOXYGEN
  559. protected:
  560. /** @internal */
  561. struct ProtectedData;
  562. ProtectedData* const pData;
  563. /*!
  564. * The constructor, protected.
  565. */
  566. CarlaEngineCVSourcePorts();
  567. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineCVSourcePorts)
  568. #endif
  569. };
  570. // -----------------------------------------------------------------------
  571. /*!
  572. * Carla Engine Client.
  573. * Each plugin requires one client from the engine (created via CarlaEngine::addClient()).
  574. * @note This is a virtual class, some engine types provide custom functionality.
  575. */
  576. class CARLA_API CarlaEngineClient
  577. {
  578. public:
  579. /*!
  580. * The destructor.
  581. */
  582. virtual ~CarlaEngineClient() noexcept;
  583. /*!
  584. * Activate this client.
  585. * Client must be deactivated before calling this function.
  586. */
  587. virtual void activate() noexcept;
  588. /*!
  589. * Deactivate this client.
  590. * Client must be activated before calling this function.
  591. */
  592. virtual void deactivate(bool willClose) noexcept;
  593. /*!
  594. * Check if the client is activated.
  595. */
  596. virtual bool isActive() const noexcept;
  597. /*!
  598. * Check if the client is ok.
  599. * Plugins will refuse to instantiate if this returns false.
  600. * @note This is always true in rack and patchbay processing modes.
  601. */
  602. virtual bool isOk() const noexcept;
  603. /*!
  604. * Get the current latency, in samples.
  605. */
  606. virtual uint32_t getLatency() const noexcept;
  607. /*!
  608. * Change the client's latency.
  609. */
  610. virtual void setLatency(uint32_t samples) noexcept;
  611. /*!
  612. * Add a new port of type @a portType.
  613. * @note This function does nothing in rack processing mode since ports are static there.
  614. */
  615. virtual CarlaEnginePort* addPort(EnginePortType portType, const char* name, bool isInput, uint32_t indexOffset);
  616. /*!
  617. * Remove a previously added port via addPort().
  618. */
  619. virtual bool removePort(EnginePortType portType, const char* name, bool isInput);
  620. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  621. /*!
  622. * Create an instance of CV source ports.
  623. * Must be called only once per client.
  624. */
  625. virtual CarlaEngineCVSourcePorts* createCVSourcePorts();
  626. #endif
  627. /*!
  628. * Get this client's engine.
  629. */
  630. const CarlaEngine& getEngine() const noexcept;
  631. /*!
  632. * Get the engine's process mode.
  633. */
  634. EngineProcessMode getProcessMode() const noexcept;
  635. /*!
  636. * Get port count for a type and mode.
  637. */
  638. uint getPortCount(EnginePortType portType, bool isInput) const noexcept;
  639. /*!
  640. * Get an audio port name.
  641. */
  642. const char* getAudioPortName(bool isInput, uint index) const noexcept;
  643. /*!
  644. * Get a CV port name.
  645. */
  646. const char* getCVPortName(bool isInput, uint index) const noexcept;
  647. /*!
  648. * Get an event port name.
  649. */
  650. const char* getEventPortName(bool isInput, uint index) const noexcept;
  651. #ifndef DOXYGEN
  652. protected:
  653. /** @internal */
  654. struct ProtectedData;
  655. ProtectedData* const pData;
  656. /*!
  657. * The constructor, protected.
  658. */
  659. CarlaEngineClient(ProtectedData* pData);
  660. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineClient)
  661. #endif
  662. };
  663. // -----------------------------------------------------------------------
  664. /*!
  665. * Carla Engine.
  666. * @note This is a virtual class for all available engine types available in Carla.
  667. */
  668. class CARLA_API CarlaEngine
  669. {
  670. protected:
  671. /*!
  672. * The constructor, protected.
  673. * @note This only initializes engine data, it doesn't actually start the engine.
  674. */
  675. CarlaEngine();
  676. public:
  677. /*!
  678. * The destructor.
  679. * The engine must have been closed before this happens.
  680. */
  681. virtual ~CarlaEngine();
  682. // -------------------------------------------------------------------
  683. // Static calls
  684. /*!
  685. * Get the number of available engine drivers.
  686. */
  687. static uint getDriverCount();
  688. /*!
  689. * Get the name of the engine driver at @a index.
  690. */
  691. static const char* getDriverName(uint index);
  692. /*!
  693. * Get the device names of the driver at @a index.
  694. */
  695. static const char* const* getDriverDeviceNames(uint index);
  696. /*!
  697. * Get device information about the driver at @a index and name @a driverName.
  698. */
  699. static const EngineDriverDeviceInfo* getDriverDeviceInfo(uint index, const char* driverName);
  700. /*!
  701. * Show a device custom control panel.
  702. * @see ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL
  703. */
  704. static bool showDriverDeviceControlPanel(uint index, const char* deviceName);
  705. /*!
  706. * Create a new engine, using driver @a driverName.
  707. * Returned value must be deleted when no longer needed.
  708. * @note This only initializes engine data, it doesn't actually start the engine.
  709. */
  710. static CarlaEngine* newDriverByName(const char* driverName);
  711. // -------------------------------------------------------------------
  712. // Constant values
  713. /*!
  714. * Maximum client name size.
  715. */
  716. virtual uint getMaxClientNameSize() const noexcept;
  717. /*!
  718. * Maximum port name size.
  719. */
  720. virtual uint getMaxPortNameSize() const noexcept;
  721. /*!
  722. * Current number of plugins loaded.
  723. */
  724. uint getCurrentPluginCount() const noexcept;
  725. /*!
  726. * Maximum number of loadable plugins allowed.
  727. * This function returns 0 if engine is not started.
  728. */
  729. uint getMaxPluginNumber() const noexcept;
  730. // -------------------------------------------------------------------
  731. // Virtual, per-engine type calls
  732. /*!
  733. * Initialize/start the engine, using @a clientName.
  734. * When the engine is initialized, you need to call idle() at regular intervals.
  735. */
  736. virtual bool init(const char* clientName) = 0;
  737. /*!
  738. * Close engine.
  739. * This function always closes the engine even if it returns false.
  740. * In other words, even when something goes wrong when closing the engine it still be closed nonetheless.
  741. */
  742. virtual bool close();
  743. /*!
  744. * Idle engine.
  745. */
  746. virtual void idle() noexcept;
  747. /*!
  748. * Check if engine is running.
  749. */
  750. virtual bool isRunning() const noexcept = 0;
  751. /*!
  752. * Check if engine is running offline (aka freewheel mode).
  753. */
  754. virtual bool isOffline() const noexcept = 0;
  755. /*!
  756. * Check if engine runs on a constant buffer size value.
  757. * Default implementation returns true.
  758. */
  759. virtual bool usesConstantBufferSize() const noexcept;
  760. /*!
  761. * Get engine type.
  762. */
  763. virtual EngineType getType() const noexcept = 0;
  764. /*!
  765. * Get the currently used driver name.
  766. */
  767. virtual const char* getCurrentDriverName() const noexcept = 0;
  768. /*!
  769. * Add new engine client.
  770. * @note This function must only be called within a plugin class.
  771. */
  772. virtual CarlaEngineClient* addClient(CarlaPluginPtr plugin);
  773. /*!
  774. * Get the current CPU load estimated by the engine.
  775. */
  776. virtual float getDSPLoad() const noexcept;
  777. /*!
  778. * Get the total number of xruns so far.
  779. */
  780. virtual uint32_t getTotalXruns() const noexcept;
  781. /*!
  782. * Clear the xrun count.
  783. */
  784. virtual void clearXruns() const noexcept;
  785. /*!
  786. * Dynamically change buffer size and/or sample rate while engine is running.
  787. * @see ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE
  788. * @see ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE
  789. */
  790. virtual bool setBufferSizeAndSampleRate(uint bufferSize, double sampleRate);
  791. /*!
  792. * Show the custom control panel for the current engine device.
  793. * @see ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL
  794. */
  795. virtual bool showDeviceControlPanel() const noexcept;
  796. // -------------------------------------------------------------------
  797. // Plugin management
  798. /*!
  799. * Add new plugin.
  800. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  801. */
  802. bool addPlugin(BinaryType btype, PluginType ptype,
  803. const char* filename, const char* name, const char* label, int64_t uniqueId,
  804. const void* extra, uint options = PLUGIN_OPTIONS_NULL);
  805. /*!
  806. * Add new plugin, using native binary type.
  807. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  808. */
  809. bool addPlugin(PluginType ptype,
  810. const char* filename, const char* name, const char* label, int64_t uniqueId,
  811. const void* extra);
  812. /*!
  813. * Remove plugin with id @a id.
  814. * @see ENGINE_CALLBACK_PLUGIN_REMOVED
  815. */
  816. virtual bool removePlugin(uint id);
  817. /*!
  818. * Remove all plugins.
  819. */
  820. bool removeAllPlugins();
  821. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  822. /*!
  823. * Rename plugin with id @a id to @a newName.
  824. * Returns the new name, or null if the operation failed.
  825. * Returned variable must be deleted if non-null.
  826. * @see ENGINE_CALLBACK_PLUGIN_RENAMED
  827. */
  828. virtual bool renamePlugin(uint id, const char* newName);
  829. /*!
  830. * Clone plugin with id @a id.
  831. */
  832. bool clonePlugin(uint id);
  833. /*!
  834. * Prepare replace of plugin with id @a id.
  835. * The next call to addPlugin() will use this id, replacing the selected plugin.
  836. * @note This function requires addPlugin() to be called afterwards, as soon as possible.
  837. */
  838. bool replacePlugin(uint id) noexcept;
  839. /*!
  840. * Switch plugins with id @a idA and @a idB.
  841. */
  842. virtual bool switchPlugins(uint idA, uint idB) noexcept;
  843. #endif
  844. /*!
  845. * Set a plugin's parameter in drag/touch mode.
  846. * Usually happens from a UI when the user is moving a parameter with a mouse or similar input.
  847. *
  848. * @param parameterId The parameter to update
  849. * @param touch The new state for the parameter
  850. */
  851. virtual void touchPluginParameter(uint id, uint32_t parameterId, bool touch) noexcept;
  852. /*!
  853. * Get plugin with id @a id.
  854. */
  855. CarlaPluginPtr getPlugin(uint id) const noexcept;
  856. /*!
  857. * Get plugin with id @a id, faster unchecked version.
  858. */
  859. CarlaPluginPtr getPluginUnchecked(uint id) const noexcept;
  860. /*!
  861. * Get a unique plugin name within the engine.
  862. * Returned variable must be deleted if non-null.
  863. */
  864. const char* getUniquePluginName(const char* name) const;
  865. // -------------------------------------------------------------------
  866. // Project management
  867. /*!
  868. * Load a file of any type.
  869. * This will try to load a generic file as a plugin,
  870. * either by direct handling (SF2 and SFZ) or by using an internal plugin (like Audio and MIDI).
  871. */
  872. bool loadFile(const char* filename);
  873. /*!
  874. * Load a project file.
  875. * @note Already loaded plugins are not removed; call removeAllPlugins() first if needed.
  876. */
  877. bool loadProject(const char* filename, bool setAsCurrentProject);
  878. /*!
  879. * Save current project to a file.
  880. */
  881. bool saveProject(const char* filename, bool setAsCurrentProject);
  882. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  883. /*!
  884. * Get the currently set project filename.
  885. */
  886. const char* getCurrentProjectFilename() const noexcept;
  887. /*!
  888. * Clear the currently set project filename.
  889. */
  890. void clearCurrentProjectFilename() noexcept;
  891. #endif
  892. // -------------------------------------------------------------------
  893. // Information (base)
  894. /*!
  895. * Get the current buffer size.
  896. */
  897. uint32_t getBufferSize() const noexcept;
  898. /*!
  899. * Get the current sample rate.
  900. */
  901. double getSampleRate() const noexcept;
  902. /*!
  903. * Get the current engine name.
  904. */
  905. const char* getName() const noexcept;
  906. /*!
  907. * Get the current engine process mode.
  908. */
  909. EngineProcessMode getProccessMode() const noexcept;
  910. /*!
  911. * Get the current engine options (read-only).
  912. */
  913. const EngineOptions& getOptions() const noexcept;
  914. /*!
  915. * Get the current Time information (read-only).
  916. */
  917. virtual EngineTimeInfo getTimeInfo() const noexcept;
  918. // -------------------------------------------------------------------
  919. // Information (peaks)
  920. /*!
  921. * Get a plugin's peak values.
  922. * @note not thread-safe if pluginId == MAIN_CARLA_PLUGIN_ID
  923. */
  924. const float* getPeaks(uint pluginId) const noexcept;
  925. /*!
  926. * Get a plugin's input peak value.
  927. */
  928. float getInputPeak(uint pluginId, bool isLeft) const noexcept;
  929. /*!
  930. * Get a plugin's output peak value.
  931. */
  932. float getOutputPeak(uint pluginId, bool isLeft) const noexcept;
  933. // -------------------------------------------------------------------
  934. // Callback
  935. /*!
  936. * Call the main engine callback, if set.
  937. * May be called by plugins.
  938. */
  939. virtual void callback(bool sendHost, bool sendOSC,
  940. EngineCallbackOpcode action, uint pluginId,
  941. int value1, int value2, int value3, float valuef, const char* valueStr) noexcept;
  942. /*!
  943. * Set the main engine callback to @a func.
  944. */
  945. void setCallback(EngineCallbackFunc func, void* ptr) noexcept;
  946. // -------------------------------------------------------------------
  947. // Callback
  948. /*!
  949. * Call the file callback, if set.
  950. * May be called by plugins.
  951. */
  952. virtual const char* runFileCallback(FileCallbackOpcode action,
  953. bool isDir, const char* title, const char* filter) noexcept;
  954. /*!
  955. * Set the file callback to @a func.
  956. */
  957. void setFileCallback(FileCallbackFunc func, void* ptr) noexcept;
  958. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  959. // -------------------------------------------------------------------
  960. // Patchbay
  961. /*!
  962. * Connect two patchbay ports.
  963. */
  964. virtual bool patchbayConnect(bool external,
  965. uint groupA, uint portA,
  966. uint groupB, uint portB);
  967. /*!
  968. * Remove a patchbay connection.
  969. */
  970. virtual bool patchbayDisconnect(bool external, uint connectionId);
  971. /*!
  972. * Set the position of a group.
  973. */
  974. virtual bool patchbaySetGroupPos(bool sendHost, bool sendOSC, bool external,
  975. uint groupId, int x1, int y1, int x2, int y2);
  976. /*!
  977. * Force the engine to resend all patchbay clients, ports and connections again.
  978. */
  979. virtual bool patchbayRefresh(bool sendHost, bool sendOSC, bool external);
  980. #endif
  981. // -------------------------------------------------------------------
  982. // Transport
  983. /*!
  984. * Start playback of the engine transport.
  985. */
  986. virtual void transportPlay() noexcept;
  987. /*!
  988. * Pause the engine transport.
  989. */
  990. virtual void transportPause() noexcept;
  991. /*!
  992. * Set the engine transport bpm to @a bpm.
  993. */
  994. virtual void transportBPM(double bpm) noexcept;
  995. /*!
  996. * Relocate the engine transport to @a frames.
  997. */
  998. virtual void transportRelocate(uint64_t frame) noexcept;
  999. // -------------------------------------------------------------------
  1000. // Error handling
  1001. /*!
  1002. * Get last error.
  1003. */
  1004. const char* getLastError() const noexcept;
  1005. /*!
  1006. * Set last error.
  1007. */
  1008. void setLastError(const char* error) const noexcept;
  1009. // -------------------------------------------------------------------
  1010. // Misc
  1011. /*!
  1012. * Check if the engine is about to close.
  1013. */
  1014. bool isAboutToClose() const noexcept;
  1015. /*!
  1016. * Tell the engine it's about to close.
  1017. * This is used to prevent the engine thread(s) from reactivating.
  1018. * Returns true if there's no pending engine events.
  1019. */
  1020. bool setAboutToClose() noexcept;
  1021. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1022. /*!
  1023. * TODO.
  1024. */
  1025. bool isLoadingProject() const noexcept;
  1026. #endif
  1027. /*!
  1028. * Tell the engine to stop the current cancelable action.
  1029. * @see ENGINE_CALLBACK_CANCELABLE_ACTION
  1030. */
  1031. void setActionCanceled(bool canceled) noexcept;
  1032. /*!
  1033. * Check wherever the last cancelable action was indeed canceled or not.
  1034. */
  1035. bool wasActionCanceled() const noexcept;
  1036. // -------------------------------------------------------------------
  1037. // Options
  1038. /*!
  1039. * Set the engine option @a option to @a value or @a valueStr.
  1040. */
  1041. virtual void setOption(EngineOption option, int value, const char* valueStr) noexcept;
  1042. // -------------------------------------------------------------------
  1043. // OSC Stuff
  1044. #ifndef BUILD_BRIDGE
  1045. /*!
  1046. * Check if OSC controller is registered.
  1047. */
  1048. bool isOscControlRegistered() const noexcept;
  1049. /*!
  1050. * Get OSC TCP server path.
  1051. */
  1052. const char* getOscServerPathTCP() const noexcept;
  1053. /*!
  1054. * Get OSC UDP server path.
  1055. */
  1056. const char* getOscServerPathUDP() const noexcept;
  1057. #endif
  1058. // -------------------------------------------------------------------
  1059. protected:
  1060. /*!
  1061. * Internal data, for CarlaEngine subclasses and friends.
  1062. */
  1063. struct ProtectedData;
  1064. ProtectedData* const pData;
  1065. /*!
  1066. * Some internal classes read directly from pData or call protected functions.
  1067. */
  1068. friend class CarlaEngineEventPort;
  1069. friend class CarlaEngineOsc;
  1070. friend class CarlaEngineThread;
  1071. friend class CarlaPluginInstance;
  1072. friend class EngineInternalGraph;
  1073. friend class PendingRtEventsRunner;
  1074. friend class ScopedActionLock;
  1075. friend class ScopedEngineEnvironmentLocker;
  1076. friend class ScopedThreadStopper;
  1077. friend class PatchbayGraph;
  1078. friend struct ExternalGraph;
  1079. friend struct RackGraph;
  1080. // -------------------------------------------------------------------
  1081. // Internal stuff
  1082. /*!
  1083. * Report to all plugins about buffer size change.
  1084. */
  1085. void bufferSizeChanged(uint32_t newBufferSize);
  1086. /*!
  1087. * Report to all plugins about sample rate change.
  1088. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  1089. */
  1090. void sampleRateChanged(double newSampleRate);
  1091. /*!
  1092. * Report to all plugins about offline mode change.
  1093. */
  1094. void offlineModeChanged(bool isOffline);
  1095. /*!
  1096. * Set a plugin (stereo) peak values.
  1097. * @note RT call
  1098. */
  1099. void setPluginPeaksRT(uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  1100. /*!
  1101. * Common save project function for main engine and plugin.
  1102. */
  1103. void saveProjectInternal(water::MemoryOutputStream& outStrm) const;
  1104. /*!
  1105. * Common load project function for main engine and plugin.
  1106. */
  1107. bool loadProjectInternal(water::XmlDocument& xmlDoc);
  1108. // -------------------------------------------------------------------
  1109. // Helper functions
  1110. /*!
  1111. * Return internal data, needed for EventPorts when used in Rack, Patchbay and Bridge modes.
  1112. * @note RT call
  1113. */
  1114. EngineEvent* getInternalEventBuffer(bool isInput) const noexcept;
  1115. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1116. // -------------------------------------------------------------------
  1117. // Patchbay stuff
  1118. /*!
  1119. * Virtual functions for handling patchbay state.
  1120. * Do not free returned data.
  1121. */
  1122. struct PatchbayPosition { const char* name; int x1, y1, x2, y2, pluginId; bool dealloc; };
  1123. virtual const char* const* getPatchbayConnections(bool external) const;
  1124. virtual const PatchbayPosition* getPatchbayPositions(bool external, uint& count) const;
  1125. virtual void restorePatchbayConnection(bool external, const char* sourcePort, const char* targetPort);
  1126. virtual void restorePatchbayGroupPosition(bool external, const PatchbayPosition& ppos);
  1127. /*!
  1128. * Virtual functions for handling external graph ports.
  1129. */
  1130. virtual bool connectExternalGraphPort(uint, uint, const char*);
  1131. virtual bool disconnectExternalGraphPort(uint, uint, const char*);
  1132. #endif
  1133. // -------------------------------------------------------------------
  1134. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngine)
  1135. };
  1136. /**@}*/
  1137. // -----------------------------------------------------------------------
  1138. CARLA_BACKEND_END_NAMESPACE
  1139. #endif // CARLA_ENGINE_HPP_INCLUDED