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.

1405 lines
35KB

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