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.

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