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.

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