Audio plugin host https://kx.studio/carla
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.

472 lines
12KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaEngine.hpp"
  18. #include "CarlaMathUtils.hpp"
  19. #include "CarlaMIDI.h"
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -----------------------------------------------------------------------
  22. // EngineControlEvent
  23. uint8_t EngineControlEvent::convertToMidiData(const uint8_t channel, uint8_t data[3]) const noexcept
  24. {
  25. switch (type)
  26. {
  27. case kEngineControlEventTypeNull:
  28. break;
  29. case kEngineControlEventTypeParameter:
  30. CARLA_SAFE_ASSERT_RETURN(param < MAX_MIDI_VALUE, 0);
  31. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  32. if (MIDI_IS_CONTROL_BANK_SELECT(param))
  33. {
  34. data[1] = MIDI_CONTROL_BANK_SELECT;
  35. data[2] = uint8_t(carla_fixedValue<float>(0.0f, static_cast<float>(MAX_MIDI_VALUE-1), value));
  36. }
  37. else
  38. {
  39. data[1] = static_cast<uint8_t>(param);
  40. data[2] = uint8_t(carla_fixedValue<float>(0.0f, 1.0f, value) * static_cast<float>(MAX_MIDI_VALUE-1));
  41. }
  42. return 3;
  43. case kEngineControlEventTypeMidiBank:
  44. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  45. data[1] = MIDI_CONTROL_BANK_SELECT;
  46. data[2] = uint8_t(carla_fixedValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
  47. return 3;
  48. case kEngineControlEventTypeMidiProgram:
  49. data[0] = static_cast<uint8_t>(MIDI_STATUS_PROGRAM_CHANGE | (channel & MIDI_CHANNEL_BIT));
  50. data[1] = uint8_t(carla_fixedValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
  51. return 2;
  52. case kEngineControlEventTypeAllSoundOff:
  53. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  54. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  55. return 2;
  56. case kEngineControlEventTypeAllNotesOff:
  57. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  58. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  59. return 2;
  60. }
  61. return 0;
  62. }
  63. // -----------------------------------------------------------------------
  64. // EngineEvent
  65. void EngineEvent::fillFromMidiData(const uint8_t size, const uint8_t* const data, const uint8_t midiPortOffset) noexcept
  66. {
  67. if (size == 0 || data == nullptr || data[0] < MIDI_STATUS_NOTE_OFF)
  68. {
  69. type = kEngineEventTypeNull;
  70. channel = 0;
  71. return;
  72. }
  73. // get channel
  74. channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data));
  75. // get status
  76. const uint8_t midiStatus(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  77. if (midiStatus == MIDI_STATUS_CONTROL_CHANGE)
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(size >= 2,);
  80. type = kEngineEventTypeControl;
  81. const uint8_t midiControl(data[1]);
  82. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  83. {
  84. CARLA_SAFE_ASSERT_RETURN(size >= 3,);
  85. const uint8_t midiBank(data[2]);
  86. ctrl.type = kEngineControlEventTypeMidiBank;
  87. ctrl.param = midiBank;
  88. ctrl.value = 0.0f;
  89. ctrl.handled = true;
  90. }
  91. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  92. {
  93. ctrl.type = kEngineControlEventTypeAllSoundOff;
  94. ctrl.param = 0;
  95. ctrl.value = 0.0f;
  96. ctrl.handled = true;
  97. }
  98. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  99. {
  100. ctrl.type = kEngineControlEventTypeAllNotesOff;
  101. ctrl.param = 0;
  102. ctrl.value = 0.0f;
  103. ctrl.handled = true;
  104. }
  105. else
  106. {
  107. CARLA_SAFE_ASSERT_RETURN(size >= 3,);
  108. const uint8_t midiValue = carla_fixedValue<uint8_t>(0, 127, data[2]); // ensures 0.0<->1.0 value range
  109. ctrl.type = kEngineControlEventTypeParameter;
  110. ctrl.param = midiControl;
  111. ctrl.value = float(midiValue)/127.0f;
  112. ctrl.handled = false;
  113. }
  114. }
  115. else if (midiStatus == MIDI_STATUS_PROGRAM_CHANGE)
  116. {
  117. CARLA_SAFE_ASSERT_RETURN(size >= 2,);
  118. type = kEngineEventTypeControl;
  119. const uint8_t midiProgram(data[1]);
  120. ctrl.type = kEngineControlEventTypeMidiProgram;
  121. ctrl.param = midiProgram;
  122. ctrl.value = 0.0f;
  123. ctrl.handled = true;
  124. }
  125. else
  126. {
  127. type = kEngineEventTypeMidi;
  128. midi.port = midiPortOffset;
  129. midi.size = size;
  130. if (size > EngineMidiEvent::kDataSize)
  131. {
  132. midi.dataExt = data;
  133. std::memset(midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  134. }
  135. else
  136. {
  137. midi.data[0] = midiStatus;
  138. uint8_t i=1;
  139. for (; i < size; ++i)
  140. midi.data[i] = data[i];
  141. for (; i < EngineMidiEvent::kDataSize; ++i)
  142. midi.data[i] = 0;
  143. midi.dataExt = nullptr;
  144. }
  145. }
  146. }
  147. // -----------------------------------------------------------------------
  148. // EngineOptions
  149. EngineOptions::EngineOptions() noexcept
  150. #ifdef CARLA_OS_LINUX
  151. : processMode(ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  152. transportMode(ENGINE_TRANSPORT_MODE_JACK),
  153. #else
  154. : processMode(ENGINE_PROCESS_MODE_PATCHBAY),
  155. transportMode(ENGINE_TRANSPORT_MODE_INTERNAL),
  156. #endif
  157. transportExtra(nullptr),
  158. forceStereo(false),
  159. resetXruns(false),
  160. preferPluginBridges(false),
  161. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  162. preferUiBridges(false),
  163. #else
  164. preferUiBridges(true),
  165. #endif
  166. uisAlwaysOnTop(true),
  167. bgColor(0x000000ff),
  168. fgColor(0xffffffff),
  169. uiScale(1.0f),
  170. maxParameters(MAX_DEFAULT_PARAMETERS),
  171. uiBridgesTimeout(4000),
  172. audioBufferSize(512),
  173. audioSampleRate(44100),
  174. audioTripleBuffer(false),
  175. audioDriver(nullptr),
  176. audioDevice(nullptr),
  177. #ifndef BUILD_BRIDGE
  178. # ifdef CARLA_OS_WIN
  179. oscEnabled(false),
  180. # else
  181. oscEnabled(true),
  182. # endif
  183. oscPortTCP(22752),
  184. oscPortUDP(22752),
  185. #endif
  186. pathAudio(nullptr),
  187. pathMIDI(nullptr),
  188. pathLADSPA(nullptr),
  189. pathDSSI(nullptr),
  190. pathLV2(nullptr),
  191. pathVST2(nullptr),
  192. pathVST3(nullptr),
  193. pathSF2(nullptr),
  194. pathSFZ(nullptr),
  195. binaryDir(nullptr),
  196. resourceDir(nullptr),
  197. clientNamePrefix(nullptr),
  198. preventBadBehaviour(false),
  199. frontendWinId(0)
  200. #ifndef CARLA_OS_WIN
  201. , wine()
  202. #endif
  203. {
  204. }
  205. EngineOptions::~EngineOptions() noexcept
  206. {
  207. if (audioDriver != nullptr)
  208. {
  209. delete[] audioDriver;
  210. audioDriver = nullptr;
  211. }
  212. if (audioDevice != nullptr)
  213. {
  214. delete[] audioDevice;
  215. audioDevice = nullptr;
  216. }
  217. if (pathAudio != nullptr)
  218. {
  219. delete[] pathAudio;
  220. pathAudio = nullptr;
  221. }
  222. if (pathMIDI != nullptr)
  223. {
  224. delete[] pathMIDI;
  225. pathMIDI = nullptr;
  226. }
  227. if (pathLADSPA != nullptr)
  228. {
  229. delete[] pathLADSPA;
  230. pathLADSPA = nullptr;
  231. }
  232. if (pathDSSI != nullptr)
  233. {
  234. delete[] pathDSSI;
  235. pathDSSI = nullptr;
  236. }
  237. if (pathLV2 != nullptr)
  238. {
  239. delete[] pathLV2;
  240. pathLV2 = nullptr;
  241. }
  242. if (pathVST2 != nullptr)
  243. {
  244. delete[] pathVST2;
  245. pathVST2 = nullptr;
  246. }
  247. if (pathVST3 != nullptr)
  248. {
  249. delete[] pathVST3;
  250. pathVST3 = nullptr;
  251. }
  252. if (pathSF2 != nullptr)
  253. {
  254. delete[] pathSF2;
  255. pathSF2 = nullptr;
  256. }
  257. if (pathSFZ != nullptr)
  258. {
  259. delete[] pathSFZ;
  260. pathSFZ = nullptr;
  261. }
  262. if (binaryDir != nullptr)
  263. {
  264. delete[] binaryDir;
  265. binaryDir = nullptr;
  266. }
  267. if (resourceDir != nullptr)
  268. {
  269. delete[] resourceDir;
  270. resourceDir = nullptr;
  271. }
  272. if (clientNamePrefix != nullptr)
  273. {
  274. delete[] clientNamePrefix;
  275. clientNamePrefix = nullptr;
  276. }
  277. }
  278. #ifndef CARLA_OS_WIN
  279. EngineOptions::Wine::Wine() noexcept
  280. : executable(nullptr),
  281. autoPrefix(true),
  282. fallbackPrefix(nullptr),
  283. rtPrio(true),
  284. baseRtPrio(15),
  285. serverRtPrio(10) {}
  286. EngineOptions::Wine::~Wine() noexcept
  287. {
  288. if (executable != nullptr)
  289. {
  290. delete[] executable;
  291. executable = nullptr;
  292. }
  293. if (fallbackPrefix != nullptr)
  294. {
  295. delete[] fallbackPrefix;
  296. fallbackPrefix = nullptr;
  297. }
  298. }
  299. #endif
  300. // -----------------------------------------------------------------------
  301. // EngineTimeInfoBBT
  302. EngineTimeInfoBBT::EngineTimeInfoBBT() noexcept
  303. : valid(false),
  304. bar(0),
  305. beat(0),
  306. tick(0.0),
  307. barStartTick(0.0),
  308. beatsPerBar(0.0f),
  309. beatType(0.0f),
  310. ticksPerBeat(0.0),
  311. beatsPerMinute(0.0) {}
  312. EngineTimeInfoBBT::EngineTimeInfoBBT(const EngineTimeInfoBBT& bbt) noexcept
  313. : valid(bbt.valid),
  314. bar(bbt.bar),
  315. beat(bbt.beat),
  316. tick(bbt.tick),
  317. barStartTick(bbt.barStartTick),
  318. beatsPerBar(bbt.beatsPerBar),
  319. beatType(bbt.beatType),
  320. ticksPerBeat(bbt.ticksPerBeat),
  321. beatsPerMinute(bbt.beatsPerMinute) {}
  322. void EngineTimeInfoBBT::clear() noexcept
  323. {
  324. valid = false;
  325. bar = 0;
  326. beat = 0;
  327. tick = 0.0;
  328. barStartTick = 0.0;
  329. beatsPerBar = 0.0f;
  330. beatType = 0.0f;
  331. ticksPerBeat = 0.0;
  332. beatsPerMinute = 0.0;
  333. }
  334. // -----------------------------------------------------------------------
  335. // EngineTimeInfo
  336. EngineTimeInfo::EngineTimeInfo() noexcept
  337. : playing(false),
  338. frame(0),
  339. usecs(0),
  340. bbt() {}
  341. void EngineTimeInfo::clear() noexcept
  342. {
  343. playing = false;
  344. frame = 0;
  345. usecs = 0;
  346. bbt.clear();
  347. }
  348. EngineTimeInfo::EngineTimeInfo(const EngineTimeInfo& info) noexcept
  349. : playing(info.playing),
  350. frame(info.frame),
  351. usecs(info.usecs),
  352. bbt(info.bbt) {}
  353. EngineTimeInfo& EngineTimeInfo::operator=(const EngineTimeInfo& info) noexcept
  354. {
  355. playing = info.playing;
  356. frame = info.frame;
  357. usecs = info.usecs;
  358. bbt.valid = info.bbt.valid;
  359. bbt.bar = info.bbt.bar;
  360. bbt.beat = info.bbt.beat;
  361. bbt.tick = info.bbt.tick;
  362. bbt.barStartTick = info.bbt.barStartTick;
  363. bbt.beatsPerBar = info.bbt.beatsPerBar;
  364. bbt.beatType = info.bbt.beatType;
  365. bbt.ticksPerBeat = info.bbt.ticksPerBeat;
  366. bbt.beatsPerMinute = info.bbt.beatsPerMinute;
  367. return *this;
  368. }
  369. bool EngineTimeInfo::compareIgnoringRollingFrames(const EngineTimeInfo& timeInfo, const uint32_t maxFrames) const noexcept
  370. {
  371. if (timeInfo.playing != playing || timeInfo.bbt.valid != bbt.valid)
  372. return false;
  373. if (bbt.valid)
  374. {
  375. if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
  376. return false;
  377. if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
  378. return false;
  379. }
  380. // frame matches, nothing else to compare
  381. if (timeInfo.frame == frame)
  382. return true;
  383. // if we went back in time, so a case of reposition
  384. if (frame > timeInfo.frame)
  385. return false;
  386. // not playing, so don't bother checking transport
  387. // assume frame changed, likely playback has stopped
  388. if (! playing)
  389. return false;
  390. // if we are within expected bounds, assume we are rolling normally
  391. if (frame + maxFrames <= timeInfo.frame)
  392. return true;
  393. // out of bounds, another reposition
  394. return false;
  395. }
  396. bool EngineTimeInfo::operator==(const EngineTimeInfo& timeInfo) const noexcept
  397. {
  398. if (timeInfo.playing != playing || timeInfo.frame != frame || timeInfo.bbt.valid != bbt.valid)
  399. return false;
  400. if (! bbt.valid)
  401. return true;
  402. if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
  403. return false;
  404. if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
  405. return false;
  406. return true;
  407. }
  408. bool EngineTimeInfo::operator!=(const EngineTimeInfo& timeInfo) const noexcept
  409. {
  410. return !operator==(timeInfo);
  411. }
  412. // -----------------------------------------------------------------------
  413. CARLA_BACKEND_END_NAMESPACE