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.

459 lines
15KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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 "CarlaEngineUtils.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "lv2/lv2.h"
  22. CARLA_BACKEND_START_NAMESPACE
  23. // -----------------------------------------------------------------------
  24. // Fallback data
  25. static const EngineEvent kFallbackEngineEvent = { kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, 0.0f }} };
  26. static CarlaEngineEventCV kFallbackEngineEventCV = { nullptr, (uint32_t)-1, 0.0f };
  27. // -----------------------------------------------------------------------
  28. // Carla Engine port (Abstract)
  29. CarlaEnginePort::CarlaEnginePort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  30. : kClient(client),
  31. kIsInput(isInputPort),
  32. kIndexOffset(indexOffset)
  33. {
  34. carla_debug("CarlaEnginePort::CarlaEnginePort(%s)", bool2str(isInputPort));
  35. }
  36. CarlaEnginePort::~CarlaEnginePort() noexcept
  37. {
  38. carla_debug("CarlaEnginePort::~CarlaEnginePort()");
  39. }
  40. void CarlaEnginePort::setMetaData(const char*, const char*, const char*)
  41. {
  42. }
  43. // -----------------------------------------------------------------------
  44. // Carla Engine Audio port
  45. CarlaEngineAudioPort::CarlaEngineAudioPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  46. : CarlaEnginePort(client, isInputPort, indexOffset),
  47. fBuffer(nullptr)
  48. {
  49. carla_debug("CarlaEngineAudioPort::CarlaEngineAudioPort(%s)", bool2str(isInputPort));
  50. }
  51. CarlaEngineAudioPort::~CarlaEngineAudioPort() noexcept
  52. {
  53. carla_debug("CarlaEngineAudioPort::~CarlaEngineAudioPort()");
  54. }
  55. void CarlaEngineAudioPort::initBuffer() noexcept
  56. {
  57. }
  58. // -----------------------------------------------------------------------
  59. // Carla Engine CV port
  60. CarlaEngineCVPort::CarlaEngineCVPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  61. : CarlaEnginePort(client, isInputPort, indexOffset),
  62. fBuffer(nullptr),
  63. fMinimum(-1.0f),
  64. fMaximum(1.0f)
  65. {
  66. carla_debug("CarlaEngineCVPort::CarlaEngineCVPort(%s)", bool2str(isInputPort));
  67. }
  68. CarlaEngineCVPort::~CarlaEngineCVPort() noexcept
  69. {
  70. carla_debug("CarlaEngineCVPort::~CarlaEngineCVPort()");
  71. }
  72. void CarlaEngineCVPort::initBuffer() noexcept
  73. {
  74. }
  75. void CarlaEngineCVPort::setRange(const float min, const float max) noexcept
  76. {
  77. fMinimum = min;
  78. fMaximum = max;
  79. char strBufMin[STR_MAX];
  80. char strBufMax[STR_MAX];
  81. carla_zeroChars(strBufMin, STR_MAX);
  82. carla_zeroChars(strBufMax, STR_MAX);
  83. {
  84. const CarlaScopedLocale csl;
  85. std::snprintf(strBufMin, STR_MAX-1, "%f", static_cast<double>(min));
  86. std::snprintf(strBufMax, STR_MAX-1, "%f", static_cast<double>(max));
  87. }
  88. setMetaData(LV2_CORE__minimum, strBufMin, "");
  89. setMetaData(LV2_CORE__maximum, strBufMax, "");
  90. }
  91. // -----------------------------------------------------------------------
  92. // Carla Engine Event port
  93. CarlaEngineEventPort::ProtectedData::ProtectedData(const EngineProcessMode pm) noexcept
  94. : buffer(nullptr),
  95. needsBufferDeletion(false),
  96. processMode(pm),
  97. cvs()
  98. {
  99. if (processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  100. {
  101. buffer = new EngineEvent[kMaxEngineEventInternalCount];
  102. carla_zeroStructs(buffer, kMaxEngineEventInternalCount);
  103. needsBufferDeletion = true;
  104. }
  105. }
  106. CarlaEngineEventPort::ProtectedData::~ProtectedData() noexcept
  107. {
  108. for (LinkedList<CarlaEngineEventCV>::Itenerator it = cvs.begin2(); it.valid(); it.next())
  109. delete it.getValue(kFallbackEngineEventCV).cvPort;
  110. cvs.clear();
  111. if (needsBufferDeletion)
  112. {
  113. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  114. delete[] buffer;
  115. buffer = nullptr;
  116. }
  117. }
  118. CarlaEngineEventPort::CarlaEngineEventPort(const CarlaEngineClient& client, const bool isInputPort, const uint32_t indexOffset) noexcept
  119. : CarlaEnginePort(client, isInputPort, indexOffset),
  120. pData(new ProtectedData(client.getEngine().getProccessMode()))
  121. {
  122. carla_debug("CarlaEngineEventPort::CarlaEngineEventPort(%s)", bool2str(isInputPort));
  123. }
  124. CarlaEngineEventPort::~CarlaEngineEventPort() noexcept
  125. {
  126. carla_debug("CarlaEngineEventPort::~CarlaEngineEventPort()");
  127. delete pData;
  128. }
  129. void CarlaEngineEventPort::initBuffer() noexcept
  130. {
  131. if (pData->processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->processMode == ENGINE_PROCESS_MODE_BRIDGE)
  132. pData->buffer = kClient.getEngine().getInternalEventBuffer(kIsInput);
  133. else if (pData->processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! kIsInput)
  134. carla_zeroStructs(pData->buffer, kMaxEngineEventInternalCount);
  135. initCvBuffers();
  136. }
  137. uint32_t CarlaEngineEventPort::getCVSourceCount() const noexcept
  138. {
  139. return static_cast<uint32_t>(pData->cvs.count());
  140. }
  141. CarlaEngineCVPort* CarlaEngineEventPort::getCVSourcePort(const uint32_t portIndexOffset) const noexcept
  142. {
  143. const CarlaEngineEventCV& ecv(pData->cvs.getAt(portIndexOffset, kFallbackEngineEventCV));
  144. // for (LinkedList<CarlaEngineEventCV>::Itenerator it = pData->cvs.begin2(); it.valid(); it.next())
  145. // {
  146. // const CarlaEngineEventCV& ecv(it.getValue(kFallbackEngineEventCV));
  147. // if (ecv.indexOffset == portIndexOffset)
  148. return ecv.cvPort;
  149. // }
  150. // return nullptr;
  151. }
  152. bool CarlaEngineEventPort::addCVSource(CarlaEngineCVPort* const port, const uint32_t portIndexOffset) noexcept
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(port != nullptr, false);
  155. CARLA_SAFE_ASSERT_RETURN(port->isInput(), false);
  156. carla_debug("CarlaEngineEventPort::addCVSource(%p)", port);
  157. const CarlaEngineEventCV ecv = { port, portIndexOffset, 0.0f };
  158. return pData->cvs.append(ecv);
  159. }
  160. bool CarlaEngineEventPort::removeCVSource(const uint32_t portIndexOffset) noexcept
  161. {
  162. carla_debug("CarlaEngineEventPort::removeCVSource(%u)", portIndexOffset);
  163. for (LinkedList<CarlaEngineEventCV>::Itenerator it = pData->cvs.begin2(); it.valid(); it.next())
  164. {
  165. CarlaEngineEventCV& ecv(it.getValue(kFallbackEngineEventCV));
  166. if (ecv.indexOffset == portIndexOffset)
  167. {
  168. pData->cvs.remove(it);
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. void CarlaEngineEventPort::mixWithCvBuffer(const float* const buffer,
  175. const uint32_t frames,
  176. const uint32_t indexOffset) noexcept
  177. {
  178. CARLA_SAFE_ASSERT_RETURN(pData->buffer != nullptr,)
  179. CARLA_SAFE_ASSERT_RETURN(kIsInput,);
  180. uint32_t eventIndex = 0;
  181. float v, min, max;
  182. for (; eventIndex < kMaxEngineEventInternalCount; ++eventIndex)
  183. {
  184. if (pData->buffer[eventIndex].type == kEngineEventTypeNull)
  185. break;
  186. }
  187. if (eventIndex == kMaxEngineEventInternalCount)
  188. return;
  189. for (LinkedList<CarlaEngineEventCV>::Itenerator it = pData->cvs.begin2(); it.valid(); it.next())
  190. {
  191. CarlaEngineEventCV& ecv(it.getValue(kFallbackEngineEventCV));
  192. if (ecv.indexOffset != indexOffset)
  193. continue;
  194. CARLA_SAFE_ASSERT_RETURN(ecv.cvPort != nullptr,);
  195. float previousValue = ecv.previousValue;
  196. ecv.cvPort->getRange(min, max);
  197. for (uint32_t i=0; i<frames; i+=32)
  198. {
  199. v = buffer[i];
  200. if (carla_isNotEqual(v, previousValue))
  201. {
  202. previousValue = v;
  203. EngineEvent& event(pData->buffer[eventIndex++]);
  204. event.type = kEngineEventTypeControl;
  205. event.time = i;
  206. event.channel = kEngineEventNonMidiChannel;
  207. event.ctrl.type = kEngineControlEventTypeParameter;
  208. event.ctrl.param = static_cast<uint16_t>(indexOffset);
  209. event.ctrl.value = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  210. }
  211. }
  212. ecv.previousValue = previousValue;
  213. break;
  214. }
  215. }
  216. uint32_t CarlaEngineEventPort::getEventCount() const noexcept
  217. {
  218. CARLA_SAFE_ASSERT_RETURN(kIsInput, 0);
  219. CARLA_SAFE_ASSERT_RETURN(pData->buffer != nullptr, 0);
  220. CARLA_SAFE_ASSERT_RETURN(pData->processMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && pData->processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, 0);
  221. uint32_t i=0;
  222. for (; i < kMaxEngineEventInternalCount; ++i)
  223. {
  224. if (pData->buffer[i].type == kEngineEventTypeNull)
  225. break;
  226. }
  227. return i;
  228. }
  229. const EngineEvent& CarlaEngineEventPort::getEvent(const uint32_t index) const noexcept
  230. {
  231. CARLA_SAFE_ASSERT_RETURN(kIsInput, kFallbackEngineEvent);
  232. CARLA_SAFE_ASSERT_RETURN(pData->buffer != nullptr, kFallbackEngineEvent);
  233. CARLA_SAFE_ASSERT_RETURN(pData->processMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && pData->processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, kFallbackEngineEvent);
  234. CARLA_SAFE_ASSERT_RETURN(index < kMaxEngineEventInternalCount, kFallbackEngineEvent);
  235. return pData->buffer[index];
  236. }
  237. const EngineEvent& CarlaEngineEventPort::getEventUnchecked(const uint32_t index) const noexcept
  238. {
  239. return pData->buffer[index];
  240. }
  241. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEvent& ctrl) noexcept
  242. {
  243. return writeControlEvent(time, channel, ctrl.type, ctrl.param, ctrl.value);
  244. }
  245. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) noexcept
  246. {
  247. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  248. CARLA_SAFE_ASSERT_RETURN(pData->buffer != nullptr, false);
  249. CARLA_SAFE_ASSERT_RETURN(pData->processMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && pData->processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  250. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  251. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  252. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  253. if (type == kEngineControlEventTypeParameter) {
  254. CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  255. }
  256. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  257. {
  258. EngineEvent& event(pData->buffer[i]);
  259. if (event.type != kEngineEventTypeNull)
  260. continue;
  261. event.type = kEngineEventTypeControl;
  262. event.time = time;
  263. event.channel = channel;
  264. event.ctrl.type = type;
  265. event.ctrl.param = param;
  266. event.ctrl.value = carla_fixedValue<float>(0.0f, 1.0f, value);
  267. return true;
  268. }
  269. carla_stderr2("CarlaEngineEventPort::writeControlEvent() - buffer full");
  270. return false;
  271. }
  272. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t size, const uint8_t* const data) noexcept
  273. {
  274. return writeMidiEvent(time, uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data)), size, data);
  275. }
  276. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const EngineMidiEvent& midi) noexcept
  277. {
  278. CARLA_SAFE_ASSERT(midi.port == kIndexOffset);
  279. return writeMidiEvent(time, channel, midi.size, midi.data);
  280. }
  281. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept
  282. {
  283. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  284. CARLA_SAFE_ASSERT_RETURN(pData->buffer != nullptr, false);
  285. CARLA_SAFE_ASSERT_RETURN(pData->processMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && pData->processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  286. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  287. CARLA_SAFE_ASSERT_RETURN(size > 0 && size <= EngineMidiEvent::kDataSize, false);
  288. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  289. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  290. {
  291. EngineEvent& event(pData->buffer[i]);
  292. if (event.type != kEngineEventTypeNull)
  293. continue;
  294. event.time = time;
  295. event.channel = channel;
  296. const uint8_t status(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  297. if (status == MIDI_STATUS_CONTROL_CHANGE)
  298. {
  299. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  300. switch (data[1])
  301. {
  302. case MIDI_CONTROL_BANK_SELECT:
  303. case MIDI_CONTROL_BANK_SELECT__LSB:
  304. CARLA_SAFE_ASSERT_RETURN(size >= 3, true);
  305. event.type = kEngineEventTypeControl;
  306. event.ctrl.type = kEngineControlEventTypeMidiBank;
  307. event.ctrl.param = data[2];
  308. event.ctrl.value = 0.0f;
  309. return true;
  310. case MIDI_CONTROL_ALL_SOUND_OFF:
  311. event.type = kEngineEventTypeControl;
  312. event.ctrl.type = kEngineControlEventTypeAllSoundOff;
  313. event.ctrl.param = 0;
  314. event.ctrl.value = 0.0f;
  315. return true;
  316. case MIDI_CONTROL_ALL_NOTES_OFF:
  317. event.type = kEngineEventTypeControl;
  318. event.ctrl.type = kEngineControlEventTypeAllNotesOff;
  319. event.ctrl.param = 0;
  320. event.ctrl.value = 0.0f;
  321. return true;
  322. }
  323. }
  324. if (status == MIDI_STATUS_PROGRAM_CHANGE)
  325. {
  326. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  327. event.type = kEngineEventTypeControl;
  328. event.ctrl.type = kEngineControlEventTypeMidiProgram;
  329. event.ctrl.param = data[1];
  330. event.ctrl.value = 0.0f;
  331. return true;
  332. }
  333. event.type = kEngineEventTypeMidi;
  334. event.midi.size = size;
  335. if (kIndexOffset < 0xFF /* uint8_t max */)
  336. {
  337. event.midi.port = static_cast<uint8_t>(kIndexOffset);
  338. }
  339. else
  340. {
  341. event.midi.port = 0;
  342. carla_safe_assert_uint("kIndexOffset < 0xFF", __FILE__, __LINE__, kIndexOffset);
  343. }
  344. event.midi.data[0] = status;
  345. uint8_t j=1;
  346. for (; j < size; ++j)
  347. event.midi.data[j] = data[j];
  348. for (; j < EngineMidiEvent::kDataSize; ++j)
  349. event.midi.data[j] = 0;
  350. return true;
  351. }
  352. carla_stderr2("CarlaEngineEventPort::writeMidiEvent() - buffer full");
  353. return false;
  354. }
  355. void CarlaEngineEventPort::initCvBuffers() noexcept
  356. {
  357. for (LinkedList<CarlaEngineEventCV>::Itenerator it = pData->cvs.begin2(); it.valid(); it.next())
  358. {
  359. CarlaEngineEventCV& ecv(it.getValue(kFallbackEngineEventCV));
  360. CARLA_SAFE_ASSERT_CONTINUE(ecv.cvPort != nullptr);
  361. ecv.cvPort->initBuffer();
  362. }
  363. }
  364. // -----------------------------------------------------------------------
  365. CARLA_BACKEND_END_NAMESPACE