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.

1386 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 <memory>
  21. namespace water {
  22. class MemoryOutputStream;
  23. class XmlDocument;
  24. }
  25. CARLA_BACKEND_START_NAMESPACE
  26. typedef std::shared_ptr<CarlaPlugin> CarlaPluginPtr;
  27. // -----------------------------------------------------------------------
  28. /*!
  29. * @defgroup CarlaEngineAPI Carla Engine API
  30. *
  31. * The Carla Engine API.
  32. * @{
  33. */
  34. /*!
  35. * The type of an engine.
  36. */
  37. enum EngineType {
  38. /*!
  39. * Null engine type.
  40. */
  41. kEngineTypeNull = 0,
  42. /*!
  43. * JACK engine type.
  44. * Provides all processing modes.
  45. */
  46. kEngineTypeJack = 1,
  47. /*!
  48. * JUCE engine type, used to provide Native Audio and MIDI support.
  49. */
  50. kEngineTypeJuce = 2,
  51. /*!
  52. * RtAudio engine type, used to provide Native Audio and MIDI support.
  53. */
  54. kEngineTypeRtAudio = 3,
  55. /*!
  56. * Plugin engine type, used to export the engine as a plugin.
  57. */
  58. kEngineTypePlugin = 4,
  59. /*!
  60. * Bridge engine type, used in BridgePlugin class.
  61. */
  62. kEngineTypeBridge = 5,
  63. /*!
  64. * Dummy engine type, does not send audio or MIDI anywhere.
  65. */
  66. kEngineTypeDummy = 6
  67. };
  68. /*!
  69. * The type of an engine port.
  70. */
  71. enum EnginePortType {
  72. /*!
  73. * Null port type.
  74. */
  75. kEnginePortTypeNull = 0,
  76. /*!
  77. * Audio port type.
  78. * @see CarlaEngineAudioPort
  79. */
  80. kEnginePortTypeAudio = 1,
  81. /*!
  82. * CV port type.
  83. * @see CarlaEngineCVPort
  84. */
  85. kEnginePortTypeCV = 2,
  86. /*!
  87. * Event port type (Control or MIDI).
  88. * @see CarlaEngineEventPort
  89. */
  90. kEnginePortTypeEvent = 3
  91. };
  92. /*!
  93. * The type of an engine event.
  94. */
  95. enum EngineEventType {
  96. /*!
  97. * Null port type.
  98. */
  99. kEngineEventTypeNull = 0,
  100. /*!
  101. * Control event type.
  102. * @see EngineControlEvent
  103. */
  104. kEngineEventTypeControl = 1,
  105. /*!
  106. * MIDI event type.
  107. * @see EngineMidiEvent
  108. */
  109. kEngineEventTypeMidi = 2
  110. };
  111. /*!
  112. * The type of an engine control event.
  113. */
  114. enum EngineControlEventType {
  115. /*!
  116. * Null event type.
  117. */
  118. kEngineControlEventTypeNull = 0,
  119. /*!
  120. * Parameter event type.
  121. * @note Value uses a normalized range of 0.0f<->1.0f.
  122. */
  123. kEngineControlEventTypeParameter = 1,
  124. /*!
  125. * MIDI Bank event type.
  126. */
  127. kEngineControlEventTypeMidiBank = 2,
  128. /*!
  129. * MIDI Program change event type.
  130. */
  131. kEngineControlEventTypeMidiProgram = 3,
  132. /*!
  133. * All sound off event type.
  134. */
  135. kEngineControlEventTypeAllSoundOff = 4,
  136. /*!
  137. * All notes off event type.
  138. */
  139. kEngineControlEventTypeAllNotesOff = 5
  140. };
  141. /*!
  142. * Special value for EngineEvent channel field, indicating a non-midi parameter event.
  143. */
  144. static const uint8_t kEngineEventNonMidiChannel = 0x30;
  145. // -----------------------------------------------------------------------
  146. /*!
  147. * Engine control event.
  148. */
  149. struct CARLA_API EngineControlEvent {
  150. EngineControlEventType type; //!< Control-Event type.
  151. uint16_t param; //!< Parameter Id, midi bank or midi program.
  152. float value; //!< Parameter value, normalized to 0.0f<->1.0f.
  153. /*!
  154. * Convert this control event into MIDI data.
  155. * Returns size.
  156. */
  157. uint8_t convertToMidiData(uint8_t channel, uint8_t data[3]) const noexcept;
  158. };
  159. /*!
  160. * Engine MIDI event.
  161. */
  162. struct CARLA_API EngineMidiEvent {
  163. static const uint8_t kDataSize = 4; //!< Size of internal data
  164. uint8_t port; //!< Port offset (usually 0)
  165. uint8_t size; //!< Number of bytes used
  166. /*!
  167. * MIDI data, without channel bit.
  168. * If size > kDataSize, dataExt is used (otherwise NULL).
  169. */
  170. uint8_t data[kDataSize];
  171. const uint8_t* dataExt;
  172. };
  173. /*!
  174. * Engine event.
  175. */
  176. struct CARLA_API EngineEvent {
  177. EngineEventType type; //!< Event Type; either Control or MIDI
  178. uint32_t time; //!< Time offset in frames
  179. uint8_t channel; //!< Channel, used for MIDI-related events
  180. /*!
  181. * Event specific data.
  182. */
  183. union {
  184. EngineControlEvent ctrl;
  185. EngineMidiEvent midi;
  186. };
  187. /*!
  188. * Fill this event from MIDI data.
  189. */
  190. void fillFromMidiData(uint8_t size, const uint8_t* data, uint8_t midiPortOffset) noexcept;
  191. };
  192. // -----------------------------------------------------------------------
  193. /*!
  194. * Engine options.
  195. */
  196. struct CARLA_API EngineOptions {
  197. EngineProcessMode processMode;
  198. EngineTransportMode transportMode;
  199. const char* transportExtra;
  200. bool forceStereo;
  201. bool resetXruns;
  202. bool preferPluginBridges;
  203. bool preferUiBridges;
  204. bool uisAlwaysOnTop;
  205. uint bgColor;
  206. uint fgColor;
  207. float uiScale;
  208. uint maxParameters;
  209. uint uiBridgesTimeout;
  210. uint audioBufferSize;
  211. uint audioSampleRate;
  212. bool audioTripleBuffer;
  213. const char* audioDriver;
  214. const char* audioDevice;
  215. #ifndef BUILD_BRIDGE
  216. bool oscEnabled;
  217. int oscPortTCP;
  218. int oscPortUDP;
  219. #endif
  220. const char* pathAudio;
  221. const char* pathMIDI;
  222. const char* pathLADSPA;
  223. const char* pathDSSI;
  224. const char* pathLV2;
  225. const char* pathVST2;
  226. const char* pathVST3;
  227. const char* pathSF2;
  228. const char* pathSFZ;
  229. const char* binaryDir;
  230. const char* resourceDir;
  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);
  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 filename.
  886. */
  887. const char* getCurrentProjectFilename() const noexcept;
  888. /*!
  889. * Clear the currently set project filename.
  890. */
  891. void clearCurrentProjectFilename() noexcept;
  892. #endif
  893. // -------------------------------------------------------------------
  894. // Information (base)
  895. /*!
  896. * Get the current buffer size.
  897. */
  898. uint32_t getBufferSize() const noexcept;
  899. /*!
  900. * Get the current sample rate.
  901. */
  902. double getSampleRate() const noexcept;
  903. /*!
  904. * Get the current engine name.
  905. */
  906. const char* getName() const noexcept;
  907. /*!
  908. * Get the current engine process mode.
  909. */
  910. EngineProcessMode getProccessMode() const noexcept;
  911. /*!
  912. * Get the current engine options (read-only).
  913. */
  914. const EngineOptions& getOptions() const noexcept;
  915. /*!
  916. * Get the current Time information (read-only).
  917. */
  918. virtual EngineTimeInfo getTimeInfo() const noexcept;
  919. // -------------------------------------------------------------------
  920. // Information (peaks)
  921. /*!
  922. * Get a plugin's peak values.
  923. * @note not thread-safe if pluginId == MAIN_CARLA_PLUGIN_ID
  924. */
  925. const float* getPeaks(uint pluginId) const noexcept;
  926. /*!
  927. * Get a plugin's input peak value.
  928. */
  929. float getInputPeak(uint pluginId, bool isLeft) const noexcept;
  930. /*!
  931. * Get a plugin's output peak value.
  932. */
  933. float getOutputPeak(uint pluginId, bool isLeft) const noexcept;
  934. // -------------------------------------------------------------------
  935. // Callback
  936. /*!
  937. * Call the main engine callback, if set.
  938. * May be called by plugins.
  939. */
  940. virtual void callback(bool sendHost, bool sendOSC,
  941. EngineCallbackOpcode action, uint pluginId,
  942. int value1, int value2, int value3, float valuef, const char* valueStr) noexcept;
  943. /*!
  944. * Set the main engine callback to @a func.
  945. */
  946. void setCallback(EngineCallbackFunc func, void* ptr) noexcept;
  947. // -------------------------------------------------------------------
  948. // Callback
  949. /*!
  950. * Call the file callback, if set.
  951. * May be called by plugins.
  952. */
  953. const char* runFileCallback(FileCallbackOpcode action, 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