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.

574 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. /*!
  122. * \class CarlaEngine
  123. *
  124. * \brief Carla Backend base engine class
  125. *
  126. * This is the base class for all available engine types available in Carla Backend.
  127. */
  128. class CarlaEngine
  129. {
  130. public:
  131. CarlaEngine();
  132. virtual ~CarlaEngine();
  133. // -------------------------------------------------------------------
  134. // static values
  135. static const unsigned short MAX_PEAKS = 2;
  136. static int maxClientNameSize();
  137. static int maxPortNameSize();
  138. // -------------------------------------------------------------------
  139. // virtual, per-engine type calls
  140. virtual bool init(const char* const clientName)
  141. {
  142. m_checkThread.start(QThread::HighPriority);
  143. m_osc.init(clientName);
  144. return true;
  145. }
  146. virtual bool close()
  147. {
  148. if (m_checkThread.isRunning())
  149. m_checkThread.stopNow();
  150. m_osc.close();
  151. return true;
  152. }
  153. virtual bool isOnAudioThread() = 0;
  154. virtual bool isOffline() = 0;
  155. virtual bool isRunning() = 0;
  156. virtual CarlaEngineClient* addClient(CarlaPlugin* const plugin) = 0;
  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. assert(pluginId < MAX_PLUGINS);
  195. 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. assert(pluginId < MAX_PLUGINS);
  201. 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. assert(pluginId < MAX_PLUGINS);
  207. 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. assert(pluginId < MAX_PLUGINS);
  213. 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(action, pluginId, value1, value2, value3);
  223. }
  224. void setCallback(const CallbackFunc func)
  225. {
  226. qDebug("CarlaEngine::setCallback(%p)", func);
  227. m_callback = func;
  228. }
  229. // -------------------------------------------------------------------
  230. // mutex locks
  231. void processLock()
  232. {
  233. m_procLock.lock();
  234. }
  235. void processUnlock()
  236. {
  237. m_procLock.unlock();
  238. }
  239. void midiLock()
  240. {
  241. m_midiLock.lock();
  242. }
  243. void midiUnlock()
  244. {
  245. m_midiLock.unlock();
  246. }
  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(int plugin_id, const char* plugin_name);
  258. void osc_send_remove_plugin(int plugin_id);
  259. 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);
  260. void osc_send_set_plugin_ports(int plugin_id, int ains, int aouts, int mins, int mouts, int cins, int couts, int ctotals);
  261. void osc_send_set_parameter_value(int plugin_id, int param_id, double value);
  262. void osc_send_set_parameter_data(int plugin_id, int param_id, int ptype, int hints, const char* name, const char* label, double current);
  263. 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);
  264. void osc_send_set_parameter_midi_channel(int plugin_id, int parameter_id, int midi_channel);
  265. void osc_send_set_parameter_midi_cc(int plugin_id, int parameter_id, int midi_cc);
  266. void osc_send_set_default_value(int plugin_id, int param_id, double value);
  267. void osc_send_set_program(int plugin_id, int program_id);
  268. void osc_send_set_program_count(int plugin_id, int program_count);
  269. void osc_send_set_program_name(int plugin_id, int program_id, const char* program_name);
  270. void osc_send_set_midi_program(int plugin_id, int midi_program_id);
  271. void osc_send_set_midi_program_count(int plugin_id, int midi_program_count);
  272. 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);
  273. void osc_send_set_input_peak_value(int plugin_id, int port_id, double value);
  274. void osc_send_set_output_peak_value(int plugin_id, int port_id, double value);
  275. void osc_send_note_on(int plugin_id, int channel, int note, int velo);
  276. void osc_send_note_off(int plugin_id, int channel, int note);
  277. void osc_send_exit();
  278. #ifndef BUILD_BRIDGE
  279. // -------------------------------------------------------------------
  280. // Rack mode
  281. static const unsigned short MAX_ENGINE_CONTROL_EVENTS = 512;
  282. static const unsigned short MAX_ENGINE_MIDI_EVENTS = 512;
  283. CarlaEngineControlEvent rackControlEventsIn[MAX_ENGINE_CONTROL_EVENTS];
  284. CarlaEngineControlEvent rackControlEventsOut[MAX_ENGINE_CONTROL_EVENTS];
  285. CarlaEngineMidiEvent rackMidiEventsIn[MAX_ENGINE_MIDI_EVENTS];
  286. CarlaEngineMidiEvent rackMidiEventsOut[MAX_ENGINE_MIDI_EVENTS];
  287. #endif
  288. // -------------------------------------
  289. protected:
  290. const char* name;
  291. double sampleRate;
  292. uint32_t bufferSize;
  293. CarlaTimeInfo timeInfo;
  294. void bufferSizeChanged(uint32_t newBufferSize);
  295. private:
  296. CarlaOsc m_osc;
  297. CarlaCheckThread m_checkThread;
  298. QMutex m_procLock;
  299. QMutex m_midiLock;
  300. CallbackFunc m_callback;
  301. CarlaPlugin* m_carlaPlugins[MAX_PLUGINS];
  302. const char* m_uniqueNames[MAX_PLUGINS];
  303. double m_insPeak[MAX_PLUGINS * MAX_PEAKS];
  304. double m_outsPeak[MAX_PLUGINS * MAX_PEAKS];
  305. };
  306. /*!
  307. * \class CarlaEngineScopedLocker
  308. *
  309. * \brief Carla engine scoped locker
  310. *
  311. * This is a handy class that temporarily locks an engine during a function scope.
  312. */
  313. class CarlaEngineScopedLocker
  314. {
  315. public:
  316. /*!
  317. * Lock the engine \a engine if \a lock is true.
  318. * The engine is unlocked in the deconstructor of this class if \a lock is true.
  319. *
  320. * \param engine The engine to lock
  321. * \param lock Wherever to lock the engine or not, true by default
  322. */
  323. CarlaEngineScopedLocker(CarlaEngine* const engine, bool lock = true) :
  324. m_engine(engine),
  325. m_lock(lock)
  326. {
  327. if (m_lock)
  328. m_engine->processLock();
  329. }
  330. ~CarlaEngineScopedLocker()
  331. {
  332. if (m_lock)
  333. m_engine->processUnlock();
  334. }
  335. private:
  336. CarlaEngine* const m_engine;
  337. const bool m_lock;
  338. };
  339. // -----------------------------------------------------------------------
  340. class CarlaEngineClient
  341. {
  342. public:
  343. CarlaEngineClient(const CarlaEngineClientNativeHandle& handle);
  344. ~CarlaEngineClient();
  345. void activate();
  346. void deactivate();
  347. bool isActive() const;
  348. bool isOk() const;
  349. const CarlaEngineBasePort* addPort(CarlaEnginePortType type, const char* const name, bool isInput);
  350. private:
  351. bool m_active;
  352. const CarlaEngineClientNativeHandle handle;
  353. };
  354. // -----------------------------------------------------------------------
  355. class CarlaEngineBasePort
  356. {
  357. public:
  358. CarlaEngineBasePort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  359. virtual ~CarlaEngineBasePort();
  360. virtual void initBuffer(CarlaEngine* const engine) = 0;
  361. protected:
  362. void* m_buffer;
  363. const bool isInput;
  364. const CarlaEnginePortNativeHandle handle;
  365. };
  366. class CarlaEngineAudioPort : public CarlaEngineBasePort
  367. {
  368. public:
  369. CarlaEngineAudioPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  370. void initBuffer(CarlaEngine* const engine);
  371. #ifdef CARLA_ENGINE_JACK
  372. float* getJackAudioBuffer(uint32_t nframes);
  373. #endif
  374. };
  375. class CarlaEngineControlPort : public CarlaEngineBasePort
  376. {
  377. public:
  378. CarlaEngineControlPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  379. void initBuffer(CarlaEngine* const engine);
  380. uint32_t getEventCount();
  381. const CarlaEngineControlEvent* getEvent(uint32_t index);
  382. void writeEvent(CarlaEngineControlEventType type, uint32_t time, uint8_t channel, uint8_t controller, double value);
  383. };
  384. class CarlaEngineMidiPort : public CarlaEngineBasePort
  385. {
  386. public:
  387. CarlaEngineMidiPort(const CarlaEnginePortNativeHandle& handle, bool isInput);
  388. void initBuffer(CarlaEngine* const engine);
  389. uint32_t getEventCount();
  390. const CarlaEngineMidiEvent* getEvent(uint32_t index);
  391. void writeEvent(uint32_t time, uint8_t* data, uint8_t size);
  392. };
  393. // -----------------------------------------------------------------------
  394. #ifdef CARLA_ENGINE_JACK
  395. class CarlaEngineJack : public CarlaEngine
  396. {
  397. public:
  398. CarlaEngineJack();
  399. ~CarlaEngineJack();
  400. // -------------------------------------
  401. bool init(const char* const clientName);
  402. bool close();
  403. bool isOnAudioThread();
  404. bool isOffline();
  405. bool isRunning();
  406. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  407. // -------------------------------------
  408. void handleSampleRateCallback(double newSampleRate);
  409. void handleBufferSizeCallback(uint32_t newBufferSize);
  410. void handleFreewheelCallback(bool isFreewheel);
  411. void handleProcessCallback(uint32_t nframes);
  412. void handleShutdownCallback();
  413. // -------------------------------------
  414. private:
  415. jack_client_t* client;
  416. jack_transport_state_t state;
  417. jack_position_t pos;
  418. bool freewheel;
  419. QThread* procThread;
  420. // -------------------------------------
  421. static const unsigned short rackPortAudioIn1 = 0;
  422. static const unsigned short rackPortAudioIn2 = 1;
  423. static const unsigned short rackPortAudioOut1 = 2;
  424. static const unsigned short rackPortAudioOut2 = 3;
  425. static const unsigned short rackPortControlIn = 4;
  426. static const unsigned short rackPortControlOut = 5;
  427. static const unsigned short rackPortMidiIn = 6;
  428. static const unsigned short rackPortMidiOut = 7;
  429. static const unsigned short rackPortCount = 8;
  430. jack_port_t* rackJackPorts[rackPortCount];
  431. };
  432. #endif
  433. // -----------------------------------------------------------------------
  434. #ifdef CARLA_ENGINE_RTAUDIO
  435. class CarlaEngineRtAudio : public CarlaEngine
  436. {
  437. public:
  438. CarlaEngineRtAudio(RtAudio::Api api);
  439. ~CarlaEngineRtAudio();
  440. // -------------------------------------
  441. bool init(const char* const clientName);
  442. bool close();
  443. bool isOnAudioThread();
  444. bool isOffline();
  445. bool isRunning();
  446. CarlaEngineClient* addClient(CarlaPlugin* const plugin);
  447. // -------------------------------------
  448. void handleProcessCallback(void* outputBuffer, void* inputBuffer, unsigned int nframes, double streamTime, RtAudioStreamStatus status);
  449. private:
  450. RtAudio adac;
  451. QThread* procThread;
  452. };
  453. #endif
  454. /**@}*/
  455. CARLA_BACKEND_END_NAMESPACE
  456. #endif // CARLA_ENGINE_H