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.

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