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.

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