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.

511 lines
17KB

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