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.

564 lines
18KB

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