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 36KB

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
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
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
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
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
10 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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. * Get the current CPU load estimated by the engine.
  675. */
  676. virtual float getDSPLoad() const noexcept;
  677. /*!
  678. * Get the total number of xruns so far.
  679. */
  680. virtual uint32_t getTotalXruns() const noexcept;
  681. // -------------------------------------------------------------------
  682. // Plugin management
  683. /*!
  684. * Add new plugin.
  685. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  686. */
  687. bool addPlugin(const BinaryType btype, const PluginType ptype,
  688. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  689. const void* const extra, const uint options);
  690. /*!
  691. * Add new plugin, using native binary type and default options.
  692. * @see ENGINE_CALLBACK_PLUGIN_ADDED
  693. */
  694. bool addPlugin(const PluginType ptype,
  695. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  696. const void* const extra);
  697. /*!
  698. * Remove plugin with id @a id.
  699. * @see ENGINE_CALLBACK_PLUGIN_REMOVED
  700. */
  701. bool removePlugin(const uint id);
  702. /*!
  703. * Remove all plugins.
  704. */
  705. bool removeAllPlugins();
  706. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  707. /*!
  708. * Rename plugin with id @a id to @a newName.
  709. * Returns the new name, or null if the operation failed.
  710. * Returned variable must be deleted if non-null.
  711. * @see ENGINE_CALLBACK_PLUGIN_RENAMED
  712. */
  713. virtual const char* renamePlugin(const uint id, const char* const newName);
  714. /*!
  715. * Clone plugin with id @a id.
  716. */
  717. bool clonePlugin(const uint id);
  718. /*!
  719. * Prepare replace of plugin with id @a id.
  720. * The next call to addPlugin() will use this id, replacing the selected plugin.
  721. * @note This function requires addPlugin() to be called afterwards, as soon as possible.
  722. */
  723. bool replacePlugin(const uint id) noexcept;
  724. /*!
  725. * Switch plugins with id @a idA and @a idB.
  726. */
  727. bool switchPlugins(const uint idA, const uint idB) noexcept;
  728. #endif
  729. /*!
  730. * Get plugin with id @a id.
  731. */
  732. CarlaPlugin* getPlugin(const uint id) const noexcept;
  733. /*!
  734. * Get plugin with id @a id, faster unchecked version.
  735. */
  736. CarlaPlugin* getPluginUnchecked(const uint id) const noexcept;
  737. /*!
  738. * Get a unique plugin name within the engine.
  739. * Returned variable must be deleted if non-null.
  740. */
  741. const char* getUniquePluginName(const char* const name) const;
  742. // -------------------------------------------------------------------
  743. // Project management
  744. /*!
  745. * Load a file of any type.
  746. * This will try to load a generic file as a plugin,
  747. * either by direct handling (SF2 and SFZ) or by using an internal plugin (like Audio and MIDI).
  748. */
  749. bool loadFile(const char* const filename);
  750. /*!
  751. * Load a project file.
  752. * @note Already loaded plugins are not removed; call removeAllPlugins() first if needed.
  753. */
  754. bool loadProject(const char* const filename, const bool setAsCurrentProject);
  755. /*!
  756. * Save current project to a file.
  757. */
  758. bool saveProject(const char* const filename, const bool setAsCurrentProject);
  759. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  760. /*!
  761. * Get the currently set project filename.
  762. */
  763. const char* getCurrentProjectFilename() const noexcept;
  764. /*!
  765. * Clear the currently set project filename.
  766. */
  767. void clearCurrentProjectFilename() noexcept;
  768. #endif
  769. // -------------------------------------------------------------------
  770. // Information (base)
  771. /*!
  772. * Get the current engine driver hints.
  773. * @see EngineDriverHints
  774. */
  775. uint getHints() const noexcept;
  776. /*!
  777. * Get the current buffer size.
  778. */
  779. uint32_t getBufferSize() const noexcept;
  780. /*!
  781. * Get the current sample rate.
  782. */
  783. double getSampleRate() const noexcept;
  784. /*!
  785. * Get the current engine name.
  786. */
  787. const char* getName() const noexcept;
  788. /*!
  789. * Get the current engine proccess mode.
  790. */
  791. EngineProcessMode getProccessMode() const noexcept;
  792. /*!
  793. * Get the current engine options (read-only).
  794. */
  795. const EngineOptions& getOptions() const noexcept;
  796. /*!
  797. * Get the current Time information (read-only).
  798. */
  799. virtual EngineTimeInfo getTimeInfo() const noexcept;
  800. // -------------------------------------------------------------------
  801. // Information (peaks)
  802. /*!
  803. * Get a plugin's peak values.
  804. */
  805. float* getPeaks(const uint pluginId) const noexcept;
  806. /*!
  807. * Get a plugin's input peak value.
  808. */
  809. float getInputPeak(const uint pluginId, const bool isLeft) const noexcept;
  810. /*!
  811. * Get a plugin's output peak value.
  812. */
  813. float getOutputPeak(const uint pluginId, const bool isLeft) const noexcept;
  814. // -------------------------------------------------------------------
  815. // Callback
  816. /*!
  817. * Call the main engine callback, if set.
  818. * May be called by plugins.
  819. */
  820. virtual void callback(const EngineCallbackOpcode action, const uint pluginId,
  821. const int value1, const int value2, const int value3,
  822. const float valueF, const char* const valueStr) noexcept;
  823. /*!
  824. * Set the main engine callback to @a func.
  825. */
  826. void setCallback(const EngineCallbackFunc func, void* const ptr) noexcept;
  827. // -------------------------------------------------------------------
  828. // Callback
  829. /*!
  830. * Call the file callback, if set.
  831. * May be called by plugins.
  832. */
  833. const char* runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept;
  834. /*!
  835. * Set the file callback to @a func.
  836. */
  837. void setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept;
  838. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  839. // -------------------------------------------------------------------
  840. // Patchbay
  841. /*!
  842. * Connect two patchbay ports.
  843. */
  844. virtual bool patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB);
  845. /*!
  846. * Remove a patchbay connection.
  847. */
  848. virtual bool patchbayDisconnect(const uint connectionId);
  849. /*!
  850. * Force the engine to resend all patchbay clients, ports and connections again.
  851. */
  852. virtual bool patchbayRefresh(const bool external);
  853. #endif
  854. // -------------------------------------------------------------------
  855. // Transport
  856. /*!
  857. * Start playback of the engine transport.
  858. */
  859. virtual void transportPlay() noexcept;
  860. /*!
  861. * Pause the engine transport.
  862. */
  863. virtual void transportPause() noexcept;
  864. /*!
  865. * Set the engine transport bpm to @a bpm.
  866. */
  867. virtual void transportBPM(const double bpm) noexcept;
  868. /*!
  869. * Relocate the engine transport to @a frames.
  870. */
  871. virtual void transportRelocate(const uint64_t frame) noexcept;
  872. // -------------------------------------------------------------------
  873. // Error handling
  874. /*!
  875. * Get last error.
  876. */
  877. const char* getLastError() const noexcept;
  878. /*!
  879. * Set last error.
  880. */
  881. void setLastError(const char* const error) const noexcept;
  882. // -------------------------------------------------------------------
  883. // Misc
  884. /*!
  885. * Check if the engine is about to close.
  886. */
  887. bool isAboutToClose() const noexcept;
  888. /*!
  889. * Tell the engine it's about to close.
  890. * This is used to prevent the engine thread(s) from reactivating.
  891. * Returns true if there's no pending engine events.
  892. */
  893. bool setAboutToClose() noexcept;
  894. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  895. /*!
  896. * TODO.
  897. */
  898. bool isLoadingProject() const noexcept;
  899. #endif
  900. /*!
  901. * Tell the engine to stop the current cancelable action.
  902. * @see ENGINE_CALLBACK_CANCELABLE_ACTION
  903. */
  904. void setActionCanceled(const bool canceled) noexcept;
  905. /*!
  906. * Check wherever the last cancelable action was indeed canceled or not.
  907. */
  908. bool wasActionCanceled() const noexcept;
  909. // -------------------------------------------------------------------
  910. // Options
  911. /*!
  912. * Set the engine option @a option to @a value or @a valueStr.
  913. */
  914. virtual void setOption(const EngineOption option, const int value, const char* const valueStr) noexcept;
  915. // -------------------------------------------------------------------
  916. // OSC Stuff
  917. #ifndef BUILD_BRIDGE
  918. /*!
  919. * Check if OSC controller is registered.
  920. */
  921. bool isOscControlRegistered() const noexcept;
  922. /*!
  923. * Idle OSC.
  924. */
  925. void idleOsc() const noexcept;
  926. /*!
  927. * Get OSC TCP server path.
  928. */
  929. const char* getOscServerPathTCP() const noexcept;
  930. /*!
  931. * Get OSC UDP server path.
  932. */
  933. const char* getOscServerPathUDP() const noexcept;
  934. #endif
  935. // -------------------------------------------------------------------
  936. // Helper functions
  937. /*!
  938. * Return internal data, needed for EventPorts when used in Rack, Patchbay and Bridge modes.
  939. * @note RT call
  940. */
  941. EngineEvent* getInternalEventBuffer(const bool isInput) const noexcept;
  942. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  943. /*!
  944. * Virtual functions for handling external graph ports.
  945. */
  946. virtual bool connectExternalGraphPort(const uint, const uint, const char* const);
  947. virtual bool disconnectExternalGraphPort(const uint, const uint, const char* const);
  948. #endif
  949. // -------------------------------------------------------------------
  950. protected:
  951. /*!
  952. * Internal data, for CarlaEngine subclasses only.
  953. */
  954. struct ProtectedData;
  955. ProtectedData* const pData;
  956. /*!
  957. * Some internal classes read directly from pData or call protected functions.
  958. */
  959. friend class CarlaPluginInstance;
  960. friend class EngineInternalGraph;
  961. friend class PendingRtEventsRunner;
  962. friend class ScopedActionLock;
  963. friend class ScopedEngineEnvironmentLocker;
  964. friend class ScopedThreadStopper;
  965. friend class PatchbayGraph;
  966. friend struct RackGraph;
  967. // -------------------------------------------------------------------
  968. // Internal stuff
  969. /*!
  970. * Report to all plugins about buffer size change.
  971. */
  972. void bufferSizeChanged(const uint32_t newBufferSize);
  973. /*!
  974. * Report to all plugins about sample rate change.
  975. * This is not supported on all plugin types, in which case they will have to be re-initiated.
  976. */
  977. void sampleRateChanged(const double newSampleRate);
  978. /*!
  979. * Report to all plugins about offline mode change.
  980. */
  981. void offlineModeChanged(const bool isOffline);
  982. /*!
  983. * Set a plugin (stereo) peak values.
  984. * @note RT call
  985. */
  986. void setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept;
  987. /*!
  988. * Common save project function for main engine and plugin.
  989. */
  990. void saveProjectInternal(water::MemoryOutputStream& outStrm) const;
  991. /*!
  992. * Common load project function for main engine and plugin.
  993. */
  994. bool loadProjectInternal(water::XmlDocument& xmlDoc);
  995. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  996. // -------------------------------------------------------------------
  997. // Patchbay stuff
  998. /*!
  999. * Virtual functions for handling patchbay state.
  1000. * Do not free returned data.
  1001. */
  1002. virtual const char* const* getPatchbayConnections(const bool external) const;
  1003. virtual void restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback);
  1004. #endif
  1005. // -------------------------------------------------------------------
  1006. public:
  1007. /*!
  1008. * Native audio APIs.
  1009. */
  1010. enum AudioApi {
  1011. AUDIO_API_NULL,
  1012. // common
  1013. AUDIO_API_JACK,
  1014. AUDIO_API_OSS,
  1015. // linux
  1016. AUDIO_API_ALSA,
  1017. AUDIO_API_PULSEAUDIO,
  1018. // macos
  1019. AUDIO_API_COREAUDIO,
  1020. // windows
  1021. AUDIO_API_ASIO,
  1022. AUDIO_API_DIRECTSOUND,
  1023. AUDIO_API_WASAPI
  1024. };
  1025. // -------------------------------------------------------------------
  1026. // Engine initializers
  1027. // JACK
  1028. static CarlaEngine* newJack();
  1029. #ifdef BUILD_BRIDGE
  1030. // Bridge
  1031. static CarlaEngine* newBridge(const char* const audioPoolBaseName,
  1032. const char* const rtClientBaseName,
  1033. const char* const nonRtClientBaseName,
  1034. const char* const nonRtServerBaseName);
  1035. #else
  1036. # ifdef USING_JUCE
  1037. // Juce
  1038. static CarlaEngine* newJuce(const AudioApi api);
  1039. static uint getJuceApiCount();
  1040. static const char* getJuceApiName(const uint index);
  1041. static const char* const* getJuceApiDeviceNames(const uint index);
  1042. static const EngineDriverDeviceInfo* getJuceDeviceInfo(const uint index, const char* const deviceName);
  1043. # else
  1044. // RtAudio
  1045. static CarlaEngine* newRtAudio(const AudioApi api);
  1046. static uint getRtAudioApiCount();
  1047. static const char* getRtAudioApiName(const uint index);
  1048. static const char* const* getRtAudioApiDeviceNames(const uint index);
  1049. static const EngineDriverDeviceInfo* getRtAudioDeviceInfo(const uint index, const char* const deviceName);
  1050. # endif
  1051. #endif
  1052. #ifndef BUILD_BRIDGE
  1053. // -------------------------------------------------------------------
  1054. // OSC Controller stuff
  1055. void oscSend_control_add_plugin_start(const uint pluginId, const char* const pluginName) const noexcept;
  1056. void oscSend_control_add_plugin_end(const uint pluginId) const noexcept;
  1057. void oscSend_control_remove_plugin(const uint pluginId) const noexcept;
  1058. void oscSend_control_set_plugin_info1(const uint pluginId, const PluginType type, const PluginCategory category, const uint hints, const int64_t uniqueId) const noexcept;
  1059. 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;
  1060. void oscSend_control_set_audio_count(const uint pluginId, const uint32_t ins, const uint32_t outs) const noexcept;
  1061. void oscSend_control_set_midi_count(const uint pluginId, const uint32_t ins, const uint32_t outs) const noexcept;
  1062. void oscSend_control_set_parameter_count(const uint pluginId, const uint32_t ins, const uint32_t outs) const noexcept;
  1063. void oscSend_control_set_program_count(const uint pluginId, const uint32_t count) const noexcept;
  1064. void oscSend_control_set_midi_program_count(const uint pluginId, const uint32_t count) const noexcept;
  1065. 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;
  1066. void oscSend_control_set_parameter_ranges1(const uint pluginId, const uint32_t index, const float def, const float min, const float max) const noexcept;
  1067. void oscSend_control_set_parameter_ranges2(const uint pluginId, const uint32_t index, const float step, const float stepSmall, const float stepLarge) const noexcept;
  1068. void oscSend_control_set_parameter_midi_cc(const uint pluginId, const uint32_t index, const int16_t cc) const noexcept;
  1069. void oscSend_control_set_parameter_midi_channel(const uint pluginId, const uint32_t index, const uint8_t channel) const noexcept;
  1070. 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)
  1071. void oscSend_control_set_default_value(const uint pluginId, const uint32_t index, const float value) const noexcept;
  1072. void oscSend_control_set_current_program(const uint pluginId, const int32_t index) const noexcept;
  1073. void oscSend_control_set_current_midi_program(const uint pluginId, const int32_t index) const noexcept;
  1074. void oscSend_control_set_program_name(const uint pluginId, const uint32_t index, const char* const name) const noexcept;
  1075. 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;
  1076. void oscSend_control_note_on(const uint pluginId, const uint8_t channel, const uint8_t note, const uint8_t velo) const noexcept;
  1077. void oscSend_control_note_off(const uint pluginId, const uint8_t channel, const uint8_t note) const noexcept;
  1078. void oscSend_control_set_peaks(const uint pluginId) const noexcept;
  1079. void oscSend_control_exit() const noexcept;
  1080. #endif
  1081. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngine)
  1082. };
  1083. /**@}*/
  1084. // -----------------------------------------------------------------------
  1085. CARLA_BACKEND_END_NAMESPACE
  1086. #endif // CARLA_ENGINE_HPP_INCLUDED