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.

467 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. }
  90. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  91. {
  92. ctrl.type = kEngineControlEventTypeAllSoundOff;
  93. ctrl.param = 0;
  94. ctrl.value = 0.0f;
  95. }
  96. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  97. {
  98. ctrl.type = kEngineControlEventTypeAllNotesOff;
  99. ctrl.param = 0;
  100. ctrl.value = 0.0f;
  101. }
  102. else
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(size >= 3,);
  105. const uint8_t midiValue = carla_fixedValue<uint8_t>(0, 127, data[2]); // ensures 0.0<->1.0 value range
  106. ctrl.type = kEngineControlEventTypeParameter;
  107. ctrl.param = midiControl;
  108. ctrl.value = float(midiValue)/127.0f;
  109. }
  110. }
  111. else if (midiStatus == MIDI_STATUS_PROGRAM_CHANGE)
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(size >= 2,);
  114. type = kEngineEventTypeControl;
  115. const uint8_t midiProgram(data[1]);
  116. ctrl.type = kEngineControlEventTypeMidiProgram;
  117. ctrl.param = midiProgram;
  118. ctrl.value = 0.0f;
  119. }
  120. else
  121. {
  122. type = kEngineEventTypeMidi;
  123. midi.port = midiPortOffset;
  124. midi.size = size;
  125. if (size > EngineMidiEvent::kDataSize)
  126. {
  127. midi.dataExt = data;
  128. std::memset(midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  129. }
  130. else
  131. {
  132. midi.data[0] = midiStatus;
  133. uint8_t i=1;
  134. for (; i < size; ++i)
  135. midi.data[i] = data[i];
  136. for (; i < EngineMidiEvent::kDataSize; ++i)
  137. midi.data[i] = 0;
  138. midi.dataExt = nullptr;
  139. }
  140. }
  141. }
  142. // -----------------------------------------------------------------------
  143. // EngineOptions
  144. EngineOptions::EngineOptions() noexcept
  145. #ifdef CARLA_OS_LINUX
  146. : processMode(ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  147. transportMode(ENGINE_TRANSPORT_MODE_JACK),
  148. #else
  149. : processMode(ENGINE_PROCESS_MODE_PATCHBAY),
  150. transportMode(ENGINE_TRANSPORT_MODE_INTERNAL),
  151. #endif
  152. transportExtra(nullptr),
  153. forceStereo(false),
  154. resetXruns(false),
  155. preferPluginBridges(false),
  156. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  157. preferUiBridges(false),
  158. #else
  159. preferUiBridges(true),
  160. #endif
  161. uisAlwaysOnTop(true),
  162. bgColor(0x000000ff),
  163. fgColor(0xffffffff),
  164. uiScale(1.0f),
  165. maxParameters(MAX_DEFAULT_PARAMETERS),
  166. uiBridgesTimeout(4000),
  167. audioBufferSize(512),
  168. audioSampleRate(44100),
  169. audioTripleBuffer(false),
  170. audioDriver(nullptr),
  171. audioDevice(nullptr),
  172. #ifndef BUILD_BRIDGE
  173. # ifdef CARLA_OS_WIN
  174. oscEnabled(false),
  175. # else
  176. oscEnabled(true),
  177. # endif
  178. oscPortTCP(22752),
  179. oscPortUDP(22752),
  180. #endif
  181. pathAudio(nullptr),
  182. pathMIDI(nullptr),
  183. pathLADSPA(nullptr),
  184. pathDSSI(nullptr),
  185. pathLV2(nullptr),
  186. pathVST2(nullptr),
  187. pathVST3(nullptr),
  188. pathSF2(nullptr),
  189. pathSFZ(nullptr),
  190. binaryDir(nullptr),
  191. resourceDir(nullptr),
  192. clientNamePrefix(nullptr),
  193. preventBadBehaviour(false),
  194. frontendWinId(0)
  195. #ifndef CARLA_OS_WIN
  196. , wine()
  197. #endif
  198. {
  199. }
  200. EngineOptions::~EngineOptions() noexcept
  201. {
  202. if (audioDriver != nullptr)
  203. {
  204. delete[] audioDriver;
  205. audioDriver = nullptr;
  206. }
  207. if (audioDevice != nullptr)
  208. {
  209. delete[] audioDevice;
  210. audioDevice = nullptr;
  211. }
  212. if (pathAudio != nullptr)
  213. {
  214. delete[] pathAudio;
  215. pathAudio = nullptr;
  216. }
  217. if (pathMIDI != nullptr)
  218. {
  219. delete[] pathMIDI;
  220. pathMIDI = nullptr;
  221. }
  222. if (pathLADSPA != nullptr)
  223. {
  224. delete[] pathLADSPA;
  225. pathLADSPA = nullptr;
  226. }
  227. if (pathDSSI != nullptr)
  228. {
  229. delete[] pathDSSI;
  230. pathDSSI = nullptr;
  231. }
  232. if (pathLV2 != nullptr)
  233. {
  234. delete[] pathLV2;
  235. pathLV2 = nullptr;
  236. }
  237. if (pathVST2 != nullptr)
  238. {
  239. delete[] pathVST2;
  240. pathVST2 = nullptr;
  241. }
  242. if (pathVST3 != nullptr)
  243. {
  244. delete[] pathVST3;
  245. pathVST3 = nullptr;
  246. }
  247. if (pathSF2 != nullptr)
  248. {
  249. delete[] pathSF2;
  250. pathSF2 = nullptr;
  251. }
  252. if (pathSFZ != nullptr)
  253. {
  254. delete[] pathSFZ;
  255. pathSFZ = nullptr;
  256. }
  257. if (binaryDir != nullptr)
  258. {
  259. delete[] binaryDir;
  260. binaryDir = nullptr;
  261. }
  262. if (resourceDir != nullptr)
  263. {
  264. delete[] resourceDir;
  265. resourceDir = nullptr;
  266. }
  267. if (clientNamePrefix != nullptr)
  268. {
  269. delete[] clientNamePrefix;
  270. clientNamePrefix = nullptr;
  271. }
  272. }
  273. #ifndef CARLA_OS_WIN
  274. EngineOptions::Wine::Wine() noexcept
  275. : executable(nullptr),
  276. autoPrefix(true),
  277. fallbackPrefix(nullptr),
  278. rtPrio(true),
  279. baseRtPrio(15),
  280. serverRtPrio(10) {}
  281. EngineOptions::Wine::~Wine() noexcept
  282. {
  283. if (executable != nullptr)
  284. {
  285. delete[] executable;
  286. executable = nullptr;
  287. }
  288. if (fallbackPrefix != nullptr)
  289. {
  290. delete[] fallbackPrefix;
  291. fallbackPrefix = nullptr;
  292. }
  293. }
  294. #endif
  295. // -----------------------------------------------------------------------
  296. // EngineTimeInfoBBT
  297. EngineTimeInfoBBT::EngineTimeInfoBBT() noexcept
  298. : valid(false),
  299. bar(0),
  300. beat(0),
  301. tick(0.0),
  302. barStartTick(0.0),
  303. beatsPerBar(0.0f),
  304. beatType(0.0f),
  305. ticksPerBeat(0.0),
  306. beatsPerMinute(0.0) {}
  307. EngineTimeInfoBBT::EngineTimeInfoBBT(const EngineTimeInfoBBT& bbt) noexcept
  308. : valid(bbt.valid),
  309. bar(bbt.bar),
  310. beat(bbt.beat),
  311. tick(bbt.tick),
  312. barStartTick(bbt.barStartTick),
  313. beatsPerBar(bbt.beatsPerBar),
  314. beatType(bbt.beatType),
  315. ticksPerBeat(bbt.ticksPerBeat),
  316. beatsPerMinute(bbt.beatsPerMinute) {}
  317. void EngineTimeInfoBBT::clear() noexcept
  318. {
  319. valid = false;
  320. bar = 0;
  321. beat = 0;
  322. tick = 0.0;
  323. barStartTick = 0.0;
  324. beatsPerBar = 0.0f;
  325. beatType = 0.0f;
  326. ticksPerBeat = 0.0;
  327. beatsPerMinute = 0.0;
  328. }
  329. // -----------------------------------------------------------------------
  330. // EngineTimeInfo
  331. EngineTimeInfo::EngineTimeInfo() noexcept
  332. : playing(false),
  333. frame(0),
  334. usecs(0),
  335. bbt() {}
  336. void EngineTimeInfo::clear() noexcept
  337. {
  338. playing = false;
  339. frame = 0;
  340. usecs = 0;
  341. bbt.clear();
  342. }
  343. EngineTimeInfo::EngineTimeInfo(const EngineTimeInfo& info) noexcept
  344. : playing(info.playing),
  345. frame(info.frame),
  346. usecs(info.usecs),
  347. bbt(info.bbt) {}
  348. EngineTimeInfo& EngineTimeInfo::operator=(const EngineTimeInfo& info) noexcept
  349. {
  350. playing = info.playing;
  351. frame = info.frame;
  352. usecs = info.usecs;
  353. bbt.valid = info.bbt.valid;
  354. bbt.bar = info.bbt.bar;
  355. bbt.beat = info.bbt.beat;
  356. bbt.tick = info.bbt.tick;
  357. bbt.barStartTick = info.bbt.barStartTick;
  358. bbt.beatsPerBar = info.bbt.beatsPerBar;
  359. bbt.beatType = info.bbt.beatType;
  360. bbt.ticksPerBeat = info.bbt.ticksPerBeat;
  361. bbt.beatsPerMinute = info.bbt.beatsPerMinute;
  362. return *this;
  363. }
  364. bool EngineTimeInfo::compareIgnoringRollingFrames(const EngineTimeInfo& timeInfo, const uint32_t maxFrames) const noexcept
  365. {
  366. if (timeInfo.playing != playing || timeInfo.bbt.valid != bbt.valid)
  367. return false;
  368. if (bbt.valid)
  369. {
  370. if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
  371. return false;
  372. if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
  373. return false;
  374. }
  375. // frame matches, nothing else to compare
  376. if (timeInfo.frame == frame)
  377. return true;
  378. // if we went back in time, so a case of reposition
  379. if (frame > timeInfo.frame)
  380. return false;
  381. // not playing, so don't bother checking transport
  382. // assume frame changed, likely playback has stopped
  383. if (! playing)
  384. return false;
  385. // if we are within expected bounds, assume we are rolling normally
  386. if (frame + maxFrames <= timeInfo.frame)
  387. return true;
  388. // out of bounds, another reposition
  389. return false;
  390. }
  391. bool EngineTimeInfo::operator==(const EngineTimeInfo& timeInfo) const noexcept
  392. {
  393. if (timeInfo.playing != playing || timeInfo.frame != frame || timeInfo.bbt.valid != bbt.valid)
  394. return false;
  395. if (! bbt.valid)
  396. return true;
  397. if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
  398. return false;
  399. if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
  400. return false;
  401. return true;
  402. }
  403. bool EngineTimeInfo::operator!=(const EngineTimeInfo& timeInfo) const noexcept
  404. {
  405. return !operator==(timeInfo);
  406. }
  407. // -----------------------------------------------------------------------
  408. CARLA_BACKEND_END_NAMESPACE