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.

608 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. 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. Q_ASSERT(pluginId < MAX_PLUGINS);
  195. Q_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. Q_ASSERT(pluginId < MAX_PLUGINS);
  201. Q_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. Q_ASSERT(pluginId < MAX_PLUGINS);
  207. Q_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. Q_ASSERT(pluginId < MAX_PLUGINS);
  213. Q_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(m_callbackPtr, action, pluginId, value1, value2, value3);
  223. }
  224. void setCallback(const CallbackFunc func, void* const ptr)
  225. {
  226. qDebug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  227. m_callback = func;
  228. m_callbackPtr = ptr;
  229. }
  230. // -------------------------------------------------------------------
  231. // Mutex locks
  232. void processLock()
  233. {
  234. m_procLock.lock();
  235. }
  236. void processUnlock()
  237. {
  238. m_procLock.unlock();
  239. }
  240. void midiLock()
  241. {
  242. m_midiLock.lock();
  243. }
  244. void midiUnlock()
  245. {
  246. m_midiLock.unlock();
  247. }
  248. // -------------------------------------------------------------------
  249. // Virtual, per-engine type calls
  250. virtual bool init(const char* const clientName)
  251. {
  252. qDebug("CarlaEngine::init(%s)", clientName);
  253. m_checkThread.start(QThread::HighPriority);
  254. m_osc.init(clientName);
  255. m_oscData = m_osc.getControllerData();
  256. return true;
  257. }
  258. virtual bool close()
  259. {
  260. qDebug("CarlaEngine::close()");
  261. m_checkThread.stopNow();
  262. m_oscData = nullptr;
  263. m_osc.close();
  264. return true;
  265. }
  266. virtual bool isOnAudioThread() = 0;
  267. virtual bool isOffline() = 0;
  268. virtual bool isRunning() = 0;
  269. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  270. #ifndef BUILD_BRIDGE
  271. // -------------------------------------------------------------------
  272. // OSC Stuff
  273. bool isOscControllerRegisted() const
  274. {
  275. return m_osc.isControllerRegistered();
  276. }
  277. const char* getOscServerPath() const
  278. {
  279. return m_osc.getServerPath();
  280. }
  281. void osc_send_add_plugin(const int32_t pluginId, const char* const pluginName);
  282. void osc_send_remove_plugin(const int32_t pluginId);
  283. 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);
  284. 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);
  285. 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);
  286. 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);
  287. void osc_send_set_parameter_midi_cc(const int32_t pluginId, const int32_t index, const int32_t cc);
  288. void osc_send_set_parameter_midi_channel(const int32_t pluginId, const int32_t index, const int32_t channel);
  289. void osc_send_set_parameter_value(const int32_t pluginId, const int32_t index, const double value);
  290. void osc_send_set_default_value(const int32_t pluginId, const int32_t index, const double value);
  291. void osc_send_set_program(const int32_t pluginId, const int32_t index);
  292. void osc_send_set_program_count(const int32_t pluginId, const int32_t count);
  293. void osc_send_set_program_name(const int32_t pluginId, const int32_t index, const char* const name);
  294. void osc_send_set_midi_program(const int32_t pluginId, const int32_t index);
  295. void osc_send_set_midi_program_count(const int32_t pluginId, const int32_t count);
  296. 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);
  297. void osc_send_set_input_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  298. void osc_send_set_output_peak_value(const int32_t pluginId, const int32_t portId, const double value);
  299. void osc_send_note_on(const int32_t pluginId, const int32_t channel, const int32_t note, const int32_t velo);
  300. void osc_send_note_off(const int32_t pluginId, const int32_t channel, const int32_t note);
  301. void osc_send_exit();
  302. // -------------------------------------------------------------------
  303. // Rack mode
  304. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  305. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  306. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  307. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  308. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  309. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  310. #endif
  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. const CarlaOscData* m_oscData;
  324. #endif
  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. };
  334. /*!
  335. * \class CarlaEngineScopedLocker
  336. *
  337. * \brief Carla engine scoped locker
  338. *
  339. * This is a handy class that temporarily locks an engine during a function scope.
  340. */
  341. class CarlaEngineScopedLocker
  342. {
  343. public:
  344. /*!
  345. * Lock the engine \a engine if \a lock is true.
  346. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  347. *
  348. * \param engine The engine to lock
  349. * \param lock Wherever to lock the engine or not, true by default
  350. */
  351. CarlaEngineScopedLocker(CarlaEngine* const engine, bool lock = true) :
  352. m_engine(engine),
  353. m_lock(lock)
  354. {
  355. if (m_lock)
  356. m_engine->processLock();
  357. }
  358. ~CarlaEngineScopedLocker()
  359. {
  360. if (m_lock)
  361. m_engine->processUnlock();
  362. }
  363. private:
  364. CarlaEngine* const m_engine;
  365. const bool m_lock;
  366. };
  367. // -----------------------------------------------------------------------
  368. class CarlaEngineClient
  369. {
  370. public:
  371. CarlaEngineClient(const CarlaEngineClientNativeHandle& handle);
  372. ~CarlaEngineClient();
  373. void activate();
  374. void deactivate();
  375. bool isActive() const;
  376. bool isOk() const;
  377. const CarlaEngineBasePort* addPort(const CarlaEnginePortType type, const char* const name, const bool isInput);
  378. private:
  379. bool m_active;
  380. const CarlaEngineClientNativeHandle handle;
  381. };
  382. // -----------------------------------------------------------------------
  383. class CarlaEngineBasePort
  384. {
  385. public:
  386. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  387. virtual ~CarlaEngineBasePort();
  388. virtual void initBuffer(CarlaEngine* const engine) = 0;
  389. protected:
  390. void* buffer;
  391. const bool isInput;
  392. const CarlaEnginePortNativeHandle handle;
  393. };
  394. class CarlaEngineAudioPort : public CarlaEngineBasePort
  395. {
  396. public:
  397. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  398. void initBuffer(CarlaEngine* const engine);
  399. #ifdef CARLA_ENGINE_JACK
  400. float* getJackAudioBuffer(uint32_t nframes);
  401. #endif
  402. };
  403. class CarlaEngineControlPort : public CarlaEngineBasePort
  404. {
  405. public:
  406. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  407. void initBuffer(CarlaEngine* const engine);
  408. uint32_t getEventCount();
  409. const CarlaEngineControlEvent* getEvent(uint32_t index);
  410. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  411. };
  412. class CarlaEngineMidiPort : public CarlaEngineBasePort
  413. {
  414. public:
  415. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  416. void initBuffer(CarlaEngine* const engine);
  417. uint32_t getEventCount();
  418. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  419. void writeEvent(uint32_t time, uint8_t* data, uint8_t size);
  420. };
  421. // -----------------------------------------------------------------------
  422. #ifdef CARLA_ENGINE_JACK
  423. class CarlaEngineJack : public CarlaEngine
  424. {
  425. public:
  426. CarlaEngineJack();
  427. ~CarlaEngineJack();
  428. // -------------------------------------
  429. bool init(const char* const clientName);
  430. bool close();
  431. bool isOnAudioThread();
  432. bool isOffline();
  433. bool isRunning();
  434. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  435. // -------------------------------------
  436. void handleSampleRateCallback(double newSampleRate);
  437. void handleBufferSizeCallback(uint32_t newBufferSize);
  438. void handleFreewheelCallback(bool isFreewheel);
  439. void handleProcessCallback(uint32_t nframes);
  440. void handleShutdownCallback();
  441. // -------------------------------------
  442. private:
  443. jack_client_t* client;
  444. jack_transport_state_t state;
  445. jack_position_t pos;
  446. bool freewheel;
  447. QThread* procThread;
  448. // -------------------------------------
  449. static const unsigned short rackPortAudioIn1 = 0;
  450. static const unsigned short rackPortAudioIn2 = 1;
  451. static const unsigned short rackPortAudioOut1 = 2;
  452. static const unsigned short rackPortAudioOut2 = 3;
  453. static const unsigned short rackPortControlIn = 4;
  454. static const unsigned short rackPortControlOut = 5;
  455. static const unsigned short rackPortMidiIn = 6;
  456. static const unsigned short rackPortMidiOut = 7;
  457. static const unsigned short rackPortCount = 8;
  458. jack_port_t* rackJackPorts[rackPortCount];
  459. };
  460. #endif
  461. // -----------------------------------------------------------------------
  462. #ifdef CARLA_ENGINE_RTAUDIO
  463. class CarlaEngineRtAudio : public CarlaEngine
  464. {
  465. public:
  466. CarlaEngineRtAudio(RtAudio::Api api);
  467. ~CarlaEngineRtAudio();
  468. // -------------------------------------
  469. bool init(const char* const clientName);
  470. bool close();
  471. bool isOnAudioThread();
  472. bool isOffline();
  473. bool isRunning();
  474. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  475. // -------------------------------------
  476. void handleProcessCallback(void* outputBuffer, void* inputBuffer, unsigned int nframes, double streamTime, RtAudioStreamStatus status);
  477. private:
  478. RtAudio adac;
  479. QThread* procThread;
  480. };
  481. #endif
  482. /**@}*/
  483. CARLA_BACKEND_END_NAMESPACE
  484. #endif // CARLA_ENGINE_H