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.

1393 lines
35KB

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