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.

295 lines
10KB

  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 "CarlaEngineUtils.hpp"
  18. #include "CarlaMathUtils.hpp"
  19. #include "CarlaMIDI.h"
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -----------------------------------------------------------------------
  22. // Fallback data
  23. static const EngineEvent kFallbackEngineEvent = { kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, 0.0f }} };
  24. // -----------------------------------------------------------------------
  25. // Carla Engine port (Abstract)
  26. CarlaEnginePort::CarlaEnginePort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  27. : kClient(client),
  28. kIsInput(isInputPort),
  29. kIndexOffset(indexOffset)
  30. {
  31. carla_debug("CarlaEnginePort::CarlaEnginePort(%s)", bool2str(isInputPort));
  32. }
  33. CarlaEnginePort::~CarlaEnginePort() noexcept
  34. {
  35. carla_debug("CarlaEnginePort::~CarlaEnginePort()");
  36. }
  37. // -----------------------------------------------------------------------
  38. // Carla Engine Audio port
  39. CarlaEngineAudioPort::CarlaEngineAudioPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  40. : CarlaEnginePort(client, isInputPort, indexOffset),
  41. fBuffer(nullptr)
  42. {
  43. carla_debug("CarlaEngineAudioPort::CarlaEngineAudioPort(%s)", bool2str(isInputPort));
  44. }
  45. CarlaEngineAudioPort::~CarlaEngineAudioPort() noexcept
  46. {
  47. carla_debug("CarlaEngineAudioPort::~CarlaEngineAudioPort()");
  48. }
  49. void CarlaEngineAudioPort::initBuffer() noexcept
  50. {
  51. }
  52. // -----------------------------------------------------------------------
  53. // Carla Engine CV port
  54. CarlaEngineCVPort::CarlaEngineCVPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  55. : CarlaEnginePort(client, isInputPort, indexOffset),
  56. fBuffer(nullptr)
  57. {
  58. carla_debug("CarlaEngineCVPort::CarlaEngineCVPort(%s)", bool2str(isInputPort));
  59. }
  60. CarlaEngineCVPort::~CarlaEngineCVPort() noexcept
  61. {
  62. carla_debug("CarlaEngineCVPort::~CarlaEngineCVPort()");
  63. }
  64. void CarlaEngineCVPort::initBuffer() noexcept
  65. {
  66. }
  67. // -----------------------------------------------------------------------
  68. // Carla Engine Event port
  69. CarlaEngineEventPort::CarlaEngineEventPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  70. : CarlaEnginePort(client, isInputPort, indexOffset),
  71. fBuffer(nullptr),
  72. kProcessMode(client.getEngine().getProccessMode())
  73. {
  74. carla_debug("CarlaEngineEventPort::CarlaEngineEventPort(%s)", bool2str(isInputPort));
  75. if (kProcessMode == ENGINE_PROCESS_MODE_PATCHBAY)
  76. fBuffer = new EngineEvent[kMaxEngineEventInternalCount];
  77. }
  78. CarlaEngineEventPort::~CarlaEngineEventPort() noexcept
  79. {
  80. carla_debug("CarlaEngineEventPort::~CarlaEngineEventPort()");
  81. if (kProcessMode == ENGINE_PROCESS_MODE_PATCHBAY)
  82. {
  83. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr,);
  84. delete[] fBuffer;
  85. fBuffer = nullptr;
  86. }
  87. }
  88. void CarlaEngineEventPort::initBuffer() noexcept
  89. {
  90. if (kProcessMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || kProcessMode == ENGINE_PROCESS_MODE_BRIDGE)
  91. fBuffer = kClient.getEngine().getInternalEventBuffer(kIsInput);
  92. else if (kProcessMode == ENGINE_PROCESS_MODE_PATCHBAY && ! kIsInput)
  93. carla_zeroStructs(fBuffer, kMaxEngineEventInternalCount);
  94. }
  95. uint32_t CarlaEngineEventPort::getEventCount() const noexcept
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(kIsInput, 0);
  98. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, 0);
  99. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, 0);
  100. uint32_t i=0;
  101. for (; i < kMaxEngineEventInternalCount; ++i)
  102. {
  103. if (fBuffer[i].type == kEngineEventTypeNull)
  104. break;
  105. }
  106. return i;
  107. }
  108. const EngineEvent& CarlaEngineEventPort::getEvent(const uint32_t index) const noexcept
  109. {
  110. CARLA_SAFE_ASSERT_RETURN(kIsInput, kFallbackEngineEvent);
  111. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, kFallbackEngineEvent);
  112. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, kFallbackEngineEvent);
  113. CARLA_SAFE_ASSERT_RETURN(index < kMaxEngineEventInternalCount, kFallbackEngineEvent);
  114. return fBuffer[index];
  115. }
  116. const EngineEvent& CarlaEngineEventPort::getEventUnchecked(const uint32_t index) const noexcept
  117. {
  118. return fBuffer[index];
  119. }
  120. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEvent& ctrl) noexcept
  121. {
  122. return writeControlEvent(time, channel, ctrl.type, ctrl.param, ctrl.value);
  123. }
  124. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) noexcept
  125. {
  126. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  127. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  128. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  129. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  130. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  131. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  132. if (type == kEngineControlEventTypeParameter) {
  133. CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  134. }
  135. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  136. {
  137. EngineEvent& event(fBuffer[i]);
  138. if (event.type != kEngineEventTypeNull)
  139. continue;
  140. event.type = kEngineEventTypeControl;
  141. event.time = time;
  142. event.channel = channel;
  143. event.ctrl.type = type;
  144. event.ctrl.param = param;
  145. event.ctrl.value = carla_fixedValue<float>(0.0f, 1.0f, value);
  146. return true;
  147. }
  148. carla_stderr2("CarlaEngineEventPort::writeControlEvent() - buffer full");
  149. return false;
  150. }
  151. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t size, const uint8_t* const data) noexcept
  152. {
  153. return writeMidiEvent(time, uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data)), size, data);
  154. }
  155. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const EngineMidiEvent& midi) noexcept
  156. {
  157. CARLA_SAFE_ASSERT(midi.port == kIndexOffset);
  158. return writeMidiEvent(time, channel, midi.size, midi.data);
  159. }
  160. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  163. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  164. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  165. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  166. CARLA_SAFE_ASSERT_RETURN(size > 0 && size <= EngineMidiEvent::kDataSize, false);
  167. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  168. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  169. {
  170. EngineEvent& event(fBuffer[i]);
  171. if (event.type != kEngineEventTypeNull)
  172. continue;
  173. event.time = time;
  174. event.channel = channel;
  175. const uint8_t status(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  176. if (status == MIDI_STATUS_CONTROL_CHANGE)
  177. {
  178. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  179. switch (data[1])
  180. {
  181. case MIDI_CONTROL_BANK_SELECT:
  182. case MIDI_CONTROL_BANK_SELECT__LSB:
  183. CARLA_SAFE_ASSERT_RETURN(size >= 3, true);
  184. event.type = kEngineEventTypeControl;
  185. event.ctrl.type = kEngineControlEventTypeMidiBank;
  186. event.ctrl.param = data[2];
  187. event.ctrl.value = 0.0f;
  188. return true;
  189. case MIDI_CONTROL_ALL_SOUND_OFF:
  190. event.type = kEngineEventTypeControl;
  191. event.ctrl.type = kEngineControlEventTypeAllSoundOff;
  192. event.ctrl.param = 0;
  193. event.ctrl.value = 0.0f;
  194. return true;
  195. case MIDI_CONTROL_ALL_NOTES_OFF:
  196. event.type = kEngineEventTypeControl;
  197. event.ctrl.type = kEngineControlEventTypeAllNotesOff;
  198. event.ctrl.param = 0;
  199. event.ctrl.value = 0.0f;
  200. return true;
  201. }
  202. }
  203. if (status == MIDI_STATUS_PROGRAM_CHANGE)
  204. {
  205. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  206. event.type = kEngineEventTypeControl;
  207. event.ctrl.type = kEngineControlEventTypeMidiBank;
  208. event.ctrl.param = data[1];
  209. event.ctrl.value = 0.0f;
  210. return true;
  211. }
  212. event.type = kEngineEventTypeMidi;
  213. event.midi.size = size;
  214. if (kIndexOffset < 0xFF /* uint8_t max */)
  215. {
  216. event.midi.port = static_cast<uint8_t>(kIndexOffset);
  217. }
  218. else
  219. {
  220. event.midi.port = 0;
  221. carla_safe_assert_uint("kIndexOffset < 0xFF", __FILE__, __LINE__, kIndexOffset);
  222. }
  223. event.midi.data[0] = status;
  224. uint8_t j=1;
  225. for (; j < size; ++j)
  226. event.midi.data[j] = data[j];
  227. for (; j < EngineMidiEvent::kDataSize; ++j)
  228. event.midi.data[j] = 0;
  229. return true;
  230. }
  231. carla_stderr2("CarlaEngineEventPort::writeMidiEvent() - buffer full");
  232. return false;
  233. }
  234. // -----------------------------------------------------------------------
  235. CARLA_BACKEND_END_NAMESPACE