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.

572 lines
18KB

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