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.

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