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.

1431 lines
36KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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. bool isInput() const noexcept
  323. {
  324. return kIsInput;
  325. }
  326. /*!
  327. * Get the index offset as passed in the constructor.
  328. */
  329. uint32_t getIndexOffset() const noexcept
  330. {
  331. return kIndexOffset;
  332. }
  333. /*!
  334. * Get this ports' engine client.
  335. */
  336. 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. 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. 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. 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. float* getBuffer() const noexcept
  423. {
  424. return fBuffer;
  425. }
  426. /*!
  427. * Get min/max range for this CV port.
  428. */
  429. 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. 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. bool addCVSource(CarlaEngineCVPort* port, uint32_t portIndexOffset);
  537. /*!
  538. * Remove a CV port as a source of events.
  539. */
  540. bool removeCVSource(uint32_t portIndexOffset);
  541. /*!
  542. * Set value range for a CV port.
  543. */
  544. bool setCVSourceRange(uint32_t portIndexOffset, float minimum, float maximum);
  545. /*!
  546. * Get events and add them to an event port.
  547. * FIXME needs a better name...
  548. */
  549. void initPortBuffers(const float* const* buffers, uint32_t frames,
  550. bool sampleAccurate, CarlaEngineEventPort* eventPort);
  551. #ifndef DOXYGEN
  552. protected:
  553. /** @internal */
  554. struct ProtectedData;
  555. ProtectedData* const pData;
  556. /*!
  557. * The constructor, protected.
  558. */
  559. CarlaEngineCVSourcePorts();
  560. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineCVSourcePorts)
  561. #endif
  562. };
  563. // -----------------------------------------------------------------------
  564. /*!
  565. * Carla Engine Client.
  566. * Each plugin requires one client from the engine (created via CarlaEngine::addClient()).
  567. * @note This is a virtual class, some engine types provide custom functionality.
  568. */
  569. class CARLA_API CarlaEngineClient
  570. {
  571. public:
  572. /*!
  573. * The destructor.
  574. */
  575. virtual ~CarlaEngineClient() noexcept;
  576. /*!
  577. * Activate this client.
  578. * Client must be deactivated before calling this function.
  579. */
  580. virtual void activate() noexcept;
  581. /*!
  582. * Deactivate this client.
  583. * Client must be activated before calling this function.
  584. */
  585. virtual void deactivate() noexcept;
  586. /*!
  587. * Check if the client is activated.
  588. */
  589. virtual bool isActive() const noexcept;
  590. /*!
  591. * Check if the client is ok.
  592. * Plugins will refuse to instantiate if this returns false.
  593. * @note This is always true in rack and patchbay processing modes.
  594. */
  595. virtual bool isOk() const noexcept;
  596. /*!
  597. * Get the current latency, in samples.
  598. */
  599. virtual uint32_t getLatency() const noexcept;
  600. /*!
  601. * Change the client's latency.
  602. */
  603. virtual void setLatency(uint32_t samples) noexcept;
  604. /*!
  605. * Add a new port of type @a portType.
  606. * @note This function does nothing in rack processing mode since ports are static there.
  607. */
  608. virtual CarlaEnginePort* addPort(EnginePortType portType, const char* name, bool isInput, uint32_t indexOffset);
  609. /*!
  610. * Remove a previously added port via addPort().
  611. */
  612. virtual bool removePort(EnginePortType portType, const char* name, bool isInput);
  613. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  614. /*!
  615. * Create an instance of CV source ports.
  616. * Must be called only once per client.
  617. */
  618. virtual CarlaEngineCVSourcePorts* createCVSourcePorts();
  619. #endif
  620. /*!
  621. * Get this client's engine.
  622. */
  623. const CarlaEngine& getEngine() const noexcept;
  624. /*!
  625. * Get the engine's process mode.
  626. */
  627. EngineProcessMode getProcessMode() const noexcept;
  628. /*!
  629. * Get port count for a type and mode.
  630. */
  631. uint getPortCount(EnginePortType portType, bool isInput) const noexcept;
  632. /*!
  633. * Get an audio port name.
  634. */
  635. const char* getAudioPortName(bool isInput, uint index) const noexcept;
  636. /*!
  637. * Get a CV port name.
  638. */
  639. const char* getCVPortName(bool isInput, uint index) const noexcept;
  640. /*!
  641. * Get an event port name.
  642. */
  643. const char* getEventPortName(bool isInput, uint index) const noexcept;
  644. #ifndef DOXYGEN
  645. protected:
  646. /** @internal */
  647. struct ProtectedData;
  648. ProtectedData* const pData;
  649. /*!
  650. * The constructor, protected.
  651. */
  652. CarlaEngineClient(ProtectedData* pData);
  653. /** internal */
  654. void _addAudioPortName(bool, const char*);
  655. void _addCVPortName(bool, const char*);
  656. void _addEventPortName(bool, const char*);
  657. const char* _getUniquePortName(const char*);
  658. void _clearPorts();
  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. 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. 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. * Force the engine to resend all patchbay clients, ports and connections again.
  971. */
  972. virtual bool patchbayRefresh(bool sendHost, bool sendOsc, bool external);
  973. #endif
  974. // -------------------------------------------------------------------
  975. // Transport
  976. /*!
  977. * Start playback of the engine transport.
  978. */
  979. virtual void transportPlay() noexcept;
  980. /*!
  981. * Pause the engine transport.
  982. */
  983. virtual void transportPause() noexcept;
  984. /*!
  985. * Set the engine transport bpm to @a bpm.
  986. */
  987. virtual void transportBPM(double bpm) noexcept;
  988. /*!
  989. * Relocate the engine transport to @a frames.
  990. */
  991. virtual void transportRelocate(uint64_t frame) noexcept;
  992. // -------------------------------------------------------------------
  993. // Error handling
  994. /*!
  995. * Get last error.
  996. */
  997. const char* getLastError() const noexcept;
  998. /*!
  999. * Set last error.
  1000. */
  1001. void setLastError(const char* error) const noexcept;
  1002. // -------------------------------------------------------------------
  1003. // Misc
  1004. /*!
  1005. * Check if the engine is about to close.
  1006. */
  1007. bool isAboutToClose() const noexcept;
  1008. /*!
  1009. * Tell the engine it's about to close.
  1010. * This is used to prevent the engine thread(s) from reactivating.
  1011. * Returns true if there's no pending engine events.
  1012. */
  1013. bool setAboutToClose() noexcept;
  1014. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1015. /*!
  1016. * TODO.
  1017. */
  1018. bool isLoadingProject() const noexcept;
  1019. #endif
  1020. /*!
  1021. * Tell the engine to stop the current cancelable action.
  1022. * @see ENGINE_CALLBACK_CANCELABLE_ACTION
  1023. */
  1024. void setActionCanceled(bool canceled) noexcept;
  1025. /*!
  1026. * Check wherever the last cancelable action was indeed canceled or not.
  1027. */
  1028. bool wasActionCanceled() const noexcept;
  1029. // -------------------------------------------------------------------
  1030. // Options
  1031. /*!
  1032. * Set the engine option @a option to @a value or @a valueStr.
  1033. */
  1034. virtual void setOption(EngineOption option, int value, const char* valueStr) noexcept;
  1035. // -------------------------------------------------------------------
  1036. // OSC Stuff
  1037. #ifndef BUILD_BRIDGE
  1038. /*!
  1039. * Check if OSC controller is registered.
  1040. */
  1041. bool isOscControlRegistered() const noexcept;
  1042. /*!
  1043. * Get OSC TCP server path.
  1044. */
  1045. const char* getOscServerPathTCP() const noexcept;
  1046. /*!
  1047. * Get OSC UDP server path.
  1048. */
  1049. const char* getOscServerPathUDP() const noexcept;
  1050. #endif
  1051. // -------------------------------------------------------------------
  1052. // Helper functions
  1053. /*!
  1054. * Return internal data, needed for EventPorts when used in Rack, Patchbay and Bridge modes.
  1055. * @note RT call
  1056. */
  1057. EngineEvent* getInternalEventBuffer(bool isInput) const noexcept;
  1058. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1059. /*!
  1060. * Virtual functions for handling external graph ports.
  1061. */
  1062. virtual bool connectExternalGraphPort(uint, uint, const char*);
  1063. virtual bool disconnectExternalGraphPort(uint, uint, const char*);
  1064. #endif
  1065. // -------------------------------------------------------------------
  1066. protected:
  1067. /*!
  1068. * Internal data, for CarlaEngine subclasses and friends.
  1069. */
  1070. struct ProtectedData;
  1071. ProtectedData* const pData;
  1072. /*!
  1073. * Some internal classes read directly from pData or call protected functions.
  1074. */
  1075. friend class CarlaEngineThread;
  1076. friend class CarlaEngineOsc;
  1077. friend class CarlaPluginInstance;
  1078. friend class EngineInternalGraph;
  1079. friend class PendingRtEventsRunner;
  1080. friend class ScopedActionLock;
  1081. friend class ScopedEngineEnvironmentLocker;
  1082. friend class ScopedThreadStopper;
  1083. friend class PatchbayGraph;
  1084. friend struct RackGraph;
  1085. // -------------------------------------------------------------------
  1086. // Internal stuff
  1087. /*!
  1088. * Report to all plugins about buffer size change.
  1089. */
  1090. void bufferSizeChanged(uint32_t newBufferSize);
  1091. /*!
  1092. * Report to all plugins about sample rate change.
  1093. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  1094. */
  1095. void sampleRateChanged(double newSampleRate);
  1096. /*!
  1097. * Report to all plugins about offline mode change.
  1098. */
  1099. void offlineModeChanged(bool isOffline);
  1100. /*!
  1101. * Set a plugin (stereo) peak values.
  1102. * @note RT call
  1103. */
  1104. void setPluginPeaksRT(uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  1105. /*!
  1106. * Common save project function for main engine and plugin.
  1107. */
  1108. void saveProjectInternal(water::MemoryOutputStream& outStrm) const;
  1109. /*!
  1110. * Common load project function for main engine and plugin.
  1111. */
  1112. bool loadProjectInternal(water::XmlDocument& xmlDoc);
  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. virtual const char* const* getPatchbayConnections(bool external) const;
  1121. virtual void restorePatchbayConnection(bool external,
  1122. const char* sourcePort,
  1123. const char* targetPort);
  1124. #endif
  1125. // -------------------------------------------------------------------
  1126. public:
  1127. /*!
  1128. * Native audio APIs.
  1129. */
  1130. enum AudioApi {
  1131. AUDIO_API_NULL,
  1132. // common
  1133. AUDIO_API_JACK,
  1134. AUDIO_API_OSS,
  1135. // linux
  1136. AUDIO_API_ALSA,
  1137. AUDIO_API_PULSEAUDIO,
  1138. // macos
  1139. AUDIO_API_COREAUDIO,
  1140. // windows
  1141. AUDIO_API_ASIO,
  1142. AUDIO_API_DIRECTSOUND,
  1143. AUDIO_API_WASAPI
  1144. };
  1145. // -------------------------------------------------------------------
  1146. // Engine initializers
  1147. // JACK
  1148. static CarlaEngine* newJack();
  1149. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1150. // Dummy
  1151. static CarlaEngine* newDummy();
  1152. #endif
  1153. #ifdef BUILD_BRIDGE
  1154. // Bridge
  1155. static CarlaEngine* newBridge(const char* audioPoolBaseName,
  1156. const char* rtClientBaseName,
  1157. const char* nonRtClientBaseName,
  1158. const char* nonRtServerBaseName);
  1159. #else
  1160. # ifdef USING_JUCE
  1161. // Juce
  1162. static CarlaEngine* newJuce(AudioApi api);
  1163. static uint getJuceApiCount();
  1164. static const char* getJuceApiName(uint index);
  1165. static const char* const* getJuceApiDeviceNames(uint index);
  1166. static const EngineDriverDeviceInfo* getJuceDeviceInfo(uint index, const char* deviceName);
  1167. static bool showJuceDeviceControlPanel(uint index, const char* deviceName);
  1168. # else
  1169. // RtAudio
  1170. static CarlaEngine* newRtAudio(AudioApi api);
  1171. static uint getRtAudioApiCount();
  1172. static const char* getRtAudioApiName(uint index);
  1173. static const char* const* getRtAudioApiDeviceNames(uint index);
  1174. static const EngineDriverDeviceInfo* getRtAudioDeviceInfo(uint index, const char* deviceName);
  1175. # endif
  1176. #endif
  1177. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngine)
  1178. };
  1179. /**@}*/
  1180. // -----------------------------------------------------------------------
  1181. CARLA_BACKEND_END_NAMESPACE
  1182. #endif // CARLA_ENGINE_HPP_INCLUDED