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.

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