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.

529 lines
16KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2012 Filipe Coelho <falktx@gmail.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 <jack/jack.h>
  24. #include <jack/midiport.h>
  25. #endif
  26. #ifdef CARLA_ENGINE_RTAUDIO
  27. #include "RtAudio.h"
  28. #include "RtMidi.h"
  29. #endif
  30. CARLA_BACKEND_START_NAMESPACE
  31. /*!
  32. * @defgroup CarlaBackendEngine Carla Backend Engine
  33. *
  34. * The Carla Backend Engine
  35. * @{
  36. */
  37. /*!
  38. * @defgroup TimeInfoValidHints TimeInfo Valid Hints
  39. *
  40. * Various hints used for CarlaTimeInfo::valid.
  41. * @{
  42. */
  43. const uint32_t CarlaEngineTimeBBT = 0x1;
  44. /**@}*/
  45. enum CarlaEngineType {
  46. CarlaEngineTypeNull,
  47. CarlaEngineTypeJack,
  48. CarlaEngineTypeRtAudio
  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) { memset(data, 0, sizeof(uint8_t)*4); }
  87. #endif
  88. };
  89. struct CarlaTimeInfo {
  90. bool playing;
  91. uint32_t frame;
  92. uint32_t time;
  93. uint32_t valid;
  94. struct {
  95. int32_t bar;
  96. int32_t beat;
  97. int32_t tick;
  98. double bar_start_tick;
  99. float beats_per_bar;
  100. float beat_type;
  101. double ticks_per_beat;
  102. double beats_per_minute;
  103. } bbt;
  104. CarlaTimeInfo()
  105. : playing(false),
  106. frame(0),
  107. time(0),
  108. #ifdef Q_COMPILER_INITIALIZER_LISTS
  109. valid(0),
  110. bbt{0, 0, 0, 0.0, 0.0f, 0.0f, 0.0, 0.0} {}
  111. #else
  112. valid(0)
  113. {
  114. bbt.bar = bbt.beat = bbt.tick = 0;
  115. bbt.beats_per_bar = bbt.beat_type = 0.0f;
  116. bbt.bar_start_tick = bbt.ticks_per_beat = bbt.beats_per_minute = 0.0;
  117. }
  118. #endif
  119. };
  120. struct CarlaEngineClientNativeHandle {
  121. #ifdef CARLA_ENGINE_JACK
  122. jack_client_t* jackClient;
  123. #endif
  124. #ifdef CARLA_ENGINE_RTAUDIO
  125. RtAudio* rtAudioPtr;
  126. #endif
  127. CarlaEngineClientNativeHandle()
  128. {
  129. #ifdef CARLA_ENGINE_JACK
  130. jackClient = nullptr;
  131. #endif
  132. #ifdef CARLA_ENGINE_RTAUDIO
  133. rtAudioPtr = nullptr;
  134. #endif
  135. }
  136. };
  137. struct CarlaEnginePortNativeHandle {
  138. #ifdef CARLA_ENGINE_JACK
  139. jack_client_t* jackClient;
  140. jack_port_t* jackPort;
  141. #endif
  142. CarlaEnginePortNativeHandle()
  143. {
  144. #ifdef CARLA_ENGINE_JACK
  145. jackClient = nullptr;
  146. jackPort = nullptr;
  147. #endif
  148. }
  149. };
  150. // -----------------------------------------------------------------------
  151. class CarlaEngineClient;
  152. class CarlaEngineBasePort;
  153. /*!
  154. * \class CarlaEngine
  155. *
  156. * \brief Carla Backend base engine class
  157. *
  158. * This is the base class for all available engine types available in Carla Backend.
  159. */
  160. class CarlaEngine
  161. {
  162. public:
  163. CarlaEngine();
  164. virtual ~CarlaEngine();
  165. // -------------------------------------------------------------------
  166. // Static values
  167. static const unsigned short MAX_PEAKS = 2;
  168. static int maxClientNameSize();
  169. static int maxPortNameSize();
  170. // -------------------------------------------------------------------
  171. // Virtual, per-engine type calls
  172. virtual bool init(const char* const clientName);
  173. virtual bool close();
  174. virtual bool isOnAudioThread() = 0;
  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* getUniqueName(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. // bridge, internal use only
  190. void __bridgePluginRegister(const unsigned short id, CarlaPlugin* const plugin)
  191. {
  192. m_carlaPlugins[id] = plugin;
  193. }
  194. // -------------------------------------------------------------------
  195. // Information (base)
  196. const char* getName() const;
  197. double getSampleRate() const;
  198. uint32_t getBufferSize() const;
  199. const CarlaTimeInfo* getTimeInfo() const;
  200. // -------------------------------------------------------------------
  201. // Information (audio peaks)
  202. double getInputPeak(const unsigned short pluginId, const unsigned short id) const;
  203. double getOutputPeak(const unsigned short pluginId, const unsigned short id) const;
  204. void setInputPeak(const unsigned short pluginId, const unsigned short id, double value);
  205. void setOutputPeak(const unsigned short pluginId, const unsigned short id, double value);
  206. // -------------------------------------------------------------------
  207. // Callback
  208. void callback(const CallbackType action, const unsigned short pluginId, const int value1, const int value2, const double value3);
  209. void setCallback(const CallbackFunc func, void* const ptr);
  210. // -------------------------------------------------------------------
  211. // Mutex locks
  212. void processLock();
  213. void processUnlock();
  214. void midiLock();
  215. void midiUnlock();
  216. #ifndef BUILD_BRIDGE
  217. // -------------------------------------------------------------------
  218. // OSC Stuff
  219. bool isOscControllerRegisted() const;
  220. const char* getOscServerPath() const;
  221. void osc_send_add_plugin(const int32_t pluginId, const char* const pluginName);
  222. void osc_send_remove_plugin(const int32_t pluginId);
  223. void osc_send_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);
  224. void osc_send_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);
  225. void osc_send_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);
  226. void osc_send_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);
  227. void osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  228. void osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  229. void osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  230. void osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  231. void osc_send_set_program(const int32_t pluginId, const int32_t index);
  232. void osc_send_set_program_count(const int32_t pluginId, const int32_t count);
  233. void osc_send_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  234. void osc_send_set_midi_program(const int32_t pluginId, const int32_t index);
  235. void osc_send_set_midi_program_count(const int32_t pluginId, const int32_t count);
  236. void osc_send_set_midi_program_data(const int32_t pluginId, const int32_t index, const int32_t bank, const int32_t program, const char* const name);
  237. void osc_send_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  238. void osc_send_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  239. void osc_send_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  240. void osc_send_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  241. void osc_send_exit();
  242. // -------------------------------------------------------------------
  243. // Rack mode
  244. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  245. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  246. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  247. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  248. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  249. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  250. #endif
  251. // -------------------------------------
  252. /*!
  253. * \class ScopedLocker
  254. *
  255. * \brief Carla engine scoped locker
  256. *
  257. * This is a handy class that temporarily locks an engine during a function scope.
  258. */
  259. class ScopedLocker
  260. {
  261. public:
  262. /*!
  263. * Lock the engine \a engine if \a lock is true.
  264. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  265. *
  266. * \param engine The engine to lock
  267. * \param lock Wherever to lock the engine or not, true by default
  268. */
  269. ScopedLocker(CarlaEngine* const engine, bool lock = true) :
  270. m_engine(engine),
  271. m_lock(lock)
  272. {
  273. if (m_lock)
  274. m_engine->processLock();
  275. }
  276. ~ScopedLocker()
  277. {
  278. if (m_lock)
  279. m_engine->processUnlock();
  280. }
  281. private:
  282. CarlaEngine* const m_engine;
  283. const bool m_lock;
  284. };
  285. // -------------------------------------
  286. protected:
  287. CarlaEngineType type;
  288. const char* name;
  289. uint32_t bufferSize;
  290. double sampleRate;
  291. CarlaTimeInfo timeInfo;
  292. void bufferSizeChanged(uint32_t newBufferSize);
  293. private:
  294. CarlaCheckThread m_checkThread;
  295. #ifndef BUILD_BRIDGE
  296. CarlaOsc m_osc;
  297. const CarlaOscData* m_oscData;
  298. #endif
  299. QMutex m_procLock;
  300. QMutex m_midiLock;
  301. CallbackFunc m_callback;
  302. void* m_callbackPtr;
  303. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  304. const char* m_uniqueNames[MAX_PLUGINS];
  305. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  306. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  307. };
  308. // -----------------------------------------------------------------------
  309. class CarlaEngineClient
  310. {
  311. public:
  312. CarlaEngineClient(const CarlaEngineType& type, const CarlaEngineClientNativeHandle& handle);
  313. ~CarlaEngineClient();
  314. void activate();
  315. void deactivate();
  316. bool isActive() const;
  317. bool isOk() const;
  318. const CarlaEngineBasePort* addPort(const CarlaEnginePortType type, const char* const name, const bool isInput);
  319. private:
  320. bool m_active;
  321. const CarlaEngineType type;
  322. const CarlaEngineClientNativeHandle handle;
  323. };
  324. // -----------------------------------------------------------------------
  325. class CarlaEngineBasePort
  326. {
  327. public:
  328. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  329. virtual ~CarlaEngineBasePort();
  330. virtual void initBuffer(CarlaEngine* const engine) = 0;
  331. protected:
  332. void* buffer;
  333. const bool isInput;
  334. const CarlaEnginePortNativeHandle handle;
  335. };
  336. class CarlaEngineAudioPort : public CarlaEngineBasePort
  337. {
  338. public:
  339. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  340. void initBuffer(CarlaEngine* const engine);
  341. #ifdef CARLA_ENGINE_JACK
  342. float* getJackAudioBuffer(uint32_t nframes);
  343. #endif
  344. };
  345. class CarlaEngineControlPort : public CarlaEngineBasePort
  346. {
  347. public:
  348. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  349. void initBuffer(CarlaEngine* const engine);
  350. uint32_t getEventCount();
  351. const CarlaEngineControlEvent* getEvent(uint32_t index);
  352. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  353. };
  354. class CarlaEngineMidiPort : public CarlaEngineBasePort
  355. {
  356. public:
  357. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  358. void initBuffer(CarlaEngine* const engine);
  359. uint32_t getEventCount();
  360. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  361. void writeEvent(uint32_t time, uint8_t* data, uint8_t size);
  362. };
  363. // -----------------------------------------------------------------------
  364. #ifdef CARLA_ENGINE_JACK
  365. class CarlaEngineJack : public CarlaEngine
  366. {
  367. public:
  368. CarlaEngineJack();
  369. ~CarlaEngineJack();
  370. // -------------------------------------
  371. bool init(const char* const clientName);
  372. bool close();
  373. bool isOnAudioThread();
  374. bool isOffline();
  375. bool isRunning();
  376. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  377. // -------------------------------------
  378. void handleSampleRateCallback(double newSampleRate);
  379. void handleBufferSizeCallback(uint32_t newBufferSize);
  380. void handleFreewheelCallback(bool isFreewheel);
  381. void handleProcessCallback(uint32_t nframes);
  382. void handleShutdownCallback();
  383. // -------------------------------------
  384. private:
  385. jack_client_t* client;
  386. jack_transport_state_t state;
  387. jack_position_t pos;
  388. bool freewheel;
  389. QThread* procThread;
  390. // -------------------------------------
  391. static const unsigned short rackPortAudioIn1 = 0;
  392. static const unsigned short rackPortAudioIn2 = 1;
  393. static const unsigned short rackPortAudioOut1 = 2;
  394. static const unsigned short rackPortAudioOut2 = 3;
  395. static const unsigned short rackPortControlIn = 4;
  396. static const unsigned short rackPortControlOut = 5;
  397. static const unsigned short rackPortMidiIn = 6;
  398. static const unsigned short rackPortMidiOut = 7;
  399. static const unsigned short rackPortCount = 8;
  400. jack_port_t* rackJackPorts[rackPortCount];
  401. };
  402. #endif
  403. // -----------------------------------------------------------------------
  404. #ifdef CARLA_ENGINE_RTAUDIO
  405. class CarlaEngineRtAudio : public CarlaEngine
  406. {
  407. public:
  408. CarlaEngineRtAudio(RtAudio::Api api);
  409. ~CarlaEngineRtAudio();
  410. // -------------------------------------
  411. bool init(const char* const clientName);
  412. bool close();
  413. bool isOnAudioThread();
  414. bool isOffline();
  415. bool isRunning();
  416. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  417. // -------------------------------------
  418. void handleProcessCallback(void* outputBuffer, void* inputBuffer, unsigned int nframes, double streamTime, RtAudioStreamStatus status);
  419. private:
  420. RtAudio adac;
  421. QThread* procThread;
  422. };
  423. #endif
  424. /**@}*/
  425. CARLA_BACKEND_END_NAMESPACE
  426. #endif // CARLA_ENGINE_H