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.

535 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, const uint32_t portIndexOffset)
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(port != nullptr, false);
  280. CARLA_SAFE_ASSERT_RETURN(port->isInput(), false);
  281. carla_debug("CarlaEngineCVSourcePorts::addCVSource(%p)", port);
  282. {
  283. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  284. const CarlaEngineEventCV ecv = { port, portIndexOffset, 0.0f };
  285. if (! pData->cvs.add(ecv))
  286. return false;
  287. if (pData->graph != nullptr && pData->plugin != nullptr)
  288. pData->graph->reconfigureForCV(pData->plugin, static_cast<uint>(pData->cvs.size()-1), true);
  289. }
  290. return true;
  291. }
  292. bool CarlaEngineCVSourcePorts::removeCVSource(const uint32_t portIndexOffset)
  293. {
  294. carla_debug("CarlaEngineCVSourcePorts::removeCVSource(%u)", portIndexOffset);
  295. {
  296. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  297. for (int i = pData->cvs.size(); --i >= 0;)
  298. {
  299. const CarlaEngineEventCV& ecv(pData->cvs[i]);
  300. if (ecv.indexOffset == portIndexOffset)
  301. {
  302. delete ecv.cvPort;
  303. pData->cvs.remove(i);
  304. if (pData->graph != nullptr && pData->plugin != nullptr)
  305. pData->graph->reconfigureForCV(pData->plugin, static_cast<uint>(i), false);
  306. carla_stdout("found cv source to remove %u", portIndexOffset);
  307. return true;
  308. }
  309. }
  310. }
  311. carla_stdout("did NOT found cv source to remove %u", portIndexOffset);
  312. return false;
  313. }
  314. void CarlaEngineCVSourcePorts::initPortBuffers(const float* const* const buffers,
  315. const uint32_t frames,
  316. const bool sampleAccurate,
  317. CarlaEngineEventPort* const eventPort)
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(buffers != nullptr,);
  320. CARLA_SAFE_ASSERT_RETURN(eventPort != nullptr,);
  321. const CarlaRecursiveMutexTryLocker crmtl(pData->rmutex);
  322. if (! crmtl.wasLocked())
  323. return;
  324. const int numCVs = pData->cvs.size();
  325. if (numCVs == 0)
  326. return;
  327. EngineEvent* const buffer = eventPort->fBuffer;
  328. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  329. uint32_t eventCount = 0;
  330. float v, min, max;
  331. for (; eventCount < kMaxEngineEventInternalCount; ++eventCount)
  332. {
  333. if (buffer[eventCount].type == kEngineEventTypeNull)
  334. break;
  335. }
  336. if (eventCount == kMaxEngineEventInternalCount)
  337. return;
  338. // TODO be sample accurate
  339. if (true || ! sampleAccurate)
  340. {
  341. const uint32_t eventFrame = eventCount == 0 ? 0 : std::min(buffer[eventCount-1].time, frames-1U);
  342. for (int i = 0; i < numCVs && eventCount < kMaxEngineEventInternalCount; ++i)
  343. {
  344. CarlaEngineEventCV& ecv(pData->cvs.getReference(i));
  345. CARLA_SAFE_ASSERT_CONTINUE(ecv.cvPort != nullptr);
  346. CARLA_SAFE_ASSERT_CONTINUE(buffers[i] != nullptr);
  347. float previousValue = ecv.previousValue;
  348. ecv.cvPort->getRange(min, max);
  349. v = buffers[i][eventFrame];
  350. if (carla_isNotEqual(v, previousValue))
  351. {
  352. previousValue = v;
  353. EngineEvent& event(buffer[eventCount++]);
  354. event.type = kEngineEventTypeControl;
  355. event.time = eventFrame;
  356. event.channel = kEngineEventNonMidiChannel;
  357. event.ctrl.type = kEngineControlEventTypeParameter;
  358. event.ctrl.param = static_cast<uint16_t>(ecv.indexOffset);
  359. event.ctrl.value = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  360. }
  361. ecv.previousValue = previousValue;
  362. }
  363. }
  364. }
  365. bool CarlaEngineCVSourcePorts::setCVSourceRange(const uint32_t portIndexOffset, const float minimum, const float maximum)
  366. {
  367. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  368. for (int i = pData->cvs.size(); --i >= 0;)
  369. {
  370. CarlaEngineEventCV& ecv(pData->cvs.getReference(i));
  371. if (ecv.indexOffset == portIndexOffset)
  372. {
  373. CARLA_SAFE_ASSERT_RETURN(ecv.cvPort != nullptr, false);
  374. ecv.cvPort->setRange(minimum, maximum);
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. void CarlaEngineCVSourcePorts::cleanup()
  381. {
  382. pData->cleanup();
  383. }
  384. /*
  385. void CarlaEngineCVSourcePorts::mixWithCvBuffer(const float* const buffer,
  386. const uint32_t frames,
  387. const uint32_t indexOffset) noexcept
  388. {
  389. for (LinkedList<CarlaEngineEventCV>::Itenerator it = pData->cvs.begin2(); it.valid(); it.next())
  390. {
  391. CarlaEngineEventCV& ecv(it.getValue(kFallbackEngineEventCV));
  392. if (ecv.indexOffset != indexOffset)
  393. continue;
  394. CARLA_SAFE_ASSERT_RETURN(ecv.cvPort != nullptr,);
  395. float previousValue = ecv.previousValue;
  396. ecv.cvPort->getRange(min, max);
  397. for (uint32_t i=0; i<frames; i+=32)
  398. {
  399. v = buffer[i];
  400. if (carla_isNotEqual(v, previousValue))
  401. {
  402. previousValue = v;
  403. EngineEvent& event(pData->buffer[eventIndex++]);
  404. event.type = kEngineEventTypeControl;
  405. event.time = i;
  406. event.channel = kEngineEventNonMidiChannel;
  407. event.ctrl.type = kEngineControlEventTypeParameter;
  408. event.ctrl.param = static_cast<uint16_t>(indexOffset);
  409. event.ctrl.value = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  410. }
  411. }
  412. ecv.previousValue = previousValue;
  413. break;
  414. }
  415. }
  416. */
  417. #endif
  418. // -----------------------------------------------------------------------
  419. CARLA_BACKEND_END_NAMESPACE