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.

CarlaEnginePorts.cpp 17KB

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