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.

1332 lines
34KB

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