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.

555 lines
18KB

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