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