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.

1430 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, protected.
  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. /*!
  522. * Carla Engine Meta CV Port.
  523. * FIXME needs a better name...
  524. */
  525. class CARLA_API CarlaEngineCVSourcePorts
  526. {
  527. public:
  528. /*!
  529. * The destructor.
  530. */
  531. virtual ~CarlaEngineCVSourcePorts();
  532. /*!
  533. * Add a CV port as a source of events.
  534. */
  535. bool addCVSource(CarlaEngineCVPort* port, uint32_t portIndexOffset);
  536. /*!
  537. * Remove a CV port as a source of events.
  538. */
  539. bool removeCVSource(uint32_t portIndexOffset);
  540. /*!
  541. * Set value range for a CV port.
  542. */
  543. bool setCVSourceRange(uint32_t portIndexOffset, float minimum, float maximum);
  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. /** @internal */
  553. struct ProtectedData;
  554. ProtectedData* const pData;
  555. /*!
  556. * The constructor, protected.
  557. */
  558. CarlaEngineCVSourcePorts();
  559. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineCVSourcePorts)
  560. #endif
  561. };
  562. // -----------------------------------------------------------------------
  563. /*!
  564. * Carla Engine Client.
  565. * Each plugin requires one client from the engine (created via CarlaEngine::addClient()).
  566. * @note This is a virtual class, some engine types provide custom functionality.
  567. */
  568. class CARLA_API CarlaEngineClient
  569. {
  570. public:
  571. /*!
  572. * The destructor.
  573. */
  574. virtual ~CarlaEngineClient() noexcept;
  575. /*!
  576. * Activate this client.
  577. * Client must be deactivated before calling this function.
  578. */
  579. virtual void activate() noexcept;
  580. /*!
  581. * Deactivate this client.
  582. * Client must be activated before calling this function.
  583. */
  584. virtual void deactivate() noexcept;
  585. /*!
  586. * Check if the client is activated.
  587. */
  588. virtual bool isActive() const noexcept;
  589. /*!
  590. * Check if the client is ok.
  591. * Plugins will refuse to instantiate if this returns false.
  592. * @note This is always true in rack and patchbay processing modes.
  593. */
  594. virtual bool isOk() const noexcept;
  595. /*!
  596. * Get the current latency, in samples.
  597. */
  598. virtual uint32_t getLatency() const noexcept;
  599. /*!
  600. * Change the client's latency.
  601. */
  602. virtual void setLatency(uint32_t samples) noexcept;
  603. /*!
  604. * Add a new port of type @a portType.
  605. * @note This function does nothing in rack processing mode since ports are static there.
  606. */
  607. virtual CarlaEnginePort* addPort(EnginePortType portType, const char* name, bool isInput, uint32_t indexOffset);
  608. /*!
  609. * Remove a previously added port via addPort().
  610. */
  611. virtual bool removePort(EnginePortType portType, const char* name, bool isInput);
  612. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  613. /*!
  614. * Create an instance of CV source ports.
  615. * Must be called only once per client.
  616. */
  617. virtual CarlaEngineCVSourcePorts* createCVSourcePorts();
  618. #endif
  619. /*!
  620. * Get this client's engine.
  621. */
  622. const CarlaEngine& getEngine() const noexcept;
  623. /*!
  624. * Get the engine's process mode.
  625. */
  626. EngineProcessMode getProcessMode() const noexcept;
  627. /*!
  628. * Get port count for a type and mode.
  629. */
  630. uint getPortCount(EnginePortType portType, bool isInput) const noexcept;
  631. /*!
  632. * Get an audio port name.
  633. */
  634. const char* getAudioPortName(bool isInput, uint index) const noexcept;
  635. /*!
  636. * Get a CV port name.
  637. */
  638. const char* getCVPortName(bool isInput, uint index) const noexcept;
  639. /*!
  640. * Get an event port name.
  641. */
  642. const char* getEventPortName(bool isInput, uint index) const noexcept;
  643. #ifndef DOXYGEN
  644. protected:
  645. /** @internal */
  646. struct ProtectedData;
  647. ProtectedData* const pData;
  648. /*!
  649. * The constructor, protected.
  650. */
  651. CarlaEngineClient(ProtectedData* pData);
  652. /** internal */
  653. void _addAudioPortName(bool, const char*);
  654. void _addCVPortName(bool, const char*);
  655. void _addEventPortName(bool, const char*);
  656. const char* _getUniquePortName(const char*);
  657. void _clearPorts();
  658. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineClient)
  659. #endif
  660. };
  661. // -----------------------------------------------------------------------
  662. /*!
  663. * Carla Engine.
  664. * @note This is a virtual class for all available engine types available in Carla.
  665. */
  666. class CARLA_API CarlaEngine
  667. {
  668. protected:
  669. /*!
  670. * The constructor, protected.
  671. * @note This only initializes engine data, it doesn't actually start the engine.
  672. */
  673. CarlaEngine();
  674. public:
  675. /*!
  676. * The destructor.
  677. * The engine must have been closed before this happens.
  678. */
  679. virtual ~CarlaEngine();
  680. // -------------------------------------------------------------------
  681. // Static calls
  682. /*!
  683. * Get the number of available engine drivers.
  684. */
  685. static uint getDriverCount();
  686. /*!
  687. * Get the name of the engine driver at @a index.
  688. */
  689. static const char* getDriverName(uint index);
  690. /*!
  691. * Get the device names of the driver at @a index.
  692. */
  693. static const char* const* getDriverDeviceNames(uint index);
  694. /*!
  695. * Get device information about the driver at @a index and name @a driverName.
  696. */
  697. static const EngineDriverDeviceInfo* getDriverDeviceInfo(uint index, const char* driverName);
  698. /*!
  699. * Show a device custom control panel.
  700. * @see ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL
  701. */
  702. static bool showDriverDeviceControlPanel(uint index, const char* deviceName);
  703. /*!
  704. * Create a new engine, using driver @a driverName.
  705. * Returned value must be deleted when no longer needed.
  706. * @note This only initializes engine data, it doesn't actually start the engine.
  707. */
  708. static CarlaEngine* newDriverByName(const char* driverName);
  709. // -------------------------------------------------------------------
  710. // Constant values
  711. /*!
  712. * Maximum client name size.
  713. */
  714. virtual uint getMaxClientNameSize() const noexcept;
  715. /*!
  716. * Maximum port name size.
  717. */
  718. virtual uint getMaxPortNameSize() const noexcept;
  719. /*!
  720. * Current number of plugins loaded.
  721. */
  722. uint getCurrentPluginCount() const noexcept;
  723. /*!
  724. * Maximum number of loadable plugins allowed.
  725. * This function returns 0 if engine is not started.
  726. */
  727. uint getMaxPluginNumber() const noexcept;
  728. // -------------------------------------------------------------------
  729. // Virtual, per-engine type calls
  730. /*!
  731. * Initialize/start the engine, using @a clientName.
  732. * When the engine is initialized, you need to call idle() at regular intervals.
  733. */
  734. virtual bool init(const char* clientName) = 0;
  735. /*!
  736. * Close engine.
  737. * This function always closes the engine even if it returns false.
  738. * In other words, even when something goes wrong when closing the engine it still be closed nonetheless.
  739. */
  740. virtual bool close();
  741. /*!
  742. * Idle engine.
  743. */
  744. virtual void idle() noexcept;
  745. /*!
  746. * Check if engine is running.
  747. */
  748. virtual bool isRunning() const noexcept = 0;
  749. /*!
  750. * Check if engine is running offline (aka freewheel mode).
  751. */
  752. virtual bool isOffline() const noexcept = 0;
  753. /*!
  754. * Check if engine runs on a constant buffer size value.
  755. * Default implementation returns true.
  756. */
  757. virtual bool usesConstantBufferSize() const noexcept;
  758. /*!
  759. * Get engine type.
  760. */
  761. virtual EngineType getType() const noexcept = 0;
  762. /*!
  763. * Get the currently used driver name.
  764. */
  765. virtual const char* getCurrentDriverName() const noexcept = 0;
  766. /*!
  767. * Add new engine client.
  768. * @note This function must only be called within a plugin class.
  769. */
  770. virtual CarlaEngineClient* addClient(CarlaPlugin* plugin);
  771. /*!
  772. * Get the current CPU load estimated by the engine.
  773. */
  774. virtual float getDSPLoad() const noexcept;
  775. /*!
  776. * Get the total number of xruns so far.
  777. */
  778. virtual uint32_t getTotalXruns() const noexcept;
  779. /*!
  780. * Clear the xrun count.
  781. */
  782. virtual void clearXruns() const noexcept;
  783. /*!
  784. * Dynamically change buffer size and/or sample rate while engine is running.
  785. * @see ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE
  786. * @see ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE
  787. */
  788. virtual bool setBufferSizeAndSampleRate(uint bufferSize, double sampleRate);
  789. /*!
  790. * Show the custom control panel for the current engine device.
  791. * @see ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL
  792. */
  793. virtual bool showDeviceControlPanel() const noexcept;
  794. // -------------------------------------------------------------------
  795. // Plugin management
  796. /*!
  797. * Add new plugin.
  798. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  799. */
  800. bool addPlugin(BinaryType btype, PluginType ptype,
  801. const char* filename, const char* name, const char* label, int64_t uniqueId,
  802. const void* extra, uint options = PLUGIN_OPTIONS_NULL);
  803. /*!
  804. * Add new plugin, using native binary type.
  805. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  806. */
  807. bool addPlugin(PluginType ptype,
  808. const char* filename, const char* name, const char* label, int64_t uniqueId,
  809. const void* extra);
  810. /*!
  811. * Remove plugin with id @a id.
  812. * @see ENGINE_CALLBACK_PLUGIN_REMOVED
  813. */
  814. bool removePlugin(uint id);
  815. /*!
  816. * Remove all plugins.
  817. */
  818. bool removeAllPlugins();
  819. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  820. /*!
  821. * Rename plugin with id @a id to @a newName.
  822. * Returns the new name, or null if the operation failed.
  823. * Returned variable must be deleted if non-null.
  824. * @see ENGINE_CALLBACK_PLUGIN_RENAMED
  825. */
  826. virtual bool renamePlugin(uint id, const char* newName);
  827. /*!
  828. * Clone plugin with id @a id.
  829. */
  830. bool clonePlugin(uint id);
  831. /*!
  832. * Prepare replace of plugin with id @a id.
  833. * The next call to addPlugin() will use this id, replacing the selected plugin.
  834. * @note This function requires addPlugin() to be called afterwards, as soon as possible.
  835. */
  836. bool replacePlugin(uint id) noexcept;
  837. /*!
  838. * Switch plugins with id @a idA and @a idB.
  839. */
  840. bool switchPlugins(uint idA, uint idB) noexcept;
  841. #endif
  842. /*!
  843. * Set a plugin's parameter in drag/touch mode.
  844. * Usually happens from a UI when the user is moving a parameter with a mouse or similar input.
  845. *
  846. * @param parameterId The parameter to update
  847. * @param touch The new state for the parameter
  848. */
  849. virtual void touchPluginParameter(uint id, uint32_t parameterId, bool touch) noexcept;
  850. /*!
  851. * Get plugin with id @a id.
  852. */
  853. CarlaPlugin* getPlugin(uint id) const noexcept;
  854. /*!
  855. * Get plugin with id @a id, faster unchecked version.
  856. */
  857. CarlaPlugin* getPluginUnchecked(uint id) const noexcept;
  858. /*!
  859. * Get a unique plugin name within the engine.
  860. * Returned variable must be deleted if non-null.
  861. */
  862. const char* getUniquePluginName(const char* name) const;
  863. // -------------------------------------------------------------------
  864. // Project management
  865. /*!
  866. * Load a file of any type.
  867. * This will try to load a generic file as a plugin,
  868. * either by direct handling (SF2 and SFZ) or by using an internal plugin (like Audio and MIDI).
  869. */
  870. bool loadFile(const char* filename);
  871. /*!
  872. * Load a project file.
  873. * @note Already loaded plugins are not removed; call removeAllPlugins() first if needed.
  874. */
  875. bool loadProject(const char* filename, bool setAsCurrentProject);
  876. /*!
  877. * Save current project to a file.
  878. */
  879. bool saveProject(const char* filename, bool setAsCurrentProject);
  880. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  881. /*!
  882. * Get the currently set project filename.
  883. */
  884. const char* getCurrentProjectFilename() const noexcept;
  885. /*!
  886. * Clear the currently set project filename.
  887. */
  888. void clearCurrentProjectFilename() noexcept;
  889. #endif
  890. // -------------------------------------------------------------------
  891. // Information (base)
  892. /*!
  893. * Get the current buffer size.
  894. */
  895. uint32_t getBufferSize() const noexcept;
  896. /*!
  897. * Get the current sample rate.
  898. */
  899. double getSampleRate() const noexcept;
  900. /*!
  901. * Get the current engine name.
  902. */
  903. const char* getName() const noexcept;
  904. /*!
  905. * Get the current engine process mode.
  906. */
  907. EngineProcessMode getProccessMode() const noexcept;
  908. /*!
  909. * Get the current engine options (read-only).
  910. */
  911. const EngineOptions& getOptions() const noexcept;
  912. /*!
  913. * Get the current Time information (read-only).
  914. */
  915. virtual EngineTimeInfo getTimeInfo() const noexcept;
  916. // -------------------------------------------------------------------
  917. // Information (peaks)
  918. /*!
  919. * Get a plugin's peak values.
  920. * @note not thread-safe if pluginId == MAIN_CARLA_PLUGIN_ID
  921. */
  922. const float* getPeaks(uint pluginId) const noexcept;
  923. /*!
  924. * Get a plugin's input peak value.
  925. */
  926. float getInputPeak(uint pluginId, bool isLeft) const noexcept;
  927. /*!
  928. * Get a plugin's output peak value.
  929. */
  930. float getOutputPeak(uint pluginId, bool isLeft) const noexcept;
  931. // -------------------------------------------------------------------
  932. // Callback
  933. /*!
  934. * Call the main engine callback, if set.
  935. * May be called by plugins.
  936. */
  937. virtual void callback(bool sendHost, bool sendOsc,
  938. EngineCallbackOpcode action, uint pluginId,
  939. int value1, int value2, int value3, float valuef, const char* valueStr) noexcept;
  940. /*!
  941. * Set the main engine callback to @a func.
  942. */
  943. void setCallback(EngineCallbackFunc func, void* ptr) noexcept;
  944. // -------------------------------------------------------------------
  945. // Callback
  946. /*!
  947. * Call the file callback, if set.
  948. * May be called by plugins.
  949. */
  950. const char* runFileCallback(FileCallbackOpcode action, bool isDir, const char* title, const char* filter) noexcept;
  951. /*!
  952. * Set the file callback to @a func.
  953. */
  954. void setFileCallback(FileCallbackFunc func, void* ptr) noexcept;
  955. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  956. // -------------------------------------------------------------------
  957. // Patchbay
  958. /*!
  959. * Connect two patchbay ports.
  960. */
  961. virtual bool patchbayConnect(bool external,
  962. uint groupA, uint portA,
  963. uint groupB, uint portB);
  964. /*!
  965. * Remove a patchbay connection.
  966. */
  967. virtual bool patchbayDisconnect(bool external, uint connectionId);
  968. /*!
  969. * Force the engine to resend all patchbay clients, ports and connections again.
  970. */
  971. virtual bool patchbayRefresh(bool sendHost, bool sendOsc, bool external);
  972. #endif
  973. // -------------------------------------------------------------------
  974. // Transport
  975. /*!
  976. * Start playback of the engine transport.
  977. */
  978. virtual void transportPlay() noexcept;
  979. /*!
  980. * Pause the engine transport.
  981. */
  982. virtual void transportPause() noexcept;
  983. /*!
  984. * Set the engine transport bpm to @a bpm.
  985. */
  986. virtual void transportBPM(double bpm) noexcept;
  987. /*!
  988. * Relocate the engine transport to @a frames.
  989. */
  990. virtual void transportRelocate(uint64_t frame) noexcept;
  991. // -------------------------------------------------------------------
  992. // Error handling
  993. /*!
  994. * Get last error.
  995. */
  996. const char* getLastError() const noexcept;
  997. /*!
  998. * Set last error.
  999. */
  1000. void setLastError(const char* error) const noexcept;
  1001. // -------------------------------------------------------------------
  1002. // Misc
  1003. /*!
  1004. * Check if the engine is about to close.
  1005. */
  1006. bool isAboutToClose() const noexcept;
  1007. /*!
  1008. * Tell the engine it's about to close.
  1009. * This is used to prevent the engine thread(s) from reactivating.
  1010. * Returns true if there's no pending engine events.
  1011. */
  1012. bool setAboutToClose() noexcept;
  1013. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1014. /*!
  1015. * TODO.
  1016. */
  1017. bool isLoadingProject() const noexcept;
  1018. #endif
  1019. /*!
  1020. * Tell the engine to stop the current cancelable action.
  1021. * @see ENGINE_CALLBACK_CANCELABLE_ACTION
  1022. */
  1023. void setActionCanceled(bool canceled) noexcept;
  1024. /*!
  1025. * Check wherever the last cancelable action was indeed canceled or not.
  1026. */
  1027. bool wasActionCanceled() const noexcept;
  1028. // -------------------------------------------------------------------
  1029. // Options
  1030. /*!
  1031. * Set the engine option @a option to @a value or @a valueStr.
  1032. */
  1033. virtual void setOption(EngineOption option, int value, const char* valueStr) noexcept;
  1034. // -------------------------------------------------------------------
  1035. // OSC Stuff
  1036. #ifndef BUILD_BRIDGE
  1037. /*!
  1038. * Check if OSC controller is registered.
  1039. */
  1040. bool isOscControlRegistered() const noexcept;
  1041. /*!
  1042. * Get OSC TCP server path.
  1043. */
  1044. const char* getOscServerPathTCP() const noexcept;
  1045. /*!
  1046. * Get OSC UDP server path.
  1047. */
  1048. const char* getOscServerPathUDP() const noexcept;
  1049. #endif
  1050. // -------------------------------------------------------------------
  1051. // Helper functions
  1052. /*!
  1053. * Return internal data, needed for EventPorts when used in Rack, Patchbay and Bridge modes.
  1054. * @note RT call
  1055. */
  1056. EngineEvent* getInternalEventBuffer(bool isInput) const noexcept;
  1057. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1058. /*!
  1059. * Virtual functions for handling external graph ports.
  1060. */
  1061. virtual bool connectExternalGraphPort(uint, uint, const char*);
  1062. virtual bool disconnectExternalGraphPort(uint, uint, const char*);
  1063. #endif
  1064. // -------------------------------------------------------------------
  1065. protected:
  1066. /*!
  1067. * Internal data, for CarlaEngine subclasses and friends.
  1068. */
  1069. struct ProtectedData;
  1070. ProtectedData* const pData;
  1071. /*!
  1072. * Some internal classes read directly from pData or call protected functions.
  1073. */
  1074. friend class CarlaEngineThread;
  1075. friend class CarlaEngineOsc;
  1076. friend class CarlaPluginInstance;
  1077. friend class EngineInternalGraph;
  1078. friend class PendingRtEventsRunner;
  1079. friend class ScopedActionLock;
  1080. friend class ScopedEngineEnvironmentLocker;
  1081. friend class ScopedThreadStopper;
  1082. friend class PatchbayGraph;
  1083. friend struct RackGraph;
  1084. // -------------------------------------------------------------------
  1085. // Internal stuff
  1086. /*!
  1087. * Report to all plugins about buffer size change.
  1088. */
  1089. void bufferSizeChanged(uint32_t newBufferSize);
  1090. /*!
  1091. * Report to all plugins about sample rate change.
  1092. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  1093. */
  1094. void sampleRateChanged(double newSampleRate);
  1095. /*!
  1096. * Report to all plugins about offline mode change.
  1097. */
  1098. void offlineModeChanged(bool isOffline);
  1099. /*!
  1100. * Set a plugin (stereo) peak values.
  1101. * @note RT call
  1102. */
  1103. void setPluginPeaksRT(uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  1104. /*!
  1105. * Common save project function for main engine and plugin.
  1106. */
  1107. void saveProjectInternal(water::MemoryOutputStream& outStrm) const;
  1108. /*!
  1109. * Common load project function for main engine and plugin.
  1110. */
  1111. bool loadProjectInternal(water::XmlDocument& xmlDoc);
  1112. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1113. // -------------------------------------------------------------------
  1114. // Patchbay stuff
  1115. /*!
  1116. * Virtual functions for handling patchbay state.
  1117. * Do not free returned data.
  1118. */
  1119. virtual const char* const* getPatchbayConnections(bool external) const;
  1120. virtual void restorePatchbayConnection(bool external,
  1121. const char* sourcePort,
  1122. const char* targetPort);
  1123. #endif
  1124. // -------------------------------------------------------------------
  1125. public:
  1126. /*!
  1127. * Native audio APIs.
  1128. */
  1129. enum AudioApi {
  1130. AUDIO_API_NULL,
  1131. // common
  1132. AUDIO_API_JACK,
  1133. AUDIO_API_OSS,
  1134. // linux
  1135. AUDIO_API_ALSA,
  1136. AUDIO_API_PULSEAUDIO,
  1137. // macos
  1138. AUDIO_API_COREAUDIO,
  1139. // windows
  1140. AUDIO_API_ASIO,
  1141. AUDIO_API_DIRECTSOUND,
  1142. AUDIO_API_WASAPI
  1143. };
  1144. // -------------------------------------------------------------------
  1145. // Engine initializers
  1146. // JACK
  1147. static CarlaEngine* newJack();
  1148. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1149. // Dummy
  1150. static CarlaEngine* newDummy();
  1151. #endif
  1152. #ifdef BUILD_BRIDGE
  1153. // Bridge
  1154. static CarlaEngine* newBridge(const char* audioPoolBaseName,
  1155. const char* rtClientBaseName,
  1156. const char* nonRtClientBaseName,
  1157. const char* nonRtServerBaseName);
  1158. #else
  1159. # ifdef USING_JUCE
  1160. // Juce
  1161. static CarlaEngine* newJuce(AudioApi api);
  1162. static uint getJuceApiCount();
  1163. static const char* getJuceApiName(uint index);
  1164. static const char* const* getJuceApiDeviceNames(uint index);
  1165. static const EngineDriverDeviceInfo* getJuceDeviceInfo(uint index, const char* deviceName);
  1166. static bool showJuceDeviceControlPanel(uint index, const char* deviceName);
  1167. # else
  1168. // RtAudio
  1169. static CarlaEngine* newRtAudio(AudioApi api);
  1170. static uint getRtAudioApiCount();
  1171. static const char* getRtAudioApiName(uint index);
  1172. static const char* const* getRtAudioApiDeviceNames(uint index);
  1173. static const EngineDriverDeviceInfo* getRtAudioDeviceInfo(uint index, const char* deviceName);
  1174. # endif
  1175. #endif
  1176. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngine)
  1177. };
  1178. /**@}*/
  1179. // -----------------------------------------------------------------------
  1180. CARLA_BACKEND_END_NAMESPACE
  1181. #endif // CARLA_ENGINE_HPP_INCLUDED