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.

1259 lines
34KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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. #ifdef BUILD_BRIDGE
  21. struct CarlaOscData;
  22. #endif
  23. namespace water {
  24. class MemoryOutputStream;
  25. class XmlDocument;
  26. }
  27. CARLA_BACKEND_START_NAMESPACE
  28. // -----------------------------------------------------------------------
  29. /*!
  30. * @defgroup CarlaEngineAPI Carla Engine API
  31. *
  32. * The Carla Engine API.
  33. * @{
  34. */
  35. /*!
  36. * The type of an engine.
  37. */
  38. enum EngineType {
  39. /*!
  40. * Null engine type.
  41. */
  42. kEngineTypeNull = 0,
  43. /*!
  44. * JACK engine type.
  45. * Provides all processing modes.
  46. */
  47. kEngineTypeJack = 1,
  48. /*!
  49. * RtAudio engine type, used to provide Native Audio and MIDI support.
  50. */
  51. kEngineTypeRtAudio = 2,
  52. /*!
  53. * Plugin engine type, used to export the engine as a plugin.
  54. */
  55. kEngineTypePlugin = 3,
  56. /*!
  57. * Bridge engine type, used in BridgePlugin class.
  58. */
  59. kEngineTypeBridge = 4
  60. };
  61. /*!
  62. * The type of an engine port.
  63. */
  64. enum EnginePortType {
  65. /*!
  66. * Null port type.
  67. */
  68. kEnginePortTypeNull = 0,
  69. /*!
  70. * Audio port type.
  71. * @see CarlaEngineAudioPort
  72. */
  73. kEnginePortTypeAudio = 1,
  74. /*!
  75. * CV port type.
  76. * @see CarlaEngineCVPort
  77. */
  78. kEnginePortTypeCV = 2,
  79. /*!
  80. * Event port type (Control or MIDI).
  81. * @see CarlaEngineEventPort
  82. */
  83. kEnginePortTypeEvent = 3
  84. };
  85. /*!
  86. * The type of an engine event.
  87. */
  88. enum EngineEventType {
  89. /*!
  90. * Null port type.
  91. */
  92. kEngineEventTypeNull = 0,
  93. /*!
  94. * Control event type.
  95. * @see EngineControlEvent
  96. */
  97. kEngineEventTypeControl = 1,
  98. /*!
  99. * MIDI event type.
  100. * @see EngineMidiEvent
  101. */
  102. kEngineEventTypeMidi = 2
  103. };
  104. /*!
  105. * The type of an engine control event.
  106. */
  107. enum EngineControlEventType {
  108. /*!
  109. * Null event type.
  110. */
  111. kEngineControlEventTypeNull = 0,
  112. /*!
  113. * Parameter event type.
  114. * @note Value uses a normalized range of 0.0f<->1.0f.
  115. */
  116. kEngineControlEventTypeParameter = 1,
  117. /*!
  118. * MIDI Bank event type.
  119. */
  120. kEngineControlEventTypeMidiBank = 2,
  121. /*!
  122. * MIDI Program change event type.
  123. */
  124. kEngineControlEventTypeMidiProgram = 3,
  125. /*!
  126. * All sound off event type.
  127. */
  128. kEngineControlEventTypeAllSoundOff = 4,
  129. /*!
  130. * All notes off event type.
  131. */
  132. kEngineControlEventTypeAllNotesOff = 5
  133. };
  134. // -----------------------------------------------------------------------
  135. /*!
  136. * Engine control event.
  137. */
  138. struct CARLA_API EngineControlEvent {
  139. EngineControlEventType type; //!< Control-Event type.
  140. uint16_t param; //!< Parameter Id, midi bank or midi program.
  141. float value; //!< Parameter value, normalized to 0.0f<->1.0f.
  142. /*!
  143. * Convert this control event into MIDI data.
  144. * Returns size.
  145. */
  146. uint8_t convertToMidiData(const uint8_t channel, uint8_t data[3]) const noexcept;
  147. };
  148. /*!
  149. * Engine MIDI event.
  150. */
  151. struct CARLA_API EngineMidiEvent {
  152. static const uint8_t kDataSize = 4; //!< Size of internal data
  153. uint8_t port; //!< Port offset (usually 0)
  154. uint8_t size; //!< Number of bytes used
  155. /*!
  156. * MIDI data, without channel bit.
  157. * If size > kDataSize, dataExt is used (otherwise NULL).
  158. */
  159. uint8_t data[kDataSize];
  160. const uint8_t* dataExt;
  161. };
  162. /*!
  163. * Engine event.
  164. */
  165. struct CARLA_API EngineEvent {
  166. EngineEventType type; //!< Event Type; either Control or MIDI
  167. uint32_t time; //!< Time offset in frames
  168. uint8_t channel; //!< Channel, used for MIDI-related events
  169. /*!
  170. * Event specific data.
  171. */
  172. union {
  173. EngineControlEvent ctrl;
  174. EngineMidiEvent midi;
  175. };
  176. /*!
  177. * Fill this event from MIDI data.
  178. */
  179. void fillFromMidiData(const uint8_t size, const uint8_t* const data, const uint8_t midiPortOffset) noexcept;
  180. };
  181. // -----------------------------------------------------------------------
  182. /*!
  183. * Engine options.
  184. */
  185. struct CARLA_API EngineOptions {
  186. EngineProcessMode processMode;
  187. EngineTransportMode transportMode;
  188. const char* transportExtra;
  189. bool forceStereo;
  190. bool preferPluginBridges;
  191. bool preferUiBridges;
  192. bool uisAlwaysOnTop;
  193. uint maxParameters;
  194. uint uiBridgesTimeout;
  195. uint audioBufferSize;
  196. uint audioSampleRate;
  197. bool audioTripleBuffer;
  198. const char* audioDevice;
  199. const char* pathLADSPA;
  200. const char* pathDSSI;
  201. const char* pathLV2;
  202. const char* pathVST2;
  203. const char* pathSF2;
  204. const char* pathSFZ;
  205. const char* binaryDir;
  206. const char* resourceDir;
  207. bool preventBadBehaviour;
  208. uintptr_t frontendWinId;
  209. #ifndef CARLA_OS_WIN
  210. struct Wine {
  211. const char* executable;
  212. bool autoPrefix;
  213. const char* fallbackPrefix;
  214. bool rtPrio;
  215. int baseRtPrio;
  216. int serverRtPrio;
  217. Wine() noexcept;
  218. ~Wine() noexcept;
  219. CARLA_DECLARE_NON_COPY_STRUCT(Wine)
  220. } wine;
  221. #endif
  222. #ifndef DOXYGEN
  223. EngineOptions() noexcept;
  224. ~EngineOptions() noexcept;
  225. CARLA_DECLARE_NON_COPY_STRUCT(EngineOptions)
  226. #endif
  227. };
  228. /*!
  229. * Engine BBT Time information.
  230. */
  231. struct CARLA_API EngineTimeInfoBBT {
  232. bool valid;
  233. int32_t bar; //!< current bar
  234. int32_t beat; //!< current beat-within-bar
  235. double tick; //!< current tick-within-beat
  236. double barStartTick;
  237. float beatsPerBar; //!< time signature "numerator"
  238. float beatType; //!< time signature "denominator"
  239. double ticksPerBeat;
  240. double beatsPerMinute;
  241. /*!
  242. * Clear.
  243. */
  244. void clear() noexcept;
  245. #ifndef DOXYGEN
  246. EngineTimeInfoBBT() noexcept;
  247. EngineTimeInfoBBT(const EngineTimeInfoBBT&) noexcept;
  248. #endif
  249. };
  250. /*!
  251. * Engine Time information.
  252. */
  253. struct CARLA_API EngineTimeInfo {
  254. bool playing;
  255. uint64_t frame;
  256. uint64_t usecs;
  257. EngineTimeInfoBBT bbt;
  258. /*!
  259. * Clear.
  260. */
  261. void clear() noexcept;
  262. #ifndef DOXYGEN
  263. EngineTimeInfo() noexcept;
  264. EngineTimeInfo(const EngineTimeInfo&) noexcept;
  265. EngineTimeInfo& operator=(const EngineTimeInfo&) noexcept;
  266. // fast comparison, doesn't check all values
  267. bool compareIgnoringRollingFrames(const EngineTimeInfo& timeInfo, const uint32_t maxFrames) const noexcept;
  268. // quick operator, doesn't check all values
  269. bool operator==(const EngineTimeInfo& timeInfo) const noexcept;
  270. bool operator!=(const EngineTimeInfo& timeInfo) const noexcept;
  271. #endif
  272. };
  273. // -----------------------------------------------------------------------
  274. /*!
  275. * Carla Engine port (Abstract).
  276. * This is the base class for all Carla Engine ports.
  277. */
  278. class CARLA_API CarlaEnginePort
  279. {
  280. protected:
  281. /*!
  282. * The constructor.
  283. * All constructor parameters are constant and will never change in the lifetime of the port.
  284. */
  285. CarlaEnginePort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept;
  286. public:
  287. /*!
  288. * The destructor.
  289. */
  290. virtual ~CarlaEnginePort() noexcept;
  291. /*!
  292. * Get the type of the port, as provided by the respective subclasses.
  293. */
  294. virtual EnginePortType getType() const noexcept = 0;
  295. /*!
  296. * Initialize the port's internal buffer.
  297. */
  298. virtual void initBuffer() noexcept = 0;
  299. /*!
  300. * Check if this port is an input.
  301. */
  302. bool isInput() const noexcept
  303. {
  304. return kIsInput;
  305. }
  306. /*!
  307. * Get this ports' engine client.
  308. */
  309. const CarlaEngineClient& getEngineClient() const noexcept
  310. {
  311. return kClient;
  312. }
  313. #ifndef DOXYGEN
  314. protected:
  315. const CarlaEngineClient& kClient;
  316. const bool kIsInput;
  317. const uint32_t kIndexOffset;
  318. CARLA_DECLARE_NON_COPY_CLASS(CarlaEnginePort)
  319. #endif
  320. };
  321. /*!
  322. * Carla Engine Audio port.
  323. */
  324. class CARLA_API CarlaEngineAudioPort : public CarlaEnginePort
  325. {
  326. public:
  327. /*!
  328. * The constructor.
  329. * All constructor parameters are constant and will never change in the lifetime of the port.
  330. */
  331. CarlaEngineAudioPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept;
  332. /*!
  333. * The destructor.
  334. */
  335. ~CarlaEngineAudioPort() noexcept override;
  336. /*!
  337. * Get the type of the port, in this case kEnginePortTypeAudio.
  338. */
  339. EnginePortType getType() const noexcept final
  340. {
  341. return kEnginePortTypeAudio;
  342. }
  343. /*!
  344. * Initialize the port's internal buffer.
  345. */
  346. void initBuffer() noexcept override;
  347. /*!
  348. * Direct access to the port's audio buffer.
  349. * May be null.
  350. */
  351. float* getBuffer() const noexcept
  352. {
  353. return fBuffer;
  354. }
  355. #ifndef DOXYGEN
  356. protected:
  357. float* fBuffer;
  358. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineAudioPort)
  359. #endif
  360. };
  361. /*!
  362. * Carla Engine CV port.
  363. */
  364. class CARLA_API CarlaEngineCVPort : public CarlaEnginePort
  365. {
  366. public:
  367. /*!
  368. * The constructor.
  369. * All constructor parameters are constant and will never change in the lifetime of the port.
  370. */
  371. CarlaEngineCVPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept;
  372. /*!
  373. * The destructor.
  374. */
  375. ~CarlaEngineCVPort() noexcept override;
  376. /*!
  377. * Get the type of the port, in this case kEnginePortTypeCV.
  378. */
  379. EnginePortType getType() const noexcept final
  380. {
  381. return kEnginePortTypeCV;
  382. }
  383. /*!
  384. * Initialize the port's internal buffer.
  385. */
  386. void initBuffer() noexcept override;
  387. /*!
  388. * Direct access to the port's CV buffer.
  389. * May be null.
  390. */
  391. float* getBuffer() const noexcept
  392. {
  393. return fBuffer;
  394. }
  395. #ifndef DOXYGEN
  396. protected:
  397. float* fBuffer;
  398. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineCVPort)
  399. #endif
  400. };
  401. /*!
  402. * Carla Engine Event port.
  403. */
  404. class CARLA_API CarlaEngineEventPort : public CarlaEnginePort
  405. {
  406. public:
  407. /*!
  408. * The constructor.
  409. * All constructor parameters are constant and will never change in the lifetime of the port.
  410. */
  411. CarlaEngineEventPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept;
  412. /*!
  413. * The destructor.
  414. */
  415. ~CarlaEngineEventPort() noexcept override;
  416. /*!
  417. * Get the type of the port, in this case kEnginePortTypeEvent.
  418. */
  419. EnginePortType getType() const noexcept final
  420. {
  421. return kEnginePortTypeEvent;
  422. }
  423. /*!
  424. * Initialize the port's internal buffer for @a engine.
  425. */
  426. void initBuffer() noexcept override;
  427. /*!
  428. * Get the number of events present in the buffer.
  429. * @note You must only call this for input ports.
  430. */
  431. virtual uint32_t getEventCount() const noexcept;
  432. /*!
  433. * Get the event at @a index.
  434. * @note You must only call this for input ports.
  435. */
  436. virtual const EngineEvent& getEvent(const uint32_t index) const noexcept;
  437. /*!
  438. * Get the event at @a index, faster unchecked version.
  439. */
  440. virtual const EngineEvent& getEventUnchecked(const uint32_t index) const noexcept;
  441. /*!
  442. * Write a control event into the buffer.
  443. * @note You must only call this for output ports.
  444. */
  445. bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEvent& ctrl) noexcept;
  446. /*!
  447. * Write a control event into the buffer.
  448. * Arguments are the same as in the EngineControlEvent struct.
  449. * @note You must only call this for output ports.
  450. */
  451. virtual bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value = 0.0f) noexcept;
  452. /*!
  453. * Write a MIDI event into the buffer.
  454. * @note You must only call this for output ports.
  455. */
  456. bool writeMidiEvent(const uint32_t time, const uint8_t size, const uint8_t* const data) noexcept;
  457. /*!
  458. * Write a MIDI event into the buffer.
  459. * @note You must only call this for output ports.
  460. */
  461. bool writeMidiEvent(const uint32_t time, const uint8_t channel, const EngineMidiEvent& midi) noexcept;
  462. /*!
  463. * Write a MIDI event into the buffer.
  464. * Arguments are the same as in the EngineMidiEvent struct.
  465. * @note You must only call this for output ports.
  466. */
  467. virtual bool writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept;
  468. #ifndef DOXYGEN
  469. protected:
  470. EngineEvent* fBuffer;
  471. const EngineProcessMode kProcessMode;
  472. friend class CarlaPluginInstance;
  473. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineEventPort)
  474. #endif
  475. };
  476. // -----------------------------------------------------------------------
  477. /*!
  478. * Carla Engine client.
  479. * Each plugin requires one client from the engine (created via CarlaEngine::addClient()).
  480. * @note This is a virtual class, some engine types provide custom funtionality.
  481. */
  482. class CARLA_API CarlaEngineClient
  483. {
  484. public:
  485. /*!
  486. * The constructor, protected.
  487. * All constructor parameters are constant and will never change in the lifetime of the client.
  488. * Client starts in deactivated state.
  489. */
  490. CarlaEngineClient(const CarlaEngine& engine);
  491. /*!
  492. * The destructor.
  493. */
  494. virtual ~CarlaEngineClient() noexcept;
  495. /*!
  496. * Activate this client.
  497. * Client must be deactivated before calling this function.
  498. */
  499. virtual void activate() noexcept;
  500. /*!
  501. * Deactivate this client.
  502. * Client must be activated before calling this function.
  503. */
  504. virtual void deactivate() noexcept;
  505. /*!
  506. * Check if the client is activated.
  507. */
  508. virtual bool isActive() const noexcept;
  509. /*!
  510. * Check if the client is ok.
  511. * Plugins will refuse to instantiate if this returns false.
  512. * @note This is always true in rack and patchbay processing modes.
  513. */
  514. virtual bool isOk() const noexcept;
  515. /*!
  516. * Get the current latency, in samples.
  517. */
  518. virtual uint32_t getLatency() const noexcept;
  519. /*!
  520. * Change the client's latency.
  521. */
  522. virtual void setLatency(const uint32_t samples) noexcept;
  523. /*!
  524. * Add a new port of type @a portType.
  525. * @note This function does nothing in rack processing mode since ports are static there.
  526. */
  527. virtual CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput, const uint32_t indexOffset);
  528. /*!
  529. * Get this client's engine.
  530. */
  531. const CarlaEngine& getEngine() const noexcept;
  532. /*!
  533. * Get the engine's process mode.
  534. */
  535. EngineProcessMode getProcessMode() const noexcept;
  536. /*!
  537. * Get an audio port name.
  538. */
  539. const char* getAudioPortName(const bool isInput, const uint index) const noexcept;
  540. /*!
  541. * Get a CV port name.
  542. */
  543. const char* getCVPortName(const bool isInput, const uint index) const noexcept;
  544. /*!
  545. * Get an event port name.
  546. */
  547. const char* getEventPortName(const bool isInput, const uint index) const noexcept;
  548. #ifndef DOXYGEN
  549. protected:
  550. /*!
  551. * Internal data, for CarlaEngineClient subclasses only.
  552. */
  553. struct ProtectedData;
  554. ProtectedData* const pData;
  555. void _addAudioPortName(const bool, const char* const);
  556. void _addCVPortName(const bool, const char* const);
  557. void _addEventPortName(const bool, const char* const);
  558. const char* _getUniquePortName(const char* const);
  559. void _clearPorts();
  560. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineClient)
  561. #endif
  562. };
  563. // -----------------------------------------------------------------------
  564. /*!
  565. * Carla Engine.
  566. * @note This is a virtual class for all available engine types available in Carla.
  567. */
  568. class CARLA_API CarlaEngine
  569. {
  570. protected:
  571. /*!
  572. * The constructor, protected.
  573. * @note This only initializes engine data, it doesn't actually start the engine.
  574. */
  575. CarlaEngine();
  576. public:
  577. /*!
  578. * The destructor.
  579. * The engine must have been closed before this happens.
  580. */
  581. virtual ~CarlaEngine();
  582. // -------------------------------------------------------------------
  583. // Static calls
  584. /*!
  585. * Get the number of available engine drivers.
  586. */
  587. static uint getDriverCount();
  588. /*!
  589. * Get the name of the engine driver at @a index.
  590. */
  591. static const char* getDriverName(const uint index);
  592. /*!
  593. * Get the device names of the driver at @a index.
  594. */
  595. static const char* const* getDriverDeviceNames(const uint index);
  596. /*!
  597. * Get device information about the driver at @a index and name @a driverName.
  598. */
  599. static const EngineDriverDeviceInfo* getDriverDeviceInfo(const uint index, const char* const driverName);
  600. /*!
  601. * Create a new engine, using driver @a driverName.
  602. * Returned value must be deleted when no longer needed.
  603. * @note This only initializes engine data, it doesn't actually start the engine.
  604. */
  605. static CarlaEngine* newDriverByName(const char* const driverName);
  606. // -------------------------------------------------------------------
  607. // Constant values
  608. /*!
  609. * Maximum client name size.
  610. */
  611. virtual uint getMaxClientNameSize() const noexcept;
  612. /*!
  613. * Maximum port name size.
  614. */
  615. virtual uint getMaxPortNameSize() const noexcept;
  616. /*!
  617. * Current number of plugins loaded.
  618. */
  619. uint getCurrentPluginCount() const noexcept;
  620. /*!
  621. * Maximum number of loadable plugins allowed.
  622. * This function returns 0 if engine is not started.
  623. */
  624. uint getMaxPluginNumber() const noexcept;
  625. // -------------------------------------------------------------------
  626. // Virtual, per-engine type calls
  627. /*!
  628. * Initialize/start the engine, using @a clientName.
  629. * When the engine is intialized, you need to call idle() at regular intervals.
  630. */
  631. virtual bool init(const char* const clientName) = 0;
  632. /*!
  633. * Close engine.
  634. * This function always closes the engine even if it returns false.
  635. * In other words, even when something goes wrong when closing the engine it still be closed nonetheless.
  636. */
  637. virtual bool close();
  638. /*!
  639. * Idle engine.
  640. */
  641. virtual void idle() noexcept;
  642. /*!
  643. * Check if engine is running.
  644. */
  645. virtual bool isRunning() const noexcept = 0;
  646. /*!
  647. * Check if engine is running offline (aka freewheel mode).
  648. */
  649. virtual bool isOffline() const noexcept = 0;
  650. /*!
  651. * Check if engine runs on a constant buffer size value.
  652. * Default implementation returns true.
  653. */
  654. virtual bool usesConstantBufferSize() const noexcept;
  655. /*!
  656. * Get engine type.
  657. */
  658. virtual EngineType getType() const noexcept = 0;
  659. /*!
  660. * Get the currently used driver name.
  661. */
  662. virtual const char* getCurrentDriverName() const noexcept = 0;
  663. /*!
  664. * Add new engine client.
  665. * @note This function must only be called within a plugin class.
  666. */
  667. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  668. // -------------------------------------------------------------------
  669. // Plugin management
  670. /*!
  671. * Add new plugin.
  672. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  673. */
  674. bool addPlugin(const BinaryType btype, const PluginType ptype,
  675. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  676. const void* const extra, const uint options);
  677. /*!
  678. * Add new plugin, using native binary type and default options.
  679. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  680. */
  681. bool addPlugin(const PluginType ptype,
  682. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  683. const void* const extra);
  684. /*!
  685. * Remove plugin with id @a id.
  686. * @see ENGINE_CALLBACK_PLUGIN_REMOVED
  687. */
  688. bool removePlugin(const uint id);
  689. /*!
  690. * Remove all plugins.
  691. */
  692. bool removeAllPlugins();
  693. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  694. /*!
  695. * Rename plugin with id @a id to @a newName.
  696. * Returns the new name, or null if the operation failed.
  697. * Returned variable must be deleted if non-null.
  698. * @see ENGINE_CALLBACK_PLUGIN_RENAMED
  699. */
  700. virtual const char* renamePlugin(const uint id, const char* const newName);
  701. /*!
  702. * Clone plugin with id @a id.
  703. */
  704. bool clonePlugin(const uint id);
  705. /*!
  706. * Prepare replace of plugin with id @a id.
  707. * The next call to addPlugin() will use this id, replacing the selected plugin.
  708. * @note This function requires addPlugin() to be called afterwards, as soon as possible.
  709. */
  710. bool replacePlugin(const uint id) noexcept;
  711. /*!
  712. * Switch plugins with id @a idA and @a idB.
  713. */
  714. bool switchPlugins(const uint idA, const uint idB) noexcept;
  715. #endif
  716. /*!
  717. * Get plugin with id @a id.
  718. */
  719. CarlaPlugin* getPlugin(const uint id) const noexcept;
  720. /*!
  721. * Get plugin with id @a id, faster unchecked version.
  722. */
  723. CarlaPlugin* getPluginUnchecked(const uint id) const noexcept;
  724. /*!
  725. * Get a unique plugin name within the engine.
  726. * Returned variable must be deleted if non-null.
  727. */
  728. const char* getUniquePluginName(const char* const name) const;
  729. // -------------------------------------------------------------------
  730. // Project management
  731. /*!
  732. * Load a file of any type.
  733. * This will try to load a generic file as a plugin,
  734. * either by direct handling (SF2 and SFZ) or by using an internal plugin (like Audio and MIDI).
  735. */
  736. bool loadFile(const char* const filename);
  737. /*!
  738. * Load a project file.
  739. * @note Already loaded plugins are not removed; call removeAllPlugins() first if needed.
  740. */
  741. bool loadProject(const char* const filename);
  742. /*!
  743. * Save current project to a file.
  744. */
  745. bool saveProject(const char* const filename);
  746. // -------------------------------------------------------------------
  747. // Information (base)
  748. /*!
  749. * Get the current engine driver hints.
  750. * @see EngineDriverHints
  751. */
  752. uint getHints() const noexcept;
  753. /*!
  754. * Get the current buffer size.
  755. */
  756. uint32_t getBufferSize() const noexcept;
  757. /*!
  758. * Get the current sample rate.
  759. */
  760. double getSampleRate() const noexcept;
  761. /*!
  762. * Get the current engine name.
  763. */
  764. const char* getName() const noexcept;
  765. /*!
  766. * Get the current engine proccess mode.
  767. */
  768. EngineProcessMode getProccessMode() const noexcept;
  769. /*!
  770. * Get the current engine options (read-only).
  771. */
  772. const EngineOptions& getOptions() const noexcept;
  773. /*!
  774. * Get the current Time information (read-only).
  775. */
  776. virtual EngineTimeInfo getTimeInfo() const noexcept;
  777. // -------------------------------------------------------------------
  778. // Information (peaks)
  779. /*!
  780. * TODO.
  781. */
  782. float getInputPeak(const uint pluginId, const bool isLeft) const noexcept;
  783. /*!
  784. * TODO.
  785. */
  786. float getOutputPeak(const uint pluginId, const bool isLeft) const noexcept;
  787. // -------------------------------------------------------------------
  788. // Callback
  789. /*!
  790. * Call the main engine callback, if set.
  791. * May be called by plugins.
  792. */
  793. virtual void callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept;
  794. /*!
  795. * Set the main engine callback to @a func.
  796. */
  797. void setCallback(const EngineCallbackFunc func, void* const ptr) noexcept;
  798. // -------------------------------------------------------------------
  799. // Callback
  800. /*!
  801. * Call the file callback, if set.
  802. * May be called by plugins.
  803. */
  804. const char* runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept;
  805. /*!
  806. * Set the file callback to @a func.
  807. */
  808. void setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept;
  809. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  810. // -------------------------------------------------------------------
  811. // Patchbay
  812. /*!
  813. * Connect two patchbay ports.
  814. */
  815. virtual bool patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB);
  816. /*!
  817. * Remove a patchbay connection.
  818. */
  819. virtual bool patchbayDisconnect(const uint connectionId);
  820. /*!
  821. * Force the engine to resend all patchbay clients, ports and connections again.
  822. */
  823. virtual bool patchbayRefresh(const bool external);
  824. #endif
  825. // -------------------------------------------------------------------
  826. // Transport
  827. /*!
  828. * Start playback of the engine transport.
  829. */
  830. virtual void transportPlay() noexcept;
  831. /*!
  832. * Pause the engine transport.
  833. */
  834. virtual void transportPause() noexcept;
  835. /*!
  836. * Set the engine transport bpm to @a bpm.
  837. */
  838. virtual void transportBPM(const double bpm) noexcept;
  839. /*!
  840. * Relocate the engine transport to @a frames.
  841. */
  842. virtual void transportRelocate(const uint64_t frame) noexcept;
  843. // -------------------------------------------------------------------
  844. // Error handling
  845. /*!
  846. * Get last error.
  847. */
  848. const char* getLastError() const noexcept;
  849. /*!
  850. * Set last error.
  851. */
  852. void setLastError(const char* const error) const noexcept;
  853. // -------------------------------------------------------------------
  854. // Misc
  855. /*!
  856. * Check if the engine is about to close.
  857. */
  858. bool isAboutToClose() const noexcept;
  859. /*!
  860. * Tell the engine it's about to close.
  861. * This is used to prevent the engine thread(s) from reactivating.
  862. * Returns true if there's no pending engine events.
  863. */
  864. bool setAboutToClose() noexcept;
  865. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  866. /*!
  867. * TODO.
  868. */
  869. bool isLoadingProject() const noexcept;
  870. #endif
  871. // -------------------------------------------------------------------
  872. // Options
  873. /*!
  874. * Set the engine option @a option to @a value or @a valueStr.
  875. */
  876. virtual void setOption(const EngineOption option, const int value, const char* const valueStr) noexcept;
  877. // -------------------------------------------------------------------
  878. // OSC Stuff
  879. #ifndef BUILD_BRIDGE
  880. /*!
  881. * Check if OSC controller is registered.
  882. */
  883. bool isOscControlRegistered() const noexcept;
  884. /*!
  885. * Idle OSC.
  886. */
  887. void idleOsc() const noexcept;
  888. /*!
  889. * Get OSC TCP server path.
  890. */
  891. const char* getOscServerPathTCP() const noexcept;
  892. /*!
  893. * Get OSC UDP server path.
  894. */
  895. const char* getOscServerPathUDP() const noexcept;
  896. #endif
  897. // -------------------------------------------------------------------
  898. // Helper functions
  899. /*!
  900. * Return internal data, needed for EventPorts when used in Rack, Patchbay and Bridge modes.
  901. * @note RT call
  902. */
  903. EngineEvent* getInternalEventBuffer(const bool isInput) const noexcept;
  904. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  905. /*!
  906. * Virtual functions for handling external graph ports.
  907. */
  908. virtual bool connectExternalGraphPort(const uint, const uint, const char* const);
  909. virtual bool disconnectExternalGraphPort(const uint, const uint, const char* const);
  910. #endif
  911. // -------------------------------------------------------------------
  912. protected:
  913. /*!
  914. * Internal data, for CarlaEngine subclasses only.
  915. */
  916. struct ProtectedData;
  917. ProtectedData* const pData;
  918. /*!
  919. * Some internal classes read directly from pData or call protected functions.
  920. */
  921. friend class CarlaPluginInstance;
  922. friend class EngineInternalGraph;
  923. friend class PendingRtEventsRunner;
  924. friend class ScopedActionLock;
  925. friend class ScopedEngineEnvironmentLocker;
  926. friend class ScopedThreadStopper;
  927. friend class PatchbayGraph;
  928. friend struct RackGraph;
  929. // -------------------------------------------------------------------
  930. // Internal stuff
  931. /*!
  932. * Report to all plugins about buffer size change.
  933. */
  934. void bufferSizeChanged(const uint32_t newBufferSize);
  935. /*!
  936. * Report to all plugins about sample rate change.
  937. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  938. */
  939. void sampleRateChanged(const double newSampleRate);
  940. /*!
  941. * Report to all plugins about offline mode change.
  942. */
  943. void offlineModeChanged(const bool isOffline);
  944. /*!
  945. * Set a plugin (stereo) peak values.
  946. * @note RT call
  947. */
  948. void setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  949. /*!
  950. * Common save project function for main engine and plugin.
  951. */
  952. void saveProjectInternal(water::MemoryOutputStream& outStrm) const;
  953. /*!
  954. * Common load project function for main engine and plugin.
  955. */
  956. bool loadProjectInternal(water::XmlDocument& xmlDoc);
  957. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  958. // -------------------------------------------------------------------
  959. // Patchbay stuff
  960. /*!
  961. * Virtual functions for handling patchbay state.
  962. * Do not free returned data.
  963. */
  964. virtual const char* const* getPatchbayConnections(const bool external) const;
  965. virtual void restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback);
  966. #endif
  967. // -------------------------------------------------------------------
  968. public:
  969. /*!
  970. * Native audio APIs.
  971. */
  972. enum AudioApi {
  973. AUDIO_API_NULL,
  974. // common
  975. AUDIO_API_JACK,
  976. AUDIO_API_OSS,
  977. // linux
  978. AUDIO_API_ALSA,
  979. AUDIO_API_PULSEAUDIO,
  980. // macos
  981. AUDIO_API_COREAUDIO,
  982. // windows
  983. AUDIO_API_ASIO,
  984. AUDIO_API_DIRECTSOUND,
  985. AUDIO_API_WASAPI
  986. };
  987. // -------------------------------------------------------------------
  988. // Engine initializers
  989. // JACK
  990. static CarlaEngine* newJack();
  991. #ifdef BUILD_BRIDGE
  992. // Bridge
  993. static CarlaEngine* newBridge(const char* const audioPoolBaseName,
  994. const char* const rtClientBaseName,
  995. const char* const nonRtClientBaseName,
  996. const char* const nonRtServerBaseName);
  997. #else
  998. // RtAudio
  999. static CarlaEngine* newRtAudio(const AudioApi api);
  1000. static uint getRtAudioApiCount();
  1001. static const char* getRtAudioApiName(const uint index);
  1002. static const char* const* getRtAudioApiDeviceNames(const uint index);
  1003. static const EngineDriverDeviceInfo* getRtAudioDeviceInfo(const uint index, const char* const deviceName);
  1004. #endif
  1005. #ifndef BUILD_BRIDGE
  1006. // -------------------------------------------------------------------
  1007. // OSC Controller stuff
  1008. void oscSend_control_add_plugin_start(const uint pluginId, const char* const pluginName) const noexcept;
  1009. void oscSend_control_add_plugin_end(const uint pluginId) const noexcept;
  1010. void oscSend_control_remove_plugin(const uint pluginId) const noexcept;
  1011. void oscSend_control_set_plugin_info1(const uint pluginId, const PluginType type, const PluginCategory category, const uint hints, const int64_t uniqueId) const noexcept;
  1012. void oscSend_control_set_plugin_info2(const uint pluginId, const char* const realName, const char* const label, const char* const maker, const char* const copyright) const noexcept;
  1013. void oscSend_control_set_audio_count(const uint pluginId, const uint32_t ins, const uint32_t outs) const noexcept;
  1014. void oscSend_control_set_midi_count(const uint pluginId, const uint32_t ins, const uint32_t outs) const noexcept;
  1015. void oscSend_control_set_parameter_count(const uint pluginId, const uint32_t ins, const uint32_t outs) const noexcept;
  1016. void oscSend_control_set_program_count(const uint pluginId, const uint32_t count) const noexcept;
  1017. void oscSend_control_set_midi_program_count(const uint pluginId, const uint32_t count) const noexcept;
  1018. void oscSend_control_set_parameter_data(const uint pluginId, const uint32_t index, const ParameterType type, const uint hints, const char* const name, const char* const unit) const noexcept;
  1019. void oscSend_control_set_parameter_ranges1(const uint pluginId, const uint32_t index, const float def, const float min, const float max) const noexcept;
  1020. void oscSend_control_set_parameter_ranges2(const uint pluginId, const uint32_t index, const float step, const float stepSmall, const float stepLarge) const noexcept;
  1021. void oscSend_control_set_parameter_midi_cc(const uint pluginId, const uint32_t index, const int16_t cc) const noexcept;
  1022. void oscSend_control_set_parameter_midi_channel(const uint pluginId, const uint32_t index, const uint8_t channel) const noexcept;
  1023. void oscSend_control_set_parameter_value(const uint pluginId, const int32_t index, const float value) const noexcept; // may be used for internal params (< 0)
  1024. void oscSend_control_set_default_value(const uint pluginId, const uint32_t index, const float value) const noexcept;
  1025. void oscSend_control_set_current_program(const uint pluginId, const int32_t index) const noexcept;
  1026. void oscSend_control_set_current_midi_program(const uint pluginId, const int32_t index) const noexcept;
  1027. void oscSend_control_set_program_name(const uint pluginId, const uint32_t index, const char* const name) const noexcept;
  1028. void oscSend_control_set_midi_program_data(const uint pluginId, const uint32_t index, const uint32_t bank, const uint32_t program, const char* const name) const noexcept;
  1029. void oscSend_control_note_on(const uint pluginId, const uint8_t channel, const uint8_t note, const uint8_t velo) const noexcept;
  1030. void oscSend_control_note_off(const uint pluginId, const uint8_t channel, const uint8_t note) const noexcept;
  1031. void oscSend_control_set_peaks(const uint pluginId) const noexcept;
  1032. void oscSend_control_exit() const noexcept;
  1033. #endif
  1034. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngine)
  1035. };
  1036. /**@}*/
  1037. // -----------------------------------------------------------------------
  1038. CARLA_BACKEND_END_NAMESPACE
  1039. #endif // CARLA_ENGINE_HPP_INCLUDED