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.

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