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. 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. // -----------------------------------------------------------------------
  147. class CarlaEngineClient;
  148. class CarlaEngineBasePort;
  149. /*!
  150. * \class CarlaEngine
  151. *
  152. * \brief Carla Backend base engine class
  153. *
  154. * This is the base class for all available engine types available in Carla Backend.
  155. */
  156. class CarlaEngine
  157. {
  158. public:
  159. CarlaEngine();
  160. virtual ~CarlaEngine();
  161. // -------------------------------------------------------------------
  162. // Static values
  163. static const unsigned short MAX_PEAKS = 2;
  164. static int maxClientNameSize();
  165. static int maxPortNameSize();
  166. static unsigned short maxPluginNumber();
  167. static const char* getFixedClientName(const char* const clientName);
  168. static unsigned int getDriverCount();
  169. static const char* getDriverName(unsigned int index);
  170. static CarlaEngine* newDriverByName(const char* driverName);
  171. // -------------------------------------------------------------------
  172. // Virtual, per-engine type calls
  173. virtual bool init(const char* const clientName);
  174. virtual bool close();
  175. virtual bool isOffline() = 0;
  176. virtual bool isRunning() = 0;
  177. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  178. // -------------------------------------------------------------------
  179. // Plugin management
  180. short getNewPluginId() const;
  181. CarlaPlugin* getPlugin(const unsigned short id) const;
  182. CarlaPlugin* getPluginUnchecked(const unsigned short id) const;
  183. const char* getUniquePluginName(const char* const name);
  184. short addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  185. short addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  186. bool removePlugin(const unsigned short id);
  187. void removeAllPlugins();
  188. void idlePluginGuis();
  189. #ifndef BUILD_BRIDGE
  190. void processRack(float* inBuf[2], float* outBuf[2], uint32_t frames);
  191. #endif
  192. // bridge, internal use only
  193. void __bridgePluginRegister(const unsigned short id, CarlaPlugin* const plugin)
  194. {
  195. m_carlaPlugins[id] = plugin;
  196. }
  197. // -------------------------------------------------------------------
  198. // Information (base)
  199. CarlaEngineType getType() const;
  200. const char* getName() const;
  201. double getSampleRate() const;
  202. uint32_t getBufferSize() const;
  203. const CarlaTimeInfo* getTimeInfo() const;
  204. // -------------------------------------------------------------------
  205. // Information (audio peaks)
  206. double getInputPeak(const unsigned short pluginId, const unsigned short id) const;
  207. double getOutputPeak(const unsigned short pluginId, const unsigned short id) const;
  208. void setInputPeak(const unsigned short pluginId, const unsigned short id, double value);
  209. void setOutputPeak(const unsigned short pluginId, const unsigned short id, double value);
  210. // -------------------------------------------------------------------
  211. // Callback
  212. void callback(const CallbackType action, const unsigned short pluginId, const int value1, const int value2, const double value3);
  213. void setCallback(const CallbackFunc func, void* const ptr);
  214. // -------------------------------------------------------------------
  215. // Mutex locks
  216. void processLock();
  217. void processUnlock();
  218. void midiLock();
  219. void midiUnlock();
  220. // -------------------------------------------------------------------
  221. // OSC Stuff
  222. bool isOscControlRegisted() const;
  223. #ifndef BUILD_BRIDGE
  224. void oscWaitEvents();
  225. const char* getOscServerPathTCP() const;
  226. const char* getOscServerPathUDP() const;
  227. #else
  228. void setOscBridgeData(const CarlaOscData* const oscData);
  229. #endif
  230. #ifdef BUILD_BRIDGE
  231. void osc_send_bridge_audio_count(const int32_t ins, const int32_t outs, const int32_t total);
  232. void osc_send_bridge_midi_count(const int32_t ins, const int32_t outs, const int32_t total);
  233. void osc_send_bridge_parameter_count(const int32_t ins, const int32_t outs, const int32_t total);
  234. void osc_send_bridge_program_count(const int32_t count);
  235. void osc_send_bridge_midi_program_count(const int32_t count);
  236. 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);
  237. void osc_send_bridge_parameter_info(const int32_t index, const char* const name, const char* const unit);
  238. 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);
  239. 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);
  240. void osc_send_bridge_program_info(const int32_t index, const char* const name);
  241. void osc_send_bridge_midi_program_info(const int32_t index, const int32_t bank, const int32_t program, const char* const label);
  242. void osc_send_bridge_configure(const char* const key, const char* const value);
  243. void osc_send_bridge_set_parameter_value(const int32_t index, const double value);
  244. void osc_send_bridge_set_default_value(const int32_t index, const double value);
  245. void osc_send_bridge_set_program(const int32_t index);
  246. void osc_send_bridge_set_midi_program(const int32_t index);
  247. void osc_send_bridge_set_custom_data(const char* const stype, const char* const key, const char* const value);
  248. void osc_send_bridge_set_chunk_data(const char* const chunkFile);
  249. void osc_send_bridge_set_inpeak(const int32_t portId, const double value);
  250. void osc_send_bridge_set_outpeak(const int32_t portId, const double value);
  251. #else
  252. void osc_send_control_add_plugin_start(const int32_t pluginId, const char* const pluginName);
  253. void osc_send_control_add_plugin_end(const int32_t pluginId);
  254. void osc_send_control_remove_plugin(const int32_t pluginId);
  255. 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);
  256. 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);
  257. 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);
  258. 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);
  259. void osc_send_control_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  260. void osc_send_control_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  261. void osc_send_control_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  262. void osc_send_control_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  263. void osc_send_control_set_program(const int32_t pluginId, const int32_t index);
  264. void osc_send_control_set_program_count(const int32_t pluginId, const int32_t count);
  265. void osc_send_control_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  266. void osc_send_control_set_midi_program(const int32_t pluginId, const int32_t index);
  267. void osc_send_control_set_midi_program_count(const int32_t pluginId, const int32_t count);
  268. 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);
  269. void osc_send_control_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  270. void osc_send_control_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  271. void osc_send_control_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  272. void osc_send_control_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  273. void osc_send_control_exit();
  274. #endif
  275. #ifndef BUILD_BRIDGE
  276. // -------------------------------------------------------------------
  277. // Rack mode
  278. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  279. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  280. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  281. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  282. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  283. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  284. #endif
  285. // -------------------------------------
  286. /*!
  287. * \class ScopedLocker
  288. *
  289. * \brief Carla engine scoped locker
  290. *
  291. * This is a handy class that temporarily locks an engine during a function scope.
  292. */
  293. class ScopedLocker
  294. {
  295. public:
  296. /*!
  297. * Lock the engine \a engine if \a lock is true.
  298. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  299. *
  300. * \param engine The engine to lock
  301. * \param lock Wherever to lock the engine or not, true by default
  302. */
  303. ScopedLocker(CarlaEngine* const engine, bool lock = true)
  304. : m_engine(engine),
  305. m_lock(lock)
  306. {
  307. if (m_lock)
  308. m_engine->processLock();
  309. }
  310. ~ScopedLocker()
  311. {
  312. if (m_lock)
  313. m_engine->processUnlock();
  314. }
  315. private:
  316. CarlaEngine* const m_engine;
  317. const bool m_lock;
  318. };
  319. // -------------------------------------
  320. protected:
  321. CarlaEngineType type;
  322. const char* name;
  323. uint32_t bufferSize;
  324. double sampleRate;
  325. CarlaTimeInfo timeInfo;
  326. void bufferSizeChanged(const uint32_t newBufferSize);
  327. void startCheckThread();
  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