Collection of tools useful for audio production
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.

577 lines
18KB

  1. /*
  2. * Carla Engine
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifndef CARLA_ENGINE_HPP
  18. #define CARLA_ENGINE_HPP
  19. #include "carla_engine_osc.hpp"
  20. #include "carla_engine_thread.hpp"
  21. #ifdef CARLA_ENGINE_JACK
  22. typedef struct _jack_client jack_client_t;
  23. typedef struct _jack_port jack_port_t;
  24. #endif
  25. #ifdef CARLA_ENGINE_RTAUDIO
  26. typedef int RtAudioApi;
  27. #endif
  28. CARLA_BACKEND_START_NAMESPACE
  29. /*!
  30. * @defgroup CarlaBackendEngine Carla Backend Engine
  31. *
  32. * The Carla Backend Engine
  33. * @{
  34. */
  35. /*!
  36. * @defgroup TimeInfoValidHints TimeInfo Valid Hints
  37. *
  38. * Various hints used for CarlaTimeInfo::valid.
  39. * @{
  40. */
  41. const uint32_t CarlaEngineTimeBBT = 0x1;
  42. /**@}*/
  43. enum CarlaEngineType {
  44. CarlaEngineTypeNull,
  45. CarlaEngineTypeJack,
  46. CarlaEngineTypeRtAudio,
  47. CarlaEngineTypePlugin
  48. };
  49. enum CarlaEnginePortType {
  50. CarlaEnginePortTypeAudio,
  51. CarlaEnginePortTypeControl,
  52. CarlaEnginePortTypeMIDI
  53. };
  54. enum CarlaEngineControlEventType {
  55. CarlaEngineEventNull = 0,
  56. CarlaEngineEventControlChange,
  57. CarlaEngineEventMidiBankChange,
  58. CarlaEngineEventMidiProgramChange,
  59. CarlaEngineEventAllSoundOff,
  60. CarlaEngineEventAllNotesOff
  61. };
  62. struct CarlaEngineControlEvent {
  63. CarlaEngineControlEventType type;
  64. uint32_t time;
  65. uint8_t channel;
  66. uint16_t controller;
  67. double value;
  68. CarlaEngineControlEvent()
  69. : type(CarlaEngineEventNull),
  70. time(0),
  71. channel(0),
  72. controller(0),
  73. value(0.0) {}
  74. };
  75. struct CarlaEngineMidiEvent {
  76. uint32_t time;
  77. uint8_t size;
  78. uint8_t data[4];
  79. CarlaEngineMidiEvent()
  80. : time(0),
  81. #ifdef Q_COMPILER_INITIALIZER_LISTS
  82. size(0),
  83. data{0} {}
  84. #else
  85. size(0) { data[0] = data[1] = data[2] = data[3] = 0; }
  86. #endif
  87. };
  88. struct CarlaTimeInfoBBT {
  89. int32_t bar;
  90. int32_t beat;
  91. int32_t tick;
  92. double bar_start_tick;
  93. float beats_per_bar;
  94. float beat_type;
  95. double ticks_per_beat;
  96. double beats_per_minute;
  97. CarlaTimeInfoBBT()
  98. : bar(0),
  99. beat(0),
  100. tick(0),
  101. bar_start_tick(0.0),
  102. beats_per_bar(0.0f),
  103. beat_type(0.0f),
  104. ticks_per_beat(0.0),
  105. beats_per_minute(0.0) {}
  106. };
  107. struct CarlaTimeInfo {
  108. bool playing;
  109. uint32_t frame;
  110. uint32_t time;
  111. uint32_t valid;
  112. CarlaTimeInfoBBT bbt;
  113. CarlaTimeInfo()
  114. : playing(false),
  115. frame(0),
  116. time(0),
  117. valid(0) {}
  118. };
  119. struct CarlaEngineClientNativeHandle {
  120. CarlaEngineType type;
  121. #ifdef CARLA_ENGINE_JACK
  122. jack_client_t* jackClient;
  123. #endif
  124. CarlaEngineClientNativeHandle()
  125. {
  126. type = CarlaEngineTypeNull;
  127. #ifdef CARLA_ENGINE_JACK
  128. jackClient = nullptr;
  129. #endif
  130. }
  131. };
  132. struct CarlaEnginePortNativeHandle {
  133. #ifdef CARLA_ENGINE_JACK
  134. jack_client_t* jackClient;
  135. jack_port_t* jackPort;
  136. #endif
  137. CarlaEnginePortNativeHandle()
  138. {
  139. #ifdef CARLA_ENGINE_JACK
  140. jackClient = nullptr;
  141. jackPort = nullptr;
  142. #endif
  143. }
  144. };
  145. #ifndef BUILD_BRIDGE
  146. // Global options
  147. struct CarlaEngineOptions {
  148. bool processHighPrecision;
  149. uint maxParameters;
  150. uint preferredBufferSize;
  151. uint preferredSampleRate;
  152. bool forceStereo;
  153. bool useDssiVstChunks;
  154. bool preferPluginBridges;
  155. bool preferUiBridges;
  156. uint oscUiTimeout;
  157. const char* bridge_posix32;
  158. const char* bridge_posix64;
  159. const char* bridge_win32;
  160. const char* bridge_win64;
  161. const char* bridge_lv2gtk2;
  162. const char* bridge_lv2gtk3;
  163. const char* bridge_lv2qt4;
  164. const char* bridge_lv2x11;
  165. const char* bridge_vsthwnd;
  166. const char* bridge_vstx11;
  167. CarlaEngineOptions()
  168. : processHighPrecision(false),
  169. maxParameters(MAX_PARAMETERS),
  170. preferredBufferSize(512),
  171. preferredSampleRate(44100),
  172. forceStereo(false),
  173. useDssiVstChunks(false),
  174. preferPluginBridges(false),
  175. preferUiBridges(true),
  176. oscUiTimeout(4000/100),
  177. bridge_posix32(nullptr),
  178. bridge_posix64(nullptr),
  179. bridge_win32(nullptr),
  180. bridge_win64(nullptr),
  181. bridge_lv2gtk2(nullptr),
  182. bridge_lv2gtk3(nullptr),
  183. bridge_lv2qt4(nullptr),
  184. bridge_lv2x11(nullptr),
  185. bridge_vsthwnd(nullptr),
  186. bridge_vstx11(nullptr) {}
  187. };
  188. #endif
  189. // -----------------------------------------------------------------------
  190. class CarlaEngineClient;
  191. class CarlaEngineBasePort;
  192. /*!
  193. * \class CarlaEngine
  194. *
  195. * \brief Carla Backend base engine class
  196. *
  197. * This is the base class for all available engine types available in Carla Backend.
  198. */
  199. class CarlaEngine
  200. {
  201. public:
  202. CarlaEngine();
  203. virtual ~CarlaEngine();
  204. // -------------------------------------------------------------------
  205. // Static values
  206. static const unsigned short MAX_PEAKS = 2;
  207. static int maxClientNameSize();
  208. static int maxPortNameSize();
  209. static unsigned short maxPluginNumber();
  210. static const char* getFixedClientName(const char* const clientName);
  211. static unsigned int getDriverCount();
  212. static const char* getDriverName(unsigned int index);
  213. static CarlaEngine* newDriverByName(const char* driverName);
  214. #ifndef BUILD_BRIDGE
  215. // -------------------------------------------------------------------
  216. // Global options
  217. CarlaEngineOptions options;
  218. static ProcessModeType processMode;
  219. void setOption(const OptionsType option, const int value, const char* const valueStr);
  220. void resetOptions();
  221. #endif
  222. // -------------------------------------------------------------------
  223. // Virtual, per-engine type calls
  224. virtual bool init(const char* const clientName);
  225. virtual bool close();
  226. virtual bool isOffline() = 0;
  227. virtual bool isRunning() = 0;
  228. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  229. // -------------------------------------------------------------------
  230. // Plugin management
  231. short getNewPluginId() const;
  232. CarlaPlugin* getPlugin(const unsigned short id) const;
  233. CarlaPlugin* getPluginUnchecked(const unsigned short id) const;
  234. const char* getUniquePluginName(const char* const name);
  235. short addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  236. short addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  237. bool removePlugin(const unsigned short id);
  238. void removeAllPlugins();
  239. void idlePluginGuis();
  240. #ifndef BUILD_BRIDGE
  241. void processRack(float* inBuf[2], float* outBuf[2], uint32_t frames);
  242. #endif
  243. // bridge, internal use only
  244. void __bridgePluginRegister(const unsigned short id, CarlaPlugin* const plugin)
  245. {
  246. m_carlaPlugins[id] = plugin;
  247. }
  248. // -------------------------------------------------------------------
  249. // Information (base)
  250. CarlaEngineType getType() const;
  251. const char* getName() const;
  252. double getSampleRate() const;
  253. uint32_t getBufferSize() const;
  254. const CarlaTimeInfo* getTimeInfo() const;
  255. // -------------------------------------------------------------------
  256. // Information (audio peaks)
  257. double getInputPeak(const unsigned short pluginId, const unsigned short id) const;
  258. double getOutputPeak(const unsigned short pluginId, const unsigned short id) const;
  259. void setInputPeak(const unsigned short pluginId, const unsigned short id, double value);
  260. void setOutputPeak(const unsigned short pluginId, const unsigned short id, double value);
  261. // -------------------------------------------------------------------
  262. // Callback
  263. void callback(const CallbackType action, const unsigned short pluginId, const int value1, const int value2, const double value3);
  264. void setCallback(const CallbackFunc func, void* const ptr);
  265. // -------------------------------------------------------------------
  266. // Error handling
  267. void setLastError(const char* error);
  268. const char* getLastError();
  269. // -------------------------------------------------------------------
  270. // Mutex locks
  271. void processLock();
  272. void processUnlock();
  273. void midiLock();
  274. void midiUnlock();
  275. // -------------------------------------------------------------------
  276. // OSC Stuff
  277. bool isOscControlRegisted() const;
  278. #ifndef BUILD_BRIDGE
  279. void oscWaitEvents();
  280. const char* getOscServerPathTCP() const;
  281. const char* getOscServerPathUDP() const;
  282. #else
  283. void setOscBridgeData(const CarlaOscData* const oscData);
  284. #endif
  285. #ifdef BUILD_BRIDGE
  286. void osc_send_bridge_audio_count(const int32_t ins, const int32_t outs, const int32_t total);
  287. void osc_send_bridge_midi_count(const int32_t ins, const int32_t outs, const int32_t total);
  288. void osc_send_bridge_parameter_count(const int32_t ins, const int32_t outs, const int32_t total);
  289. void osc_send_bridge_program_count(const int32_t count);
  290. void osc_send_bridge_midi_program_count(const int32_t count);
  291. void osc_send_bridge_plugin_info(const int32_t category, const int32_t hints, const char* const name, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId);
  292. void osc_send_bridge_parameter_info(const int32_t index, const char* const name, const char* const unit);
  293. void osc_send_bridge_parameter_data(const int32_t index, const int32_t type, const int32_t rindex, const int32_t hints, const int32_t midiChannel, const int32_t midiCC);
  294. void osc_send_bridge_parameter_ranges(const int32_t index, const double def, const double min, const double max, const double step, const double stepSmall, const double stepLarge);
  295. void osc_send_bridge_program_info(const int32_t index, const char* const name);
  296. void osc_send_bridge_midi_program_info(const int32_t index, const int32_t bank, const int32_t program, const char* const label);
  297. void osc_send_bridge_configure(const char* const key, const char* const value);
  298. void osc_send_bridge_set_parameter_value(const int32_t index, const double value);
  299. void osc_send_bridge_set_default_value(const int32_t index, const double value);
  300. void osc_send_bridge_set_program(const int32_t index);
  301. void osc_send_bridge_set_midi_program(const int32_t index);
  302. void osc_send_bridge_set_custom_data(const char* const stype, const char* const key, const char* const value);
  303. void osc_send_bridge_set_chunk_data(const char* const chunkFile);
  304. void osc_send_bridge_set_inpeak(const int32_t portId, const double value);
  305. void osc_send_bridge_set_outpeak(const int32_t portId, const double value);
  306. #else
  307. void osc_send_control_add_plugin_start(const int32_t pluginId, const char* const pluginName);
  308. void osc_send_control_add_plugin_end(const int32_t pluginId);
  309. void osc_send_control_remove_plugin(const int32_t pluginId);
  310. void osc_send_control_set_plugin_data(const int32_t pluginId, const int32_t type, const int32_t category, const int32_t hints, const char* const realName, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId);
  311. void osc_send_control_set_plugin_ports(const int32_t pluginId, const int32_t audioIns, const int32_t audioOuts, const int32_t midiIns, const int32_t midiOuts, const int32_t cIns, const int32_t cOuts, const int32_t cTotals);
  312. void osc_send_control_set_parameter_data(const int32_t pluginId, const int32_t index, const int32_t type, const int32_t hints, const char* const name, const char* const label, const double current);
  313. void osc_send_control_set_parameter_ranges(const int32_t pluginId, const int32_t index, const double min, const double max, const double def, const double step, const double stepSmall, const double stepLarge);
  314. void osc_send_control_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  315. void osc_send_control_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  316. void osc_send_control_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  317. void osc_send_control_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  318. void osc_send_control_set_program(const int32_t pluginId, const int32_t index);
  319. void osc_send_control_set_program_count(const int32_t pluginId, const int32_t count);
  320. void osc_send_control_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  321. void osc_send_control_set_midi_program(const int32_t pluginId, const int32_t index);
  322. void osc_send_control_set_midi_program_count(const int32_t pluginId, const int32_t count);
  323. void osc_send_control_set_midi_program_data(const int32_t pluginId, const int32_t index, const int32_t bank, const int32_t program, const char* const name);
  324. void osc_send_control_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  325. void osc_send_control_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  326. void osc_send_control_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  327. void osc_send_control_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  328. void osc_send_control_exit();
  329. #endif
  330. #ifndef BUILD_BRIDGE
  331. // -------------------------------------------------------------------
  332. // Rack mode
  333. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  334. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  335. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  336. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  337. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  338. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  339. #endif
  340. // -------------------------------------
  341. /*!
  342. * \class ScopedLocker
  343. *
  344. * \brief Carla engine scoped locker
  345. *
  346. * This is a handy class that temporarily locks an engine during a function scope.
  347. */
  348. class ScopedLocker
  349. {
  350. public:
  351. /*!
  352. * Lock the engine \a engine if \a lock is true.
  353. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  354. *
  355. * \param engine The engine to lock
  356. * \param lock Wherever to lock the engine or not, true by default
  357. */
  358. ScopedLocker(CarlaEngine* const engine, bool lock = true)
  359. : m_engine(engine),
  360. m_lock(lock)
  361. {
  362. if (m_lock)
  363. m_engine->processLock();
  364. }
  365. ~ScopedLocker()
  366. {
  367. if (m_lock)
  368. m_engine->processUnlock();
  369. }
  370. private:
  371. CarlaEngine* const m_engine;
  372. const bool m_lock;
  373. };
  374. // -------------------------------------
  375. protected:
  376. CarlaEngineType type;
  377. const char* name;
  378. uint32_t bufferSize;
  379. double sampleRate;
  380. CarlaTimeInfo timeInfo;
  381. void bufferSizeChanged(const uint32_t newBufferSize);
  382. void startCheckThread();
  383. private:
  384. CarlaEngineThread m_thread;
  385. #ifndef BUILD_BRIDGE
  386. CarlaEngineOsc m_osc;
  387. #endif
  388. const CarlaOscData* m_oscData;
  389. QMutex m_procLock;
  390. QMutex m_midiLock;
  391. CallbackFunc m_callback;
  392. void* m_callbackPtr;
  393. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  394. const char* m_uniqueNames[MAX_PLUGINS];
  395. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  396. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  397. static unsigned short m_maxPluginNumber;
  398. #ifdef CARLA_ENGINE_JACK
  399. static CarlaEngine* newJack();
  400. #endif
  401. #ifdef CARLA_ENGINE_RTAUDIO
  402. static CarlaEngine* newRtAudio(RtAudioApi api);
  403. #endif
  404. };
  405. // -----------------------------------------------------------------------
  406. class CarlaEngineClient
  407. {
  408. public:
  409. CarlaEngineClient(const CarlaEngineClientNativeHandle& handle);
  410. ~CarlaEngineClient();
  411. void activate();
  412. void deactivate();
  413. bool isActive() const;
  414. bool isOk() const;
  415. uint32_t getLatency() const;
  416. void setLatency(const uint32_t samples);
  417. const CarlaEngineBasePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput);
  418. private:
  419. bool m_active;
  420. uint32_t m_latency;
  421. const CarlaEngineClientNativeHandle handle;
  422. };
  423. // -----------------------------------------------------------------------
  424. // base
  425. class CarlaEngineBasePort
  426. {
  427. public:
  428. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  429. virtual ~CarlaEngineBasePort();
  430. virtual void initBuffer(CarlaEngine* const engine) = 0;
  431. const CarlaEnginePortNativeHandle& getHandle() const
  432. {
  433. return handle;
  434. }
  435. protected:
  436. void* buffer;
  437. const bool isInput;
  438. const CarlaEnginePortNativeHandle handle;
  439. };
  440. // audio
  441. class CarlaEngineAudioPort : public CarlaEngineBasePort
  442. {
  443. public:
  444. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  445. void initBuffer(CarlaEngine* const engine);
  446. #ifdef CARLA_ENGINE_JACK
  447. float* getJackAudioBuffer(uint32_t nframes);
  448. #endif
  449. };
  450. // control
  451. class CarlaEngineControlPort : public CarlaEngineBasePort
  452. {
  453. public:
  454. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  455. void initBuffer(CarlaEngine* const engine);
  456. uint32_t getEventCount();
  457. const CarlaEngineControlEvent* getEvent(uint32_t index);
  458. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  459. };
  460. // midi
  461. class CarlaEngineMidiPort : public CarlaEngineBasePort
  462. {
  463. public:
  464. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  465. void initBuffer(CarlaEngine* const engine);
  466. uint32_t getEventCount();
  467. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  468. void writeEvent(uint32_t time, const uint8_t* data, uint8_t size);
  469. };
  470. // -----------------------------------------------------------------------
  471. /**@}*/
  472. CARLA_BACKEND_END_NAMESPACE
  473. #endif // CARLA_ENGINE_HPP