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.

1395 lines
35KB

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