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.

1262 lines
33KB

  1. /*
  2. * Carla Engine API
  3. * Copyright (C) 2012-2013 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. #include "CarlaMIDI.h"
  21. #include "CarlaString.hpp"
  22. #ifdef BUILD_BRIDGE
  23. struct CarlaOscData;
  24. #endif
  25. CARLA_BACKEND_START_NAMESPACE
  26. #if 0
  27. } // Fix editor indentation
  28. #endif
  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.\n
  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.\n
  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. * Engine control event.
  140. */
  141. struct EngineControlEvent {
  142. EngineControlEventType type; //!< Control-Event type.
  143. uint16_t param; //!< Parameter Id, midi bank or midi program.
  144. float value; //!< Parameter value, normalized to 0.0f<->1.0f.
  145. void clear() noexcept
  146. {
  147. type = kEngineControlEventTypeNull;
  148. param = 0;
  149. value = 0.0f;
  150. }
  151. };
  152. /*!
  153. * Engine MIDI event.
  154. */
  155. struct EngineMidiEvent {
  156. uint8_t port; //!< Port offset (usually 0)
  157. uint8_t data[4]; //!< MIDI data, without channel bit
  158. uint8_t size; //!< Number of bytes used
  159. void clear() noexcept
  160. {
  161. port = 0;
  162. data[0] = 0;
  163. data[1] = 0;
  164. data[2] = 0;
  165. data[3] = 0;
  166. size = 0;
  167. }
  168. };
  169. /*!
  170. * Engine event.
  171. */
  172. struct EngineEvent {
  173. EngineEventType type; //!< Event Type; either Control or MIDI
  174. uint32_t time; //!< Time offset in frames
  175. uint8_t channel; //!< Channel, used for MIDI-related events
  176. union {
  177. EngineControlEvent ctrl;
  178. EngineMidiEvent midi;
  179. };
  180. EngineEvent() noexcept
  181. {
  182. clear();
  183. }
  184. void clear() noexcept
  185. {
  186. type = kEngineEventTypeNull;
  187. time = 0;
  188. channel = 0;
  189. }
  190. };
  191. /*!
  192. * Engine options.
  193. */
  194. struct EngineOptions {
  195. EngineProcessMode processMode;
  196. EngineTransportMode transportMode;
  197. bool forceStereo;
  198. bool preferPluginBridges;
  199. bool preferUiBridges;
  200. bool uisAlwaysOnTop;
  201. unsigned int maxParameters;
  202. unsigned int uiBridgesTimeout;
  203. unsigned int audioNumPeriods;
  204. unsigned int audioBufferSize;
  205. unsigned int audioSampleRate;
  206. CarlaString audioDevice;
  207. CarlaString binaryDir;
  208. CarlaString resourceDir;
  209. EngineOptions()
  210. #ifdef CARLA_OS_LINUX
  211. : processMode(ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  212. transportMode(ENGINE_TRANSPORT_MODE_JACK),
  213. #else
  214. : processMode(ENGINE_PROCESS_MODE_CONTINUOUS_RACK),
  215. transportMode(ENGINE_TRANSPORT_MODE_INTERNAL),
  216. #endif
  217. forceStereo(false),
  218. preferPluginBridges(false),
  219. preferUiBridges(true),
  220. uisAlwaysOnTop(true),
  221. maxParameters(MAX_DEFAULT_PARAMETERS),
  222. uiBridgesTimeout(4000),
  223. audioNumPeriods(2),
  224. audioBufferSize(512),
  225. audioSampleRate(44100) {}
  226. };
  227. /*!
  228. * Engine BBT Time information.
  229. */
  230. struct EngineTimeInfoBBT {
  231. int32_t bar; //!< current bar
  232. int32_t beat; //!< current beat-within-bar
  233. int32_t tick; //!< current tick-within-beat
  234. double barStartTick;
  235. float beatsPerBar; //!< time signature "numerator"
  236. float beatType; //!< time signature "denominator"
  237. double ticksPerBeat;
  238. double beatsPerMinute;
  239. EngineTimeInfoBBT() noexcept
  240. : bar(0),
  241. beat(0),
  242. tick(0),
  243. barStartTick(0.0),
  244. beatsPerBar(0.0f),
  245. beatType(0.0f),
  246. ticksPerBeat(0.0),
  247. beatsPerMinute(0.0) {}
  248. };
  249. /*!
  250. * Engine Time information.
  251. */
  252. struct EngineTimeInfo {
  253. static const uint32_t ValidBBT = 0x1;
  254. bool playing;
  255. uint64_t frame;
  256. uint64_t usecs;
  257. uint32_t valid;
  258. EngineTimeInfoBBT bbt;
  259. EngineTimeInfo() noexcept
  260. {
  261. clear();
  262. }
  263. void clear() noexcept
  264. {
  265. playing = false;
  266. frame = 0;
  267. usecs = 0;
  268. valid = 0x0;
  269. }
  270. // quick operator, doesn't check all values
  271. bool operator==(const EngineTimeInfo& timeInfo) const noexcept
  272. {
  273. if (timeInfo.playing != playing || timeInfo.frame != frame)
  274. return false;
  275. if (timeInfo.valid != valid)
  276. return false;
  277. if (timeInfo.bbt.beatsPerMinute != bbt.beatsPerMinute)
  278. return false;
  279. return true;
  280. }
  281. bool operator!=(const EngineTimeInfo& timeInfo) const noexcept
  282. {
  283. return !operator==(timeInfo);
  284. }
  285. };
  286. // -----------------------------------------------------------------------
  287. /*!
  288. * Carla Engine port (Abstract).\n
  289. * This is the base class for all Carla Engine ports.
  290. */
  291. class CarlaEnginePort
  292. {
  293. protected:
  294. /*!
  295. * The constructor.\n
  296. * Param \a isInput defines wherever this is an input port or not (output otherwise).\n
  297. * Input/output and process mode are constant for the lifetime of the port.
  298. */
  299. CarlaEnginePort(const CarlaEngine& engine, const bool isInput);
  300. public:
  301. /*!
  302. * The destructor.
  303. */
  304. virtual ~CarlaEnginePort();
  305. /*!
  306. * Get the type of the port, as provided by the respective subclasses.
  307. */
  308. virtual EnginePortType getType() const noexcept = 0;
  309. /*!
  310. * Initialize the port's internal buffer.
  311. */
  312. virtual void initBuffer() = 0;
  313. /*!
  314. * Check if this port is an input.
  315. */
  316. bool isInput() const noexcept
  317. {
  318. return fIsInput;
  319. }
  320. #ifndef DOXYGEN
  321. protected:
  322. const CarlaEngine& fEngine;
  323. const bool fIsInput;
  324. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEnginePort)
  325. #endif
  326. };
  327. /*!
  328. * Carla Engine Audio port.
  329. */
  330. class CarlaEngineAudioPort : public CarlaEnginePort
  331. {
  332. public:
  333. /*!
  334. * The constructor.\n
  335. * All constructor parameters are constant and will never change in the lifetime of the port.
  336. */
  337. CarlaEngineAudioPort(const CarlaEngine& engine, const bool isInput);
  338. /*!
  339. * The destructor.
  340. */
  341. virtual ~CarlaEngineAudioPort() override;
  342. /*!
  343. * Get the type of the port, in this case kEnginePortTypeAudio.
  344. */
  345. EnginePortType getType() const noexcept override
  346. {
  347. return kEnginePortTypeAudio;
  348. }
  349. /*!
  350. * Initialize the port's internal buffer.
  351. */
  352. virtual void initBuffer() override
  353. {
  354. }
  355. /*!
  356. * Direct access to the port's audio buffer.
  357. */
  358. float* getBuffer() const noexcept
  359. {
  360. return fBuffer;
  361. }
  362. #ifndef DOXYGEN
  363. protected:
  364. float* fBuffer;
  365. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineAudioPort)
  366. #endif
  367. };
  368. /*!
  369. * Carla Engine CV port.
  370. */
  371. class CarlaEngineCVPort : public CarlaEnginePort
  372. {
  373. public:
  374. /*!
  375. * The constructor.\n
  376. * All constructor parameters are constant and will never change in the lifetime of the port.
  377. */
  378. CarlaEngineCVPort(const CarlaEngine& engine, const bool isInput);
  379. /*!
  380. * The destructor.
  381. */
  382. virtual ~CarlaEngineCVPort() override;
  383. /*!
  384. * Get the type of the port, in this case kEnginePortTypeCV.
  385. */
  386. EnginePortType getType() const noexcept override
  387. {
  388. return kEnginePortTypeCV;
  389. }
  390. /*!
  391. * Initialize the port's internal buffer for \a engine.
  392. */
  393. virtual void initBuffer() override;
  394. /*!
  395. * Write buffer back into the engine.
  396. */
  397. virtual void writeBuffer(const uint32_t frames, const uint32_t timeOffset);
  398. /*!
  399. * Set a new buffer size.
  400. */
  401. void setBufferSize(const uint32_t bufferSize);
  402. /*!
  403. * Direct access to the port's buffer.
  404. */
  405. float* getBuffer() const noexcept
  406. {
  407. return fBuffer;
  408. }
  409. #ifndef DOXYGEN
  410. protected:
  411. float* fBuffer;
  412. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineCVPort)
  413. #endif
  414. };
  415. /*!
  416. * Carla Engine Event port.
  417. */
  418. class CarlaEngineEventPort : public CarlaEnginePort
  419. {
  420. public:
  421. /*!
  422. * The constructor.\n
  423. * All constructor parameters are constant and will never change in the lifetime of the port.
  424. */
  425. CarlaEngineEventPort(const CarlaEngine& engine, const bool isInput);
  426. /*!
  427. * The destructor.
  428. */
  429. virtual ~CarlaEngineEventPort() override;
  430. /*!
  431. * Get the type of the port, in this case kEnginePortTypeEvent.
  432. */
  433. EnginePortType getType() const noexcept override
  434. {
  435. return kEnginePortTypeEvent;
  436. }
  437. /*!
  438. * Initialize the port's internal buffer for \a engine.
  439. */
  440. virtual void initBuffer() override;
  441. /*!
  442. * Get the number of events present in the buffer.
  443. * \note You must only call this for input ports.
  444. */
  445. virtual uint32_t getEventCount() const;
  446. /*!
  447. * Get the event at \a index.
  448. * \note You must only call this for input ports.
  449. */
  450. virtual const EngineEvent& getEvent(const uint32_t index);
  451. /*!
  452. * Get the event at \a index, faster unchecked version.
  453. */
  454. virtual const EngineEvent& getEventUnchecked(const uint32_t index);
  455. /*!
  456. * Write a control event into the buffer.\n
  457. * Arguments are the same as in the EngineControlEvent struct.
  458. * \note You must only call this for output ports.
  459. */
  460. virtual bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value = 0.0f);
  461. /*!
  462. * Write a control event into the buffer, overloaded call.
  463. */
  464. bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEvent& ctrl)
  465. {
  466. return writeControlEvent(time, channel, ctrl.type, ctrl.param, ctrl.value);
  467. }
  468. /*!
  469. * Write a MIDI event into the buffer.\n
  470. * Arguments are the same as in the EngineMidiEvent struct.
  471. * \note You must only call this for output ports.
  472. */
  473. virtual bool writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t port, const uint8_t* const data, const uint8_t size);
  474. /*!
  475. * Write a MIDI event into the buffer, overloaded call.
  476. */
  477. bool writeMidiEvent(const uint32_t time, const uint8_t* const data, const uint8_t size)
  478. {
  479. return writeMidiEvent(time, MIDI_GET_CHANNEL_FROM_DATA(data), 0, data, size);
  480. }
  481. /*!
  482. * Write a MIDI event into the buffer, overloaded call.
  483. */
  484. bool writeMidiEvent(const uint32_t time, const uint8_t channel, const EngineMidiEvent& midi)
  485. {
  486. return writeMidiEvent(time, channel, midi.port, midi.data, midi.size);
  487. }
  488. #ifndef DOXYGEN
  489. protected:
  490. EngineEvent* fBuffer;
  491. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineEventPort)
  492. #endif
  493. };
  494. // -----------------------------------------------------------------------
  495. /*!
  496. * Carla Engine client.\n
  497. * Each plugin requires one client from the engine (created via CarlaEngine::addClient()).\n
  498. * \note This is a virtual class, some engine types provide custom funtionality.
  499. */
  500. class CarlaEngineClient
  501. {
  502. public:
  503. /*!
  504. * The constructor, protected.\n
  505. * All constructor parameters are constant and will never change in the lifetime of the client.\n
  506. * Client starts in deactivated state.
  507. */
  508. CarlaEngineClient(const CarlaEngine& engine);
  509. /*!
  510. * The destructor.
  511. */
  512. virtual ~CarlaEngineClient();
  513. /*!
  514. * Activate this client.\n
  515. * Client must be deactivated before calling this function.
  516. */
  517. virtual void activate();
  518. /*!
  519. * Deactivate this client.\n
  520. * Client must be activated before calling this function.
  521. */
  522. virtual void deactivate();
  523. /*!
  524. * Check if the client is activated.
  525. */
  526. virtual bool isActive() const noexcept;
  527. /*!
  528. * Check if the client is ok.\n
  529. * Plugins will refuse to instantiate if this returns false.
  530. * \note This is always true in rack and patchbay processing modes.
  531. */
  532. virtual bool isOk() const noexcept;
  533. /*!
  534. * Get the current latency, in samples.
  535. */
  536. virtual uint32_t getLatency() const noexcept;
  537. /*!
  538. * Change the client's latency.
  539. */
  540. virtual void setLatency(const uint32_t samples) noexcept;
  541. /*!
  542. * Add a new port of type \a portType.
  543. * \note This function does nothing in rack processing mode since ports are static there (2 audio + 1 event for both input and output).
  544. */
  545. virtual CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput);
  546. #ifndef DOXYGEN
  547. protected:
  548. const CarlaEngine& fEngine;
  549. bool fActive;
  550. uint32_t fLatency;
  551. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineClient)
  552. #endif
  553. };
  554. // -----------------------------------------------------------------------
  555. /*!
  556. * Protected data used in CarlaEngine and subclasses.\n
  557. * Non-engine code MUST NEVER have direct access to this.
  558. */
  559. struct CarlaEngineProtectedData;
  560. /*!
  561. * Carla Engine.
  562. * \note This is a virtual class for all available engine types available in Carla.
  563. */
  564. class CarlaEngine
  565. {
  566. protected:
  567. /*!
  568. * The constructor, protected.\n
  569. * \note This only initializes engine data, it doesn't start the engine (audio).
  570. */
  571. CarlaEngine();
  572. public:
  573. /*!
  574. * The decontructor.
  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 unsigned int getDriverCount();
  584. /*!
  585. * Get the name of the engine driver at \a index.
  586. */
  587. static const char* getDriverName(const unsigned int index);
  588. /*!
  589. * Get the device names of driver at \a index.
  590. */
  591. static const char* const* getDriverDeviceNames(const unsigned int index);
  592. /*!
  593. * Get the device names of driver at \a index.
  594. */
  595. static const EngineDriverDeviceInfo* getDriverDeviceInfo(const unsigned int index, const char* const driverName);
  596. /*!
  597. * Create a new engine, using driver \a driverName. \n
  598. * Returned variable must be deleted when no longer needed.
  599. * \note This only initializes engine data, it doesn't initialize the engine itself.
  600. */
  601. static CarlaEngine* newDriverByName(const char* const driverName);
  602. // -------------------------------------------------------------------
  603. // Constant values
  604. /*!
  605. * Maximum client name size.
  606. */
  607. virtual unsigned int getMaxClientNameSize() const noexcept;
  608. /*!
  609. * Maximum port name size.
  610. */
  611. virtual unsigned int getMaxPortNameSize() const noexcept;
  612. /*!
  613. * Current number of plugins loaded.
  614. */
  615. unsigned int getCurrentPluginCount() const noexcept;
  616. /*!
  617. * Maximum number of loadable plugins allowed.
  618. * \note This function returns 0 if engine is not started.
  619. */
  620. unsigned int getMaxPluginNumber() const noexcept;
  621. // -------------------------------------------------------------------
  622. // Virtual, per-engine type calls
  623. /*!
  624. * Initialize engine, using \a clientName.
  625. */
  626. virtual bool init(const char* const clientName);
  627. /*!
  628. * Close engine.
  629. */
  630. virtual bool close();
  631. /*!
  632. * Idle engine.
  633. */
  634. virtual void idle();
  635. /*!
  636. * Check if engine is running.
  637. */
  638. virtual bool isRunning() const noexcept = 0;
  639. /*!
  640. * Check if engine is running offline (aka freewheel mode).
  641. */
  642. virtual bool isOffline() const noexcept = 0;
  643. /*!
  644. * Get engine type.
  645. */
  646. virtual EngineType getType() const noexcept = 0;
  647. /*!
  648. * Get the currently used driver name.
  649. */
  650. virtual const char* getCurrentDriverName() const noexcept = 0;
  651. /*!
  652. * Add new engine client.
  653. * \note This function must only be called within a plugin class.
  654. */
  655. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  656. // -------------------------------------------------------------------
  657. // Plugin management
  658. /*!
  659. * Add new plugin.
  660. */
  661. bool addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, const void* const extra = nullptr);
  662. /*!
  663. * Add new plugin, using native binary type.
  664. */
  665. bool addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, const void* const extra = nullptr)
  666. {
  667. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, extra);
  668. }
  669. /*!
  670. * Remove plugin with id \a id.
  671. */
  672. bool removePlugin(const unsigned int id);
  673. /*!
  674. * Remove all plugins.
  675. */
  676. bool removeAllPlugins();
  677. /*!
  678. * Rename plugin with id \a id to \a newName.\n
  679. * Returns the new name, or nullptr if the operation failed.
  680. */
  681. virtual const char* renamePlugin(const unsigned int id, const char* const newName);
  682. /*!
  683. * Clone plugin with id \a id.
  684. */
  685. bool clonePlugin(const unsigned int id);
  686. /*!
  687. * Prepare replace of plugin with id \a id.\n
  688. * The next call to addPlugin() will use this id, replacing the selected plugin.
  689. * \note This function requires addPlugin() to be called afterwards, as soon as possible.
  690. */
  691. bool replacePlugin(const unsigned int id);
  692. /*!
  693. * Switch plugins with id \a idA and \a idB.
  694. */
  695. bool switchPlugins(const unsigned int idA, const unsigned int idB);
  696. /*!
  697. * Get plugin with id \a id.
  698. */
  699. CarlaPlugin* getPlugin(const unsigned int id);
  700. /*!
  701. * Get plugin with id \a id, faster unchecked version.
  702. */
  703. CarlaPlugin* getPluginUnchecked(const unsigned int id) const noexcept;
  704. /*!
  705. * Get a unique plugin name within the engine.\n
  706. * Returned variable must NOT be free'd.
  707. */
  708. const char* getUniquePluginName(const char* const name) const;
  709. // -------------------------------------------------------------------
  710. // Project management
  711. /*!
  712. * Load \a filename of any type.\n
  713. * This will try to load a generic file as a plugin,
  714. * either by direct handling (GIG, SF2 and SFZ) or by using an internal plugin (like Audio and MIDI).
  715. */
  716. bool loadFilename(const char* const filename);
  717. /*!
  718. * Load \a filename project file.\n
  719. * (project files have *.carxp extension)
  720. * \note Already loaded plugins are not removed; call removeAllPlugins() first if needed.
  721. */
  722. bool loadProject(const char* const filename);
  723. /*!
  724. * Save current project to \a filename.\n
  725. * (project files have *.carxp extension)
  726. */
  727. bool saveProject(const char* const filename);
  728. // -------------------------------------------------------------------
  729. // Information (base)
  730. /*!
  731. * Get the current engine driver hints.
  732. */
  733. unsigned int getHints() const noexcept
  734. {
  735. return fHints;
  736. }
  737. /*!
  738. * Get the current buffer size.
  739. */
  740. uint32_t getBufferSize() const noexcept
  741. {
  742. return fBufferSize;
  743. }
  744. /*!
  745. * Get the current sample rate.
  746. */
  747. double getSampleRate() const noexcept
  748. {
  749. return fSampleRate;
  750. }
  751. /*!
  752. * Get the current engine name.
  753. */
  754. const char* getName() const noexcept
  755. {
  756. return (const char*)fName;
  757. }
  758. /*!
  759. * Get the current engine proccess mode.
  760. */
  761. EngineProcessMode getProccessMode() const noexcept
  762. {
  763. return fOptions.processMode;
  764. }
  765. /*!
  766. * Get the current engine options (read-only).
  767. */
  768. const EngineOptions& getOptions() const noexcept
  769. {
  770. return fOptions;
  771. }
  772. /*!
  773. * Get the current Time information (read-only).
  774. */
  775. const EngineTimeInfo& getTimeInfo() const noexcept
  776. {
  777. return fTimeInfo;
  778. }
  779. // -------------------------------------------------------------------
  780. // Information (peaks)
  781. /*!
  782. * TODO.
  783. * \a id must be either 1 or 2.
  784. */
  785. float getInputPeak(const unsigned int pluginId, const unsigned short id) const;
  786. /*!
  787. * TODO.
  788. * \a id must be either 1 or 2.
  789. */
  790. float getOutputPeak(const unsigned int pluginId, const unsigned short id) const;
  791. // -------------------------------------------------------------------
  792. // Callback
  793. /*!
  794. * TODO.
  795. */
  796. void callback(const EngineCallbackOpcode action, const unsigned int pluginId, const int value1, const int value2, const float value3, const char* const valueStr);
  797. /*!
  798. * TODO.
  799. */
  800. void setCallback(const EngineCallbackFunc func, void* const ptr);
  801. // -------------------------------------------------------------------
  802. // Patchbay
  803. /*!
  804. * Connect patchbay ports \a portA and \a portB.
  805. */
  806. virtual bool patchbayConnect(int portA, int portB);
  807. /*!
  808. * Disconnect patchbay connection \a connectionId.
  809. */
  810. virtual bool patchbayDisconnect(int connectionId);
  811. /*!
  812. * Force the engine to resend all patchbay clients, ports and connections again.
  813. */
  814. virtual void patchbayRefresh();
  815. // -------------------------------------------------------------------
  816. // Transport
  817. /*!
  818. * Start playback of the engine transport.
  819. */
  820. virtual void transportPlay();
  821. /*!
  822. * Pause the engine transport.
  823. */
  824. virtual void transportPause();
  825. /*!
  826. * Relocate the engine transport to \a frames.
  827. */
  828. virtual void transportRelocate(const uint32_t frame);
  829. // -------------------------------------------------------------------
  830. // Error handling
  831. /*!
  832. * Get last error.
  833. */
  834. const char* getLastError() const noexcept;
  835. /*!
  836. * Set last error.
  837. */
  838. void setLastError(const char* const error);
  839. // -------------------------------------------------------------------
  840. // Misc
  841. /*!
  842. * Tell the engine it's about to close.\n
  843. * This is used to prevent the engine thread(s) from reactivating.
  844. */
  845. void setAboutToClose();
  846. // -------------------------------------------------------------------
  847. // Options
  848. /*!
  849. * Set the engine option \a option.
  850. */
  851. void setOption(const EngineOption option, const int value, const char* const valueStr);
  852. // -------------------------------------------------------------------
  853. // OSC Stuff
  854. #ifdef BUILD_BRIDGE
  855. /*!
  856. * Check if OSC bridge is registered.
  857. */
  858. bool isOscBridgeRegistered() const noexcept;
  859. #else
  860. /*!
  861. * Check if OSC controller is registered.
  862. */
  863. bool isOscControlRegistered() const noexcept;
  864. #endif
  865. /*!
  866. * Idle OSC.
  867. */
  868. void idleOsc();
  869. /*!
  870. * Get OSC TCP server path.
  871. */
  872. const char* getOscServerPathTCP() const noexcept;
  873. /*!
  874. * Get OSC UDP server path.
  875. */
  876. const char* getOscServerPathUDP() const noexcept;
  877. #ifdef BUILD_BRIDGE
  878. /*!
  879. * Set OSC bridge data.
  880. */
  881. void setOscBridgeData(const CarlaOscData* const oscData) noexcept;
  882. #endif
  883. // -------------------------------------------------------------------
  884. // Helper functions
  885. /*!
  886. * Return internal data, needed for EventPorts when used in Rack and Bridge modes.
  887. * \note RT call
  888. */
  889. EngineEvent* getInternalEventBuffer(const bool isInput) const noexcept;
  890. /*!
  891. * Force register a plugin into slot \a id. \n
  892. * This is needed so we can receive OSC events for a plugin while it initializes.
  893. */
  894. void registerEnginePlugin(const unsigned int id, CarlaPlugin* const plugin);
  895. // -------------------------------------------------------------------
  896. protected:
  897. /*!
  898. * Current engine driver hints.
  899. * \see getHints()
  900. */
  901. unsigned int fHints;
  902. /*!
  903. * Current buffer size.
  904. * \see getBufferSize()
  905. */
  906. uint32_t fBufferSize;
  907. /*!
  908. * Current sample rate.
  909. * \see getSampleRate()
  910. */
  911. double fSampleRate;
  912. /*!
  913. * Engine name.
  914. * \see getName()
  915. */
  916. CarlaString fName;
  917. /*!
  918. * Engine options.
  919. * \see getOptions() and setOption()
  920. */
  921. EngineOptions fOptions;
  922. /*!
  923. * Current time-pos information.
  924. * \see getTimeInfo()
  925. */
  926. EngineTimeInfo fTimeInfo;
  927. /*!
  928. * Internal data, for CarlaEngine subclasses only.
  929. */
  930. CarlaEngineProtectedData* const pData;
  931. friend struct CarlaEngineProtectedData;
  932. // -------------------------------------------------------------------
  933. // Internal stuff
  934. /*!
  935. * Report to all plugins about buffer size change.
  936. */
  937. void bufferSizeChanged(const uint32_t newBufferSize);
  938. /*!
  939. * Report to all plugins about sample rate change.\n
  940. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  941. */
  942. void sampleRateChanged(const double newSampleRate);
  943. /*!
  944. * Report to all plugins about offline mode change.
  945. */
  946. void offlineModeChanged(const bool isOffline);
  947. /*!
  948. * Run any pending RT events.\n
  949. * Must always be called at the end of audio processing.
  950. * \note RT call
  951. */
  952. void runPendingRtEvents();
  953. /*!
  954. * Set a plugin (stereo) peak values.
  955. * \note RT call
  956. */
  957. void setPluginPeaks(const unsigned int pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  958. #ifndef BUILD_BRIDGE
  959. /*!
  960. * Proccess audio buffer in rack mode.
  961. * \note RT call
  962. */
  963. void processRack(float* inBuf[2], float* outBuf[2], const uint32_t frames);
  964. /*!
  965. * Proccess audio buffer in patchbay mode.
  966. * In \a bufCount, [0]=inBufCount and [1]=outBufCount
  967. * \note RT call
  968. */
  969. void processPatchbay(float** inBuf, float** outBuf, const uint32_t bufCount[2], const uint32_t frames);
  970. #endif
  971. // -------------------------------------------------------------------
  972. // Engine initializers
  973. #ifdef BUILD_BRIDGE
  974. public:
  975. static CarlaEngine* newBridge(const char* const audioBaseName, const char* const controlBaseName);
  976. #endif
  977. private:
  978. static CarlaEngine* newJack();
  979. #ifndef BUILD_BRIDGE
  980. enum AudioApi {
  981. AUDIO_API_NULL = 0,
  982. // common
  983. AUDIO_API_JACK = 1,
  984. // linux
  985. AUDIO_API_ALSA = 2,
  986. AUDIO_API_OSS = 3,
  987. AUDIO_API_PULSE = 4,
  988. // macos
  989. AUDIO_API_CORE = 5,
  990. // windows
  991. AUDIO_API_ASIO = 6,
  992. AUDIO_API_DS = 7
  993. };
  994. static CarlaEngine* newRtAudio(const AudioApi api);
  995. static size_t getRtAudioApiCount();
  996. static const char* getRtAudioApiName(const unsigned int index);
  997. static const char* const* getRtAudioApiDeviceNames(const unsigned int index);
  998. static const EngineDriverDeviceInfo* getRtAudioDeviceInfo(const unsigned int index, const char* const deviceName);
  999. # ifdef USE_JUCE
  1000. static CarlaEngine* newJuce(const AudioApi api);
  1001. static size_t getJuceApiCount();
  1002. static const char* getJuceApiName(const unsigned int index);
  1003. static const char* const* getJuceApiDeviceNames(const unsigned int index);
  1004. static const EngineDriverDeviceInfo* getJuceDeviceInfo(const unsigned int index, const char* const deviceName);
  1005. # endif
  1006. #endif
  1007. // -------------------------------------------------------------------
  1008. // Bridge/Controller OSC stuff
  1009. public:
  1010. #ifdef BUILD_BRIDGE
  1011. void oscSend_bridge_audio_count(const int32_t ins, const int32_t outs, const int32_t total);
  1012. void oscSend_bridge_midi_count(const int32_t ins, const int32_t outs, const int32_t total);
  1013. void oscSend_bridge_parameter_count(const int32_t ins, const int32_t outs, const int32_t total);
  1014. void oscSend_bridge_program_count(const int32_t count);
  1015. void oscSend_bridge_midi_program_count(const int32_t count);
  1016. void oscSend_bridge_plugin_info(const int32_t category, const int32_t hints, const char* const name, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId);
  1017. void oscSend_bridge_parameter_info(const int32_t index, const char* const name, const char* const unit);
  1018. void oscSend_bridge_parameter_data(const int32_t index, const int32_t rindex, const int32_t hints, const int32_t midiChannel, const int32_t midiCC);
  1019. void oscSend_bridge_parameter_ranges(const int32_t index, const float def, const float min, const float max, const float step, const float stepSmall, const float stepLarge);
  1020. void oscSend_bridge_program_info(const int32_t index, const char* const name);
  1021. void oscSend_bridge_midi_program_info(const int32_t index, const int32_t bank, const int32_t program, const char* const label);
  1022. void oscSend_bridge_configure(const char* const key, const char* const value);
  1023. void oscSend_bridge_set_parameter_value(const int32_t index, const float value);
  1024. void oscSend_bridge_set_default_value(const int32_t index, const float value);
  1025. void oscSend_bridge_set_program(const int32_t index);
  1026. void oscSend_bridge_set_midi_program(const int32_t index);
  1027. void oscSend_bridge_set_custom_data(const char* const type, const char* const key, const char* const value);
  1028. void oscSend_bridge_set_chunk_data(const char* const chunkFile);
  1029. void oscSend_bridge_set_peaks();
  1030. #else
  1031. void oscSend_control_add_plugin_start(const int32_t pluginId, const char* const pluginName);
  1032. void oscSend_control_add_plugin_end(const int32_t pluginId);
  1033. void oscSend_control_remove_plugin(const int32_t pluginId);
  1034. void oscSend_control_set_plugin_data(const int32_t pluginId, const int32_t type, const int32_t category, const int32_t hints, const char* const realName, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId);
  1035. void oscSend_control_set_plugin_ports(const int32_t pluginId, const int32_t audioIns, const int32_t audioOuts, const int32_t midiIns, const int32_t midiOuts, const int32_t cIns, const int32_t cOuts);
  1036. void oscSend_control_set_parameter_data(const int32_t pluginId, const int32_t index, const int32_t hints, const char* const name, const char* const unit, const float current);
  1037. void oscSend_control_set_parameter_ranges(const int32_t pluginId, const int32_t index, const float min, const float max, const float def, const float step, const float stepSmall, const float stepLarge);
  1038. void oscSend_control_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  1039. void oscSend_control_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  1040. void oscSend_control_set_parameter_value(const int32_t pluginId, const int32_t index, const float value);
  1041. void oscSend_control_set_default_value(const int32_t pluginId, const int32_t index, const float value);
  1042. void oscSend_control_set_program(const int32_t pluginId, const int32_t index);
  1043. void oscSend_control_set_program_count(const int32_t pluginId, const int32_t count);
  1044. void oscSend_control_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  1045. void oscSend_control_set_midi_program(const int32_t pluginId, const int32_t index);
  1046. void oscSend_control_set_midi_program_count(const int32_t pluginId, const int32_t count);
  1047. void oscSend_control_set_midi_program_data(const int32_t pluginId, const int32_t index, const int32_t bank, const int32_t program, const char* const name);
  1048. void oscSend_control_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  1049. void oscSend_control_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  1050. void oscSend_control_set_peaks(const int32_t pluginId);
  1051. void oscSend_control_exit();
  1052. #endif
  1053. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngine)
  1054. };
  1055. /**@}*/
  1056. CARLA_BACKEND_END_NAMESPACE
  1057. #endif // CARLA_ENGINE_HPP_INCLUDED