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.

530 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. unsigned short maxPluginNumber;
  293. void bufferSizeChanged(uint32_t newBufferSize);
  294. private:
  295. CarlaCheckThread m_checkThread;
  296. #ifndef BUILD_BRIDGE
  297. CarlaOsc m_osc;
  298. const CarlaOscData* m_oscData;
  299. #endif
  300. QMutex m_procLock;
  301. QMutex m_midiLock;
  302. CallbackFunc m_callback;
  303. void* m_callbackPtr;
  304. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  305. const char* m_uniqueNames[MAX_PLUGINS];
  306. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  307. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  308. };
  309. // -----------------------------------------------------------------------
  310. class CarlaEngineClient
  311. {
  312. public:
  313. CarlaEngineClient(const CarlaEngineType& type, const CarlaEngineClientNativeHandle& handle);
  314. ~CarlaEngineClient();
  315. void activate();
  316. void deactivate();
  317. bool isActive() const;
  318. bool isOk() const;
  319. const CarlaEngineBasePort* addPort(const CarlaEnginePortType type, const char* const name, const bool isInput);
  320. private:
  321. bool m_active;
  322. const CarlaEngineType type;
  323. const CarlaEngineClientNativeHandle handle;
  324. };
  325. // -----------------------------------------------------------------------
  326. class CarlaEngineBasePort
  327. {
  328. public:
  329. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  330. virtual ~CarlaEngineBasePort();
  331. virtual void initBuffer(CarlaEngine* const engine) = 0;
  332. protected:
  333. void* buffer;
  334. const bool isInput;
  335. const CarlaEnginePortNativeHandle handle;
  336. };
  337. class CarlaEngineAudioPort : public CarlaEngineBasePort
  338. {
  339. public:
  340. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  341. void initBuffer(CarlaEngine* const engine);
  342. #ifdef CARLA_ENGINE_JACK
  343. float* getJackAudioBuffer(uint32_t nframes);
  344. #endif
  345. };
  346. class CarlaEngineControlPort : public CarlaEngineBasePort
  347. {
  348. public:
  349. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  350. void initBuffer(CarlaEngine* const engine);
  351. uint32_t getEventCount();
  352. const CarlaEngineControlEvent* getEvent(uint32_t index);
  353. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  354. };
  355. class CarlaEngineMidiPort : public CarlaEngineBasePort
  356. {
  357. public:
  358. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  359. void initBuffer(CarlaEngine* const engine);
  360. uint32_t getEventCount();
  361. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  362. void writeEvent(uint32_t time, const uint8_t* data, uint8_t size);
  363. };
  364. // -----------------------------------------------------------------------
  365. #ifdef CARLA_ENGINE_JACK
  366. class CarlaEngineJack : public CarlaEngine
  367. {
  368. public:
  369. CarlaEngineJack();
  370. ~CarlaEngineJack();
  371. // -------------------------------------
  372. bool init(const char* const clientName);
  373. bool close();
  374. bool isOnAudioThread();
  375. bool isOffline();
  376. bool isRunning();
  377. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  378. // -------------------------------------
  379. void handleSampleRateCallback(double newSampleRate);
  380. void handleBufferSizeCallback(uint32_t newBufferSize);
  381. void handleFreewheelCallback(bool isFreewheel);
  382. void handleProcessCallback(uint32_t nframes);
  383. void handleShutdownCallback();
  384. // -------------------------------------
  385. private:
  386. jack_client_t* client;
  387. jack_transport_state_t state;
  388. jack_position_t pos;
  389. bool freewheel;
  390. QThread* procThread;
  391. // -------------------------------------
  392. static const unsigned short rackPortAudioIn1 = 0;
  393. static const unsigned short rackPortAudioIn2 = 1;
  394. static const unsigned short rackPortAudioOut1 = 2;
  395. static const unsigned short rackPortAudioOut2 = 3;
  396. static const unsigned short rackPortControlIn = 4;
  397. static const unsigned short rackPortControlOut = 5;
  398. static const unsigned short rackPortMidiIn = 6;
  399. static const unsigned short rackPortMidiOut = 7;
  400. static const unsigned short rackPortCount = 8;
  401. jack_port_t* rackJackPorts[rackPortCount];
  402. };
  403. #endif
  404. // -----------------------------------------------------------------------
  405. #ifdef CARLA_ENGINE_RTAUDIO
  406. class CarlaEngineRtAudio : public CarlaEngine
  407. {
  408. public:
  409. CarlaEngineRtAudio(RtAudio::Api api);
  410. ~CarlaEngineRtAudio();
  411. // -------------------------------------
  412. bool init(const char* const clientName);
  413. bool close();
  414. bool isOnAudioThread();
  415. bool isOffline();
  416. bool isRunning();
  417. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  418. // -------------------------------------
  419. void handleProcessCallback(void* outputBuffer, void* inputBuffer, unsigned int nframes, double streamTime, RtAudioStreamStatus status);
  420. private:
  421. RtAudio adac;
  422. QThread* procThread;
  423. };
  424. #endif
  425. /**@}*/
  426. CARLA_BACKEND_END_NAMESPACE
  427. #endif // CARLA_ENGINE_H