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.

281 lines
9.5KB

  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. }
  76. CarlaEngineEventPort::~CarlaEngineEventPort() noexcept
  77. {
  78. carla_debug("CarlaEngineEventPort::~CarlaEngineEventPort()");
  79. }
  80. void CarlaEngineEventPort::initBuffer() noexcept
  81. {
  82. if (kProcessMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || kProcessMode == ENGINE_PROCESS_MODE_BRIDGE)
  83. fBuffer = kClient.getEngine().getInternalEventBuffer(kIsInput);
  84. }
  85. uint32_t CarlaEngineEventPort::getEventCount() const noexcept
  86. {
  87. CARLA_SAFE_ASSERT_RETURN(kIsInput, 0);
  88. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, 0);
  89. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, 0);
  90. uint32_t i=0;
  91. for (; i < kMaxEngineEventInternalCount; ++i)
  92. {
  93. if (fBuffer[i].type == kEngineEventTypeNull)
  94. break;
  95. }
  96. return i;
  97. }
  98. const EngineEvent& CarlaEngineEventPort::getEvent(const uint32_t index) const noexcept
  99. {
  100. CARLA_SAFE_ASSERT_RETURN(kIsInput, kFallbackEngineEvent);
  101. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, kFallbackEngineEvent);
  102. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, kFallbackEngineEvent);
  103. CARLA_SAFE_ASSERT_RETURN(index < kMaxEngineEventInternalCount, kFallbackEngineEvent);
  104. return fBuffer[index];
  105. }
  106. const EngineEvent& CarlaEngineEventPort::getEventUnchecked(const uint32_t index) const noexcept
  107. {
  108. return fBuffer[index];
  109. }
  110. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEvent& ctrl) noexcept
  111. {
  112. return writeControlEvent(time, channel, ctrl.type, ctrl.param, ctrl.value);
  113. }
  114. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) noexcept
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  117. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  118. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  119. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  120. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  121. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  122. if (type == kEngineControlEventTypeParameter) {
  123. CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  124. }
  125. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  126. {
  127. EngineEvent& event(fBuffer[i]);
  128. if (event.type != kEngineEventTypeNull)
  129. continue;
  130. event.type = kEngineEventTypeControl;
  131. event.time = time;
  132. event.channel = channel;
  133. event.ctrl.type = type;
  134. event.ctrl.param = param;
  135. event.ctrl.value = carla_fixedValue<float>(0.0f, 1.0f, value);
  136. return true;
  137. }
  138. carla_stderr2("CarlaEngineEventPort::writeControlEvent() - buffer full");
  139. return false;
  140. }
  141. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t size, const uint8_t* const data) noexcept
  142. {
  143. return writeMidiEvent(time, uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data)), size, data);
  144. }
  145. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const EngineMidiEvent& midi) noexcept
  146. {
  147. CARLA_SAFE_ASSERT(midi.port == kIndexOffset);
  148. return writeMidiEvent(time, channel, midi.size, midi.data);
  149. }
  150. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept
  151. {
  152. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  153. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  154. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  155. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  156. CARLA_SAFE_ASSERT_RETURN(size > 0 && size <= EngineMidiEvent::kDataSize, false);
  157. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  158. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  159. {
  160. EngineEvent& event(fBuffer[i]);
  161. if (event.type != kEngineEventTypeNull)
  162. continue;
  163. event.time = time;
  164. event.channel = channel;
  165. const uint8_t status(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  166. if (status == MIDI_STATUS_CONTROL_CHANGE)
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(size >= 3, true);
  169. switch (data[1])
  170. {
  171. case MIDI_CONTROL_BANK_SELECT:
  172. case MIDI_CONTROL_BANK_SELECT__LSB:
  173. event.type = kEngineEventTypeControl;
  174. event.ctrl.type = kEngineControlEventTypeMidiBank;
  175. event.ctrl.param = data[2];
  176. event.ctrl.value = 0.0f;
  177. return true;
  178. case MIDI_CONTROL_ALL_SOUND_OFF:
  179. event.type = kEngineEventTypeControl;
  180. event.ctrl.type = kEngineControlEventTypeAllSoundOff;
  181. event.ctrl.param = 0;
  182. event.ctrl.value = 0.0f;
  183. return true;
  184. case MIDI_CONTROL_ALL_NOTES_OFF:
  185. event.type = kEngineEventTypeControl;
  186. event.ctrl.type = kEngineControlEventTypeAllNotesOff;
  187. event.ctrl.param = 0;
  188. event.ctrl.value = 0.0f;
  189. return true;
  190. }
  191. }
  192. if (status == MIDI_STATUS_PROGRAM_CHANGE)
  193. {
  194. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  195. event.type = kEngineEventTypeControl;
  196. event.ctrl.type = kEngineControlEventTypeMidiBank;
  197. event.ctrl.param = data[1];
  198. event.ctrl.value = 0.0f;
  199. return true;
  200. }
  201. event.type = kEngineEventTypeMidi;
  202. event.midi.size = size;
  203. if (kIndexOffset < 0xFF /* uint8_t max */)
  204. {
  205. event.midi.port = static_cast<uint8_t>(kIndexOffset);
  206. }
  207. else
  208. {
  209. event.midi.port = 0;
  210. carla_safe_assert_uint("kIndexOffset < 0xFF", __FILE__, __LINE__, kIndexOffset);
  211. }
  212. event.midi.data[0] = status;
  213. uint8_t j=1;
  214. for (; j < size; ++j)
  215. event.midi.data[j] = data[j];
  216. for (; j < EngineMidiEvent::kDataSize; ++j)
  217. event.midi.data[j] = 0;
  218. return true;
  219. }
  220. carla_stderr2("CarlaEngineEventPort::writeMidiEvent() - buffer full");
  221. return false;
  222. }
  223. // -----------------------------------------------------------------------
  224. CARLA_BACKEND_END_NAMESPACE