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.

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