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.

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