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.

1396 lines
35KB

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