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.

1429 lines
36KB

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