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.

CarlaEngineData.cpp 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 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. void EngineControlEvent::convertToMidiData(const uint8_t channel, uint8_t& size, uint8_t data[3]) const noexcept
  24. {
  25. size = 0;
  26. switch (type)
  27. {
  28. case kEngineControlEventTypeNull:
  29. break;
  30. case kEngineControlEventTypeParameter:
  31. if (param >= MAX_MIDI_VALUE)
  32. {
  33. // out of bounds. do nothing
  34. }
  35. else if (MIDI_IS_CONTROL_BANK_SELECT(param))
  36. {
  37. size = 3;
  38. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  39. data[1] = MIDI_CONTROL_BANK_SELECT;
  40. data[2] = uint8_t(carla_fixedValue<float>(0.0f, float(MAX_MIDI_VALUE-1), value));
  41. }
  42. else
  43. {
  44. size = 3;
  45. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  46. data[1] = static_cast<uint8_t>(param);
  47. data[2] = uint8_t(carla_fixedValue<float>(0.0f, 1.0f, value) * float(MAX_MIDI_VALUE-1));
  48. }
  49. break;
  50. case kEngineControlEventTypeMidiBank:
  51. size = 3;
  52. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  53. data[1] = MIDI_CONTROL_BANK_SELECT;
  54. data[2] = uint8_t(carla_fixedValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
  55. break;
  56. case kEngineControlEventTypeMidiProgram:
  57. size = 2;
  58. data[0] = static_cast<uint8_t>(MIDI_STATUS_PROGRAM_CHANGE | (channel & MIDI_CHANNEL_BIT));
  59. data[1] = uint8_t(carla_fixedValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
  60. break;
  61. case kEngineControlEventTypeAllSoundOff:
  62. size = 2;
  63. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  64. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  65. break;
  66. case kEngineControlEventTypeAllNotesOff:
  67. size = 2;
  68. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  69. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  70. break;
  71. }
  72. }
  73. // -----------------------------------------------------------------------
  74. // EngineEvent
  75. void EngineEvent::fillFromMidiData(const uint8_t size, const uint8_t* const data, const uint8_t midiPortOffset) noexcept
  76. {
  77. if (size == 0 || data == nullptr || data[0] < MIDI_STATUS_NOTE_OFF)
  78. {
  79. type = kEngineEventTypeNull;
  80. channel = 0;
  81. return;
  82. }
  83. // get channel
  84. channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data));
  85. // get status
  86. const uint8_t midiStatus(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  87. if (midiStatus == MIDI_STATUS_CONTROL_CHANGE)
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(size >= 2,);
  90. type = kEngineEventTypeControl;
  91. const uint8_t midiControl(data[1]);
  92. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  93. {
  94. CARLA_SAFE_ASSERT_RETURN(size >= 3,);
  95. const uint8_t midiBank(data[2]);
  96. ctrl.type = kEngineControlEventTypeMidiBank;
  97. ctrl.param = midiBank;
  98. ctrl.value = 0.0f;
  99. }
  100. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  101. {
  102. ctrl.type = kEngineControlEventTypeAllSoundOff;
  103. ctrl.param = 0;
  104. ctrl.value = 0.0f;
  105. }
  106. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  107. {
  108. ctrl.type = kEngineControlEventTypeAllNotesOff;
  109. ctrl.param = 0;
  110. ctrl.value = 0.0f;
  111. }
  112. else
  113. {
  114. CARLA_SAFE_ASSERT_RETURN(size >= 3,);
  115. const uint8_t midiValue(carla_fixedValue<uint8_t>(0, 127, data[2])); // ensures 0.0<->1.0 value range
  116. ctrl.type = kEngineControlEventTypeParameter;
  117. ctrl.param = midiControl;
  118. ctrl.value = float(midiValue)/127.0f;
  119. }
  120. }
  121. else if (midiStatus == MIDI_STATUS_PROGRAM_CHANGE)
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(size >= 2,);
  124. type = kEngineEventTypeControl;
  125. const uint8_t midiProgram(data[1]);
  126. ctrl.type = kEngineControlEventTypeMidiProgram;
  127. ctrl.param = midiProgram;
  128. ctrl.value = 0.0f;
  129. }
  130. else
  131. {
  132. type = kEngineEventTypeMidi;
  133. midi.port = midiPortOffset;
  134. midi.size = size;
  135. if (size > EngineMidiEvent::kDataSize)
  136. {
  137. midi.dataExt = data;
  138. std::memset(midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  139. }
  140. else
  141. {
  142. midi.data[0] = midiStatus;
  143. uint8_t i=1;
  144. for (; i < size; ++i)
  145. midi.data[i] = data[i];
  146. for (; i < EngineMidiEvent::kDataSize; ++i)
  147. midi.data[i] = 0;
  148. midi.dataExt = nullptr;
  149. }
  150. }
  151. }
  152. // -----------------------------------------------------------------------
  153. // EngineOptions
  154. EngineOptions::EngineOptions() noexcept
  155. #ifdef CARLA_OS_LINUX
  156. : processMode(ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  157. transportMode(ENGINE_TRANSPORT_MODE_JACK),
  158. #else
  159. : processMode(ENGINE_PROCESS_MODE_PATCHBAY),
  160. transportMode(ENGINE_TRANSPORT_MODE_INTERNAL),
  161. #endif
  162. transportExtra(nullptr),
  163. forceStereo(false),
  164. preferPluginBridges(false),
  165. #ifdef CARLA_OS_WIN
  166. preferUiBridges(false),
  167. #else
  168. preferUiBridges(true),
  169. #endif
  170. uisAlwaysOnTop(true),
  171. maxParameters(MAX_DEFAULT_PARAMETERS),
  172. uiBridgesTimeout(4000),
  173. audioNumPeriods(2),
  174. audioBufferSize(512),
  175. audioSampleRate(44100),
  176. audioDevice(nullptr),
  177. pathLADSPA(nullptr),
  178. pathDSSI(nullptr),
  179. pathLV2(nullptr),
  180. pathVST2(nullptr),
  181. pathGIG(nullptr),
  182. pathSF2(nullptr),
  183. pathSFZ(nullptr),
  184. binaryDir(nullptr),
  185. resourceDir(nullptr),
  186. preventBadBehaviour(false),
  187. frontendWinId(0),
  188. wine() {}
  189. EngineOptions::~EngineOptions() noexcept
  190. {
  191. if (audioDevice != nullptr)
  192. {
  193. delete[] audioDevice;
  194. audioDevice = nullptr;
  195. }
  196. if (pathLADSPA != nullptr)
  197. {
  198. delete[] pathLADSPA;
  199. pathLADSPA = nullptr;
  200. }
  201. if (pathDSSI != nullptr)
  202. {
  203. delete[] pathDSSI;
  204. pathDSSI = nullptr;
  205. }
  206. if (pathLV2 != nullptr)
  207. {
  208. delete[] pathLV2;
  209. pathLV2 = nullptr;
  210. }
  211. if (pathVST2 != nullptr)
  212. {
  213. delete[] pathVST2;
  214. pathVST2 = nullptr;
  215. }
  216. if (pathGIG != nullptr)
  217. {
  218. delete[] pathGIG;
  219. pathGIG = nullptr;
  220. }
  221. if (pathSF2 != nullptr)
  222. {
  223. delete[] pathSF2;
  224. pathSF2 = nullptr;
  225. }
  226. if (pathSFZ != nullptr)
  227. {
  228. delete[] pathSFZ;
  229. pathSFZ = nullptr;
  230. }
  231. if (binaryDir != nullptr)
  232. {
  233. delete[] binaryDir;
  234. binaryDir = nullptr;
  235. }
  236. if (resourceDir != nullptr)
  237. {
  238. delete[] resourceDir;
  239. resourceDir = nullptr;
  240. }
  241. }
  242. EngineOptions::Wine::Wine() noexcept
  243. : executable(nullptr),
  244. autoPrefix(true),
  245. fallbackPrefix(nullptr),
  246. rtPrio(true),
  247. baseRtPrio(15),
  248. serverRtPrio(10) {}
  249. EngineOptions::Wine::~Wine() noexcept
  250. {
  251. if (executable != nullptr)
  252. {
  253. delete[] executable;
  254. executable = nullptr;
  255. }
  256. if (fallbackPrefix != nullptr)
  257. {
  258. delete[] fallbackPrefix;
  259. fallbackPrefix = nullptr;
  260. }
  261. }
  262. // -----------------------------------------------------------------------
  263. // EngineTimeInfoBBT
  264. EngineTimeInfoBBT::EngineTimeInfoBBT() noexcept
  265. : bar(0),
  266. beat(0),
  267. tick(0),
  268. barStartTick(0.0),
  269. beatsPerBar(0.0f),
  270. beatType(0.0f),
  271. ticksPerBeat(0.0),
  272. beatsPerMinute(0.0) {}
  273. // -----------------------------------------------------------------------
  274. // EngineTimeInfo
  275. EngineTimeInfo::EngineTimeInfo() noexcept
  276. : playing(false),
  277. frame(0),
  278. usecs(0),
  279. valid(0x0),
  280. bbt() {}
  281. void EngineTimeInfo::clear() noexcept
  282. {
  283. playing = false;
  284. frame = 0;
  285. usecs = 0;
  286. valid = 0x0;
  287. }
  288. bool EngineTimeInfo::operator==(const EngineTimeInfo& timeInfo) const noexcept
  289. {
  290. if (timeInfo.playing != playing || timeInfo.frame != frame || timeInfo.valid != valid)
  291. return false;
  292. if ((valid & kValidBBT) == 0)
  293. return true;
  294. if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
  295. return false;
  296. return true;
  297. }
  298. bool EngineTimeInfo::operator!=(const EngineTimeInfo& timeInfo) const noexcept
  299. {
  300. return !operator==(timeInfo);
  301. }
  302. // -----------------------------------------------------------------------
  303. CARLA_BACKEND_END_NAMESPACE