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.

CarlaEngine.hpp 34KB

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