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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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. preferPluginBridges(false),
  155. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  156. preferUiBridges(false),
  157. #else
  158. preferUiBridges(true),
  159. #endif
  160. uisAlwaysOnTop(true),
  161. maxParameters(MAX_DEFAULT_PARAMETERS),
  162. uiBridgesTimeout(4000),
  163. audioNumPeriods(2),
  164. audioBufferSize(512),
  165. audioSampleRate(44100),
  166. audioDevice(nullptr),
  167. pathLADSPA(nullptr),
  168. pathDSSI(nullptr),
  169. pathLV2(nullptr),
  170. pathVST2(nullptr),
  171. pathGIG(nullptr),
  172. pathSF2(nullptr),
  173. pathSFZ(nullptr),
  174. binaryDir(nullptr),
  175. resourceDir(nullptr),
  176. preventBadBehaviour(false),
  177. frontendWinId(0) {}
  178. EngineOptions::~EngineOptions() noexcept
  179. {
  180. if (audioDevice != nullptr)
  181. {
  182. delete[] audioDevice;
  183. audioDevice = nullptr;
  184. }
  185. if (pathLADSPA != nullptr)
  186. {
  187. delete[] pathLADSPA;
  188. pathLADSPA = nullptr;
  189. }
  190. if (pathDSSI != nullptr)
  191. {
  192. delete[] pathDSSI;
  193. pathDSSI = nullptr;
  194. }
  195. if (pathLV2 != nullptr)
  196. {
  197. delete[] pathLV2;
  198. pathLV2 = nullptr;
  199. }
  200. if (pathVST2 != nullptr)
  201. {
  202. delete[] pathVST2;
  203. pathVST2 = nullptr;
  204. }
  205. if (pathGIG != nullptr)
  206. {
  207. delete[] pathGIG;
  208. pathGIG = nullptr;
  209. }
  210. if (pathSF2 != nullptr)
  211. {
  212. delete[] pathSF2;
  213. pathSF2 = nullptr;
  214. }
  215. if (pathSFZ != nullptr)
  216. {
  217. delete[] pathSFZ;
  218. pathSFZ = nullptr;
  219. }
  220. if (binaryDir != nullptr)
  221. {
  222. delete[] binaryDir;
  223. binaryDir = nullptr;
  224. }
  225. if (resourceDir != nullptr)
  226. {
  227. delete[] resourceDir;
  228. resourceDir = nullptr;
  229. }
  230. }
  231. #ifndef CARLA_OS_WIN
  232. EngineOptions::Wine::Wine() noexcept
  233. : executable(nullptr),
  234. autoPrefix(true),
  235. fallbackPrefix(nullptr),
  236. rtPrio(true),
  237. baseRtPrio(15),
  238. serverRtPrio(10) {}
  239. EngineOptions::Wine::~Wine() noexcept
  240. {
  241. if (executable != nullptr)
  242. {
  243. delete[] executable;
  244. executable = nullptr;
  245. }
  246. if (fallbackPrefix != nullptr)
  247. {
  248. delete[] fallbackPrefix;
  249. fallbackPrefix = nullptr;
  250. }
  251. }
  252. #endif
  253. // -----------------------------------------------------------------------
  254. // EngineTimeInfoBBT
  255. EngineTimeInfoBBT::EngineTimeInfoBBT() noexcept
  256. : valid(false),
  257. bar(0),
  258. beat(0),
  259. tick(0),
  260. barStartTick(0.0),
  261. beatsPerBar(0.0f),
  262. beatType(0.0f),
  263. ticksPerBeat(0.0),
  264. beatsPerMinute(0.0) {}
  265. // -----------------------------------------------------------------------
  266. // EngineTimeInfo
  267. EngineTimeInfo::EngineTimeInfo() noexcept
  268. : playing(false),
  269. frame(0),
  270. usecs(0),
  271. bbt() {}
  272. void EngineTimeInfo::clear() noexcept
  273. {
  274. playing = false;
  275. frame = 0;
  276. usecs = 0;
  277. carla_zeroStruct(bbt);
  278. }
  279. bool EngineTimeInfo::operator==(const EngineTimeInfo& timeInfo) const noexcept
  280. {
  281. if (timeInfo.playing != playing || timeInfo.frame != frame || timeInfo.bbt.valid != bbt.valid)
  282. return false;
  283. if (! bbt.valid)
  284. return true;
  285. if (carla_isNotEqual(timeInfo.bbt.beatsPerBar, bbt.beatsPerBar))
  286. return false;
  287. if (carla_isNotEqual(timeInfo.bbt.beatsPerMinute, bbt.beatsPerMinute))
  288. return false;
  289. return true;
  290. }
  291. bool EngineTimeInfo::operator!=(const EngineTimeInfo& timeInfo) const noexcept
  292. {
  293. return !operator==(timeInfo);
  294. }
  295. // -----------------------------------------------------------------------
  296. CARLA_BACKEND_END_NAMESPACE