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.

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