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.

622 lines
17KB

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