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.

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