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 18KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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, -1, 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.midiValue, ctrl.normalizedValue);
  154. }
  155. bool CarlaEngineEventPort::writeControlEvent(const uint32_t time, const uint8_t channel,
  156. const EngineControlEventType type,
  157. const uint16_t param,
  158. const int8_t midiValue,
  159. const float normalizedValue) noexcept
  160. {
  161. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  162. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  163. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  164. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  165. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  166. CARLA_SAFE_ASSERT(normalizedValue >= 0.0f && normalizedValue <= 1.0f);
  167. if (type == kEngineControlEventTypeParameter) {
  168. CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  169. }
  170. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  171. {
  172. EngineEvent& event(fBuffer[i]);
  173. if (event.type != kEngineEventTypeNull)
  174. continue;
  175. event.type = kEngineEventTypeControl;
  176. event.time = time;
  177. event.channel = channel;
  178. event.ctrl.type = type;
  179. event.ctrl.param = param;
  180. event.ctrl.midiValue = midiValue;
  181. event.ctrl.normalizedValue = carla_fixedValue<float>(0.0f, 1.0f, normalizedValue);
  182. return true;
  183. }
  184. carla_stderr2("CarlaEngineEventPort::writeControlEvent() - buffer full");
  185. return false;
  186. }
  187. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t size, const uint8_t* const data) noexcept
  188. {
  189. return writeMidiEvent(time, uint8_t(MIDI_GET_CHANNEL_FROM_DATA(data)), size, data);
  190. }
  191. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const EngineMidiEvent& midi) noexcept
  192. {
  193. CARLA_SAFE_ASSERT(midi.port == kIndexOffset);
  194. return writeMidiEvent(time, channel, midi.size, midi.data);
  195. }
  196. bool CarlaEngineEventPort::writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept
  197. {
  198. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  199. CARLA_SAFE_ASSERT_RETURN(fBuffer != nullptr, false);
  200. CARLA_SAFE_ASSERT_RETURN(kProcessMode != ENGINE_PROCESS_MODE_SINGLE_CLIENT && kProcessMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS, false);
  201. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  202. CARLA_SAFE_ASSERT_RETURN(size > 0 && size <= EngineMidiEvent::kDataSize, false);
  203. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  204. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  205. {
  206. EngineEvent& event(fBuffer[i]);
  207. if (event.type != kEngineEventTypeNull)
  208. continue;
  209. event.time = time;
  210. event.channel = channel;
  211. const uint8_t status(uint8_t(MIDI_GET_STATUS_FROM_DATA(data)));
  212. if (status == MIDI_STATUS_CONTROL_CHANGE)
  213. {
  214. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  215. switch (data[1])
  216. {
  217. case MIDI_CONTROL_BANK_SELECT:
  218. case MIDI_CONTROL_BANK_SELECT__LSB:
  219. CARLA_SAFE_ASSERT_RETURN(size >= 3, true);
  220. event.type = kEngineEventTypeControl;
  221. event.ctrl.type = kEngineControlEventTypeMidiBank;
  222. event.ctrl.param = data[2];
  223. event.ctrl.midiValue = -1;
  224. event.ctrl.normalizedValue = 0.0f;
  225. event.ctrl.handled = true;
  226. return true;
  227. case MIDI_CONTROL_ALL_SOUND_OFF:
  228. event.type = kEngineEventTypeControl;
  229. event.ctrl.type = kEngineControlEventTypeAllSoundOff;
  230. event.ctrl.param = 0;
  231. event.ctrl.midiValue = -1;
  232. event.ctrl.normalizedValue = 0.0f;
  233. event.ctrl.handled = true;
  234. return true;
  235. case MIDI_CONTROL_ALL_NOTES_OFF:
  236. event.type = kEngineEventTypeControl;
  237. event.ctrl.type = kEngineControlEventTypeAllNotesOff;
  238. event.ctrl.param = 0;
  239. event.ctrl.midiValue = -1;
  240. event.ctrl.normalizedValue = 0.0f;
  241. event.ctrl.handled = true;
  242. return true;
  243. }
  244. }
  245. if (status == MIDI_STATUS_PROGRAM_CHANGE)
  246. {
  247. CARLA_SAFE_ASSERT_RETURN(size >= 2, true);
  248. event.type = kEngineEventTypeControl;
  249. event.ctrl.type = kEngineControlEventTypeMidiProgram;
  250. event.ctrl.param = data[1];
  251. event.ctrl.midiValue = -1;
  252. event.ctrl.normalizedValue = 0.0f;
  253. event.ctrl.handled = true;
  254. return true;
  255. }
  256. event.type = kEngineEventTypeMidi;
  257. event.midi.size = size;
  258. if (kIndexOffset < 0xFF /* uint8_t max */)
  259. {
  260. event.midi.port = static_cast<uint8_t>(kIndexOffset);
  261. }
  262. else
  263. {
  264. event.midi.port = 0;
  265. carla_safe_assert_uint("kIndexOffset < 0xFF", __FILE__, __LINE__, kIndexOffset);
  266. }
  267. event.midi.data[0] = status;
  268. uint8_t j=1;
  269. for (; j < size; ++j)
  270. event.midi.data[j] = data[j];
  271. for (; j < EngineMidiEvent::kDataSize; ++j)
  272. event.midi.data[j] = 0;
  273. return true;
  274. }
  275. carla_stderr2("CarlaEngineEventPort::writeMidiEvent() - buffer full");
  276. return false;
  277. }
  278. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  279. // -----------------------------------------------------------------------
  280. // Carla Engine Meta CV port
  281. CarlaEngineCVSourcePorts::CarlaEngineCVSourcePorts()
  282. : pData(new ProtectedData())
  283. {
  284. carla_debug("CarlaEngineCVSourcePorts::CarlaEngineCVSourcePorts()");
  285. }
  286. CarlaEngineCVSourcePorts::~CarlaEngineCVSourcePorts()
  287. {
  288. carla_debug("CarlaEngineCVSourcePorts::~CarlaEngineCVSourcePorts()");
  289. delete pData;
  290. }
  291. bool CarlaEngineCVSourcePorts::addCVSource(CarlaEngineCVPort* const port,
  292. const uint32_t portIndexOffset,
  293. const bool reconfigureNow)
  294. {
  295. CARLA_SAFE_ASSERT_RETURN(port != nullptr, false);
  296. CARLA_SAFE_ASSERT_RETURN(port->isInput(), false);
  297. carla_debug("CarlaEngineCVSourcePorts::addCVSource(%p, %u)", port, portIndexOffset);
  298. {
  299. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  300. const CarlaEngineEventCV ecv = { port, portIndexOffset, 0.0f };
  301. if (! pData->cvs.add(ecv))
  302. return false;
  303. if (reconfigureNow && pData->graph != nullptr && pData->plugin.get() != nullptr)
  304. pData->graph->reconfigureForCV(pData->plugin, static_cast<uint>(pData->cvs.size()-1), true);
  305. }
  306. return true;
  307. }
  308. bool CarlaEngineCVSourcePorts::removeCVSource(const uint32_t portIndexOffset)
  309. {
  310. carla_debug("CarlaEngineCVSourcePorts::removeCVSource(%u)", portIndexOffset);
  311. {
  312. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  313. for (int i = pData->cvs.size(); --i >= 0;)
  314. {
  315. const CarlaEngineEventCV& ecv(pData->cvs[i]);
  316. if (ecv.indexOffset == portIndexOffset)
  317. {
  318. delete ecv.cvPort;
  319. pData->cvs.remove(i);
  320. if (pData->graph != nullptr && pData->plugin.get() != nullptr)
  321. pData->graph->reconfigureForCV(pData->plugin, static_cast<uint>(i), false);
  322. carla_stdout("found cv source to remove %u", portIndexOffset);
  323. return true;
  324. }
  325. }
  326. }
  327. carla_stdout("did NOT found cv source to remove %u", portIndexOffset);
  328. return false;
  329. }
  330. void CarlaEngineCVSourcePorts::initPortBuffers(const float* const* const buffers,
  331. const uint32_t frames,
  332. const bool sampleAccurate,
  333. CarlaEngineEventPort* const eventPort)
  334. {
  335. CARLA_SAFE_ASSERT_RETURN(buffers != nullptr,);
  336. CARLA_SAFE_ASSERT_RETURN(eventPort != nullptr,);
  337. const CarlaRecursiveMutexTryLocker crmtl(pData->rmutex);
  338. if (! crmtl.wasLocked())
  339. return;
  340. const int numCVs = pData->cvs.size();
  341. if (numCVs == 0)
  342. return;
  343. EngineEvent* const buffer = eventPort->fBuffer;
  344. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  345. uint32_t eventCount = 0;
  346. float v, min, max;
  347. for (; eventCount < kMaxEngineEventInternalCount; ++eventCount)
  348. {
  349. if (buffer[eventCount].type == kEngineEventTypeNull)
  350. break;
  351. }
  352. if (eventCount == kMaxEngineEventInternalCount)
  353. return;
  354. // TODO be sample accurate
  355. if (true || ! sampleAccurate)
  356. {
  357. const uint32_t eventFrame = eventCount == 0 ? 0 : std::min(buffer[eventCount-1].time, frames-1U);
  358. for (int i = 0; i < numCVs && eventCount < kMaxEngineEventInternalCount; ++i)
  359. {
  360. CarlaEngineEventCV& ecv(pData->cvs.getReference(i));
  361. CARLA_SAFE_ASSERT_CONTINUE(ecv.cvPort != nullptr);
  362. CARLA_SAFE_ASSERT_CONTINUE(buffers[i] != nullptr);
  363. float previousValue = ecv.previousValue;
  364. ecv.cvPort->getRange(min, max);
  365. v = buffers[i][eventFrame];
  366. if (carla_isNotEqual(v, previousValue))
  367. {
  368. previousValue = v;
  369. EngineEvent& event(buffer[eventCount++]);
  370. event.type = kEngineEventTypeControl;
  371. event.time = eventFrame;
  372. event.channel = kEngineEventNonMidiChannel;
  373. event.ctrl.type = kEngineControlEventTypeParameter;
  374. event.ctrl.param = static_cast<uint16_t>(ecv.indexOffset);
  375. event.ctrl.midiValue = -1;
  376. event.ctrl.normalizedValue = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  377. }
  378. ecv.previousValue = previousValue;
  379. }
  380. }
  381. }
  382. bool CarlaEngineCVSourcePorts::setCVSourceRange(const uint32_t portIndexOffset, const float minimum, const float maximum)
  383. {
  384. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  385. for (int i = pData->cvs.size(); --i >= 0;)
  386. {
  387. CarlaEngineEventCV& ecv(pData->cvs.getReference(i));
  388. if (ecv.indexOffset == portIndexOffset)
  389. {
  390. CARLA_SAFE_ASSERT_RETURN(ecv.cvPort != nullptr, false);
  391. ecv.cvPort->setRange(minimum, maximum);
  392. return true;
  393. }
  394. }
  395. return false;
  396. }
  397. void CarlaEngineCVSourcePorts::cleanup()
  398. {
  399. pData->cleanup();
  400. }
  401. /*
  402. void CarlaEngineCVSourcePorts::mixWithCvBuffer(const float* const buffer,
  403. const uint32_t frames,
  404. const uint32_t indexOffset) noexcept
  405. {
  406. for (LinkedList<CarlaEngineEventCV>::Itenerator it = pData->cvs.begin2(); it.valid(); it.next())
  407. {
  408. CarlaEngineEventCV& ecv(it.getValue(kFallbackEngineEventCV));
  409. if (ecv.indexOffset != indexOffset)
  410. continue;
  411. CARLA_SAFE_ASSERT_RETURN(ecv.cvPort != nullptr,);
  412. float previousValue = ecv.previousValue;
  413. ecv.cvPort->getRange(min, max);
  414. for (uint32_t i=0; i<frames; i+=32)
  415. {
  416. v = buffer[i];
  417. if (carla_isNotEqual(v, previousValue))
  418. {
  419. previousValue = v;
  420. EngineEvent& event(pData->buffer[eventIndex++]);
  421. event.type = kEngineEventTypeControl;
  422. event.time = i;
  423. event.channel = kEngineEventNonMidiChannel;
  424. event.ctrl.type = kEngineControlEventTypeParameter;
  425. event.ctrl.param = static_cast<uint16_t>(indexOffset);
  426. event.ctrl.value = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  427. }
  428. }
  429. ecv.previousValue = previousValue;
  430. break;
  431. }
  432. }
  433. */
  434. #endif
  435. // -----------------------------------------------------------------------
  436. CARLA_BACKEND_END_NAMESPACE