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.

578 lines
16KB

  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. #include <cassert>
  23. #include <QtCore/QMutex>
  24. #ifdef CARLA_ENGINE_JACK
  25. #include <jack/jack.h>
  26. #include <jack/midiport.h>
  27. #endif
  28. #ifdef CARLA_ENGINE_RTAUDIO
  29. #include "RtAudio.h"
  30. #include "RtMidi.h"
  31. #endif
  32. CARLA_BACKEND_START_NAMESPACE
  33. #if 0
  34. } /* adjust editor indent */
  35. #endif
  36. /*!
  37. * @defgroup CarlaBackendEngine Carla Backend Engine
  38. *
  39. * The Carla Backend Engine
  40. * @{
  41. */
  42. /*!
  43. * @defgroup TimeInfoValidHints TimeInfo Valid Hints
  44. *
  45. * Various hints used for CarlaTimeInfo::valid.
  46. * @{
  47. */
  48. const uint32_t CarlaEngineTimeBBT = 0x1;
  49. /**@}*/
  50. enum CarlaEnginePortType {
  51. CarlaEnginePortTypeAudio,
  52. CarlaEnginePortTypeControl,
  53. CarlaEnginePortTypeMIDI
  54. };
  55. enum CarlaEngineControlEventType {
  56. CarlaEngineEventNull = 0,
  57. CarlaEngineEventControlChange,
  58. CarlaEngineEventMidiBankChange,
  59. CarlaEngineEventMidiProgramChange,
  60. CarlaEngineEventAllSoundOff,
  61. CarlaEngineEventAllNotesOff
  62. };
  63. struct CarlaEngineControlEvent {
  64. CarlaEngineControlEventType type;
  65. uint32_t time;
  66. uint8_t channel;
  67. uint16_t controller;
  68. double value;
  69. CarlaEngineControlEvent()
  70. : type(CarlaEngineEventNull),
  71. time(0),
  72. channel(0),
  73. controller(0),
  74. value(0.0) {}
  75. };
  76. struct CarlaEngineMidiEvent {
  77. uint32_t time;
  78. uint8_t size;
  79. uint8_t data[4];
  80. CarlaEngineMidiEvent()
  81. : time(0),
  82. size(0),
  83. data{0} {}
  84. };
  85. struct CarlaTimeInfo {
  86. bool playing;
  87. uint32_t frame;
  88. uint32_t time;
  89. uint32_t valid;
  90. struct {
  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. } bbt;
  100. CarlaTimeInfo()
  101. : playing(false),
  102. frame(0),
  103. time(0),
  104. valid(0),
  105. bbt{0, 0, 0, 0.0, 0.0f, 0.0f, 0.0, 0.0} {}
  106. };
  107. struct CarlaEngineClientNativeHandle {
  108. #ifdef CARLA_ENGINE_JACK
  109. jack_client_t* client;
  110. #endif
  111. };
  112. struct CarlaEnginePortNativeHandle {
  113. #ifdef CARLA_ENGINE_JACK
  114. jack_client_t* client;
  115. jack_port_t* port;
  116. #endif
  117. };
  118. // -----------------------------------------------------------------------
  119. class CarlaEngineClient;
  120. class CarlaEngineBasePort;
  121. /*!
  122. * \class CarlaEngine
  123. *
  124. * \brief Carla Backend base engine class
  125. *
  126. * This is the base class for all available engine types available in Carla Backend.
  127. */
  128. class CarlaEngine
  129. {
  130. public:
  131. CarlaEngine();
  132. virtual ~CarlaEngine();
  133. // -------------------------------------------------------------------
  134. // static values
  135. static const unsigned short MAX_PEAKS = 2;
  136. static int maxClientNameSize();
  137. static int maxPortNameSize();
  138. // -------------------------------------------------------------------
  139. // virtual, per-engine type calls
  140. virtual bool init(const char* const clientName)
  141. {
  142. m_checkThread.start(QThread::HighPriority);
  143. m_osc.init(clientName);
  144. return true;
  145. }
  146. virtual bool close()
  147. {
  148. if (m_checkThread.isRunning())
  149. m_checkThread.stopNow();
  150. m_osc.close();
  151. return true;
  152. }
  153. virtual bool isOnAudioThread() = 0;
  154. virtual bool isOffline() = 0;
  155. virtual bool isRunning() = 0;
  156. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  157. // -------------------------------------------------------------------
  158. // Plugin management
  159. short getNewPluginId() const;
  160. CarlaPlugin* getPlugin(const unsigned short id) const;
  161. const char* getUniqueName(const char* const name);
  162. short addPlugin(const BinaryType btype, const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  163. short addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, void* const extra = nullptr);
  164. bool removePlugin(const unsigned short id);
  165. void removeAllPlugins();
  166. void idlePluginGuis();
  167. // bridge, internal use only
  168. void __bridgePluginRegister(const unsigned short id, CarlaPlugin* const plugin)
  169. {
  170. m_carlaPlugins[id] = plugin;
  171. }
  172. // -------------------------------------------------------------------
  173. // Information (base)
  174. const char* getName() const
  175. {
  176. return name;
  177. }
  178. double getSampleRate() const
  179. {
  180. return sampleRate;
  181. }
  182. uint32_t getBufferSize() const
  183. {
  184. return bufferSize;
  185. }
  186. const CarlaTimeInfo* getTimeInfo() const
  187. {
  188. return &timeInfo;
  189. }
  190. // -------------------------------------------------------------------
  191. // Information (audio peaks)
  192. double getInputPeak(const unsigned short pluginId, const unsigned short id) const
  193. {
  194. assert(pluginId < MAX_PLUGINS);
  195. assert(id < MAX_PEAKS);
  196. return m_insPeak[pluginId*MAX_PEAKS + id];
  197. }
  198. double getOutputPeak(const unsigned short pluginId, const unsigned short id) const
  199. {
  200. assert(pluginId < MAX_PLUGINS);
  201. assert(id < MAX_PEAKS);
  202. return m_outsPeak[pluginId*MAX_PEAKS + id];
  203. }
  204. void setInputPeak(const unsigned short pluginId, const unsigned short id, double value)
  205. {
  206. assert(pluginId < MAX_PLUGINS);
  207. assert(id < MAX_PEAKS);
  208. m_insPeak[pluginId*MAX_PEAKS + id] = value;
  209. }
  210. void setOutputPeak(const unsigned short pluginId, const unsigned short id, double value)
  211. {
  212. assert(pluginId < MAX_PLUGINS);
  213. assert(id < MAX_PEAKS);
  214. m_outsPeak[pluginId*MAX_PEAKS + id] = value;
  215. }
  216. // -------------------------------------------------------------------
  217. // Callback
  218. void callback(const CallbackType action, const unsigned short pluginId, const int value1, const int value2, const double value3)
  219. {
  220. qDebug("CarlaEngine::callback(%s, %i, %i, %i, %f)", CallbackType2str(action), pluginId, value1, value2, value3);
  221. if (m_callback)
  222. m_callback(action, pluginId, value1, value2, value3);
  223. }
  224. void setCallback(const CallbackFunc func)
  225. {
  226. qDebug("CarlaEngine::setCallback(%p)", func);
  227. m_callback = func;
  228. }
  229. // -------------------------------------------------------------------
  230. // mutex locks
  231. void processLock()
  232. {
  233. m_procLock.lock();
  234. }
  235. void processUnlock()
  236. {
  237. m_procLock.unlock();
  238. }
  239. void midiLock()
  240. {
  241. m_midiLock.lock();
  242. }
  243. void midiUnlock()
  244. {
  245. m_midiLock.unlock();
  246. }
  247. // -------------------------------------------------------------------
  248. // OSC Stuff
  249. bool isOscControllerRegisted() const
  250. {
  251. return m_osc.isControllerRegistered();
  252. }
  253. const char* getOscServerPath() const
  254. {
  255. return m_osc.getServerPath();
  256. }
  257. void osc_send_add_plugin(const int32_t pluginId, const char* const pluginName);
  258. void osc_send_remove_plugin(const int32_t pluginId);
  259. void osc_send_set_plugin_data(const int32_t pluginId, const int32_t type, const int32_t category, const int32_t hints,
  260. const char* const realName, const char* const label, const char* const maker, const char* const copyright, const int64_t uniqueId);
  261. void osc_send_set_plugin_ports(const int32_t pluginId, const int32_t audioIns, const int32_t audioOuts, const int32_t midiIns, const int32_t midiOuts,
  262. const int32_t cIns, const int32_t cOuts, const int32_t cTotals);
  263. void osc_send_set_parameter_data(const int32_t pluginId, const int32_t index, const int32_t type, const int32_t hints,
  264. const char* const name, const char* const label, const double current);
  265. void osc_send_set_parameter_ranges(const int32_t pluginId, const int32_t index, const double min, const double max, const double def,
  266. const double step, const double stepSmall, const double stepLarge);
  267. void osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  268. void osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  269. void osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  270. void osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  271. void osc_send_set_program(const int32_t pluginId, const int32_t index);
  272. void osc_send_set_program_count(int32_t pluginId, int program_count);
  273. void osc_send_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  274. void osc_send_set_midi_program(const int32_t pluginId, const int32_t index);
  275. void osc_send_set_midi_program_count(const int32_t pluginId, const int32_t count);
  276. void osc_send_set_midi_program_data(const int32_t pluginId, const int32_t index, const int32_t bank, const int32_t program, const char* const name);
  277. void osc_send_set_input_peak_value(int32_t pluginId, int port_id, double value);
  278. void osc_send_set_output_peak_value(int32_t pluginId, int port_id, double value);
  279. void osc_send_note_on(int32_t pluginId, int channel, int note, int velo);
  280. void osc_send_note_off(int32_t pluginId, int channel, int note);
  281. void osc_send_exit();
  282. #ifndef BUILD_BRIDGE
  283. // -------------------------------------------------------------------
  284. // Rack mode
  285. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  286. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  287. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  288. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  289. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  290. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  291. #endif
  292. // -------------------------------------
  293. protected:
  294. const char* name;
  295. double sampleRate;
  296. uint32_t bufferSize;
  297. CarlaTimeInfo timeInfo;
  298. void bufferSizeChanged(uint32_t newBufferSize);
  299. private:
  300. CarlaOsc m_osc;
  301. CarlaCheckThread m_checkThread;
  302. QMutex m_procLock;
  303. QMutex m_midiLock;
  304. CallbackFunc m_callback;
  305. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  306. const char* m_uniqueNames[MAX_PLUGINS];
  307. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  308. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  309. };
  310. /*!
  311. * \class CarlaEngineScopedLocker
  312. *
  313. * \brief Carla engine scoped locker
  314. *
  315. * This is a handy class that temporarily locks an engine during a function scope.
  316. */
  317. class CarlaEngineScopedLocker
  318. {
  319. public:
  320. /*!
  321. * Lock the engine \a engine if \a lock is true.
  322. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  323. *
  324. * \param engine The engine to lock
  325. * \param lock Wherever to lock the engine or not, true by default
  326. */
  327. CarlaEngineScopedLocker(CarlaEngine* const engine, bool lock = true) :
  328. m_engine(engine),
  329. m_lock(lock)
  330. {
  331. if (m_lock)
  332. m_engine->processLock();
  333. }
  334. ~CarlaEngineScopedLocker()
  335. {
  336. if (m_lock)
  337. m_engine->processUnlock();
  338. }
  339. private:
  340. CarlaEngine* const m_engine;
  341. const bool m_lock;
  342. };
  343. // -----------------------------------------------------------------------
  344. class CarlaEngineClient
  345. {
  346. public:
  347. CarlaEngineClient(const CarlaEngineClientNativeHandle& handle);
  348. ~CarlaEngineClient();
  349. void activate();
  350. void deactivate();
  351. bool isActive() const;
  352. bool isOk() const;
  353. const CarlaEngineBasePort* addPort(CarlaEnginePortType type, const char* const name, bool isInput);
  354. private:
  355. bool m_active;
  356. const CarlaEngineClientNativeHandle handle;
  357. };
  358. // -----------------------------------------------------------------------
  359. class CarlaEngineBasePort
  360. {
  361. public:
  362. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  363. virtual ~CarlaEngineBasePort();
  364. virtual void initBuffer(CarlaEngine* const engine) = 0;
  365. protected:
  366. void* m_buffer;
  367. const bool isInput;
  368. const CarlaEnginePortNativeHandle handle;
  369. };
  370. class CarlaEngineAudioPort : public CarlaEngineBasePort
  371. {
  372. public:
  373. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  374. void initBuffer(CarlaEngine* const engine);
  375. #ifdef CARLA_ENGINE_JACK
  376. float* getJackAudioBuffer(uint32_t nframes);
  377. #endif
  378. };
  379. class CarlaEngineControlPort : public CarlaEngineBasePort
  380. {
  381. public:
  382. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  383. void initBuffer(CarlaEngine* const engine);
  384. uint32_t getEventCount();
  385. const CarlaEngineControlEvent* getEvent(uint32_t index);
  386. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  387. };
  388. class CarlaEngineMidiPort : public CarlaEngineBasePort
  389. {
  390. public:
  391. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  392. void initBuffer(CarlaEngine* const engine);
  393. uint32_t getEventCount();
  394. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  395. void writeEvent(uint32_t time, uint8_t* data, uint8_t size);
  396. };
  397. // -----------------------------------------------------------------------
  398. #ifdef CARLA_ENGINE_JACK
  399. class CarlaEngineJack : public CarlaEngine
  400. {
  401. public:
  402. CarlaEngineJack();
  403. ~CarlaEngineJack();
  404. // -------------------------------------
  405. bool init(const char* const clientName);
  406. bool close();
  407. bool isOnAudioThread();
  408. bool isOffline();
  409. bool isRunning();
  410. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  411. // -------------------------------------
  412. void handleSampleRateCallback(double newSampleRate);
  413. void handleBufferSizeCallback(uint32_t newBufferSize);
  414. void handleFreewheelCallback(bool isFreewheel);
  415. void handleProcessCallback(uint32_t nframes);
  416. void handleShutdownCallback();
  417. // -------------------------------------
  418. private:
  419. jack_client_t* client;
  420. jack_transport_state_t state;
  421. jack_position_t pos;
  422. bool freewheel;
  423. QThread* procThread;
  424. // -------------------------------------
  425. static const unsigned short rackPortAudioIn1 = 0;
  426. static const unsigned short rackPortAudioIn2 = 1;
  427. static const unsigned short rackPortAudioOut1 = 2;
  428. static const unsigned short rackPortAudioOut2 = 3;
  429. static const unsigned short rackPortControlIn = 4;
  430. static const unsigned short rackPortControlOut = 5;
  431. static const unsigned short rackPortMidiIn = 6;
  432. static const unsigned short rackPortMidiOut = 7;
  433. static const unsigned short rackPortCount = 8;
  434. jack_port_t* rackJackPorts[rackPortCount];
  435. };
  436. #endif
  437. // -----------------------------------------------------------------------
  438. #ifdef CARLA_ENGINE_RTAUDIO
  439. class CarlaEngineRtAudio : public CarlaEngine
  440. {
  441. public:
  442. CarlaEngineRtAudio(RtAudio::Api api);
  443. ~CarlaEngineRtAudio();
  444. // -------------------------------------
  445. bool init(const char* const clientName);
  446. bool close();
  447. bool isOnAudioThread();
  448. bool isOffline();
  449. bool isRunning();
  450. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  451. // -------------------------------------
  452. void handleProcessCallback(void* outputBuffer, void* inputBuffer, unsigned int nframes, double streamTime, RtAudioStreamStatus status);
  453. private:
  454. RtAudio adac;
  455. QThread* procThread;
  456. };
  457. #endif
  458. /**@}*/
  459. CARLA_BACKEND_END_NAMESPACE
  460. #endif // CARLA_ENGINE_H