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. #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. * Get a plugin's peak values.
  777. */
  778. float* getPeaks(const uint pluginId) const noexcept;
  779. /*!
  780. * Get a plugin's input peak value.
  781. */
  782. float getInputPeak(const uint pluginId, const bool isLeft) const noexcept;
  783. /*!
  784. * Get a plugin's output peak value.
  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