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