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.

286 lines
7.9KB

  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 "CarlaEngineInternal.hpp"
  18. #include "CarlaMIDI.h"
  19. #include "CarlaMathUtils.hpp"
  20. // -----------------------------------------------------------------------
  21. CARLA_BACKEND_START_NAMESPACE
  22. #if 0
  23. } // Fix editor indentation
  24. #endif
  25. // -----------------------------------------------------------------------
  26. // EngineControlEvent
  27. void EngineControlEvent::dumpToMidiData(const uint8_t channel, uint8_t& size, uint8_t data[3]) const noexcept
  28. {
  29. size = 0;
  30. switch (type)
  31. {
  32. case kEngineControlEventTypeNull:
  33. break;
  34. case kEngineControlEventTypeParameter:
  35. if (param >= MAX_MIDI_VALUE)
  36. {
  37. // out of bounds. do nothing
  38. }
  39. else if (MIDI_IS_CONTROL_BANK_SELECT(param))
  40. {
  41. size = 3;
  42. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  43. data[1] = MIDI_CONTROL_BANK_SELECT;
  44. data[2] = uint8_t(carla_fixValue<float>(0.0f, float(MAX_MIDI_VALUE-1), value));
  45. }
  46. else
  47. {
  48. size = 3;
  49. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  50. data[1] = static_cast<uint8_t>(param);
  51. data[2] = uint8_t(carla_fixValue<float>(0.0f, 1.0f, value) * float(MAX_MIDI_VALUE-1));
  52. }
  53. break;
  54. case kEngineControlEventTypeMidiBank:
  55. size = 3;
  56. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  57. data[1] = MIDI_CONTROL_BANK_SELECT;
  58. data[2] = uint8_t(carla_fixValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
  59. break;
  60. case kEngineControlEventTypeMidiProgram:
  61. size = 2;
  62. data[0] = static_cast<uint8_t>(MIDI_STATUS_PROGRAM_CHANGE | (channel & MIDI_CHANNEL_BIT));
  63. data[1] = uint8_t(carla_fixValue<uint16_t>(0, MAX_MIDI_VALUE-1, param));
  64. break;
  65. case kEngineControlEventTypeAllSoundOff:
  66. size = 2;
  67. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  68. data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  69. break;
  70. case kEngineControlEventTypeAllNotesOff:
  71. size = 2;
  72. data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  73. data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  74. break;
  75. }
  76. }
  77. // -----------------------------------------------------------------------
  78. // EngineEvent
  79. void EngineEvent::fillFromMidiData(const uint8_t size, const uint8_t* const data) noexcept
  80. {
  81. if (size == 0 || data == nullptr || data[0] < MIDI_STATUS_NOTE_OFF)
  82. {
  83. type = kEngineEventTypeNull;
  84. channel = 0;
  85. return;
  86. }
  87. // get channel
  88. channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data));
  89. // get status
  90. const uint8_t midiStatus(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  91. if (midiStatus == MIDI_STATUS_CONTROL_CHANGE)
  92. {
  93. type = kEngineEventTypeControl;
  94. const uint8_t midiControl(data[1]);
  95. if (MIDI_IS_CONTROL_BANK_SELECT(midiControl))
  96. {
  97. CARLA_SAFE_ASSERT_INT(size == 3, size);
  98. const uint8_t midiBank(data[2]);
  99. ctrl.type = kEngineControlEventTypeMidiBank;
  100. ctrl.param = midiBank;
  101. ctrl.value = 0.0f;
  102. }
  103. else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF)
  104. {
  105. CARLA_SAFE_ASSERT_INT(size == 2, size);
  106. ctrl.type = kEngineControlEventTypeAllSoundOff;
  107. ctrl.param = 0;
  108. ctrl.value = 0.0f;
  109. }
  110. else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF)
  111. {
  112. CARLA_SAFE_ASSERT_INT(size == 2, size);
  113. ctrl.type = kEngineControlEventTypeAllNotesOff;
  114. ctrl.param = 0;
  115. ctrl.value = 0.0f;
  116. }
  117. else
  118. {
  119. CARLA_SAFE_ASSERT_INT2(size == 3, size, midiControl);
  120. const uint8_t midiValue(carla_fixValue<uint8_t>(0, 127, data[2])); // ensures 0.0<->1.0 value range
  121. ctrl.type = kEngineControlEventTypeParameter;
  122. ctrl.param = midiControl;
  123. ctrl.value = float(midiValue)/127.0f;
  124. }
  125. }
  126. else if (midiStatus == MIDI_STATUS_PROGRAM_CHANGE)
  127. {
  128. CARLA_SAFE_ASSERT_INT2(size == 2, size, data[1]);
  129. type = kEngineEventTypeControl;
  130. const uint8_t midiProgram(data[1]);
  131. ctrl.type = kEngineControlEventTypeMidiProgram;
  132. ctrl.param = midiProgram;
  133. ctrl.value = 0.0f;
  134. }
  135. else
  136. {
  137. type = kEngineEventTypeMidi;
  138. midi.port = 0;
  139. midi.size = size;
  140. if (size > EngineMidiEvent::kDataSize)
  141. {
  142. midi.dataExt = data;
  143. std::memset(midi.data, 0, sizeof(uint8_t)*EngineMidiEvent::kDataSize);
  144. }
  145. else
  146. {
  147. midi.data[0] = midiStatus;
  148. uint8_t i=1;
  149. for (; i < midi.size; ++i)
  150. midi.data[i] = data[i];
  151. for (; i < EngineMidiEvent::kDataSize; ++i)
  152. midi.data[i] = 0;
  153. midi.dataExt = nullptr;
  154. }
  155. }
  156. }
  157. // -----------------------------------------------------------------------
  158. // EngineOptions
  159. EngineOptions::EngineOptions() noexcept
  160. #ifdef CARLA_OS_LINUX
  161. : processMode(ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  162. transportMode(ENGINE_TRANSPORT_MODE_JACK),
  163. #else
  164. : processMode(ENGINE_PROCESS_MODE_CONTINUOUS_RACK),
  165. transportMode(ENGINE_TRANSPORT_MODE_INTERNAL),
  166. #endif
  167. forceStereo(false),
  168. preferPluginBridges(false),
  169. preferUiBridges(true),
  170. uisAlwaysOnTop(true),
  171. maxParameters(MAX_DEFAULT_PARAMETERS),
  172. uiBridgesTimeout(4000),
  173. audioNumPeriods(2),
  174. audioBufferSize(512),
  175. audioSampleRate(44100),
  176. audioDevice(nullptr),
  177. binaryDir(nullptr),
  178. resourceDir(nullptr),
  179. frontendWinId(0) {}
  180. EngineOptions::~EngineOptions() noexcept
  181. {
  182. if (audioDevice != nullptr)
  183. {
  184. delete[] audioDevice;
  185. audioDevice = nullptr;
  186. }
  187. if (binaryDir != nullptr)
  188. {
  189. delete[] binaryDir;
  190. binaryDir = nullptr;
  191. }
  192. if (resourceDir != nullptr)
  193. {
  194. delete[] resourceDir;
  195. resourceDir = nullptr;
  196. }
  197. }
  198. // -----------------------------------------------------------------------
  199. // EngineTimeInfoBBT
  200. EngineTimeInfoBBT::EngineTimeInfoBBT() noexcept
  201. : bar(0),
  202. beat(0),
  203. tick(0),
  204. barStartTick(0.0),
  205. beatsPerBar(0.0f),
  206. beatType(0.0f),
  207. ticksPerBeat(0.0),
  208. beatsPerMinute(0.0) {}
  209. // -----------------------------------------------------------------------
  210. // EngineTimeInfo
  211. EngineTimeInfo::EngineTimeInfo() noexcept
  212. : playing(false),
  213. frame(0),
  214. usecs(0),
  215. valid(0x0) {}
  216. void EngineTimeInfo::clear() noexcept
  217. {
  218. playing = false;
  219. frame = 0;
  220. usecs = 0;
  221. valid = 0x0;
  222. }
  223. bool EngineTimeInfo::operator==(const EngineTimeInfo& timeInfo) const noexcept
  224. {
  225. if (timeInfo.playing != playing || timeInfo.frame != frame || timeInfo.valid != valid)
  226. return false;
  227. if ((valid & kValidBBT) == 0)
  228. return true;
  229. if (timeInfo.bbt.beatsPerMinute != bbt.beatsPerMinute)
  230. return false;
  231. return true;
  232. }
  233. bool EngineTimeInfo::operator!=(const EngineTimeInfo& timeInfo) const noexcept
  234. {
  235. return !operator==(timeInfo);
  236. }
  237. // -----------------------------------------------------------------------
  238. CARLA_BACKEND_END_NAMESPACE