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.

1412 lines
36KB

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