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.

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