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.

554 lines
17KB

  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. // -------------------------------------------------------------------
  217. // OSC Stuff
  218. bool isOscControllerRegisted() const;
  219. #ifndef BUILD_BRIDGE
  220. const char* getOscServerPath() const;
  221. #else
  222. void setOscBridgeData(const CarlaOscData* const oscData);
  223. #endif
  224. void osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  225. void osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  226. void osc_send_set_program(const int32_t pluginId, const int32_t index);
  227. void osc_send_set_midi_program(const int32_t pluginId, const int32_t index);
  228. void osc_send_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  229. void osc_send_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  230. void osc_send_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  231. void osc_send_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  232. #ifndef BUILD_BRIDGE
  233. void osc_send_add_plugin(const int32_t pluginId, const char* const pluginName);
  234. void osc_send_remove_plugin(const int32_t pluginId);
  235. 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);
  236. 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);
  237. 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);
  238. 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);
  239. void osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  240. void osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  241. void osc_send_set_program_count(const int32_t pluginId, const int32_t count);
  242. void osc_send_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  243. void osc_send_set_midi_program_count(const int32_t pluginId, const int32_t count);
  244. 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);
  245. void osc_send_exit();
  246. #else
  247. void osc_send_bridge_audio_count(const int32_t ins, const int32_t outs, const int32_t total);
  248. void osc_send_bridge_midi_count(const int32_t ins, const int32_t outs, const int32_t total);
  249. void osc_send_bridge_param_count(const int32_t ins, const int32_t outs, const int32_t total);
  250. void osc_send_bridge_program_count(const int32_t count);
  251. void osc_send_bridge_midi_program_count(const int32_t count);
  252. 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 long uniqueId);
  253. void osc_send_bridge_param_info(const int32_t index, const char* const name, const char* const unit);
  254. void osc_send_bridge_param_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);
  255. void osc_send_bridge_param_ranges(const int32_t index, const double def, const double min, const double max, const double step, const double stepSmall, const double stepLarge);
  256. void osc_send_bridge_program_info(const int32_t index, const char* const name);
  257. void osc_send_bridge_midi_program_info(const int32_t index, const int32_t bank, const int32_t program, const char* const label);
  258. void osc_send_bridge_custom_data(const char* const stype, const char* const key, const char* const value);
  259. void osc_send_bridge_chunk_data(const char* const stringData);
  260. void osc_send_bridge_update();
  261. #endif
  262. #ifndef BUILD_BRIDGE
  263. // -------------------------------------------------------------------
  264. // Rack mode
  265. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  266. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  267. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  268. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  269. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  270. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  271. #endif
  272. // -------------------------------------
  273. /*!
  274. * \class ScopedLocker
  275. *
  276. * \brief Carla engine scoped locker
  277. *
  278. * This is a handy class that temporarily locks an engine during a function scope.
  279. */
  280. class ScopedLocker
  281. {
  282. public:
  283. /*!
  284. * Lock the engine \a engine if \a lock is true.
  285. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  286. *
  287. * \param engine The engine to lock
  288. * \param lock Wherever to lock the engine or not, true by default
  289. */
  290. ScopedLocker(CarlaEngine* const engine, bool lock = true) :
  291. m_engine(engine),
  292. m_lock(lock)
  293. {
  294. if (m_lock)
  295. m_engine->processLock();
  296. }
  297. ~ScopedLocker()
  298. {
  299. if (m_lock)
  300. m_engine->processUnlock();
  301. }
  302. private:
  303. CarlaEngine* const m_engine;
  304. const bool m_lock;
  305. };
  306. // -------------------------------------
  307. protected:
  308. CarlaEngineType type;
  309. const char* name;
  310. uint32_t bufferSize;
  311. double sampleRate;
  312. CarlaTimeInfo timeInfo;
  313. unsigned short maxPluginNumber;
  314. void bufferSizeChanged(uint32_t newBufferSize);
  315. private:
  316. CarlaCheckThread m_checkThread;
  317. #ifndef BUILD_BRIDGE
  318. CarlaOsc m_osc;
  319. #endif
  320. const CarlaOscData* m_oscData;
  321. QMutex m_procLock;
  322. QMutex m_midiLock;
  323. CallbackFunc m_callback;
  324. void* m_callbackPtr;
  325. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  326. const char* m_uniqueNames[MAX_PLUGINS];
  327. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  328. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  329. };
  330. // -----------------------------------------------------------------------
  331. class CarlaEngineClient
  332. {
  333. public:
  334. CarlaEngineClient(const CarlaEngineType& type, const CarlaEngineClientNativeHandle& handle);
  335. ~CarlaEngineClient();
  336. void activate();
  337. void deactivate();
  338. bool isActive() const;
  339. bool isOk() const;
  340. const CarlaEngineBasePort* addPort(const CarlaEnginePortType type, const char* const name, const bool isInput);
  341. private:
  342. bool m_active;
  343. const CarlaEngineType type;
  344. const CarlaEngineClientNativeHandle handle;
  345. };
  346. // -----------------------------------------------------------------------
  347. class CarlaEngineBasePort
  348. {
  349. public:
  350. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  351. virtual ~CarlaEngineBasePort();
  352. virtual void initBuffer(CarlaEngine* const engine) = 0;
  353. protected:
  354. void* buffer;
  355. const bool isInput;
  356. const CarlaEnginePortNativeHandle handle;
  357. };
  358. class CarlaEngineAudioPort : public CarlaEngineBasePort
  359. {
  360. public:
  361. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  362. void initBuffer(CarlaEngine* const engine);
  363. #ifdef CARLA_ENGINE_JACK
  364. float* getJackAudioBuffer(uint32_t nframes);
  365. #endif
  366. };
  367. class CarlaEngineControlPort : public CarlaEngineBasePort
  368. {
  369. public:
  370. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  371. void initBuffer(CarlaEngine* const engine);
  372. uint32_t getEventCount();
  373. const CarlaEngineControlEvent* getEvent(uint32_t index);
  374. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  375. };
  376. class CarlaEngineMidiPort : public CarlaEngineBasePort
  377. {
  378. public:
  379. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  380. void initBuffer(CarlaEngine* const engine);
  381. uint32_t getEventCount();
  382. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  383. void writeEvent(uint32_t time, const uint8_t* data, uint8_t size);
  384. };
  385. // -----------------------------------------------------------------------
  386. #ifdef CARLA_ENGINE_JACK
  387. class CarlaEngineJack : public CarlaEngine
  388. {
  389. public:
  390. CarlaEngineJack();
  391. ~CarlaEngineJack();
  392. // -------------------------------------
  393. bool init(const char* const clientName);
  394. bool close();
  395. bool isOnAudioThread();
  396. bool isOffline();
  397. bool isRunning();
  398. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  399. // -------------------------------------
  400. void handleSampleRateCallback(double newSampleRate);
  401. void handleBufferSizeCallback(uint32_t newBufferSize);
  402. void handleFreewheelCallback(bool isFreewheel);
  403. void handleProcessCallback(uint32_t nframes);
  404. void handleShutdownCallback();
  405. // -------------------------------------
  406. private:
  407. jack_client_t* client;
  408. jack_transport_state_t state;
  409. jack_position_t pos;
  410. bool freewheel;
  411. QThread* procThread;
  412. // -------------------------------------
  413. static const unsigned short rackPortAudioIn1 = 0;
  414. static const unsigned short rackPortAudioIn2 = 1;
  415. static const unsigned short rackPortAudioOut1 = 2;
  416. static const unsigned short rackPortAudioOut2 = 3;
  417. static const unsigned short rackPortControlIn = 4;
  418. static const unsigned short rackPortControlOut = 5;
  419. static const unsigned short rackPortMidiIn = 6;
  420. static const unsigned short rackPortMidiOut = 7;
  421. static const unsigned short rackPortCount = 8;
  422. jack_port_t* rackJackPorts[rackPortCount];
  423. };
  424. #endif
  425. // -----------------------------------------------------------------------
  426. #ifdef CARLA_ENGINE_RTAUDIO
  427. class CarlaEngineRtAudio : public CarlaEngine
  428. {
  429. public:
  430. CarlaEngineRtAudio(RtAudio::Api api);
  431. ~CarlaEngineRtAudio();
  432. // -------------------------------------
  433. bool init(const char* const clientName);
  434. bool close();
  435. bool isOnAudioThread();
  436. bool isOffline();
  437. bool isRunning();
  438. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  439. // -------------------------------------
  440. void handleProcessCallback(void* outputBuffer, void* inputBuffer, unsigned int nframes, double streamTime, RtAudioStreamStatus status);
  441. private:
  442. RtAudio adac;
  443. QThread* procThread;
  444. };
  445. #endif
  446. /**@}*/
  447. CARLA_BACKEND_END_NAMESPACE
  448. #endif // CARLA_ENGINE_H