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.

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