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.

537 lines
17KB

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