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.

576 lines
15KB

  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. Q_COMPILER_INITIALIZER_LISTS
  122. /*!
  123. * \class CarlaEngine
  124. *
  125. * \brief Carla Backend base engine class
  126. *
  127. * This is the base class for all available engine types available in Carla Backend.
  128. */
  129. class CarlaEngine
  130. {
  131. public:
  132. CarlaEngine();
  133. virtual ~CarlaEngine();
  134. // -------------------------------------------------------------------
  135. // static values
  136. static const unsigned short MAX_PEAKS = 2;
  137. static int maxClientNameSize();
  138. static int maxPortNameSize();
  139. // -------------------------------------------------------------------
  140. // virtual, per-engine type calls
  141. virtual bool init(const char* const clientName)
  142. {
  143. m_checkThread.start(QThread::HighPriority);
  144. m_osc.init(clientName);
  145. return true;
  146. }
  147. virtual bool close()
  148. {
  149. if (m_checkThread.isRunning())
  150. m_checkThread.stopNow();
  151. m_osc.close();
  152. return true;
  153. }
  154. virtual bool isOnAudioThread() = 0;
  155. virtual bool isOffline() = 0;
  156. virtual bool isRunning() = 0;
  157. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  158. // -------------------------------------------------------------------
  159. // Plugin management
  160. short getNewPluginId() const;
  161. CarlaPlugin* getPlugin(const unsigned short id) const;
  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. assert(pluginId < MAX_PLUGINS);
  196. 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. assert(pluginId < MAX_PLUGINS);
  202. 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. assert(pluginId < MAX_PLUGINS);
  208. 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. assert(pluginId < MAX_PLUGINS);
  214. 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(action, pluginId, value1, value2, value3);
  224. }
  225. void setCallback(const CallbackFunc func)
  226. {
  227. qDebug("CarlaEngine::setCallback(%p)", func);
  228. m_callback = func;
  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. // OSC Stuff
  250. bool isOscControllerRegisted() const
  251. {
  252. return m_osc.isControllerRegistered();
  253. }
  254. const char* getOscServerPath() const
  255. {
  256. return m_osc.getServerPath();
  257. }
  258. void osc_send_add_plugin(int plugin_id, const char* plugin_name);
  259. void osc_send_remove_plugin(int plugin_id);
  260. void osc_send_set_plugin_data(int plugin_id, int type, int category, int hints, const char* real_name, const char* label, const char* maker, const char* copyright, long unique_id);
  261. void osc_send_set_plugin_ports(int plugin_id, int ains, int aouts, int mins, int mouts, int cins, int couts, int ctotals);
  262. void osc_send_set_parameter_value(int plugin_id, int param_id, double value);
  263. void osc_send_set_parameter_data(int plugin_id, int param_id, int ptype, int hints, const char* name, const char* label, double current);
  264. void osc_send_set_parameter_ranges(int plugin_id, int param_id, double x_min, double x_max, double x_def, double x_step, double x_step_small, double x_step_large);
  265. void osc_send_set_parameter_midi_channel(int plugin_id, int parameter_id, int midi_channel);
  266. void osc_send_set_parameter_midi_cc(int plugin_id, int parameter_id, int midi_cc);
  267. void osc_send_set_default_value(int plugin_id, int param_id, double value);
  268. void osc_send_set_program(int plugin_id, int program_id);
  269. void osc_send_set_program_count(int plugin_id, int program_count);
  270. void osc_send_set_program_name(int plugin_id, int program_id, const char* program_name);
  271. void osc_send_set_midi_program(int plugin_id, int midi_program_id);
  272. void osc_send_set_midi_program_count(int plugin_id, int midi_program_count);
  273. void osc_send_set_midi_program_data(int plugin_id, int midi_program_id, int bank_id, int program_id, const char* midi_program_name);
  274. void osc_send_set_input_peak_value(int plugin_id, int port_id, double value);
  275. void osc_send_set_output_peak_value(int plugin_id, int port_id, double value);
  276. void osc_send_note_on(int plugin_id, int note, int velo);
  277. void osc_send_note_off(int plugin_id, int note);
  278. void osc_send_exit();
  279. #ifndef BUILD_BRIDGE
  280. // -------------------------------------------------------------------
  281. // Rack mode
  282. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  283. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  284. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  285. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  286. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  287. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  288. #endif
  289. // -------------------------------------
  290. protected:
  291. const char* name;
  292. double sampleRate;
  293. uint32_t bufferSize;
  294. CarlaTimeInfo timeInfo;
  295. void bufferSizeChanged(uint32_t newBufferSize);
  296. private:
  297. CarlaOsc m_osc;
  298. CarlaCheckThread m_checkThread;
  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