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