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 "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. };
  49. enum CarlaEnginePortType {
  50. CarlaEnginePortTypeAudio,
  51. CarlaEnginePortTypeControl,
  52. CarlaEnginePortTypeMIDI
  53. };
  54. enum CarlaEngineControlEventType {
  55. CarlaEngineEventNull = 0,
  56. CarlaEngineEventControlChange,
  57. CarlaEngineEventMidiBankChange,
  58. CarlaEngineEventMidiProgramChange,
  59. CarlaEngineEventAllSoundOff,
  60. CarlaEngineEventAllNotesOff
  61. };
  62. struct CarlaEngineControlEvent {
  63. CarlaEngineControlEventType type;
  64. uint32_t time;
  65. uint8_t channel;
  66. uint16_t controller;
  67. double value;
  68. CarlaEngineControlEvent()
  69. : type(CarlaEngineEventNull),
  70. time(0),
  71. channel(0),
  72. controller(0),
  73. value(0.0) {}
  74. };
  75. struct CarlaEngineMidiEvent {
  76. uint32_t time;
  77. uint8_t size;
  78. uint8_t data[4];
  79. CarlaEngineMidiEvent()
  80. : time(0),
  81. #ifdef Q_COMPILER_INITIALIZER_LISTS
  82. size(0),
  83. data{0} {}
  84. #else
  85. size(0) { memset(data, 0, sizeof(uint8_t)*4); }
  86. #endif
  87. };
  88. struct CarlaTimeInfo {
  89. bool playing;
  90. uint32_t frame;
  91. uint32_t time;
  92. uint32_t valid;
  93. struct {
  94. int32_t bar;
  95. int32_t beat;
  96. int32_t tick;
  97. double bar_start_tick;
  98. float beats_per_bar;
  99. float beat_type;
  100. double ticks_per_beat;
  101. double beats_per_minute;
  102. } bbt;
  103. CarlaTimeInfo()
  104. : playing(false),
  105. frame(0),
  106. time(0),
  107. #ifdef Q_COMPILER_INITIALIZER_LISTS
  108. valid(0),
  109. bbt{0, 0, 0, 0.0, 0.0f, 0.0f, 0.0, 0.0} {}
  110. #else
  111. valid(0)
  112. {
  113. bbt.bar = bbt.beat = bbt.tick = 0;
  114. bbt.beats_per_bar = bbt.beat_type = 0.0f;
  115. bbt.bar_start_tick = bbt.ticks_per_beat = bbt.beats_per_minute = 0.0;
  116. }
  117. #endif
  118. };
  119. struct CarlaEngineClientNativeHandle {
  120. #ifdef CARLA_ENGINE_JACK
  121. jack_client_t* jackClient;
  122. #endif
  123. #ifdef CARLA_ENGINE_RTAUDIO
  124. RtAudio* rtAudioPtr;
  125. #endif
  126. CarlaEngineClientNativeHandle()
  127. {
  128. #ifdef CARLA_ENGINE_JACK
  129. jackClient = nullptr;
  130. #endif
  131. #ifdef CARLA_ENGINE_RTAUDIO
  132. rtAudioPtr = nullptr;
  133. #endif
  134. }
  135. };
  136. struct CarlaEnginePortNativeHandle {
  137. #ifdef CARLA_ENGINE_JACK
  138. jack_client_t* jackClient;
  139. jack_port_t* jackPort;
  140. #endif
  141. CarlaEnginePortNativeHandle()
  142. {
  143. #ifdef CARLA_ENGINE_JACK
  144. jackClient = nullptr;
  145. jackPort = nullptr;
  146. #endif
  147. }
  148. };
  149. // -----------------------------------------------------------------------
  150. class CarlaEngineClient;
  151. class CarlaEngineBasePort;
  152. /*!
  153. * \class CarlaEngine
  154. *
  155. * \brief Carla Backend base engine class
  156. *
  157. * This is the base class for all available engine types available in Carla Backend.
  158. */
  159. class CarlaEngine
  160. {
  161. public:
  162. CarlaEngine();
  163. virtual ~CarlaEngine();
  164. // -------------------------------------------------------------------
  165. // Static values
  166. static const unsigned short MAX_PEAKS = 2;
  167. static int maxClientNameSize();
  168. static int maxPortNameSize();
  169. static unsigned short maxPluginNumber();
  170. // -------------------------------------------------------------------
  171. // Virtual, per-engine type calls
  172. virtual bool init(const char* const clientName);
  173. virtual bool close();
  174. virtual bool isOffline() = 0;
  175. virtual bool isRunning() = 0;
  176. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  177. // -------------------------------------------------------------------
  178. // Plugin management
  179. short getNewPluginId() const;
  180. CarlaPlugin* getPlugin(const unsigned short id) const;
  181. CarlaPlugin* getPluginUnchecked(const unsigned short id) const;
  182. const char* getUniqueName(const char* const name);
  183. short addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  184. short addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  185. bool removePlugin(const unsigned short id);
  186. void removeAllPlugins();
  187. void idlePluginGuis();
  188. // bridge, internal use only
  189. void __bridgePluginRegister(const unsigned short id, CarlaPlugin* const plugin)
  190. {
  191. m_carlaPlugins[id] = plugin;
  192. }
  193. // -------------------------------------------------------------------
  194. // Information (base)
  195. const char* getName() const;
  196. double getSampleRate() const;
  197. uint32_t getBufferSize() const;
  198. const CarlaTimeInfo* getTimeInfo() const;
  199. // -------------------------------------------------------------------
  200. // Information (audio peaks)
  201. double getInputPeak(const unsigned short pluginId, const unsigned short id) const;
  202. double getOutputPeak(const unsigned short pluginId, const unsigned short id) const;
  203. void setInputPeak(const unsigned short pluginId, const unsigned short id, double value);
  204. void setOutputPeak(const unsigned short pluginId, const unsigned short id, double value);
  205. // -------------------------------------------------------------------
  206. // Callback
  207. void callback(const CallbackType action, const unsigned short pluginId, const int value1, const int value2, const double value3);
  208. void setCallback(const CallbackFunc func, void* const ptr);
  209. // -------------------------------------------------------------------
  210. // Mutex locks
  211. void processLock();
  212. void processUnlock();
  213. void midiLock();
  214. void midiUnlock();
  215. // -------------------------------------------------------------------
  216. // OSC Stuff
  217. #ifndef BUILD_BRIDGE
  218. bool isOscControllerRegisted() const;
  219. const char* getOscServerPath() const;
  220. #else
  221. void setOscBridgeData(const CarlaOscData* const oscData);
  222. #endif
  223. #ifdef BUILD_BRIDGE
  224. void osc_send_bridge_audio_count(const int32_t ins, const int32_t outs, const int32_t total);
  225. void osc_send_bridge_midi_count(const int32_t ins, const int32_t outs, const int32_t total);
  226. void osc_send_bridge_parameter_count(const int32_t ins, const int32_t outs, const int32_t total);
  227. void osc_send_bridge_program_count(const int32_t count);
  228. void osc_send_bridge_midi_program_count(const int32_t count);
  229. 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);
  230. void osc_send_bridge_parameter_info(const int32_t index, const char* const name, const char* const unit);
  231. 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);
  232. 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);
  233. void osc_send_bridge_program_info(const int32_t index, const char* const name);
  234. void osc_send_bridge_midi_program_info(const int32_t index, const int32_t bank, const int32_t program, const char* const label);
  235. void osc_send_bridge_set_parameter_value(const int32_t index, const double value);
  236. void osc_send_bridge_set_default_value(const int32_t index, const double value);
  237. void osc_send_bridge_set_program(const int32_t index);
  238. void osc_send_bridge_set_midi_program(const int32_t index);
  239. void osc_send_bridge_set_input_peak_value(const int32_t portId, const double value);
  240. void osc_send_bridge_set_output_peak_value(const int32_t portId, const double value);
  241. //void osc_send_bridge_custom_data(const char* const stype, const char* const key, const char* const value);
  242. //void osc_send_bridge_chunk_data(const char* const stringData);
  243. #else
  244. void osc_send_control_add_plugin(const int32_t pluginId, const char* const pluginName);
  245. void osc_send_control_remove_plugin(const int32_t pluginId);
  246. 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);
  247. 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);
  248. 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);
  249. 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);
  250. void osc_send_control_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  251. void osc_send_control_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  252. void osc_send_control_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  253. void osc_send_control_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  254. void osc_send_control_set_program(const int32_t pluginId, const int32_t index);
  255. void osc_send_control_set_program_count(const int32_t pluginId, const int32_t count);
  256. void osc_send_control_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  257. void osc_send_control_set_midi_program(const int32_t pluginId, const int32_t index);
  258. void osc_send_control_set_midi_program_count(const int32_t pluginId, const int32_t count);
  259. 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);
  260. void osc_send_control_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  261. void osc_send_control_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  262. void osc_send_control_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  263. void osc_send_control_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  264. void osc_send_control_exit();
  265. #endif
  266. #ifndef BUILD_BRIDGE
  267. // -------------------------------------------------------------------
  268. // Rack mode
  269. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  270. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  271. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  272. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  273. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  274. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  275. #endif
  276. // -------------------------------------
  277. /*!
  278. * \class ScopedLocker
  279. *
  280. * \brief Carla engine scoped locker
  281. *
  282. * This is a handy class that temporarily locks an engine during a function scope.
  283. */
  284. class ScopedLocker
  285. {
  286. public:
  287. /*!
  288. * Lock the engine \a engine if \a lock is true.
  289. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  290. *
  291. * \param engine The engine to lock
  292. * \param lock Wherever to lock the engine or not, true by default
  293. */
  294. ScopedLocker(CarlaEngine* const engine, bool lock = true) :
  295. m_engine(engine),
  296. m_lock(lock)
  297. {
  298. if (m_lock)
  299. m_engine->processLock();
  300. }
  301. ~ScopedLocker()
  302. {
  303. if (m_lock)
  304. m_engine->processUnlock();
  305. }
  306. private:
  307. CarlaEngine* const m_engine;
  308. const bool m_lock;
  309. };
  310. // -------------------------------------
  311. protected:
  312. CarlaEngineType type;
  313. const char* name;
  314. uint32_t bufferSize;
  315. double sampleRate;
  316. CarlaTimeInfo timeInfo;
  317. void bufferSizeChanged(uint32_t newBufferSize);
  318. private:
  319. CarlaCheckThread m_checkThread;
  320. #ifndef BUILD_BRIDGE
  321. CarlaOsc m_osc;
  322. #endif
  323. const CarlaOscData* m_oscData;
  324. QMutex m_procLock;
  325. QMutex m_midiLock;
  326. CallbackFunc m_callback;
  327. void* m_callbackPtr;
  328. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  329. const char* m_uniqueNames[MAX_PLUGINS];
  330. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  331. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  332. static unsigned short m_maxPluginNumber;
  333. };
  334. // -----------------------------------------------------------------------
  335. class CarlaEngineClient
  336. {
  337. public:
  338. CarlaEngineClient(const CarlaEngineType& type, const CarlaEngineClientNativeHandle& handle);
  339. ~CarlaEngineClient();
  340. void activate();
  341. void deactivate();
  342. bool isActive() const;
  343. bool isOk() const;
  344. const CarlaEngineBasePort* addPort(const CarlaEnginePortType type, const char* const name, const bool isInput);
  345. private:
  346. bool m_active;
  347. const CarlaEngineType type;
  348. const CarlaEngineClientNativeHandle handle;
  349. };
  350. // -----------------------------------------------------------------------
  351. class CarlaEngineBasePort
  352. {
  353. public:
  354. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  355. virtual ~CarlaEngineBasePort();
  356. virtual void initBuffer(CarlaEngine* const engine) = 0;
  357. protected:
  358. void* buffer;
  359. const bool isInput;
  360. const CarlaEnginePortNativeHandle handle;
  361. };
  362. class CarlaEngineAudioPort : public CarlaEngineBasePort
  363. {
  364. public:
  365. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  366. void initBuffer(CarlaEngine* const engine);
  367. #ifdef CARLA_ENGINE_JACK
  368. float* getJackAudioBuffer(uint32_t nframes);
  369. #endif
  370. };
  371. class CarlaEngineControlPort : public CarlaEngineBasePort
  372. {
  373. public:
  374. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  375. void initBuffer(CarlaEngine* const engine);
  376. uint32_t getEventCount();
  377. const CarlaEngineControlEvent* getEvent(uint32_t index);
  378. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  379. };
  380. class CarlaEngineMidiPort : public CarlaEngineBasePort
  381. {
  382. public:
  383. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, const bool isInput);
  384. void initBuffer(CarlaEngine* const engine);
  385. uint32_t getEventCount();
  386. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  387. void writeEvent(uint32_t time, const uint8_t* data, uint8_t size);
  388. };
  389. // -----------------------------------------------------------------------
  390. #ifdef CARLA_ENGINE_JACK
  391. class CarlaEngineJack : public CarlaEngine
  392. {
  393. public:
  394. CarlaEngineJack();
  395. ~CarlaEngineJack();
  396. // -------------------------------------
  397. bool init(const char* const clientName);
  398. bool close();
  399. bool isOnAudioThread();
  400. bool isOffline();
  401. bool isRunning();
  402. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  403. // -------------------------------------
  404. void handleSampleRateCallback(double newSampleRate);
  405. void handleBufferSizeCallback(uint32_t newBufferSize);
  406. void handleFreewheelCallback(bool isFreewheel);
  407. void handleProcessCallback(uint32_t nframes);
  408. void handleShutdownCallback();
  409. // -------------------------------------
  410. private:
  411. jack_client_t* client;
  412. jack_transport_state_t state;
  413. jack_position_t pos;
  414. bool freewheel;
  415. // -------------------------------------
  416. static const unsigned short rackPortAudioIn1 = 0;
  417. static const unsigned short rackPortAudioIn2 = 1;
  418. static const unsigned short rackPortAudioOut1 = 2;
  419. static const unsigned short rackPortAudioOut2 = 3;
  420. static const unsigned short rackPortControlIn = 4;
  421. static const unsigned short rackPortControlOut = 5;
  422. static const unsigned short rackPortMidiIn = 6;
  423. static const unsigned short rackPortMidiOut = 7;
  424. static const unsigned short rackPortCount = 8;
  425. jack_port_t* rackJackPorts[rackPortCount];
  426. };
  427. #endif
  428. // -----------------------------------------------------------------------
  429. #ifdef CARLA_ENGINE_RTAUDIO
  430. class CarlaEngineRtAudio : public CarlaEngine
  431. {
  432. public:
  433. CarlaEngineRtAudio(RtAudio::Api api);
  434. ~CarlaEngineRtAudio();
  435. // -------------------------------------
  436. bool init(const char* const clientName);
  437. bool close();
  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. };
  446. #endif
  447. /**@}*/
  448. CARLA_BACKEND_END_NAMESPACE
  449. #endif // CARLA_ENGINE_H