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.

1406 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* 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 is running.
  760. */
  761. virtual bool isRunning() const noexcept = 0;
  762. /*!
  763. * Check if engine is running offline (aka freewheel mode).
  764. */
  765. virtual bool isOffline() const noexcept = 0;
  766. /*!
  767. * Check if engine runs on a constant buffer size value.
  768. * Default implementation returns true.
  769. */
  770. virtual bool usesConstantBufferSize() const noexcept;
  771. /*!
  772. * Get engine type.
  773. */
  774. virtual EngineType getType() const noexcept = 0;
  775. /*!
  776. * Get the currently used driver name.
  777. */
  778. virtual const char* getCurrentDriverName() const noexcept = 0;
  779. /*!
  780. * Add new engine client.
  781. * @note This function must only be called within a plugin class.
  782. */
  783. virtual CarlaEngineClient* addClient(CarlaPluginPtr plugin);
  784. /*!
  785. * Get the current CPU load estimated by the engine.
  786. */
  787. virtual float getDSPLoad() const noexcept;
  788. /*!
  789. * Get the total number of xruns so far.
  790. */
  791. virtual uint32_t getTotalXruns() const noexcept;
  792. /*!
  793. * Clear the xrun count.
  794. */
  795. virtual void clearXruns() const noexcept;
  796. /*!
  797. * Dynamically change buffer size and/or sample rate while engine is running.
  798. * @see ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE
  799. * @see ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE
  800. */
  801. virtual bool setBufferSizeAndSampleRate(uint bufferSize, double sampleRate);
  802. /*!
  803. * Show the custom control panel for the current engine device.
  804. * @see ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL
  805. */
  806. virtual bool showDeviceControlPanel() const noexcept;
  807. // -------------------------------------------------------------------
  808. // Plugin management
  809. /*!
  810. * Add new plugin.
  811. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  812. */
  813. bool addPlugin(BinaryType btype, PluginType ptype,
  814. const char* filename, const char* name, const char* label, int64_t uniqueId,
  815. const void* extra, uint options = PLUGIN_OPTIONS_NULL);
  816. /*!
  817. * Add new plugin, using native binary type.
  818. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  819. */
  820. bool addPlugin(PluginType ptype,
  821. const char* filename, const char* name, const char* label, int64_t uniqueId,
  822. const void* extra);
  823. /*!
  824. * Remove plugin with id @a id.
  825. * @see ENGINE_CALLBACK_PLUGIN_REMOVED
  826. */
  827. virtual bool removePlugin(uint id);
  828. /*!
  829. * Remove all plugins.
  830. */
  831. bool removeAllPlugins();
  832. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  833. /*!
  834. * Rename plugin with id @a id to @a newName.
  835. * Returns the new name, or null if the operation failed.
  836. * Returned variable must be deleted if non-null.
  837. * @see ENGINE_CALLBACK_PLUGIN_RENAMED
  838. */
  839. virtual bool renamePlugin(uint id, const char* newName);
  840. /*!
  841. * Clone plugin with id @a id.
  842. */
  843. bool clonePlugin(uint id);
  844. /*!
  845. * Prepare replace of plugin with id @a id.
  846. * The next call to addPlugin() will use this id, replacing the selected plugin.
  847. * @note This function requires addPlugin() to be called afterwards, as soon as possible.
  848. */
  849. bool replacePlugin(uint id) noexcept;
  850. /*!
  851. * Switch plugins with id @a idA and @a idB.
  852. */
  853. virtual bool switchPlugins(uint idA, uint idB) noexcept;
  854. #endif
  855. /*!
  856. * Set a plugin's parameter in drag/touch mode.
  857. * Usually happens from a UI when the user is moving a parameter with a mouse or similar input.
  858. *
  859. * @param parameterId The parameter to update
  860. * @param touch The new state for the parameter
  861. */
  862. virtual void touchPluginParameter(uint id, uint32_t parameterId, bool touch) noexcept;
  863. /*!
  864. * Get plugin with id @a id.
  865. */
  866. CarlaPluginPtr getPlugin(uint id) const noexcept;
  867. /*!
  868. * Get plugin with id @a id, faster unchecked version.
  869. */
  870. CarlaPluginPtr getPluginUnchecked(uint id) const noexcept;
  871. /*!
  872. * Get a unique plugin name within the engine.
  873. * Returned variable must be deleted if non-null.
  874. */
  875. const char* getUniquePluginName(const char* name) const;
  876. // -------------------------------------------------------------------
  877. // Project management
  878. /*!
  879. * Load a file of any type.
  880. * This will try to load a generic file as a plugin,
  881. * either by direct handling (SF2 and SFZ) or by using an internal plugin (like Audio and MIDI).
  882. */
  883. bool loadFile(const char* filename);
  884. /*!
  885. * Load a project file.
  886. * @note Already loaded plugins are not removed; call removeAllPlugins() first if needed.
  887. */
  888. bool loadProject(const char* filename, bool setAsCurrentProject);
  889. /*!
  890. * Save current project to a file.
  891. */
  892. bool saveProject(const char* filename, bool setAsCurrentProject);
  893. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  894. /*!
  895. * Get the currently set project folder.
  896. * @note Valid for both standalone and plugin versions.
  897. */
  898. virtual const char* getCurrentProjectFolder() const noexcept;
  899. /*!
  900. * Get the currently set project filename.
  901. * @note Valid only for both standalone version.
  902. */
  903. const char* getCurrentProjectFilename() const noexcept;
  904. /*!
  905. * Clear the currently set project filename.
  906. */
  907. void clearCurrentProjectFilename() noexcept;
  908. #endif
  909. // -------------------------------------------------------------------
  910. // Information (base)
  911. /*!
  912. * Get the current buffer size.
  913. */
  914. uint32_t getBufferSize() const noexcept;
  915. /*!
  916. * Get the current sample rate.
  917. */
  918. double getSampleRate() const noexcept;
  919. /*!
  920. * Get the current engine name.
  921. */
  922. const char* getName() const noexcept;
  923. /*!
  924. * Get the current engine process mode.
  925. */
  926. EngineProcessMode getProccessMode() const noexcept;
  927. /*!
  928. * Get the current engine options (read-only).
  929. */
  930. const EngineOptions& getOptions() const noexcept;
  931. /*!
  932. * Get the current Time information (read-only).
  933. */
  934. virtual EngineTimeInfo getTimeInfo() const noexcept;
  935. // -------------------------------------------------------------------
  936. // Information (peaks)
  937. /*!
  938. * Get a plugin's peak values.
  939. * @note not thread-safe if pluginId == MAIN_CARLA_PLUGIN_ID
  940. */
  941. const float* getPeaks(uint pluginId) const noexcept;
  942. /*!
  943. * Get a plugin's input peak value.
  944. */
  945. float getInputPeak(uint pluginId, bool isLeft) const noexcept;
  946. /*!
  947. * Get a plugin's output peak value.
  948. */
  949. float getOutputPeak(uint pluginId, bool isLeft) const noexcept;
  950. // -------------------------------------------------------------------
  951. // Callback
  952. /*!
  953. * Call the main engine callback, if set.
  954. * May be called by plugins.
  955. */
  956. virtual void callback(bool sendHost, bool sendOSC,
  957. EngineCallbackOpcode action, uint pluginId,
  958. int value1, int value2, int value3, float valuef, const char* valueStr) noexcept;
  959. /*!
  960. * Set the main engine callback to @a func.
  961. */
  962. void setCallback(EngineCallbackFunc func, void* ptr) noexcept;
  963. // -------------------------------------------------------------------
  964. // Callback
  965. /*!
  966. * Call the file callback, if set.
  967. * May be called by plugins.
  968. */
  969. virtual const char* runFileCallback(FileCallbackOpcode action,
  970. bool isDir, const char* title, const char* filter) noexcept;
  971. /*!
  972. * Set the file callback to @a func.
  973. */
  974. void setFileCallback(FileCallbackFunc func, void* ptr) noexcept;
  975. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  976. // -------------------------------------------------------------------
  977. // Patchbay
  978. /*!
  979. * Connect two patchbay ports.
  980. */
  981. virtual bool patchbayConnect(bool external,
  982. uint groupA, uint portA,
  983. uint groupB, uint portB);
  984. /*!
  985. * Remove a patchbay connection.
  986. */
  987. virtual bool patchbayDisconnect(bool external, uint connectionId);
  988. /*!
  989. * Set the position of a group.
  990. */
  991. virtual bool patchbaySetGroupPos(bool sendHost, bool sendOSC, bool external,
  992. uint groupId, int x1, int y1, int x2, int y2);
  993. /*!
  994. * Force the engine to resend all patchbay clients, ports and connections again.
  995. */
  996. virtual bool patchbayRefresh(bool sendHost, bool sendOSC, bool external);
  997. #endif
  998. // -------------------------------------------------------------------
  999. // Transport
  1000. /*!
  1001. * Start playback of the engine transport.
  1002. */
  1003. virtual void transportPlay() noexcept;
  1004. /*!
  1005. * Pause the engine transport.
  1006. */
  1007. virtual void transportPause() noexcept;
  1008. /*!
  1009. * Set the engine transport bpm to @a bpm.
  1010. */
  1011. virtual void transportBPM(double bpm) noexcept;
  1012. /*!
  1013. * Relocate the engine transport to @a frames.
  1014. */
  1015. virtual void transportRelocate(uint64_t frame) noexcept;
  1016. // -------------------------------------------------------------------
  1017. // Error handling
  1018. /*!
  1019. * Get last error.
  1020. */
  1021. const char* getLastError() const noexcept;
  1022. /*!
  1023. * Set last error.
  1024. */
  1025. void setLastError(const char* error) const noexcept;
  1026. // -------------------------------------------------------------------
  1027. // Misc
  1028. /*!
  1029. * Check if the engine is about to close.
  1030. */
  1031. bool isAboutToClose() const noexcept;
  1032. /*!
  1033. * Tell the engine it's about to close.
  1034. * This is used to prevent the engine thread(s) from reactivating.
  1035. * Returns true if there's no pending engine events.
  1036. */
  1037. bool setAboutToClose() noexcept;
  1038. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1039. /*!
  1040. * TODO.
  1041. */
  1042. bool isLoadingProject() const noexcept;
  1043. #endif
  1044. /*!
  1045. * Tell the engine to stop the current cancelable action.
  1046. * @see ENGINE_CALLBACK_CANCELABLE_ACTION
  1047. */
  1048. void setActionCanceled(bool canceled) noexcept;
  1049. /*!
  1050. * Check wherever the last cancelable action was indeed canceled or not.
  1051. */
  1052. bool wasActionCanceled() const noexcept;
  1053. // -------------------------------------------------------------------
  1054. // Options
  1055. /*!
  1056. * Set the engine option @a option to @a value or @a valueStr.
  1057. */
  1058. virtual void setOption(EngineOption option, int value, const char* valueStr) noexcept;
  1059. // -------------------------------------------------------------------
  1060. // OSC Stuff
  1061. #ifndef BUILD_BRIDGE
  1062. /*!
  1063. * Check if OSC controller is registered.
  1064. */
  1065. bool isOscControlRegistered() const noexcept;
  1066. /*!
  1067. * Get OSC TCP server path.
  1068. */
  1069. const char* getOscServerPathTCP() const noexcept;
  1070. /*!
  1071. * Get OSC UDP server path.
  1072. */
  1073. const char* getOscServerPathUDP() const noexcept;
  1074. #endif
  1075. // -------------------------------------------------------------------
  1076. protected:
  1077. /*!
  1078. * Internal data, for CarlaEngine subclasses and friends.
  1079. */
  1080. struct ProtectedData;
  1081. ProtectedData* const pData;
  1082. /*!
  1083. * Some internal classes read directly from pData or call protected functions.
  1084. */
  1085. friend class CarlaEngineEventPort;
  1086. friend class CarlaEngineOsc;
  1087. friend class CarlaEngineRunner;
  1088. friend class CarlaPluginInstance;
  1089. friend class EngineInternalGraph;
  1090. friend class PendingRtEventsRunner;
  1091. friend class ScopedActionLock;
  1092. friend class ScopedEngineEnvironmentLocker;
  1093. friend class ScopedRunnerStopper;
  1094. friend class PatchbayGraph;
  1095. friend struct ExternalGraph;
  1096. friend struct RackGraph;
  1097. // -------------------------------------------------------------------
  1098. // Internal stuff
  1099. /*!
  1100. * Report to all plugins about buffer size change.
  1101. */
  1102. void bufferSizeChanged(uint32_t newBufferSize);
  1103. /*!
  1104. * Report to all plugins about sample rate change.
  1105. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  1106. */
  1107. void sampleRateChanged(double newSampleRate);
  1108. /*!
  1109. * Report to all plugins about offline mode change.
  1110. */
  1111. void offlineModeChanged(bool isOffline);
  1112. /*!
  1113. * Set a plugin (stereo) peak values.
  1114. * @note RT call
  1115. */
  1116. void setPluginPeaksRT(uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  1117. public:
  1118. /*!
  1119. * Common save project function for main engine and plugin.
  1120. */
  1121. void saveProjectInternal(water::MemoryOutputStream& outStrm) const;
  1122. /*!
  1123. * Common load project function for main engine and plugin.
  1124. */
  1125. bool loadProjectInternal(water::XmlDocument& xmlDoc, bool alwaysLoadConnections);
  1126. protected:
  1127. // -------------------------------------------------------------------
  1128. // Helper functions
  1129. /*!
  1130. * Return internal data, needed for EventPorts when used in Rack, Patchbay and Bridge modes.
  1131. * @note RT call
  1132. */
  1133. EngineEvent* getInternalEventBuffer(bool isInput) const noexcept;
  1134. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1135. // -------------------------------------------------------------------
  1136. // Patchbay stuff
  1137. /*!
  1138. * Virtual functions for handling patchbay state.
  1139. * Do not free returned data.
  1140. */
  1141. struct PatchbayPosition { const char* name; int x1, y1, x2, y2, pluginId; bool dealloc; };
  1142. virtual const char* const* getPatchbayConnections(bool external) const;
  1143. virtual const PatchbayPosition* getPatchbayPositions(bool external, uint& count) const;
  1144. virtual void restorePatchbayConnection(bool external, const char* sourcePort, const char* targetPort);
  1145. // returns true if plugin name mapping found, ppos.name updated to its converted name
  1146. virtual bool restorePatchbayGroupPosition(bool external, PatchbayPosition& ppos);
  1147. /*!
  1148. * Virtual functions for handling external graph ports.
  1149. */
  1150. virtual bool connectExternalGraphPort(uint, uint, const char*);
  1151. virtual bool disconnectExternalGraphPort(uint, uint, const char*);
  1152. #endif
  1153. // -------------------------------------------------------------------
  1154. CARLA_DECLARE_NON_COPYABLE(CarlaEngine)
  1155. };
  1156. /**@}*/
  1157. // -----------------------------------------------------------------------
  1158. CARLA_BACKEND_END_NAMESPACE
  1159. #endif // CARLA_ENGINE_HPP_INCLUDED