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.

1302 lines
36KB

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