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.

5940 lines
224KB

  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2014 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. // testing macros
  18. // #define LV2_UIS_ONLY_BRIDGES
  19. // #define LV2_UIS_ONLY_INPROCESS
  20. #include "CarlaPluginInternal.hpp"
  21. #include "CarlaEngine.hpp"
  22. #include "CarlaLv2Utils.hpp"
  23. #include "CarlaBase64Utils.hpp"
  24. #include "CarlaPluginUI.hpp"
  25. #include "Lv2AtomRingBuffer.hpp"
  26. #include "../engine/CarlaEngineOsc.hpp"
  27. extern "C" {
  28. #include "rtmempool/rtmempool-lv2.h"
  29. }
  30. #include "juce_core.h"
  31. using juce::File;
  32. #define URI_CARLA_ATOM_WORKER "http://kxstudio.sf.net/ns/carla/atomWorker"
  33. CARLA_BACKEND_START_NAMESPACE
  34. // -----------------------------------------------------
  35. // Maximum default buffer size
  36. const uint MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  37. // Extra Plugin Hints
  38. const uint PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  39. const uint PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  40. const uint PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  41. const uint PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  42. // Extra Parameter Hints
  43. const uint PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  44. const uint PARAMETER_IS_TRIGGER = 0x2000;
  45. // LV2 Event Data/Types
  46. const uint CARLA_EVENT_DATA_ATOM = 0x01;
  47. const uint CARLA_EVENT_DATA_EVENT = 0x02;
  48. const uint CARLA_EVENT_DATA_MIDI_LL = 0x04;
  49. const uint CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  50. const uint CARLA_EVENT_TYPE_MIDI = 0x20;
  51. const uint CARLA_EVENT_TYPE_TIME = 0x40;
  52. // LV2 URI Map Ids
  53. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  56. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  57. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  58. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 22;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 23;
  77. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 24;
  78. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 25;
  79. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 26;
  80. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 27;
  81. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 28;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 29;
  83. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 30;
  84. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 31; // base type
  85. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 32; // values
  86. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 33;
  87. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 34;
  88. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 35;
  89. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 36;
  90. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 37;
  91. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 38;
  92. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 39;
  93. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 40;
  94. const uint32_t CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT = 41;
  95. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 42;
  96. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 43;
  97. const uint32_t CARLA_URI_MAP_ID_UI_WINDOW_TITLE = 44;
  98. const uint32_t CARLA_URI_MAP_ID_CARLA_ATOM_WORKER = 45;
  99. const uint32_t CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID = 46;
  100. const uint32_t CARLA_URI_MAP_ID_COUNT = 47;
  101. // LV2 Feature Ids
  102. const uint32_t kFeatureIdBufSizeBounded = 0;
  103. const uint32_t kFeatureIdBufSizeFixed = 1;
  104. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  105. const uint32_t kFeatureIdEvent = 3;
  106. const uint32_t kFeatureIdHardRtCapable = 4;
  107. const uint32_t kFeatureIdInPlaceBroken = 5;
  108. const uint32_t kFeatureIdIsLive = 6;
  109. const uint32_t kFeatureIdLogs = 7;
  110. const uint32_t kFeatureIdOptions = 8;
  111. const uint32_t kFeatureIdPrograms = 9;
  112. const uint32_t kFeatureIdResizePort = 10;
  113. const uint32_t kFeatureIdRtMemPool = 11;
  114. const uint32_t kFeatureIdRtMemPoolOld = 12;
  115. const uint32_t kFeatureIdStateMakePath = 13;
  116. const uint32_t kFeatureIdStateMapPath = 14;
  117. const uint32_t kFeatureIdStrictBounds = 15;
  118. const uint32_t kFeatureIdUriMap = 16;
  119. const uint32_t kFeatureIdUridMap = 17;
  120. const uint32_t kFeatureIdUridUnmap = 18;
  121. const uint32_t kFeatureIdWorker = 19;
  122. const uint32_t kFeatureCountPlugin = 20;
  123. const uint32_t kFeatureIdUiDataAccess = 20;
  124. const uint32_t kFeatureIdUiInstanceAccess = 21;
  125. const uint32_t kFeatureIdUiIdleInterface = 22;
  126. const uint32_t kFeatureIdUiFixedSize = 23;
  127. const uint32_t kFeatureIdUiMakeResident = 24;
  128. const uint32_t kFeatureIdUiNoUserResize = 25;
  129. const uint32_t kFeatureIdUiParent = 26;
  130. const uint32_t kFeatureIdUiPortMap = 27;
  131. const uint32_t kFeatureIdUiPortSubscribe = 28;
  132. const uint32_t kFeatureIdUiResize = 29;
  133. const uint32_t kFeatureIdUiTouch = 30;
  134. const uint32_t kFeatureIdExternalUi = 31;
  135. const uint32_t kFeatureIdExternalUiOld = 32;
  136. const uint32_t kFeatureCountAll = 33;
  137. // -----------------------------------------------------
  138. struct Lv2EventData {
  139. uint32_t type;
  140. uint32_t rindex;
  141. CarlaEngineEventPort* port;
  142. union {
  143. LV2_Atom_Buffer* atom;
  144. LV2_Event_Buffer* event;
  145. LV2_MIDI midi;
  146. };
  147. Lv2EventData() noexcept
  148. : type(0x0),
  149. rindex(0),
  150. port(nullptr) {}
  151. ~Lv2EventData() noexcept
  152. {
  153. if (port != nullptr)
  154. {
  155. delete port;
  156. port = nullptr;
  157. }
  158. const uint32_t rtype(type);
  159. type = 0x0;
  160. if (rtype & CARLA_EVENT_DATA_ATOM)
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  163. std::free(atom);
  164. atom = nullptr;
  165. }
  166. else if (rtype & CARLA_EVENT_DATA_EVENT)
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  169. std::free(event);
  170. event = nullptr;
  171. }
  172. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  173. {
  174. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  175. delete[] midi.data;
  176. midi.data = nullptr;
  177. }
  178. }
  179. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  180. };
  181. struct Lv2PluginEventData {
  182. uint32_t count;
  183. Lv2EventData* data;
  184. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  185. uint32_t ctrlIndex;
  186. Lv2PluginEventData() noexcept
  187. : count(0),
  188. data(nullptr),
  189. ctrl(nullptr),
  190. ctrlIndex(0) {}
  191. ~Lv2PluginEventData() noexcept
  192. {
  193. CARLA_SAFE_ASSERT_INT(count == 0, count);
  194. CARLA_SAFE_ASSERT(data == nullptr);
  195. CARLA_SAFE_ASSERT(ctrl == nullptr);
  196. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  197. }
  198. void createNew(const uint32_t newCount)
  199. {
  200. CARLA_SAFE_ASSERT_INT(count == 0, count);
  201. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  202. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  203. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  204. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  205. data = new Lv2EventData[newCount];
  206. count = newCount;
  207. ctrl = nullptr;
  208. ctrlIndex = 0;
  209. }
  210. void clear() noexcept
  211. {
  212. if (data != nullptr)
  213. {
  214. for (uint32_t i=0; i < count; ++i)
  215. {
  216. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  217. data[i].port = nullptr;
  218. }
  219. delete[] data;
  220. data = nullptr;
  221. }
  222. count = 0;
  223. ctrl = nullptr;
  224. ctrlIndex = 0;
  225. }
  226. void initBuffers() const noexcept
  227. {
  228. for (uint32_t i=0; i < count; ++i)
  229. {
  230. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  231. data[i].port->initBuffer();
  232. }
  233. }
  234. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginEventData)
  235. };
  236. // -----------------------------------------------------
  237. struct Lv2PluginOptions {
  238. enum OptIndex {
  239. MaxBlockLenth = 0,
  240. MinBlockLenth,
  241. SequenceSize,
  242. SampleRate,
  243. FrontendWinId,
  244. WindowTitle,
  245. Null,
  246. Count
  247. };
  248. int maxBufferSize;
  249. int minBufferSize;
  250. int sequenceSize;
  251. double sampleRate;
  252. int64_t frontendWinId;
  253. const char* windowTitle;
  254. LV2_Options_Option opts[Count];
  255. Lv2PluginOptions() noexcept
  256. : maxBufferSize(0),
  257. minBufferSize(0),
  258. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  259. sampleRate(0.0),
  260. frontendWinId(0),
  261. windowTitle(nullptr)
  262. {
  263. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  264. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  265. optMaxBlockLenth.subject = 0;
  266. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  267. optMaxBlockLenth.size = sizeof(int);
  268. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  269. optMaxBlockLenth.value = &maxBufferSize;
  270. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  271. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  272. optMinBlockLenth.subject = 0;
  273. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  274. optMinBlockLenth.size = sizeof(int);
  275. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  276. optMinBlockLenth.value = &minBufferSize;
  277. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  278. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  279. optSequenceSize.subject = 0;
  280. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  281. optSequenceSize.size = sizeof(int);
  282. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  283. optSequenceSize.value = &sequenceSize;
  284. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  285. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  286. optSampleRate.subject = 0;
  287. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  288. optSampleRate.size = sizeof(double);
  289. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  290. optSampleRate.value = &sampleRate;
  291. LV2_Options_Option& optFrontendWinId(opts[FrontendWinId]);
  292. optFrontendWinId.context = LV2_OPTIONS_INSTANCE;
  293. optFrontendWinId.subject = 0;
  294. optFrontendWinId.key = CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  295. optFrontendWinId.size = sizeof(int64_t);
  296. optFrontendWinId.type = CARLA_URI_MAP_ID_ATOM_LONG;
  297. optFrontendWinId.value = &frontendWinId;
  298. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  299. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  300. optWindowTitle.subject = 0;
  301. optWindowTitle.key = CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  302. optWindowTitle.size = 0;
  303. optWindowTitle.type = CARLA_URI_MAP_ID_ATOM_STRING;
  304. optWindowTitle.value = nullptr;
  305. LV2_Options_Option& optNull(opts[Null]);
  306. optNull.context = LV2_OPTIONS_INSTANCE;
  307. optNull.subject = 0;
  308. optNull.key = CARLA_URI_MAP_ID_NULL;
  309. optNull.size = 0;
  310. optNull.type = CARLA_URI_MAP_ID_NULL;
  311. optNull.value = nullptr;
  312. }
  313. ~Lv2PluginOptions() noexcept
  314. {
  315. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  316. optWindowTitle.size = 0;
  317. optWindowTitle.value = nullptr;
  318. if (windowTitle != nullptr)
  319. {
  320. delete[] windowTitle;
  321. windowTitle = nullptr;
  322. }
  323. }
  324. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginOptions);
  325. };
  326. // -----------------------------------------------------
  327. class Lv2Plugin : public CarlaPlugin,
  328. CarlaPluginUI::CloseCallback
  329. {
  330. public:
  331. Lv2Plugin(CarlaEngine* const engine, const uint id)
  332. : CarlaPlugin(engine, id),
  333. fHandle(nullptr),
  334. fHandle2(nullptr),
  335. fDescriptor(nullptr),
  336. fRdfDescriptor(nullptr),
  337. fAudioInBuffers(nullptr),
  338. fAudioOutBuffers(nullptr),
  339. fCvInBuffers(nullptr),
  340. fCvOutBuffers(nullptr),
  341. fParamBuffers(nullptr),
  342. fCanInit2(true),
  343. fLatencyChanged(false),
  344. fLatencyIndex(-1),
  345. fAtomBufferIn(),
  346. fAtomBufferOut(),
  347. fAtomForge(),
  348. fEventsIn(),
  349. fEventsOut(),
  350. fLv2Options(),
  351. fCustomURIDs(),
  352. fFirstActive(true),
  353. fLastStateChunk(nullptr),
  354. fLastTimeInfo(),
  355. fExt(),
  356. fUI(),
  357. leakDetector_Lv2Plugin()
  358. {
  359. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  360. carla_fill<LV2_Feature*>(fFeatures, nullptr, kFeatureCountAll+1);
  361. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  362. fCustomURIDs.append(nullptr);
  363. #if defined(__clang__)
  364. # pragma clang diagnostic push
  365. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  366. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  367. # pragma GCC diagnostic push
  368. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  369. #endif
  370. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  371. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  372. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  373. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  374. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  375. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  376. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  377. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  378. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  379. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  380. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  381. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  382. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  383. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  384. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  385. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  386. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  387. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  388. #if defined(__clang__)
  389. # pragma clang diagnostic pop
  390. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  391. # pragma GCC diagnostic pop
  392. #endif
  393. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  394. }
  395. ~Lv2Plugin() override
  396. {
  397. carla_debug("Lv2Plugin::~Lv2Plugin()");
  398. // close UI
  399. if (fUI.type != UI::TYPE_NULL)
  400. {
  401. showCustomUI(false);
  402. if (fUI.type == UI::TYPE_OSC)
  403. {
  404. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  405. }
  406. else
  407. {
  408. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  409. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  410. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  411. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  412. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  413. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  414. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  415. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  416. fUI.descriptor = nullptr;
  417. pData->uiLibClose();
  418. }
  419. #ifndef LV2_UIS_ONLY_BRIDGES
  420. if (fUI.window != nullptr)
  421. {
  422. delete fUI.window;
  423. fUI.window = nullptr;
  424. }
  425. #endif
  426. fUI.rdfDescriptor = nullptr;
  427. }
  428. pData->singleMutex.lock();
  429. pData->masterMutex.lock();
  430. if (pData->client != nullptr && pData->client->isActive())
  431. pData->client->deactivate();
  432. if (pData->active)
  433. {
  434. deactivate();
  435. pData->active = false;
  436. }
  437. if (fDescriptor != nullptr)
  438. {
  439. if (fDescriptor->cleanup != nullptr)
  440. {
  441. if (fHandle != nullptr)
  442. fDescriptor->cleanup(fHandle);
  443. if (fHandle2 != nullptr)
  444. fDescriptor->cleanup(fHandle2);
  445. }
  446. fHandle = nullptr;
  447. fHandle2 = nullptr;
  448. fDescriptor = nullptr;
  449. }
  450. if (fRdfDescriptor != nullptr)
  451. {
  452. delete fRdfDescriptor;
  453. fRdfDescriptor = nullptr;
  454. }
  455. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  456. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  457. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  458. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  459. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  460. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  461. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  462. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  463. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  464. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  465. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  466. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  467. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  468. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  469. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  470. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  471. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  472. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  473. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  474. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  475. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  476. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  477. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  478. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  479. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  480. {
  481. if (fFeatures[i] != nullptr)
  482. {
  483. delete fFeatures[i];
  484. fFeatures[i] = nullptr;
  485. }
  486. }
  487. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  488. {
  489. const char* const uri(it.getValue());
  490. if (uri != nullptr)
  491. delete[] uri;
  492. }
  493. fCustomURIDs.clear();
  494. if (fLastStateChunk != nullptr)
  495. {
  496. std::free(fLastStateChunk);
  497. fLastStateChunk = nullptr;
  498. }
  499. clearBuffers();
  500. }
  501. // -------------------------------------------------------------------
  502. // Information (base)
  503. PluginType getType() const noexcept override
  504. {
  505. return PLUGIN_LV2;
  506. }
  507. PluginCategory getCategory() const noexcept override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  510. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  511. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  512. if (LV2_IS_DELAY(cat1, cat2))
  513. return PLUGIN_CATEGORY_DELAY;
  514. if (LV2_IS_DISTORTION(cat1, cat2))
  515. return PLUGIN_CATEGORY_OTHER;
  516. if (LV2_IS_DYNAMICS(cat1, cat2))
  517. return PLUGIN_CATEGORY_DYNAMICS;
  518. if (LV2_IS_EQ(cat1, cat2))
  519. return PLUGIN_CATEGORY_EQ;
  520. if (LV2_IS_FILTER(cat1, cat2))
  521. return PLUGIN_CATEGORY_FILTER;
  522. if (LV2_IS_GENERATOR(cat1, cat2))
  523. return PLUGIN_CATEGORY_SYNTH;
  524. if (LV2_IS_MODULATOR(cat1, cat2))
  525. return PLUGIN_CATEGORY_MODULATOR;
  526. if (LV2_IS_REVERB(cat1, cat2))
  527. return PLUGIN_CATEGORY_DELAY;
  528. if (LV2_IS_SIMULATOR(cat1, cat2))
  529. return PLUGIN_CATEGORY_OTHER;
  530. if (LV2_IS_SPATIAL(cat1, cat2))
  531. return PLUGIN_CATEGORY_OTHER;
  532. if (LV2_IS_SPECTRAL(cat1, cat2))
  533. return PLUGIN_CATEGORY_UTILITY;
  534. if (LV2_IS_UTILITY(cat1, cat2))
  535. return PLUGIN_CATEGORY_UTILITY;
  536. return CarlaPlugin::getCategory();
  537. }
  538. int64_t getUniqueId() const noexcept override
  539. {
  540. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  541. return static_cast<int64_t>(fRdfDescriptor->UniqueID);
  542. }
  543. // -------------------------------------------------------------------
  544. // Information (count)
  545. uint32_t getMidiInCount() const noexcept override
  546. {
  547. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  548. uint32_t count = 0;
  549. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  550. {
  551. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  552. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  553. ++count;
  554. }
  555. return count;
  556. }
  557. uint32_t getMidiOutCount() const noexcept override
  558. {
  559. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  560. uint32_t count = 0;
  561. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  562. {
  563. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  564. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  565. ++count;
  566. }
  567. return count;
  568. }
  569. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  570. {
  571. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  572. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  573. const int32_t rindex(pData->param.data[parameterId].rindex);
  574. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  575. {
  576. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  577. return port->ScalePointCount;
  578. }
  579. return 0;
  580. }
  581. // -------------------------------------------------------------------
  582. // Information (current data)
  583. // nothing
  584. // -------------------------------------------------------------------
  585. // Information (per-plugin data)
  586. uint getOptionsAvailable() const noexcept override
  587. {
  588. const bool hasMidiIn(getMidiInCount() > 0);
  589. uint options = 0x0;
  590. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  591. if (fLatencyIndex == -1 && ! (hasMidiIn || needsFixedBuffer()))
  592. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  593. if (fCanInit2 && pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  594. {
  595. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  596. options |= PLUGIN_OPTION_FORCE_STEREO;
  597. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  598. options |= PLUGIN_OPTION_FORCE_STEREO;
  599. }
  600. if (hasMidiIn)
  601. {
  602. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  603. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  604. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  605. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  606. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  607. }
  608. return options;
  609. }
  610. float getParameterValue(const uint32_t parameterId) const noexcept override
  611. {
  612. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  613. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  614. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  615. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  616. return fParamBuffers[parameterId];
  617. }
  618. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  619. {
  620. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  621. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  622. const int32_t rindex(pData->param.data[parameterId].rindex);
  623. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  624. {
  625. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  626. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, 0.0f);
  627. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  628. return portScalePoint->Value;
  629. }
  630. return 0.0f;
  631. }
  632. void getLabel(char* const strBuf) const noexcept override
  633. {
  634. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  635. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  636. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  637. }
  638. void getMaker(char* const strBuf) const noexcept override
  639. {
  640. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  641. if (fRdfDescriptor->Author != nullptr)
  642. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  643. else
  644. CarlaPlugin::getMaker(strBuf);
  645. }
  646. void getCopyright(char* const strBuf) const noexcept override
  647. {
  648. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  649. if (fRdfDescriptor->License != nullptr)
  650. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  651. else
  652. CarlaPlugin::getCopyright(strBuf);
  653. }
  654. void getRealName(char* const strBuf) const noexcept override
  655. {
  656. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  657. if (fRdfDescriptor->Name != nullptr)
  658. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  659. else
  660. CarlaPlugin::getRealName(strBuf);
  661. }
  662. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  663. {
  664. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  665. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  666. const int32_t rindex(pData->param.data[parameterId].rindex);
  667. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  668. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  669. else
  670. CarlaPlugin::getParameterName(parameterId, strBuf);
  671. }
  672. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  673. {
  674. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  675. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  676. const int32_t rindex(pData->param.data[parameterId].rindex);
  677. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  678. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  679. else
  680. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  681. }
  682. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  683. {
  684. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  685. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  686. const int32_t rindex(pData->param.data[parameterId].rindex);
  687. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  688. {
  689. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  690. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  691. {
  692. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  693. return;
  694. }
  695. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  696. {
  697. switch (port->Unit.Unit)
  698. {
  699. case LV2_PORT_UNIT_BAR:
  700. std::strncpy(strBuf, "bars", STR_MAX);
  701. return;
  702. case LV2_PORT_UNIT_BEAT:
  703. std::strncpy(strBuf, "beats", STR_MAX);
  704. return;
  705. case LV2_PORT_UNIT_BPM:
  706. std::strncpy(strBuf, "BPM", STR_MAX);
  707. return;
  708. case LV2_PORT_UNIT_CENT:
  709. std::strncpy(strBuf, "ct", STR_MAX);
  710. return;
  711. case LV2_PORT_UNIT_CM:
  712. std::strncpy(strBuf, "cm", STR_MAX);
  713. return;
  714. case LV2_PORT_UNIT_COEF:
  715. std::strncpy(strBuf, "(coef)", STR_MAX);
  716. return;
  717. case LV2_PORT_UNIT_DB:
  718. std::strncpy(strBuf, "dB", STR_MAX);
  719. return;
  720. case LV2_PORT_UNIT_DEGREE:
  721. std::strncpy(strBuf, "deg", STR_MAX);
  722. return;
  723. case LV2_PORT_UNIT_FRAME:
  724. std::strncpy(strBuf, "frames", STR_MAX);
  725. return;
  726. case LV2_PORT_UNIT_HZ:
  727. std::strncpy(strBuf, "Hz", STR_MAX);
  728. return;
  729. case LV2_PORT_UNIT_INCH:
  730. std::strncpy(strBuf, "in", STR_MAX);
  731. return;
  732. case LV2_PORT_UNIT_KHZ:
  733. std::strncpy(strBuf, "kHz", STR_MAX);
  734. return;
  735. case LV2_PORT_UNIT_KM:
  736. std::strncpy(strBuf, "km", STR_MAX);
  737. return;
  738. case LV2_PORT_UNIT_M:
  739. std::strncpy(strBuf, "m", STR_MAX);
  740. return;
  741. case LV2_PORT_UNIT_MHZ:
  742. std::strncpy(strBuf, "MHz", STR_MAX);
  743. return;
  744. case LV2_PORT_UNIT_MIDINOTE:
  745. std::strncpy(strBuf, "note", STR_MAX);
  746. return;
  747. case LV2_PORT_UNIT_MILE:
  748. std::strncpy(strBuf, "mi", STR_MAX);
  749. return;
  750. case LV2_PORT_UNIT_MIN:
  751. std::strncpy(strBuf, "min", STR_MAX);
  752. return;
  753. case LV2_PORT_UNIT_MM:
  754. std::strncpy(strBuf, "mm", STR_MAX);
  755. return;
  756. case LV2_PORT_UNIT_MS:
  757. std::strncpy(strBuf, "ms", STR_MAX);
  758. return;
  759. case LV2_PORT_UNIT_OCT:
  760. std::strncpy(strBuf, "oct", STR_MAX);
  761. return;
  762. case LV2_PORT_UNIT_PC:
  763. std::strncpy(strBuf, "%", STR_MAX);
  764. return;
  765. case LV2_PORT_UNIT_S:
  766. std::strncpy(strBuf, "s", STR_MAX);
  767. return;
  768. case LV2_PORT_UNIT_SEMITONE:
  769. std::strncpy(strBuf, "semi", STR_MAX);
  770. return;
  771. }
  772. }
  773. }
  774. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  775. }
  776. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  777. {
  778. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  779. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  780. const int32_t rindex(pData->param.data[parameterId].rindex);
  781. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  782. {
  783. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  784. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount,);
  785. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  786. if (portScalePoint->Label != nullptr)
  787. {
  788. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  789. return;
  790. }
  791. }
  792. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  793. }
  794. // -------------------------------------------------------------------
  795. // Set data (state)
  796. void prepareForSave() override
  797. {
  798. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  799. if (fExt.state != nullptr && fExt.state->save != nullptr)
  800. {
  801. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  802. if (fHandle2 != nullptr)
  803. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  804. }
  805. }
  806. // -------------------------------------------------------------------
  807. // Set data (internal stuff)
  808. void setName(const char* const newName) override
  809. {
  810. CarlaPlugin::setName(newName);
  811. if (fLv2Options.windowTitle == nullptr)
  812. return;
  813. CarlaString guiTitle(pData->name);
  814. guiTitle += " (GUI)";
  815. delete[] fLv2Options.windowTitle;
  816. fLv2Options.windowTitle = guiTitle.dup();
  817. fLv2Options.opts[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  818. fLv2Options.opts[Lv2PluginOptions::WindowTitle].value = fLv2Options.windowTitle;
  819. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  820. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fLv2Options.windowTitle;
  821. #ifndef LV2_UIS_ONLY_BRIDGES
  822. if (fUI.window != nullptr)
  823. fUI.window->setTitle(fLv2Options.windowTitle);
  824. #endif
  825. }
  826. // -------------------------------------------------------------------
  827. // Set data (plugin-specific stuff)
  828. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  829. {
  830. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  831. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  832. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  833. fParamBuffers[parameterId] = fixedValue;
  834. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  835. }
  836. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  837. {
  838. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  839. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  840. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  841. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  842. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  843. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  844. // we should only call state restore once
  845. // so inject this in CarlaPlugin::loadSaveState
  846. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "CarlaLoadLv2StateNow") == 0 && std::strcmp(value, "true") == 0)
  847. {
  848. if (fExt.state == nullptr)
  849. return;
  850. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  851. {
  852. const ScopedSingleProcessLocker spl(this, true);
  853. try {
  854. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  855. } catch(...) {}
  856. if (fHandle2 != nullptr)
  857. {
  858. try {
  859. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  860. } catch(...) {}
  861. }
  862. }
  863. switch (status)
  864. {
  865. case LV2_STATE_SUCCESS:
  866. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  867. break;
  868. case LV2_STATE_ERR_UNKNOWN:
  869. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  870. break;
  871. case LV2_STATE_ERR_BAD_TYPE:
  872. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  873. break;
  874. case LV2_STATE_ERR_BAD_FLAGS:
  875. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  876. break;
  877. case LV2_STATE_ERR_NO_FEATURE:
  878. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  879. break;
  880. case LV2_STATE_ERR_NO_PROPERTY:
  881. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  882. break;
  883. }
  884. return;
  885. }
  886. CarlaPlugin::setCustomData(type, key, value, sendGui);
  887. }
  888. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  889. {
  890. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  891. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  892. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  893. {
  894. if (LilvState* const state = Lv2WorldClass::getInstance().getState(fRdfDescriptor->Presets[index].URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  895. {
  896. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  897. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  898. if (fHandle2 != nullptr)
  899. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  900. lilv_state_free(state);
  901. }
  902. }
  903. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  904. }
  905. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  906. {
  907. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  908. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  909. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  910. {
  911. const uint32_t bank(pData->midiprog.data[index].bank);
  912. const uint32_t program(pData->midiprog.data[index].program);
  913. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  914. try {
  915. fExt.programs->select_program(fHandle, bank, program);
  916. } catch(...) {}
  917. if (fHandle2 != nullptr)
  918. {
  919. try {
  920. fExt.programs->select_program(fHandle2, bank, program);
  921. } catch(...) {}
  922. }
  923. }
  924. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  925. }
  926. // -------------------------------------------------------------------
  927. // Set ui stuff
  928. void showCustomUI(const bool yesNo) override
  929. {
  930. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  931. if (! yesNo)
  932. pData->transientTryCounter = 0;
  933. if (fUI.type == UI::TYPE_OSC)
  934. {
  935. if (yesNo)
  936. {
  937. pData->osc.data.clear();
  938. pData->osc.thread.startThread();
  939. }
  940. else
  941. {
  942. if (pData->osc.data.target != nullptr)
  943. {
  944. osc_send_hide(pData->osc.data);
  945. osc_send_quit(pData->osc.data);
  946. pData->osc.data.clear();
  947. }
  948. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  949. }
  950. return;
  951. }
  952. // take some precautions
  953. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  954. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr,);
  955. if (yesNo)
  956. {
  957. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->instantiate != nullptr,);
  958. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->cleanup != nullptr,);
  959. }
  960. else
  961. {
  962. if (fUI.handle == nullptr)
  963. return;
  964. }
  965. if (yesNo)
  966. {
  967. if (fUI.handle == nullptr)
  968. {
  969. #ifndef LV2_UIS_ONLY_BRIDGES
  970. if (fUI.type == UI::TYPE_EMBED)
  971. {
  972. const char* msg = nullptr;
  973. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  974. switch (fUI.rdfDescriptor->Type)
  975. {
  976. case LV2_UI_GTK2:
  977. case LV2_UI_GTK3:
  978. case LV2_UI_QT4:
  979. case LV2_UI_QT5:
  980. case LV2_UI_EXTERNAL:
  981. case LV2_UI_OLD_EXTERNAL:
  982. msg = "Invalid UI type";
  983. break;
  984. case LV2_UI_COCOA:
  985. # ifdef CARLA_OS_MAC
  986. fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId);
  987. # else
  988. msg = "UI is for MacOS only";
  989. # endif
  990. break;
  991. case LV2_UI_WINDOWS:
  992. # ifdef CARLA_OS_WIN
  993. fUI.window = CarlaPluginUI::newWindows(this, frontendWinId);
  994. # else
  995. msg = "UI is for Windows only";
  996. # endif
  997. break;
  998. case LV2_UI_X11:
  999. # ifdef HAVE_X11
  1000. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, isUiResizable());
  1001. # else
  1002. msg = "UI is only for systems with X11";
  1003. # endif
  1004. break;
  1005. default:
  1006. msg = "Unknown UI type";
  1007. break;
  1008. }
  1009. if (fUI.window == nullptr && fExt.uishow == nullptr)
  1010. {
  1011. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  1012. // unused
  1013. (void)frontendWinId;
  1014. }
  1015. if (fUI.window != nullptr)
  1016. {
  1017. fUI.window->setTitle(fLv2Options.windowTitle);
  1018. fFeatures[kFeatureIdUiParent]->data = fUI.window->getPtr();
  1019. }
  1020. }
  1021. #endif
  1022. fUI.widget = nullptr;
  1023. fUI.handle = fUI.descriptor->instantiate(fUI.descriptor, fRdfDescriptor->URI, fUI.rdfDescriptor->Bundle,
  1024. carla_lv2_ui_write_function, this, &fUI.widget, fFeatures);
  1025. }
  1026. CARLA_SAFE_ASSERT(fUI.handle != nullptr);
  1027. CARLA_SAFE_ASSERT(fUI.type != UI::TYPE_EXTERNAL || fUI.widget != nullptr);
  1028. if (fUI.handle == nullptr || (fUI.type == UI::TYPE_EXTERNAL && fUI.widget == nullptr))
  1029. {
  1030. fUI.widget = nullptr;
  1031. if (fUI.handle != nullptr)
  1032. {
  1033. fUI.descriptor->cleanup(fUI.handle);
  1034. fUI.handle = nullptr;
  1035. }
  1036. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  1037. }
  1038. updateUi();
  1039. #ifndef LV2_UIS_ONLY_BRIDGES
  1040. if (fUI.type == UI::TYPE_EMBED)
  1041. {
  1042. if (fUI.window != nullptr)
  1043. {
  1044. fUI.window->show();
  1045. }
  1046. else if (fExt.uishow != nullptr)
  1047. {
  1048. fExt.uishow->show(fUI.handle);
  1049. # ifndef BUILD_BRIDGE
  1050. pData->tryTransient();
  1051. # endif
  1052. }
  1053. }
  1054. else
  1055. #endif
  1056. {
  1057. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUI.widget);
  1058. #ifndef BUILD_BRIDGE
  1059. pData->tryTransient();
  1060. #endif
  1061. }
  1062. }
  1063. else
  1064. {
  1065. #ifndef LV2_UIS_ONLY_BRIDGES
  1066. if (fUI.type == UI::TYPE_EMBED)
  1067. {
  1068. if (fUI.window != nullptr)
  1069. fUI.window->hide();
  1070. else if (fExt.uishow != nullptr)
  1071. fExt.uishow->hide(fUI.handle);
  1072. }
  1073. else
  1074. #endif
  1075. {
  1076. CARLA_SAFE_ASSERT(fUI.widget != nullptr);
  1077. if (fUI.widget != nullptr)
  1078. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUI.widget);
  1079. }
  1080. fUI.descriptor->cleanup(fUI.handle);
  1081. fUI.handle = nullptr;
  1082. fUI.widget = nullptr;
  1083. }
  1084. }
  1085. void idle() override
  1086. {
  1087. if (fAtomBufferOut.isDataAvailableForReading())
  1088. {
  1089. uint8_t dumpBuf[fAtomBufferOut.getSize()];
  1090. Lv2AtomRingBuffer tmpRingBuffer(fAtomBufferOut, dumpBuf);
  1091. CARLA_SAFE_ASSERT(tmpRingBuffer.isDataAvailableForReading());
  1092. uint32_t portIndex;
  1093. const LV2_Atom* atom;
  1094. const bool hasPortEvent(fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr);
  1095. for (; tmpRingBuffer.get(atom, portIndex);)
  1096. {
  1097. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  1098. {
  1099. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1100. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1101. }
  1102. else if (fUI.type == UI::TYPE_OSC)
  1103. {
  1104. if (pData->osc.data.target != nullptr)
  1105. {
  1106. CarlaString chunk(CarlaString::asBase64(atom, sizeof(LV2_Atom)+atom->size));
  1107. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.buffer());
  1108. }
  1109. }
  1110. else
  1111. {
  1112. if (hasPortEvent)
  1113. fUI.descriptor->port_event(fUI.handle, portIndex, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1114. }
  1115. }
  1116. }
  1117. if (fLatencyChanged && fLatencyIndex != -1)
  1118. {
  1119. fLatencyChanged = false;
  1120. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  1121. if (latency >= 0)
  1122. {
  1123. const uint32_t ulatency(static_cast<uint32_t>(latency));
  1124. if (pData->latency != ulatency)
  1125. {
  1126. carla_stdout("latency changed to %i", latency);
  1127. const ScopedSingleProcessLocker sspl(this, true);
  1128. pData->latency = ulatency;
  1129. pData->client->setLatency(ulatency);
  1130. #ifndef BUILD_BRIDGE
  1131. pData->recreateLatencyBuffers();
  1132. #endif
  1133. }
  1134. }
  1135. else
  1136. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  1137. }
  1138. if (fUI.handle != nullptr && fUI.descriptor != nullptr)
  1139. {
  1140. if (fUI.type == UI::TYPE_EXTERNAL && fUI.widget != nullptr)
  1141. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUI.widget);
  1142. #ifndef LV2_UIS_ONLY_BRIDGES
  1143. else if (fUI.type == UI::TYPE_EMBED && fUI.window != nullptr)
  1144. fUI.window->idle();
  1145. // note: UI might have been closed by ext-ui or window idle
  1146. if (fUI.handle != nullptr && fExt.uiidle != nullptr && fExt.uiidle->idle(fUI.handle) != 0)
  1147. {
  1148. showCustomUI(false);
  1149. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1150. }
  1151. #endif
  1152. }
  1153. CarlaPlugin::idle();
  1154. }
  1155. // -------------------------------------------------------------------
  1156. // Plugin state
  1157. void reload() override
  1158. {
  1159. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1160. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1161. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1162. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1163. carla_debug("Lv2Plugin::reload() - start");
  1164. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1165. // Safely disable plugin for reload
  1166. const ScopedDisabler sd(this);
  1167. if (pData->active)
  1168. deactivate();
  1169. clearBuffers();
  1170. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1171. const uint32_t portCount(fRdfDescriptor->PortCount);
  1172. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1173. aIns = aOuts = cvIns = cvOuts = params = 0;
  1174. LinkedList<uint> evIns, evOuts;
  1175. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1176. bool forcedStereoIn, forcedStereoOut;
  1177. forcedStereoIn = forcedStereoOut = false;
  1178. bool needsCtrlIn, needsCtrlOut;
  1179. needsCtrlIn = needsCtrlOut = false;
  1180. for (uint32_t i=0; i < portCount; ++i)
  1181. {
  1182. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1183. if (LV2_IS_PORT_AUDIO(portTypes))
  1184. {
  1185. if (LV2_IS_PORT_INPUT(portTypes))
  1186. aIns += 1;
  1187. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1188. aOuts += 1;
  1189. }
  1190. else if (LV2_IS_PORT_CV(portTypes))
  1191. {
  1192. if (LV2_IS_PORT_INPUT(portTypes))
  1193. cvIns += 1;
  1194. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1195. cvOuts += 1;
  1196. }
  1197. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1198. {
  1199. if (LV2_IS_PORT_INPUT(portTypes))
  1200. evIns.append(CARLA_EVENT_DATA_ATOM);
  1201. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1202. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1203. }
  1204. else if (LV2_IS_PORT_EVENT(portTypes))
  1205. {
  1206. if (LV2_IS_PORT_INPUT(portTypes))
  1207. evIns.append(CARLA_EVENT_DATA_EVENT);
  1208. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1209. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1210. }
  1211. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1212. {
  1213. if (LV2_IS_PORT_INPUT(portTypes))
  1214. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1215. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1216. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1217. }
  1218. else if (LV2_IS_PORT_CONTROL(portTypes))
  1219. params += 1;
  1220. }
  1221. recheckExtensions();
  1222. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1223. {
  1224. if (fHandle2 == nullptr)
  1225. {
  1226. try {
  1227. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1228. } catch(...) {}
  1229. }
  1230. if (fHandle2 != nullptr)
  1231. {
  1232. if (aIns == 1)
  1233. {
  1234. aIns = 2;
  1235. forcedStereoIn = true;
  1236. }
  1237. if (aOuts == 1)
  1238. {
  1239. aOuts = 2;
  1240. forcedStereoOut = true;
  1241. }
  1242. }
  1243. }
  1244. if (aIns > 0)
  1245. {
  1246. pData->audioIn.createNew(aIns);
  1247. fAudioInBuffers = new float*[aIns];
  1248. for (uint32_t i=0; i < aIns; ++i)
  1249. fAudioInBuffers[i] = nullptr;
  1250. }
  1251. if (aOuts > 0)
  1252. {
  1253. pData->audioOut.createNew(aOuts);
  1254. fAudioOutBuffers = new float*[aOuts];
  1255. needsCtrlIn = true;
  1256. for (uint32_t i=0; i < aOuts; ++i)
  1257. fAudioOutBuffers[i] = nullptr;
  1258. }
  1259. if (cvIns > 0)
  1260. {
  1261. pData->cvIn.createNew(cvIns);
  1262. fCvInBuffers = new float*[cvIns];
  1263. for (uint32_t i=0; i < cvIns; ++i)
  1264. fCvInBuffers[i] = nullptr;
  1265. }
  1266. if (cvOuts > 0)
  1267. {
  1268. pData->cvOut.createNew(cvOuts);
  1269. fCvOutBuffers = new float*[cvOuts];
  1270. for (uint32_t i=0; i < cvOuts; ++i)
  1271. fCvOutBuffers[i] = nullptr;
  1272. }
  1273. if (params > 0)
  1274. {
  1275. pData->param.createNew(params, true);
  1276. fParamBuffers = new float[params];
  1277. FloatVectorOperations::clear(fParamBuffers, static_cast<int>(params));
  1278. }
  1279. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1280. {
  1281. fEventsIn.createNew(count);
  1282. for (uint32_t i=0; i < count; ++i)
  1283. {
  1284. const uint32_t& type(evIns.getAt(i, 0x0));
  1285. if (type == CARLA_EVENT_DATA_ATOM)
  1286. {
  1287. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1288. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1289. }
  1290. else if (type == CARLA_EVENT_DATA_EVENT)
  1291. {
  1292. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1293. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1294. }
  1295. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1296. {
  1297. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1298. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1299. fEventsIn.data[i].midi.data = new uchar[eventBufferSize];
  1300. }
  1301. }
  1302. }
  1303. else
  1304. {
  1305. fEventsIn.createNew(1);
  1306. fEventsIn.ctrl = &fEventsIn.data[0];
  1307. }
  1308. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1309. {
  1310. fEventsOut.createNew(count);
  1311. for (uint32_t i=0; i < count; ++i)
  1312. {
  1313. const uint32_t& type(evOuts.getAt(i, 0x0));
  1314. if (type == CARLA_EVENT_DATA_ATOM)
  1315. {
  1316. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1317. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1318. }
  1319. else if (type == CARLA_EVENT_DATA_EVENT)
  1320. {
  1321. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1322. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1323. }
  1324. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1325. {
  1326. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1327. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1328. fEventsOut.data[i].midi.data = new uchar[eventBufferSize];
  1329. }
  1330. }
  1331. }
  1332. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1333. CarlaString portName;
  1334. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1335. {
  1336. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1337. portName.clear();
  1338. if (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_CV(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  1339. {
  1340. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1341. {
  1342. portName = pData->name;
  1343. portName += ":";
  1344. }
  1345. portName += fRdfDescriptor->Ports[i].Name;
  1346. portName.truncate(portNameSize);
  1347. }
  1348. if (LV2_IS_PORT_AUDIO(portTypes))
  1349. {
  1350. if (LV2_IS_PORT_INPUT(portTypes))
  1351. {
  1352. const uint32_t j = iAudioIn++;
  1353. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1354. pData->audioIn.ports[j].rindex = i;
  1355. if (forcedStereoIn)
  1356. {
  1357. portName += "_2";
  1358. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1359. pData->audioIn.ports[1].rindex = i;
  1360. }
  1361. }
  1362. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1363. {
  1364. const uint32_t j = iAudioOut++;
  1365. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1366. pData->audioOut.ports[j].rindex = i;
  1367. if (forcedStereoOut)
  1368. {
  1369. portName += "_2";
  1370. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1371. pData->audioOut.ports[1].rindex = i;
  1372. }
  1373. }
  1374. else
  1375. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1376. }
  1377. else if (LV2_IS_PORT_CV(portTypes))
  1378. {
  1379. if (LV2_IS_PORT_INPUT(portTypes))
  1380. {
  1381. const uint32_t j = iCvIn++;
  1382. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1383. pData->cvIn.ports[j].rindex = i;
  1384. }
  1385. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1386. {
  1387. const uint32_t j = iCvOut++;
  1388. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1389. pData->cvOut.ports[j].rindex = i;
  1390. }
  1391. else
  1392. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1393. }
  1394. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1395. {
  1396. if (LV2_IS_PORT_INPUT(portTypes))
  1397. {
  1398. const uint32_t j = iEvIn++;
  1399. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1400. if (fHandle2 != nullptr)
  1401. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1402. fEventsIn.data[j].rindex = i;
  1403. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1404. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1405. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1406. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1407. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1408. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1409. if (evIns.count() == 1)
  1410. {
  1411. fEventsIn.ctrl = &fEventsIn.data[j];
  1412. fEventsIn.ctrlIndex = j;
  1413. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1414. needsCtrlIn = true;
  1415. }
  1416. else
  1417. {
  1418. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1419. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1420. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1421. {
  1422. fEventsIn.ctrl = &fEventsIn.data[j];
  1423. fEventsIn.ctrlIndex = j;
  1424. }
  1425. }
  1426. }
  1427. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1428. {
  1429. const uint32_t j = iEvOut++;
  1430. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1431. if (fHandle2 != nullptr)
  1432. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1433. fEventsOut.data[j].rindex = i;
  1434. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1435. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1436. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1437. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1438. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1439. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1440. if (evOuts.count() == 1)
  1441. {
  1442. fEventsOut.ctrl = &fEventsOut.data[j];
  1443. fEventsOut.ctrlIndex = j;
  1444. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1445. needsCtrlOut = true;
  1446. }
  1447. else
  1448. {
  1449. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1450. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1451. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1452. {
  1453. fEventsOut.ctrl = &fEventsOut.data[j];
  1454. fEventsOut.ctrlIndex = j;
  1455. }
  1456. }
  1457. }
  1458. else
  1459. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1460. }
  1461. else if (LV2_IS_PORT_EVENT(portTypes))
  1462. {
  1463. if (LV2_IS_PORT_INPUT(portTypes))
  1464. {
  1465. const uint32_t j = iEvIn++;
  1466. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1467. if (fHandle2 != nullptr)
  1468. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1469. fEventsIn.data[j].rindex = i;
  1470. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1471. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1472. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1473. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1474. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1475. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1476. if (evIns.count() == 1)
  1477. {
  1478. fEventsIn.ctrl = &fEventsIn.data[j];
  1479. fEventsIn.ctrlIndex = j;
  1480. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1481. needsCtrlIn = true;
  1482. }
  1483. else
  1484. {
  1485. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1486. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1487. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1488. {
  1489. fEventsIn.ctrl = &fEventsIn.data[j];
  1490. fEventsIn.ctrlIndex = j;
  1491. }
  1492. }
  1493. }
  1494. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1495. {
  1496. const uint32_t j = iEvOut++;
  1497. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1498. if (fHandle2 != nullptr)
  1499. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1500. fEventsOut.data[j].rindex = i;
  1501. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1502. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1503. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1504. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1505. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1506. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1507. if (evOuts.count() == 1)
  1508. {
  1509. fEventsOut.ctrl = &fEventsOut.data[j];
  1510. fEventsOut.ctrlIndex = j;
  1511. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1512. needsCtrlOut = true;
  1513. }
  1514. else
  1515. {
  1516. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1517. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1518. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1519. {
  1520. fEventsOut.ctrl = &fEventsOut.data[j];
  1521. fEventsOut.ctrlIndex = j;
  1522. }
  1523. }
  1524. }
  1525. else
  1526. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1527. }
  1528. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1529. {
  1530. if (LV2_IS_PORT_INPUT(portTypes))
  1531. {
  1532. const uint32_t j = iEvIn++;
  1533. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1534. if (fHandle2 != nullptr)
  1535. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1536. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1537. fEventsIn.data[j].rindex = i;
  1538. if (evIns.count() == 1)
  1539. {
  1540. needsCtrlIn = true;
  1541. fEventsIn.ctrl = &fEventsIn.data[j];
  1542. fEventsIn.ctrlIndex = j;
  1543. }
  1544. else
  1545. {
  1546. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1547. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1548. {
  1549. fEventsIn.ctrl = &fEventsIn.data[j];
  1550. fEventsIn.ctrlIndex = j;
  1551. }
  1552. }
  1553. }
  1554. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1555. {
  1556. const uint32_t j = iEvOut++;
  1557. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1558. if (fHandle2 != nullptr)
  1559. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1560. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1561. fEventsOut.data[j].rindex = i;
  1562. if (evOuts.count() == 1)
  1563. {
  1564. needsCtrlOut = true;
  1565. fEventsOut.ctrl = &fEventsOut.data[j];
  1566. fEventsOut.ctrlIndex = j;
  1567. }
  1568. else
  1569. {
  1570. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1571. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1572. {
  1573. fEventsOut.ctrl = &fEventsOut.data[j];
  1574. fEventsOut.ctrlIndex = j;
  1575. }
  1576. }
  1577. }
  1578. else
  1579. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1580. }
  1581. else if (LV2_IS_PORT_CONTROL(portTypes))
  1582. {
  1583. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1584. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1585. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1586. const uint32_t j = iCtrl++;
  1587. pData->param.data[j].index = static_cast<int32_t>(j);
  1588. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1589. float min, max, def, step, stepSmall, stepLarge;
  1590. // min value
  1591. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1592. min = portPoints.Minimum;
  1593. else
  1594. min = 0.0f;
  1595. // max value
  1596. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1597. max = portPoints.Maximum;
  1598. else
  1599. max = 1.0f;
  1600. if (min > max)
  1601. max = min;
  1602. // stupid hack for ir.lv2 (broken plugin)
  1603. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1604. {
  1605. min = 0.0f;
  1606. max = (float)0xffffff;
  1607. }
  1608. if (carla_compareFloats(min, max))
  1609. {
  1610. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", fRdfDescriptor->Ports[i].Name);
  1611. max = min + 0.1f;
  1612. }
  1613. // default value
  1614. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1615. {
  1616. def = portPoints.Default;
  1617. }
  1618. else
  1619. {
  1620. // no default value
  1621. if (min < 0.0f && max > 0.0f)
  1622. def = 0.0f;
  1623. else
  1624. def = min;
  1625. }
  1626. if (def < min)
  1627. def = min;
  1628. else if (def > max)
  1629. def = max;
  1630. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1631. {
  1632. min *= sampleRate;
  1633. max *= sampleRate;
  1634. def *= sampleRate;
  1635. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1636. }
  1637. if (LV2_IS_PORT_TOGGLED(portProps))
  1638. {
  1639. step = max - min;
  1640. stepSmall = step;
  1641. stepLarge = step;
  1642. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1643. }
  1644. else if (LV2_IS_PORT_INTEGER(portProps))
  1645. {
  1646. step = 1.0f;
  1647. stepSmall = 1.0f;
  1648. stepLarge = 10.0f;
  1649. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1650. }
  1651. else
  1652. {
  1653. float range = max - min;
  1654. step = range/100.0f;
  1655. stepSmall = range/1000.0f;
  1656. stepLarge = range/10.0f;
  1657. }
  1658. if (LV2_IS_PORT_INPUT(portTypes))
  1659. {
  1660. pData->param.data[j].type = PARAMETER_INPUT;
  1661. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1662. {
  1663. carla_stderr("Plugin has latency input port, this should not happen!");
  1664. }
  1665. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1666. {
  1667. def = sampleRate;
  1668. step = 1.0f;
  1669. stepSmall = 1.0f;
  1670. stepLarge = 1.0f;
  1671. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1672. }
  1673. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1674. {
  1675. pData->param.special[j] = PARAMETER_SPECIAL_FREEWHEEL;
  1676. }
  1677. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1678. {
  1679. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  1680. }
  1681. else
  1682. {
  1683. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1684. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1685. needsCtrlIn = true;
  1686. }
  1687. // MIDI CC value
  1688. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1689. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1690. {
  1691. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1692. pData->param.data[j].midiCC = static_cast<int16_t>(portMidiMap.Number);
  1693. }
  1694. }
  1695. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1696. {
  1697. pData->param.data[j].type = PARAMETER_OUTPUT;
  1698. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1699. {
  1700. min = 0.0f;
  1701. max = sampleRate;
  1702. def = 0.0f;
  1703. step = 1.0f;
  1704. stepSmall = 1.0f;
  1705. stepLarge = 1.0f;
  1706. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1707. CARLA_SAFE_ASSERT(fLatencyIndex == -1);
  1708. fLatencyIndex = static_cast<int32_t>(j);
  1709. }
  1710. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1711. {
  1712. def = sampleRate;
  1713. step = 1.0f;
  1714. stepSmall = 1.0f;
  1715. stepLarge = 1.0f;
  1716. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1717. }
  1718. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1719. {
  1720. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1721. }
  1722. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1723. {
  1724. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  1725. }
  1726. else
  1727. {
  1728. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1729. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1730. needsCtrlOut = true;
  1731. }
  1732. }
  1733. else
  1734. {
  1735. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1736. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1737. }
  1738. // extra parameter hints
  1739. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1740. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1741. if (LV2_IS_PORT_TRIGGER(portProps))
  1742. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1743. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1744. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1745. if (LV2_IS_PORT_ENUMERATION(portProps))
  1746. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1747. // check if parameter is not enabled or automable
  1748. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1749. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1750. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps))
  1751. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1752. else if (LV2_IS_PORT_NOT_AUTOMATIC(portProps) || LV2_IS_PORT_NON_AUTOMABLE(portProps))
  1753. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1754. pData->param.ranges[j].min = min;
  1755. pData->param.ranges[j].max = max;
  1756. pData->param.ranges[j].def = def;
  1757. pData->param.ranges[j].step = step;
  1758. pData->param.ranges[j].stepSmall = stepSmall;
  1759. pData->param.ranges[j].stepLarge = stepLarge;
  1760. // Start parameters in their default values (except freewheel, which is off by default)
  1761. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_FREEWHEEL)
  1762. fParamBuffers[j] = min;
  1763. else
  1764. fParamBuffers[j] = def;
  1765. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1766. if (fHandle2 != nullptr)
  1767. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1768. }
  1769. else
  1770. {
  1771. // Port Type not supported, but it's optional anyway
  1772. fDescriptor->connect_port(fHandle, i, nullptr);
  1773. if (fHandle2 != nullptr)
  1774. fDescriptor->connect_port(fHandle2, i, nullptr);
  1775. }
  1776. }
  1777. if (needsCtrlIn)
  1778. {
  1779. portName.clear();
  1780. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1781. {
  1782. portName = pData->name;
  1783. portName += ":";
  1784. }
  1785. portName += "events-in";
  1786. portName.truncate(portNameSize);
  1787. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1788. }
  1789. if (needsCtrlOut)
  1790. {
  1791. portName.clear();
  1792. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1793. {
  1794. portName = pData->name;
  1795. portName += ":";
  1796. }
  1797. portName += "events-out";
  1798. portName.truncate(portNameSize);
  1799. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1800. }
  1801. if (fExt.worker != nullptr || (fUI.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1802. fAtomBufferIn.createBuffer(eventBufferSize);
  1803. if (fExt.worker != nullptr || (fUI.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1804. fAtomBufferOut.createBuffer(eventBufferSize);
  1805. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1806. fEventsIn.ctrl->port = pData->event.portIn;
  1807. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1808. fEventsOut.ctrl->port = pData->event.portOut;
  1809. if (fCanInit2 && (forcedStereoIn || forcedStereoOut))
  1810. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1811. else
  1812. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1813. // plugin hints
  1814. pData->hints = 0x0;
  1815. if (isRealtimeSafe())
  1816. pData->hints |= PLUGIN_IS_RTSAFE;
  1817. if (fUI.type != UI::TYPE_NULL)
  1818. {
  1819. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1820. if (fUI.type == UI::TYPE_EMBED)
  1821. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1822. }
  1823. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1824. pData->hints |= PLUGIN_IS_SYNTH;
  1825. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1826. pData->hints |= PLUGIN_CAN_DRYWET;
  1827. if (aOuts > 0)
  1828. pData->hints |= PLUGIN_CAN_VOLUME;
  1829. if (aOuts >= 2 && aOuts % 2 == 0)
  1830. pData->hints |= PLUGIN_CAN_BALANCE;
  1831. // extra plugin hints
  1832. pData->extraHints = 0x0;
  1833. if (! fCanInit2)
  1834. {
  1835. // can't run in rack
  1836. }
  1837. else if (fExt.state != nullptr || fExt.worker != nullptr)
  1838. {
  1839. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1840. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1841. }
  1842. else
  1843. {
  1844. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1845. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1846. }
  1847. // check latency
  1848. if (fLatencyIndex >= 0)
  1849. {
  1850. // we need to pre-run the plugin so it can update its latency control-port
  1851. float tmpIn [(aIns > 0) ? aIns : 1][2];
  1852. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  1853. for (uint32_t j=0; j < aIns; ++j)
  1854. {
  1855. tmpIn[j][0] = 0.0f;
  1856. tmpIn[j][1] = 0.0f;
  1857. try {
  1858. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  1859. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency input");
  1860. }
  1861. for (uint32_t j=0; j < aOuts; ++j)
  1862. {
  1863. tmpOut[j][0] = 0.0f;
  1864. tmpOut[j][1] = 0.0f;
  1865. try {
  1866. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  1867. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency output");
  1868. }
  1869. if (fDescriptor->activate != nullptr)
  1870. {
  1871. try {
  1872. fDescriptor->activate(fHandle);
  1873. } CARLA_SAFE_EXCEPTION("LV2 latency activate");
  1874. }
  1875. try {
  1876. fDescriptor->run(fHandle, 2);
  1877. } CARLA_SAFE_EXCEPTION("LV2 latency run");
  1878. if (fDescriptor->deactivate != nullptr)
  1879. {
  1880. try {
  1881. fDescriptor->deactivate(fHandle);
  1882. } CARLA_SAFE_EXCEPTION("LV2 latency deactivate");
  1883. }
  1884. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  1885. if (latency >= 0)
  1886. {
  1887. const uint32_t ulatency(static_cast<uint32_t>(latency));
  1888. if (pData->latency != ulatency)
  1889. {
  1890. carla_stdout("latency = %i", latency);
  1891. pData->latency = ulatency;
  1892. pData->client->setLatency(ulatency);
  1893. #ifndef BUILD_BRIDGE
  1894. pData->recreateLatencyBuffers();
  1895. #endif
  1896. }
  1897. }
  1898. else
  1899. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  1900. fLatencyChanged = false;
  1901. }
  1902. bufferSizeChanged(pData->engine->getBufferSize());
  1903. reloadPrograms(true);
  1904. evIns.clear();
  1905. evOuts.clear();
  1906. if (pData->active)
  1907. activate();
  1908. carla_debug("Lv2Plugin::reload() - end");
  1909. }
  1910. void reloadPrograms(const bool doInit) override
  1911. {
  1912. carla_debug("Lv2Plugin::reloadPrograms(%s)", bool2str(doInit));
  1913. const uint32_t oldCount = pData->midiprog.count;
  1914. const int32_t current = pData->midiprog.current;
  1915. // special LV2 programs handling
  1916. if (doInit)
  1917. {
  1918. pData->prog.clear();
  1919. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  1920. if (presetCount > 0)
  1921. {
  1922. pData->prog.createNew(presetCount);
  1923. for (uint32_t i=0; i < presetCount; ++i)
  1924. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1925. }
  1926. }
  1927. // Delete old programs
  1928. pData->midiprog.clear();
  1929. // Query new programs
  1930. uint32_t newCount = 0;
  1931. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1932. {
  1933. for (; fExt.programs->get_program(fHandle, newCount);)
  1934. ++newCount;
  1935. }
  1936. if (newCount > 0)
  1937. {
  1938. pData->midiprog.createNew(newCount);
  1939. // Update data
  1940. for (uint32_t i=0; i < newCount; ++i)
  1941. {
  1942. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1943. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  1944. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  1945. pData->midiprog.data[i].bank = pdesc->bank;
  1946. pData->midiprog.data[i].program = pdesc->program;
  1947. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1948. }
  1949. }
  1950. #ifndef BUILD_BRIDGE
  1951. // Update OSC Names
  1952. if (pData->engine->isOscControlRegistered())
  1953. {
  1954. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  1955. for (uint32_t i=0; i < newCount; ++i)
  1956. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  1957. }
  1958. #endif
  1959. if (doInit)
  1960. {
  1961. if (newCount > 0)
  1962. {
  1963. setMidiProgram(0, false, false, false);
  1964. }
  1965. else
  1966. {
  1967. // load default state
  1968. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1969. {
  1970. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1971. if (fHandle2 != nullptr)
  1972. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1973. }
  1974. }
  1975. }
  1976. else
  1977. {
  1978. // Check if current program is invalid
  1979. bool programChanged = false;
  1980. if (newCount == oldCount+1)
  1981. {
  1982. // one midi program added, probably created by user
  1983. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1984. programChanged = true;
  1985. }
  1986. else if (current < 0 && newCount > 0)
  1987. {
  1988. // programs exist now, but not before
  1989. pData->midiprog.current = 0;
  1990. programChanged = true;
  1991. }
  1992. else if (current >= 0 && newCount == 0)
  1993. {
  1994. // programs existed before, but not anymore
  1995. pData->midiprog.current = -1;
  1996. programChanged = true;
  1997. }
  1998. else if (current >= static_cast<int32_t>(newCount))
  1999. {
  2000. // current midi program > count
  2001. pData->midiprog.current = 0;
  2002. programChanged = true;
  2003. }
  2004. else
  2005. {
  2006. // no change
  2007. pData->midiprog.current = current;
  2008. }
  2009. if (programChanged)
  2010. setMidiProgram(pData->midiprog.current, true, true, true);
  2011. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  2012. }
  2013. }
  2014. // -------------------------------------------------------------------
  2015. // Plugin processing
  2016. void activate() noexcept override
  2017. {
  2018. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2019. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2020. if (fDescriptor->activate != nullptr)
  2021. {
  2022. try {
  2023. fDescriptor->activate(fHandle);
  2024. } CARLA_SAFE_EXCEPTION("LV2 activate");
  2025. if (fHandle2 != nullptr)
  2026. {
  2027. try {
  2028. fDescriptor->activate(fHandle2);
  2029. } CARLA_SAFE_EXCEPTION("LV2 activate #2");
  2030. }
  2031. }
  2032. fFirstActive = true;
  2033. }
  2034. void deactivate() noexcept override
  2035. {
  2036. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2037. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2038. if (fDescriptor->deactivate != nullptr)
  2039. {
  2040. try {
  2041. fDescriptor->deactivate(fHandle);
  2042. } CARLA_SAFE_EXCEPTION("LV2 deactivate");
  2043. if (fHandle2 != nullptr)
  2044. {
  2045. try {
  2046. fDescriptor->deactivate(fHandle2);
  2047. } CARLA_SAFE_EXCEPTION("LV2 deactivate #2");
  2048. }
  2049. }
  2050. }
  2051. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  2052. {
  2053. // --------------------------------------------------------------------------------------------------------
  2054. // Check if active
  2055. if (! pData->active)
  2056. {
  2057. // disable any output sound
  2058. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2059. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  2060. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2061. FloatVectorOperations::clear(cvOut[i], static_cast<int>(frames));
  2062. return;
  2063. }
  2064. // --------------------------------------------------------------------------------------------------------
  2065. // Event itenerators from different APIs (input)
  2066. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  2067. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  2068. LV2_MIDIState evInMidiStates[fEventsIn.count];
  2069. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2070. {
  2071. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2072. {
  2073. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  2074. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  2075. }
  2076. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2077. {
  2078. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  2079. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  2080. }
  2081. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2082. {
  2083. fEventsIn.data[i].midi.event_count = 0;
  2084. fEventsIn.data[i].midi.size = 0;
  2085. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  2086. evInMidiStates[i].frame_count = frames;
  2087. evInMidiStates[i].position = 0;
  2088. }
  2089. }
  2090. for (uint32_t i=0; i < fEventsOut.count; ++i)
  2091. {
  2092. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2093. {
  2094. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  2095. }
  2096. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2097. {
  2098. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  2099. }
  2100. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2101. {
  2102. // not needed
  2103. }
  2104. }
  2105. // --------------------------------------------------------------------------------------------------------
  2106. // Check if needs reset
  2107. if (pData->needsReset)
  2108. {
  2109. uint8_t midiData[3] = { 0, 0, 0 };
  2110. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  2111. {
  2112. const uint32_t j = fEventsIn.ctrlIndex;
  2113. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2114. {
  2115. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  2116. {
  2117. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2118. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2119. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2120. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2121. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2122. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2123. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2124. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2125. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2126. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2127. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2128. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2129. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2130. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2131. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2132. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2133. }
  2134. }
  2135. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2136. {
  2137. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2138. {
  2139. midiData[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  2140. midiData[1] = k;
  2141. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2142. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2143. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2144. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2145. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2146. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  2147. }
  2148. }
  2149. }
  2150. #ifndef BUILD_BRIDGE
  2151. if (pData->latency > 0)
  2152. {
  2153. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2154. FloatVectorOperations::clear(pData->latencyBuffers[i], static_cast<int>(pData->latency));
  2155. }
  2156. #endif
  2157. pData->needsReset = false;
  2158. }
  2159. // --------------------------------------------------------------------------------------------------------
  2160. // TimeInfo
  2161. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2162. if (fFirstActive || fLastTimeInfo != timeInfo)
  2163. {
  2164. bool doPostRt;
  2165. int32_t rindex;
  2166. // update input ports
  2167. for (uint32_t k=0; k < pData->param.count; ++k)
  2168. {
  2169. if (pData->param.data[k].type != PARAMETER_INPUT)
  2170. continue;
  2171. if (pData->param.special[k] != PARAMETER_SPECIAL_TIME)
  2172. continue;
  2173. doPostRt = false;
  2174. rindex = pData->param.data[k].rindex;
  2175. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2176. switch (fRdfDescriptor->Ports[rindex].Designation)
  2177. {
  2178. // Non-BBT
  2179. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2180. if (fLastTimeInfo.playing != timeInfo.playing)
  2181. {
  2182. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2183. doPostRt = true;
  2184. }
  2185. break;
  2186. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2187. if (fLastTimeInfo.frame != timeInfo.frame)
  2188. {
  2189. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2190. doPostRt = true;
  2191. }
  2192. break;
  2193. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2194. break;
  2195. // BBT
  2196. case LV2_PORT_DESIGNATION_TIME_BAR:
  2197. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2198. {
  2199. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2200. doPostRt = true;
  2201. }
  2202. break;
  2203. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2204. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2205. !carla_compareFloats(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat)))
  2206. {
  2207. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2208. doPostRt = true;
  2209. }
  2210. break;
  2211. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2212. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2213. {
  2214. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2215. doPostRt = true;
  2216. }
  2217. break;
  2218. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2219. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.beatType, timeInfo.bbt.beatType))
  2220. {
  2221. fParamBuffers[k] = timeInfo.bbt.beatType;
  2222. doPostRt = true;
  2223. }
  2224. break;
  2225. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2226. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.beatsPerBar, timeInfo.bbt.beatsPerBar))
  2227. {
  2228. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2229. doPostRt = true;
  2230. }
  2231. break;
  2232. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2233. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.beatsPerMinute, timeInfo.bbt.beatsPerMinute))
  2234. {
  2235. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2236. doPostRt = true;
  2237. }
  2238. break;
  2239. case LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT:
  2240. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat))
  2241. {
  2242. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.ticksPerBeat);
  2243. doPostRt = true;
  2244. }
  2245. break;
  2246. }
  2247. if (doPostRt)
  2248. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2249. }
  2250. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2251. {
  2252. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2253. continue;
  2254. uint8_t timeInfoBuf[256];
  2255. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2256. LV2_Atom_Forge_Frame forgeFrame;
  2257. lv2_atom_forge_object(&fAtomForge, &forgeFrame, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_TIME_POSITION);
  2258. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2259. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2260. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2261. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2262. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2263. {
  2264. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2265. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2266. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2267. lv2_atom_forge_float(&fAtomForge, static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat)));
  2268. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2269. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2270. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2271. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2272. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2273. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2274. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2275. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2276. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT);
  2277. lv2_atom_forge_double(&fAtomForge, static_cast<float>(timeInfo.bbt.ticksPerBeat));
  2278. }
  2279. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2280. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2281. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2282. // send only deprecated blank object for now
  2283. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, CARLA_URI_MAP_ID_ATOM_BLANK, atom->size, LV2_ATOM_BODY_CONST(atom));
  2284. // for atom:object
  2285. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2286. }
  2287. pData->postRtEvents.trySplice();
  2288. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2289. }
  2290. // --------------------------------------------------------------------------------------------------------
  2291. // Event Input and Processing
  2292. if (fEventsIn.ctrl != nullptr)
  2293. {
  2294. // ----------------------------------------------------------------------------------------------------
  2295. // Message Input
  2296. if (fAtomBufferIn.tryLock())
  2297. {
  2298. if (fAtomBufferIn.isDataAvailableForReading())
  2299. {
  2300. const LV2_Atom* atom;
  2301. uint32_t j, portIndex;
  2302. for (; fAtomBufferIn.get(atom, portIndex);)
  2303. {
  2304. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2305. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  2306. {
  2307. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2308. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2309. }
  2310. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2311. {
  2312. carla_stdout("Event input buffer full, at least 1 message lost");
  2313. continue;
  2314. }
  2315. }
  2316. }
  2317. fAtomBufferIn.unlock();
  2318. }
  2319. // ----------------------------------------------------------------------------------------------------
  2320. // MIDI Input (External)
  2321. if (pData->extNotes.mutex.tryLock())
  2322. {
  2323. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2324. {
  2325. // does not handle MIDI
  2326. pData->extNotes.data.clear();
  2327. }
  2328. else
  2329. {
  2330. const uint32_t j = fEventsIn.ctrlIndex;
  2331. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  2332. {
  2333. const ExternalMidiNote& note(it.getValue());
  2334. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2335. uint8_t midiEvent[3];
  2336. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2337. midiEvent[1] = note.note;
  2338. midiEvent[2] = note.velo;
  2339. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2340. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2341. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2342. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2343. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2344. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2345. }
  2346. pData->extNotes.data.clear();
  2347. }
  2348. pData->extNotes.mutex.unlock();
  2349. } // End of MIDI Input (External)
  2350. // ----------------------------------------------------------------------------------------------------
  2351. // Event Input (System)
  2352. #ifndef BUILD_BRIDGE
  2353. bool allNotesOffSent = false;
  2354. #endif
  2355. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2356. uint32_t startTime = 0;
  2357. uint32_t timeOffset = 0;
  2358. uint32_t nextBankId;
  2359. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2360. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2361. else
  2362. nextBankId = 0;
  2363. const uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2364. for (uint32_t i=0; i < numEvents; ++i)
  2365. {
  2366. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2367. if (event.time >= frames)
  2368. continue;
  2369. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2370. if (isSampleAccurate && event.time > timeOffset)
  2371. {
  2372. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  2373. {
  2374. startTime = 0;
  2375. timeOffset = event.time;
  2376. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2377. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2378. else
  2379. nextBankId = 0;
  2380. // reset iters
  2381. const uint32_t j = fEventsIn.ctrlIndex;
  2382. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2383. {
  2384. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2385. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2386. }
  2387. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2388. {
  2389. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2390. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2391. }
  2392. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2393. {
  2394. fEventsIn.data[j].midi.event_count = 0;
  2395. fEventsIn.data[j].midi.size = 0;
  2396. evInMidiStates[j].position = event.time;
  2397. }
  2398. }
  2399. else
  2400. startTime += timeOffset;
  2401. }
  2402. switch (event.type)
  2403. {
  2404. case kEngineEventTypeNull:
  2405. break;
  2406. case kEngineEventTypeControl: {
  2407. const EngineControlEvent& ctrlEvent(event.ctrl);
  2408. switch (ctrlEvent.type)
  2409. {
  2410. case kEngineControlEventTypeNull:
  2411. break;
  2412. case kEngineControlEventTypeParameter: {
  2413. #ifndef BUILD_BRIDGE
  2414. // Control backend stuff
  2415. if (event.channel == pData->ctrlChannel)
  2416. {
  2417. float value;
  2418. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2419. {
  2420. value = ctrlEvent.value;
  2421. setDryWet(value, false, false);
  2422. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2423. break;
  2424. }
  2425. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2426. {
  2427. value = ctrlEvent.value*127.0f/100.0f;
  2428. setVolume(value, false, false);
  2429. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2430. break;
  2431. }
  2432. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2433. {
  2434. float left, right;
  2435. value = ctrlEvent.value/0.5f - 1.0f;
  2436. if (value < 0.0f)
  2437. {
  2438. left = -1.0f;
  2439. right = (value*2.0f)+1.0f;
  2440. }
  2441. else if (value > 0.0f)
  2442. {
  2443. left = (value*2.0f)-1.0f;
  2444. right = 1.0f;
  2445. }
  2446. else
  2447. {
  2448. left = -1.0f;
  2449. right = 1.0f;
  2450. }
  2451. setBalanceLeft(left, false, false);
  2452. setBalanceRight(right, false, false);
  2453. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2454. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2455. break;
  2456. }
  2457. }
  2458. #endif
  2459. // Control plugin parameters
  2460. uint32_t k;
  2461. for (k=0; k < pData->param.count; ++k)
  2462. {
  2463. if (pData->param.data[k].midiChannel != event.channel)
  2464. continue;
  2465. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2466. continue;
  2467. if (pData->param.data[k].type != PARAMETER_INPUT)
  2468. continue;
  2469. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2470. continue;
  2471. float value;
  2472. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2473. {
  2474. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2475. }
  2476. else
  2477. {
  2478. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2479. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2480. value = std::rint(value);
  2481. }
  2482. setParameterValue(k, value, false, false, false);
  2483. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2484. break;
  2485. }
  2486. // check if event is already handled
  2487. if (k != pData->param.count)
  2488. break;
  2489. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2490. {
  2491. uint8_t midiData[3];
  2492. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2493. midiData[1] = uint8_t(ctrlEvent.param);
  2494. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2495. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2496. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2497. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2498. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2499. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2500. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2501. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2502. }
  2503. break;
  2504. } // case kEngineControlEventTypeParameter
  2505. case kEngineControlEventTypeMidiBank:
  2506. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2507. nextBankId = ctrlEvent.param;
  2508. break;
  2509. case kEngineControlEventTypeMidiProgram:
  2510. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2511. {
  2512. const uint32_t nextProgramId = ctrlEvent.param;
  2513. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2514. {
  2515. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2516. {
  2517. const int32_t index(static_cast<int32_t>(k));
  2518. setMidiProgram(index, false, false, false);
  2519. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2520. break;
  2521. }
  2522. }
  2523. }
  2524. break;
  2525. case kEngineControlEventTypeAllSoundOff:
  2526. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2527. {
  2528. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2529. uint8_t midiData[3];
  2530. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2531. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2532. midiData[2] = 0;
  2533. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2534. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2535. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2536. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2537. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2538. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2539. }
  2540. break;
  2541. case kEngineControlEventTypeAllNotesOff:
  2542. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2543. {
  2544. #ifndef BUILD_BRIDGE
  2545. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2546. {
  2547. allNotesOffSent = true;
  2548. sendMidiAllNotesOffToCallback();
  2549. }
  2550. #endif
  2551. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2552. uint8_t midiData[3];
  2553. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2554. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2555. midiData[2] = 0;
  2556. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2557. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2558. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2559. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2560. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2561. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2562. }
  2563. break;
  2564. } // switch (ctrlEvent.type)
  2565. break;
  2566. } // case kEngineEventTypeControl
  2567. case kEngineEventTypeMidi: {
  2568. const EngineMidiEvent& midiEvent(event.midi);
  2569. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  2570. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  2571. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2572. continue;
  2573. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2574. continue;
  2575. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2576. continue;
  2577. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2578. continue;
  2579. // Fix bad note-off (per LV2 spec)
  2580. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  2581. status = MIDI_STATUS_NOTE_OFF;
  2582. const uint32_t j = fEventsIn.ctrlIndex;
  2583. const uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2584. // put back channel in data
  2585. uint8_t midiData2[midiEvent.size];
  2586. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  2587. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  2588. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2589. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2590. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2591. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2592. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2593. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiData2);
  2594. if (status == MIDI_STATUS_NOTE_ON)
  2595. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiData[1], midiData[2]);
  2596. else if (status == MIDI_STATUS_NOTE_OFF)
  2597. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiData[1], 0.0f);
  2598. } break;
  2599. } // switch (event.type)
  2600. }
  2601. pData->postRtEvents.trySplice();
  2602. if (frames > timeOffset)
  2603. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  2604. } // End of Event Input and Processing
  2605. // --------------------------------------------------------------------------------------------------------
  2606. // Plugin processing (no events)
  2607. else
  2608. {
  2609. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  2610. } // End of Plugin processing (no events)
  2611. #ifndef BUILD_BRIDGE
  2612. // --------------------------------------------------------------------------------------------------------
  2613. // Latency, save values for next callback
  2614. if (fLatencyIndex != -1)
  2615. {
  2616. if (pData->latency != static_cast<uint32_t>(fParamBuffers[fLatencyIndex]))
  2617. {
  2618. fLatencyChanged = true;
  2619. }
  2620. else if (pData->latency > 0)
  2621. {
  2622. if (pData->latency <= frames)
  2623. {
  2624. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2625. FloatVectorOperations::copy(pData->latencyBuffers[i], audioIn[i]+(frames-pData->latency), static_cast<int>(pData->latency));
  2626. }
  2627. else
  2628. {
  2629. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  2630. {
  2631. for (k=0; k < pData->latency-frames; ++k)
  2632. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  2633. for (j=0; k < pData->latency; ++j, ++k)
  2634. pData->latencyBuffers[i][k] = audioIn[i][j];
  2635. }
  2636. }
  2637. }
  2638. }
  2639. #endif
  2640. // --------------------------------------------------------------------------------------------------------
  2641. // MIDI Output
  2642. if (fEventsOut.ctrl != nullptr)
  2643. {
  2644. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2645. {
  2646. const LV2_Atom_Event* ev;
  2647. LV2_Atom_Buffer_Iterator iter;
  2648. uint8_t* data;
  2649. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2650. for (;;)
  2651. {
  2652. data = nullptr;
  2653. ev = lv2_atom_buffer_get(&iter, &data);
  2654. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2655. break;
  2656. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2657. {
  2658. if (fEventsOut.ctrl->port != nullptr)
  2659. {
  2660. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2661. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2662. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2663. }
  2664. }
  2665. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2666. {
  2667. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2668. fAtomBufferOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2669. }
  2670. lv2_atom_buffer_increment(&iter);
  2671. }
  2672. }
  2673. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2674. {
  2675. const LV2_Event* ev;
  2676. LV2_Event_Iterator iter;
  2677. uint8_t* data;
  2678. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2679. for (;;)
  2680. {
  2681. data = nullptr;
  2682. ev = lv2_event_get(&iter, &data);
  2683. if (ev == nullptr || data == nullptr)
  2684. break;
  2685. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2686. {
  2687. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2688. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2689. }
  2690. lv2_event_increment(&iter);
  2691. }
  2692. }
  2693. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2694. {
  2695. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2696. uint32_t eventSize;
  2697. double eventTime;
  2698. uchar* eventData;
  2699. for (;;)
  2700. {
  2701. eventSize = 0;
  2702. eventTime = 0.0;
  2703. eventData = nullptr;
  2704. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2705. if (eventData == nullptr || eventSize == 0)
  2706. break;
  2707. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2708. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2709. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2710. lv2midi_step(&state);
  2711. }
  2712. }
  2713. }
  2714. #ifndef BUILD_BRIDGE
  2715. // --------------------------------------------------------------------------------------------------------
  2716. // Control Output
  2717. if (pData->event.portOut != nullptr)
  2718. {
  2719. uint8_t channel;
  2720. uint16_t param;
  2721. float value;
  2722. for (uint32_t k=0; k < pData->param.count; ++k)
  2723. {
  2724. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2725. continue;
  2726. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2727. if (pData->param.data[k].midiCC > 0)
  2728. {
  2729. channel = pData->param.data[k].midiChannel;
  2730. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2731. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2732. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2733. }
  2734. }
  2735. } // End of Control Output
  2736. #endif
  2737. // --------------------------------------------------------------------------------------------------------
  2738. // Final work
  2739. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2740. {
  2741. fExt.worker->end_run(fHandle);
  2742. if (fHandle2 != nullptr)
  2743. fExt.worker->end_run(fHandle2);
  2744. }
  2745. fFirstActive = false;
  2746. // --------------------------------------------------------------------------------------------------------
  2747. }
  2748. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  2749. {
  2750. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2751. if (pData->audioIn.count > 0)
  2752. {
  2753. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  2754. }
  2755. if (pData->audioOut.count > 0)
  2756. {
  2757. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  2758. }
  2759. if (pData->cvIn.count > 0)
  2760. {
  2761. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  2762. }
  2763. if (pData->cvOut.count > 0)
  2764. {
  2765. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  2766. }
  2767. // --------------------------------------------------------------------------------------------------------
  2768. // Try lock, silence otherwise
  2769. if (pData->engine->isOffline())
  2770. {
  2771. pData->singleMutex.lock();
  2772. }
  2773. else if (! pData->singleMutex.tryLock())
  2774. {
  2775. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2776. {
  2777. for (uint32_t k=0; k < frames; ++k)
  2778. audioOut[i][k+timeOffset] = 0.0f;
  2779. }
  2780. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2781. {
  2782. for (uint32_t k=0; k < frames; ++k)
  2783. cvOut[i][k+timeOffset] = 0.0f;
  2784. }
  2785. return false;
  2786. }
  2787. // --------------------------------------------------------------------------------------------------------
  2788. // Set audio buffers
  2789. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2790. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, static_cast<int>(frames));
  2791. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2792. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  2793. // --------------------------------------------------------------------------------------------------------
  2794. // Set CV buffers
  2795. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  2796. FloatVectorOperations::copy(fCvInBuffers[i], cvIn[i]+timeOffset, static_cast<int>(frames));
  2797. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2798. FloatVectorOperations::clear(fCvOutBuffers[i], static_cast<int>(frames));
  2799. // --------------------------------------------------------------------------------------------------------
  2800. // Run plugin
  2801. fDescriptor->run(fHandle, frames);
  2802. if (fHandle2 != nullptr)
  2803. fDescriptor->run(fHandle2, frames);
  2804. // --------------------------------------------------------------------------------------------------------
  2805. // Handle trigger parameters
  2806. for (uint32_t k=0; k < pData->param.count; ++k)
  2807. {
  2808. if (pData->param.data[k].type != PARAMETER_INPUT)
  2809. continue;
  2810. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2811. {
  2812. if (! carla_compareFloats(fParamBuffers[k], pData->param.ranges[k].def))
  2813. {
  2814. fParamBuffers[k] = pData->param.ranges[k].def;
  2815. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2816. }
  2817. }
  2818. }
  2819. pData->postRtEvents.trySplice();
  2820. #ifndef BUILD_BRIDGE
  2821. // --------------------------------------------------------------------------------------------------------
  2822. // Post-processing (dry/wet, volume and balance)
  2823. {
  2824. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  2825. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  2826. const bool isMono = (pData->audioIn.count == 1);
  2827. bool isPair;
  2828. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2829. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2830. {
  2831. // Dry/Wet
  2832. if (doDryWet)
  2833. {
  2834. for (uint32_t k=0; k < frames; ++k)
  2835. {
  2836. if (k < pData->latency)
  2837. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  2838. else if (pData->latency < frames)
  2839. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  2840. else
  2841. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  2842. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2843. }
  2844. }
  2845. // Balance
  2846. if (doBalance)
  2847. {
  2848. isPair = (i % 2 == 0);
  2849. if (isPair)
  2850. {
  2851. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2852. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  2853. }
  2854. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2855. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2856. for (uint32_t k=0; k < frames; ++k)
  2857. {
  2858. if (isPair)
  2859. {
  2860. // left
  2861. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2862. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2863. }
  2864. else
  2865. {
  2866. // right
  2867. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2868. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2869. }
  2870. }
  2871. }
  2872. // Volume (and buffer copy)
  2873. {
  2874. for (uint32_t k=0; k < frames; ++k)
  2875. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2876. }
  2877. }
  2878. } // End of Post-processing
  2879. #else // BUILD_BRIDGE
  2880. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2881. {
  2882. for (uint32_t k=0; k < frames; ++k)
  2883. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2884. }
  2885. #endif
  2886. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2887. {
  2888. for (uint32_t k=0; k < frames; ++k)
  2889. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  2890. }
  2891. // --------------------------------------------------------------------------------------------------------
  2892. pData->singleMutex.unlock();
  2893. return true;
  2894. }
  2895. void bufferSizeChanged(const uint32_t newBufferSize) override
  2896. {
  2897. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2898. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2899. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2900. {
  2901. if (fAudioInBuffers[i] != nullptr)
  2902. delete[] fAudioInBuffers[i];
  2903. fAudioInBuffers[i] = new float[newBufferSize];
  2904. }
  2905. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2906. {
  2907. if (fAudioOutBuffers[i] != nullptr)
  2908. delete[] fAudioOutBuffers[i];
  2909. fAudioOutBuffers[i] = new float[newBufferSize];
  2910. }
  2911. if (fHandle2 == nullptr)
  2912. {
  2913. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2914. {
  2915. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2916. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2917. }
  2918. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2919. {
  2920. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2921. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2922. }
  2923. }
  2924. else
  2925. {
  2926. if (pData->audioIn.count > 0)
  2927. {
  2928. CARLA_ASSERT(pData->audioIn.count == 2);
  2929. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2930. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2931. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2932. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2933. }
  2934. if (pData->audioOut.count > 0)
  2935. {
  2936. CARLA_ASSERT(pData->audioOut.count == 2);
  2937. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2938. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2939. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2940. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2941. }
  2942. }
  2943. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  2944. {
  2945. if (fCvInBuffers[i] != nullptr)
  2946. delete[] fCvInBuffers[i];
  2947. fCvInBuffers[i] = new float[newBufferSize];
  2948. fDescriptor->connect_port(fHandle, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  2949. if (fHandle2 != nullptr)
  2950. fDescriptor->connect_port(fHandle2, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  2951. }
  2952. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2953. {
  2954. if (fCvOutBuffers[i] != nullptr)
  2955. delete[] fCvOutBuffers[i];
  2956. fCvOutBuffers[i] = new float[newBufferSize];
  2957. fDescriptor->connect_port(fHandle, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  2958. if (fHandle2 != nullptr)
  2959. fDescriptor->connect_port(fHandle2, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  2960. }
  2961. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2962. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2963. {
  2964. fLv2Options.maxBufferSize = newBufferSizeInt;
  2965. if (fLv2Options.minBufferSize != 1)
  2966. fLv2Options.minBufferSize = newBufferSizeInt;
  2967. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2968. {
  2969. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2970. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2971. }
  2972. }
  2973. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2974. }
  2975. void sampleRateChanged(const double newSampleRate) override
  2976. {
  2977. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2978. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2979. if (! carla_compareFloats(fLv2Options.sampleRate, newSampleRate))
  2980. {
  2981. fLv2Options.sampleRate = newSampleRate;
  2982. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2983. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2984. }
  2985. for (uint32_t k=0; k < pData->param.count; ++k)
  2986. {
  2987. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2988. {
  2989. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2990. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2991. break;
  2992. }
  2993. }
  2994. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2995. }
  2996. void offlineModeChanged(const bool isOffline) override
  2997. {
  2998. for (uint32_t k=0; k < pData->param.count; ++k)
  2999. {
  3000. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_FREEWHEEL)
  3001. {
  3002. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  3003. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3004. break;
  3005. }
  3006. }
  3007. }
  3008. // -------------------------------------------------------------------
  3009. // Plugin buffers
  3010. void initBuffers() const noexcept override
  3011. {
  3012. fEventsIn.initBuffers();
  3013. fEventsOut.initBuffers();
  3014. CarlaPlugin::initBuffers();
  3015. }
  3016. void clearBuffers() noexcept override
  3017. {
  3018. carla_debug("Lv2Plugin::clearBuffers() - start");
  3019. if (fAudioInBuffers != nullptr)
  3020. {
  3021. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3022. {
  3023. if (fAudioInBuffers[i] != nullptr)
  3024. {
  3025. delete[] fAudioInBuffers[i];
  3026. fAudioInBuffers[i] = nullptr;
  3027. }
  3028. }
  3029. delete[] fAudioInBuffers;
  3030. fAudioInBuffers = nullptr;
  3031. }
  3032. if (fAudioOutBuffers != nullptr)
  3033. {
  3034. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3035. {
  3036. if (fAudioOutBuffers[i] != nullptr)
  3037. {
  3038. delete[] fAudioOutBuffers[i];
  3039. fAudioOutBuffers[i] = nullptr;
  3040. }
  3041. }
  3042. delete[] fAudioOutBuffers;
  3043. fAudioOutBuffers = nullptr;
  3044. }
  3045. if (fCvInBuffers != nullptr)
  3046. {
  3047. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3048. {
  3049. if (fCvInBuffers[i] != nullptr)
  3050. {
  3051. delete[] fCvInBuffers[i];
  3052. fCvInBuffers[i] = nullptr;
  3053. }
  3054. }
  3055. delete[] fCvInBuffers;
  3056. fCvInBuffers = nullptr;
  3057. }
  3058. if (fCvOutBuffers != nullptr)
  3059. {
  3060. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3061. {
  3062. if (fCvOutBuffers[i] != nullptr)
  3063. {
  3064. delete[] fCvOutBuffers[i];
  3065. fCvOutBuffers[i] = nullptr;
  3066. }
  3067. }
  3068. delete[] fCvOutBuffers;
  3069. fCvOutBuffers = nullptr;
  3070. }
  3071. if (fParamBuffers != nullptr)
  3072. {
  3073. delete[] fParamBuffers;
  3074. fParamBuffers = nullptr;
  3075. }
  3076. fEventsIn.clear();
  3077. fEventsOut.clear();
  3078. CarlaPlugin::clearBuffers();
  3079. carla_debug("Lv2Plugin::clearBuffers() - end");
  3080. }
  3081. // -------------------------------------------------------------------
  3082. // OSC stuff
  3083. bool updateOscDataExtra() override
  3084. {
  3085. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  3086. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i, nullptr));
  3087. osc_send_lv2_urid_map(pData->osc.data, CARLA_URI_MAP_ID_NULL, "Complete");
  3088. return true;
  3089. }
  3090. // -------------------------------------------------------------------
  3091. // Post-poned UI Stuff
  3092. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3093. {
  3094. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3095. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3096. if (fUI.type == UI::TYPE_OSC)
  3097. {
  3098. if (pData->osc.data.target != nullptr)
  3099. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  3100. }
  3101. else
  3102. {
  3103. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr)
  3104. {
  3105. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3106. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  3107. }
  3108. }
  3109. }
  3110. void uiMidiProgramChange(const uint32_t index) noexcept override
  3111. {
  3112. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3113. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3114. if (fUI.type == UI::TYPE_OSC)
  3115. {
  3116. if (pData->osc.data.target != nullptr)
  3117. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3118. }
  3119. else
  3120. {
  3121. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  3122. fExt.uiprograms->select_program(fUI.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3123. }
  3124. }
  3125. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3126. {
  3127. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3128. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3129. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3130. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3131. if (fUI.type == UI::TYPE_OSC)
  3132. {
  3133. if (pData->osc.data.target != nullptr)
  3134. {
  3135. uint8_t midiData[4] = { 0, 0, 0, 0 };
  3136. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3137. midiData[2] = note;
  3138. midiData[3] = velo;
  3139. osc_send_midi(pData->osc.data, midiData);
  3140. }
  3141. }
  3142. else
  3143. {
  3144. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3145. {
  3146. LV2_Atom_MidiEvent midiEv;
  3147. midiEv.event.time.frames = 0;
  3148. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3149. midiEv.event.body.size = 3;
  3150. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3151. midiEv.data[1] = note;
  3152. midiEv.data[2] = velo;
  3153. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3154. }
  3155. }
  3156. }
  3157. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3158. {
  3159. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3160. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3161. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3162. if (fUI.type == UI::TYPE_OSC)
  3163. {
  3164. if (pData->osc.data.target != nullptr)
  3165. {
  3166. uint8_t midiData[4] = { 0, 0, 0, 0 };
  3167. midiData[1] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3168. midiData[2] = note;
  3169. osc_send_midi(pData->osc.data, midiData);
  3170. }
  3171. }
  3172. else
  3173. {
  3174. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3175. {
  3176. LV2_Atom_MidiEvent midiEv;
  3177. midiEv.event.time.frames = 0;
  3178. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3179. midiEv.event.body.size = 3;
  3180. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3181. midiEv.data[1] = note;
  3182. midiEv.data[2] = 0;
  3183. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3184. }
  3185. }
  3186. }
  3187. // -------------------------------------------------------------------
  3188. bool isRealtimeSafe() const noexcept
  3189. {
  3190. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3191. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3192. {
  3193. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3194. return true;
  3195. }
  3196. return false;
  3197. }
  3198. bool needsFixedBuffer() const noexcept
  3199. {
  3200. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3201. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3202. {
  3203. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3204. return true;
  3205. }
  3206. return false;
  3207. }
  3208. // -------------------------------------------------------------------
  3209. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3210. {
  3211. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3212. #ifndef LV2_UIS_ONLY_INPROCESS
  3213. const LV2_RDF_UI* const rdfUI(&fRdfDescriptor->UIs[uiId]);
  3214. if (std::strstr(rdfUI->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3215. return false;
  3216. for (uint32_t i=0; i < rdfUI->FeatureCount; ++i)
  3217. {
  3218. if (std::strcmp(rdfUI->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3219. return false;
  3220. if (std::strcmp(rdfUI->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3221. return false;
  3222. }
  3223. return true;
  3224. #else
  3225. return false;
  3226. #endif
  3227. }
  3228. bool isUiResizable() const noexcept
  3229. {
  3230. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr, false);
  3231. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  3232. {
  3233. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3234. return false;
  3235. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3236. return false;
  3237. }
  3238. return true;
  3239. }
  3240. const char* getUiBridgeBinary(const LV2_Property type) const
  3241. {
  3242. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3243. if (bridgeBinary.isEmpty())
  3244. return nullptr;
  3245. switch (type)
  3246. {
  3247. case LV2_UI_GTK2:
  3248. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk2";
  3249. break;
  3250. case LV2_UI_GTK3:
  3251. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk3";
  3252. break;
  3253. case LV2_UI_QT4:
  3254. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt4";
  3255. break;
  3256. case LV2_UI_QT5:
  3257. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt5";
  3258. break;
  3259. case LV2_UI_COCOA:
  3260. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-cocoa";
  3261. break;
  3262. case LV2_UI_WINDOWS:
  3263. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-windows";
  3264. break;
  3265. case LV2_UI_X11:
  3266. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-x11";
  3267. break;
  3268. case LV2_UI_EXTERNAL:
  3269. case LV2_UI_OLD_EXTERNAL:
  3270. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-external";
  3271. break;
  3272. default:
  3273. return nullptr;
  3274. }
  3275. #ifdef CARLA_OS_WIN
  3276. bridgeBinary += ".exe";
  3277. #endif
  3278. if (! File(bridgeBinary.buffer()).existsAsFile())
  3279. return nullptr;
  3280. return bridgeBinary.dup();
  3281. }
  3282. // -------------------------------------------------------------------
  3283. void recheckExtensions()
  3284. {
  3285. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3286. carla_debug("Lv2Plugin::recheckExtensions()");
  3287. fExt.options = nullptr;
  3288. fExt.programs = nullptr;
  3289. fExt.state = nullptr;
  3290. fExt.worker = nullptr;
  3291. if (fRdfDescriptor->ExtensionCount == 0 || fDescriptor->extension_data == nullptr)
  3292. return;
  3293. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3294. {
  3295. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->Extensions[i] != nullptr);
  3296. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3297. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3298. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3299. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3300. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3301. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3302. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3303. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3304. else
  3305. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3306. }
  3307. if (fDescriptor->extension_data != nullptr)
  3308. {
  3309. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3310. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3311. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3312. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3313. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3314. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3315. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3316. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3317. // check if invalid
  3318. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3319. fExt.options = nullptr;
  3320. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3321. fExt.programs = nullptr;
  3322. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3323. fExt.state = nullptr;
  3324. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3325. fExt.worker = nullptr;
  3326. }
  3327. }
  3328. // -------------------------------------------------------------------
  3329. void updateUi()
  3330. {
  3331. CARLA_SAFE_ASSERT_RETURN(fUI.handle != nullptr,);
  3332. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  3333. carla_debug("Lv2Plugin::updateUi()");
  3334. // update midi program
  3335. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3336. {
  3337. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3338. fExt.uiprograms->select_program(fUI.handle, curData.bank, curData.program);
  3339. }
  3340. // update control ports
  3341. if (fUI.descriptor->port_event != nullptr)
  3342. {
  3343. float value;
  3344. for (uint32_t i=0; i < pData->param.count; ++i)
  3345. {
  3346. value = getParameterValue(i);
  3347. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3348. }
  3349. }
  3350. }
  3351. // -------------------------------------------------------------------
  3352. LV2_URID getCustomURID(const char* const uri)
  3353. {
  3354. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3355. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3356. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3357. {
  3358. const char* const thisUri(fCustomURIDs.getAt(i, nullptr));
  3359. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3360. return static_cast<LV2_URID>(i);
  3361. }
  3362. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3363. fCustomURIDs.append(carla_strdup(uri));
  3364. if (fUI.type == UI::TYPE_OSC && pData->osc.data.target != nullptr)
  3365. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3366. return urid;
  3367. }
  3368. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3369. {
  3370. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  3371. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  3372. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3373. return fCustomURIDs.getAt(urid, nullptr);
  3374. }
  3375. // -------------------------------------------------------------------
  3376. void handleProgramChanged(const int32_t index)
  3377. {
  3378. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3379. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3380. if (index == -1)
  3381. {
  3382. const ScopedSingleProcessLocker spl(this, true);
  3383. return reloadPrograms(false);
  3384. }
  3385. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3386. {
  3387. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3388. {
  3389. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3390. if (pData->midiprog.data[index].name != nullptr)
  3391. delete[] pData->midiprog.data[index].name;
  3392. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3393. if (index == pData->midiprog.current)
  3394. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3395. else
  3396. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3397. }
  3398. }
  3399. }
  3400. // -------------------------------------------------------------------
  3401. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3402. {
  3403. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3404. carla_debug("Lv2Plugin::handleResizePort(%i, " P_SIZE ")", index, size);
  3405. // TODO
  3406. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3407. (void)index;
  3408. }
  3409. // -------------------------------------------------------------------
  3410. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3411. {
  3412. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3413. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3414. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3415. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3416. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3417. carla_debug("Lv2Plugin::handleStateStore(%i:\"%s\", %p, " P_SIZE ", %i:\"%s\", %i)", key, carla_lv2_urid_unmap(this, key), value, size, type, carla_lv2_urid_unmap(this, type), flags);
  3418. const char* const skey(carla_lv2_urid_unmap(this, key));
  3419. const char* const stype(carla_lv2_urid_unmap(this, type));
  3420. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3421. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3422. // Check if we already have this key
  3423. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3424. {
  3425. CustomData& data(it.getValue());
  3426. if (std::strcmp(data.key, skey) == 0)
  3427. {
  3428. // found it
  3429. if (data.value != nullptr)
  3430. delete[] data.value;
  3431. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3432. data.value = carla_strdup((const char*)value);
  3433. else
  3434. data.value = CarlaString::asBase64(value, size).dup();
  3435. return LV2_STATE_SUCCESS;
  3436. }
  3437. }
  3438. // Otherwise store it
  3439. CustomData newData;
  3440. newData.type = carla_strdup(stype);
  3441. newData.key = carla_strdup(skey);
  3442. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3443. newData.value = carla_strdup((const char*)value);
  3444. else
  3445. newData.value = CarlaString::asBase64(value, size).dup();
  3446. pData->custom.append(newData);
  3447. return LV2_STATE_SUCCESS;
  3448. }
  3449. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3450. {
  3451. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3452. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3453. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3454. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3455. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3456. const char* const skey(carla_lv2_urid_unmap(this, key));
  3457. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3458. const char* stype = nullptr;
  3459. const char* stringData = nullptr;
  3460. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3461. {
  3462. const CustomData& data(it.getValue());
  3463. if (std::strcmp(data.key, skey) == 0)
  3464. {
  3465. stype = data.type;
  3466. stringData = data.value;
  3467. break;
  3468. }
  3469. }
  3470. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3471. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3472. *type = carla_lv2_urid_map(this, stype);
  3473. *flags = LV2_STATE_IS_POD;
  3474. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3475. {
  3476. *size = std::strlen(stringData);
  3477. return stringData;
  3478. }
  3479. else
  3480. {
  3481. if (fLastStateChunk != nullptr)
  3482. {
  3483. std::free(fLastStateChunk);
  3484. fLastStateChunk = nullptr;
  3485. }
  3486. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stringData));
  3487. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3488. fLastStateChunk = std::malloc(chunk.size());
  3489. CARLA_SAFE_ASSERT_RETURN(fLastStateChunk != nullptr, nullptr);
  3490. std::memcpy(fLastStateChunk, chunk.data(), chunk.size());
  3491. *size = chunk.size();
  3492. return fLastStateChunk;
  3493. }
  3494. }
  3495. // -------------------------------------------------------------------
  3496. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3497. {
  3498. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3499. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3500. carla_debug("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3501. if (pData->engine->isOffline())
  3502. {
  3503. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3504. return LV2_WORKER_SUCCESS;
  3505. }
  3506. LV2_Atom atom;
  3507. atom.size = size;
  3508. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3509. return fAtomBufferOut.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3510. }
  3511. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3512. {
  3513. carla_debug("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3514. LV2_Atom atom;
  3515. atom.size = size;
  3516. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3517. return fAtomBufferIn.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3518. }
  3519. // -------------------------------------------------------------------
  3520. void handleExternalUIClosed()
  3521. {
  3522. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EXTERNAL,);
  3523. carla_debug("Lv2Plugin::handleExternalUIClosed()");
  3524. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->cleanup != nullptr)
  3525. fUI.descriptor->cleanup(fUI.handle);
  3526. fUI.handle = nullptr;
  3527. fUI.widget = nullptr;
  3528. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3529. }
  3530. void handlePluginUIClosed() override
  3531. {
  3532. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  3533. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  3534. carla_debug("Lv2Plugin::handlePluginUIClosed()");
  3535. fUI.window->hide();
  3536. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->cleanup != nullptr)
  3537. fUI.descriptor->cleanup(fUI.handle);
  3538. fUI.handle = nullptr;
  3539. fUI.widget = nullptr;
  3540. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3541. }
  3542. void handlePluginUIResized(const uint width, const uint height) override
  3543. {
  3544. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  3545. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  3546. carla_stdout("Lv2Plugin::handlePluginUIResized(%u, %u)", width, height);
  3547. if (fUI.handle != nullptr && fExt.uiresize != nullptr)
  3548. fExt.uiresize->ui_resize(fUI.handle, static_cast<int>(width), static_cast<int>(height));
  3549. }
  3550. // -------------------------------------------------------------------
  3551. uint32_t handleUIPortMap(const char* const symbol) const noexcept
  3552. {
  3553. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3554. carla_debug("Lv2Plugin::handleUIPortMap(\"%s\")", symbol);
  3555. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3556. {
  3557. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3558. return i;
  3559. }
  3560. return LV2UI_INVALID_PORT_INDEX;
  3561. }
  3562. int handleUIResize(const int width, const int height)
  3563. {
  3564. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr, 1);
  3565. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3566. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3567. carla_debug("Lv2Plugin::handleUIResize(%i, %i)", width, height);
  3568. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3569. return 0;
  3570. }
  3571. void handleUIWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3572. {
  3573. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3574. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3575. carla_debug("Lv2Plugin::handleUIWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3576. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  3577. switch (format)
  3578. {
  3579. case CARLA_URI_MAP_ID_NULL: {
  3580. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3581. for (uint32_t i=0; i < pData->param.count; ++i)
  3582. {
  3583. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  3584. continue;
  3585. index = i;
  3586. break;
  3587. }
  3588. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  3589. const float value(*(const float*)buffer);
  3590. //if (! carla_compareFloats(fParamBuffers[index], value))
  3591. setParameterValue(index, value, false, true, true);
  3592. } break;
  3593. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3594. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT: {
  3595. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  3596. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  3597. // plugins sometimes fail on this, not good...
  3598. CARLA_SAFE_ASSERT_INT2(bufferSize == lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom)+atom->size)), bufferSize, atom->size);
  3599. for (uint32_t i=0; i < fEventsIn.count; ++i)
  3600. {
  3601. if (fEventsIn.data[i].rindex != rindex)
  3602. continue;
  3603. index = i;
  3604. break;
  3605. }
  3606. // for bad plugins
  3607. if (index == LV2UI_INVALID_PORT_INDEX)
  3608. {
  3609. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  3610. index = fEventsIn.ctrlIndex;
  3611. }
  3612. fAtomBufferIn.put(atom, index);
  3613. } break;
  3614. default:
  3615. carla_stdout("Lv2Plugin::handleUIWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3616. break;
  3617. }
  3618. }
  3619. // -------------------------------------------------------------------
  3620. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3621. {
  3622. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3623. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3624. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3625. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3626. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3627. int32_t rindex = -1;
  3628. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3629. {
  3630. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3631. {
  3632. rindex = static_cast<int32_t>(i);
  3633. break;
  3634. }
  3635. }
  3636. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3637. float paramValue;
  3638. switch (type)
  3639. {
  3640. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3641. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3642. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3643. break;
  3644. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3645. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3646. paramValue = static_cast<float>((*(const double*)value));
  3647. break;
  3648. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3649. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3650. paramValue = (*(const float*)value);
  3651. break;
  3652. case CARLA_URI_MAP_ID_ATOM_INT:
  3653. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3654. paramValue = static_cast<float>((*(const int32_t*)value));
  3655. break;
  3656. case CARLA_URI_MAP_ID_ATOM_LONG:
  3657. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3658. paramValue = static_cast<float>((*(const int64_t*)value));
  3659. break;
  3660. default:
  3661. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3662. return;
  3663. }
  3664. for (uint32_t i=0; i < pData->param.count; ++i)
  3665. {
  3666. if (pData->param.data[i].rindex == rindex)
  3667. {
  3668. setParameterValue(i, paramValue, true, true, true);
  3669. break;
  3670. }
  3671. }
  3672. }
  3673. // -------------------------------------------------------------------
  3674. void* getNativeHandle() const noexcept override
  3675. {
  3676. return fHandle;
  3677. }
  3678. const void* getNativeDescriptor() const noexcept override
  3679. {
  3680. return fDescriptor;
  3681. }
  3682. // -------------------------------------------------------------------
  3683. public:
  3684. bool init(const char* const bundle, const char* const name, const char* const uri)
  3685. {
  3686. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3687. // ---------------------------------------------------------------
  3688. // first checks
  3689. if (pData->client != nullptr)
  3690. {
  3691. pData->engine->setLastError("Plugin client is already registered");
  3692. return false;
  3693. }
  3694. if (bundle == nullptr || bundle[0] == '\0')
  3695. {
  3696. pData->engine->setLastError("null bundle");
  3697. return false;
  3698. }
  3699. if (uri == nullptr || uri[0] == '\0')
  3700. {
  3701. pData->engine->setLastError("null uri");
  3702. return false;
  3703. }
  3704. // ---------------------------------------------------------------
  3705. // Init LV2 World if needed, sets LV2_PATH for lilv
  3706. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3707. lv2World.initIfNeeded(pData->engine->getOptions().pathLV2);
  3708. // ---------------------------------------------------------------
  3709. // get plugin from lv2_rdf (lilv)
  3710. fRdfDescriptor = lv2_rdf_new(uri, true);
  3711. if (fRdfDescriptor == nullptr)
  3712. {
  3713. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3714. return false;
  3715. }
  3716. // ---------------------------------------------------------------
  3717. // open DLL
  3718. if (! pData->libOpen(fRdfDescriptor->Binary))
  3719. {
  3720. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3721. return false;
  3722. }
  3723. // ---------------------------------------------------------------
  3724. // try to get DLL main entry via new mode
  3725. if (const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)pData->libSymbol("lv2_lib_descriptor"))
  3726. {
  3727. // -----------------------------------------------------------
  3728. // all ok, get lib descriptor
  3729. const LV2_Lib_Descriptor* const libDesc = libDescFn(bundle, nullptr);
  3730. if (libDesc == nullptr)
  3731. {
  3732. pData->engine->setLastError("Could not find the LV2 Descriptor");
  3733. return false;
  3734. }
  3735. // -----------------------------------------------------------
  3736. // get descriptor that matches URI (new mode)
  3737. uint32_t i = 0;
  3738. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3739. {
  3740. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3741. break;
  3742. }
  3743. }
  3744. else
  3745. {
  3746. // -----------------------------------------------------------
  3747. // get DLL main entry (old mode)
  3748. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3749. if (descFn == nullptr)
  3750. {
  3751. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3752. return false;
  3753. }
  3754. // -----------------------------------------------------------
  3755. // get descriptor that matches URI (old mode)
  3756. uint32_t i = 0;
  3757. while ((fDescriptor = descFn(i++)))
  3758. {
  3759. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3760. break;
  3761. }
  3762. }
  3763. if (fDescriptor == nullptr)
  3764. {
  3765. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3766. return false;
  3767. }
  3768. // ---------------------------------------------------------------
  3769. // check supported port-types and features
  3770. bool canContinue = true;
  3771. // Check supported ports
  3772. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3773. {
  3774. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3775. if (! is_lv2_port_supported(portTypes))
  3776. {
  3777. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3778. {
  3779. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3780. canContinue = false;
  3781. break;
  3782. }
  3783. }
  3784. }
  3785. // Check supported features
  3786. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3787. {
  3788. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[j]);
  3789. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3790. {
  3791. carla_stderr("Plugin DSP wants UI feature '%s', ignoring this", feature.URI);
  3792. }
  3793. else if (LV2_IS_FEATURE_REQUIRED(feature.Type) && ! is_lv2_feature_supported(feature.URI))
  3794. {
  3795. CarlaString msg("Plugin wants a feature that is not supported:\n");
  3796. msg += feature.URI;
  3797. canContinue = false;
  3798. pData->engine->setLastError(msg);
  3799. break;
  3800. }
  3801. }
  3802. if (! canContinue)
  3803. {
  3804. // error already set
  3805. return false;
  3806. }
  3807. // ---------------------------------------------------------------
  3808. // get info
  3809. if (name != nullptr && name[0] != '\0')
  3810. pData->name = pData->engine->getUniquePluginName(name);
  3811. else
  3812. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3813. pData->filename = carla_strdup(bundle);
  3814. // ---------------------------------------------------------------
  3815. // register client
  3816. pData->client = pData->engine->addClient(this);
  3817. if (pData->client == nullptr || ! pData->client->isOk())
  3818. {
  3819. pData->engine->setLastError("Failed to register plugin client");
  3820. return false;
  3821. }
  3822. // ---------------------------------------------------------------
  3823. // initialize options
  3824. fLv2Options.minBufferSize = 1;
  3825. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3826. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3827. fLv2Options.frontendWinId = static_cast<int64_t>(pData->engine->getOptions().frontendWinId);
  3828. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3829. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3830. {
  3831. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3832. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3833. {
  3834. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3835. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3836. }
  3837. }
  3838. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3839. // ---------------------------------------------------------------
  3840. // initialize features (part 1)
  3841. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3842. eventFt->callback_data = this;
  3843. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3844. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3845. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3846. logFt->handle = this;
  3847. logFt->printf = carla_lv2_log_printf;
  3848. logFt->vprintf = carla_lv2_log_vprintf;
  3849. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3850. stateMakePathFt->handle = this;
  3851. stateMakePathFt->path = carla_lv2_state_make_path;
  3852. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3853. stateMapPathFt->handle = this;
  3854. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3855. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3856. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3857. programsFt->handle = this;
  3858. programsFt->program_changed = carla_lv2_program_changed;
  3859. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3860. rsPortFt->data = this;
  3861. rsPortFt->resize = carla_lv2_resize_port;
  3862. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3863. lv2_rtmempool_init(rtMemPoolFt);
  3864. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3865. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3866. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3867. uriMapFt->callback_data = this;
  3868. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3869. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3870. uridMapFt->handle = this;
  3871. uridMapFt->map = carla_lv2_urid_map;
  3872. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3873. uridUnmapFt->handle = this;
  3874. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3875. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3876. workerFt->handle = this;
  3877. workerFt->schedule_work = carla_lv2_worker_schedule;
  3878. // ---------------------------------------------------------------
  3879. // initialize features (part 2)
  3880. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3881. fFeatures[j] = new LV2_Feature;
  3882. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3883. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3884. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3885. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3886. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3887. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3888. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3889. fFeatures[kFeatureIdEvent]->data = eventFt;
  3890. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3891. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3892. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3893. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3894. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3895. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3896. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3897. fFeatures[kFeatureIdLogs]->data = logFt;
  3898. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3899. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3900. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3901. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3902. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3903. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3904. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3905. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3906. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3907. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3908. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3909. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3910. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3911. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3912. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3913. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3914. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3915. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3916. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3917. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3918. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3919. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3920. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3921. fFeatures[kFeatureIdWorker]->data = workerFt;
  3922. // if a fixed buffer is not needed, skip its feature
  3923. if (! needsFixedBuffer())
  3924. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3925. // if power of 2 is not possible, skip its feature FIXME
  3926. //if (fLv2Options.maxBufferSize)
  3927. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3928. // ---------------------------------------------------------------
  3929. // initialize plugin
  3930. try {
  3931. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3932. } catch(...) {}
  3933. if (fHandle == nullptr)
  3934. {
  3935. pData->engine->setLastError("Plugin failed to initialize");
  3936. return false;
  3937. }
  3938. if (std::strcmp(uri, "http://hyperglitch.com/dev/VocProc") == 0)
  3939. fCanInit2 = false;
  3940. // ---------------------------------------------------------------
  3941. // set default options
  3942. pData->options = 0x0;
  3943. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3944. if (fLatencyIndex >= 0 || getMidiInCount() > 0 || needsFixedBuffer())
  3945. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3946. if (fCanInit2 && pData->engine->getOptions().forceStereo)
  3947. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3948. if (getMidiInCount() > 0)
  3949. {
  3950. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3951. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3952. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3953. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3954. }
  3955. // ---------------------------------------------------------------
  3956. // gui stuff
  3957. if (fRdfDescriptor->UICount != 0)
  3958. initUi();
  3959. return true;
  3960. }
  3961. // -------------------------------------------------------------------
  3962. void initUi()
  3963. {
  3964. // ---------------------------------------------------------------
  3965. // find the most appropriate ui
  3966. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3967. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3968. #if defined(BUILD_BRIDGE)
  3969. const bool preferUiBridges(false);
  3970. #elif defined(LV2_UIS_ONLY_BRIDGES)
  3971. const bool preferUiBridges(true);
  3972. #else
  3973. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3974. #endif
  3975. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3976. {
  3977. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3978. const int ii(static_cast<int>(i));
  3979. switch (fRdfDescriptor->UIs[i].Type)
  3980. {
  3981. case LV2_UI_QT4:
  3982. if (isUiBridgeable(i))
  3983. eQt4 = ii;
  3984. break;
  3985. case LV2_UI_QT5:
  3986. if (isUiBridgeable(i))
  3987. eQt5 = ii;
  3988. break;
  3989. case LV2_UI_GTK2:
  3990. if (isUiBridgeable(i))
  3991. eGtk2 = ii;
  3992. break;
  3993. case LV2_UI_GTK3:
  3994. if (isUiBridgeable(i))
  3995. eGtk3 = ii;
  3996. break;
  3997. case LV2_UI_COCOA:
  3998. if (isUiBridgeable(i) && preferUiBridges)
  3999. eCocoa = ii;
  4000. iCocoa = ii;
  4001. break;
  4002. case LV2_UI_WINDOWS:
  4003. if (isUiBridgeable(i) && preferUiBridges)
  4004. eWindows = ii;
  4005. iWindows = ii;
  4006. break;
  4007. case LV2_UI_X11:
  4008. if (isUiBridgeable(i) && preferUiBridges)
  4009. eX11 = ii;
  4010. iX11 = ii;
  4011. break;
  4012. case LV2_UI_EXTERNAL:
  4013. case LV2_UI_OLD_EXTERNAL:
  4014. if (isUiBridgeable(i))
  4015. eExt = ii;
  4016. iExt = ii;
  4017. break;
  4018. default:
  4019. break;
  4020. }
  4021. }
  4022. if (eQt4 >= 0)
  4023. iFinal = eQt4;
  4024. else if (eQt5 >= 0)
  4025. iFinal = eQt5;
  4026. else if (eGtk2 >= 0)
  4027. iFinal = eGtk2;
  4028. else if (eGtk3 >= 0)
  4029. iFinal = eGtk3;
  4030. #ifdef CARLA_OS_MAC
  4031. else if (eCocoa >= 0)
  4032. iFinal = eCocoa;
  4033. #endif
  4034. #ifdef CARLA_OS_WIN
  4035. else if (eWindows >= 0)
  4036. iFinal = eWindows;
  4037. #endif
  4038. #ifdef HAVE_X11
  4039. else if (eX11 >= 0)
  4040. iFinal = eX11;
  4041. #endif
  4042. //else if (eExt >= 0) // TODO
  4043. // iFinal = eExt;
  4044. #ifndef LV2_UIS_ONLY_BRIDGES
  4045. # ifdef CARLA_OS_MAC
  4046. else if (iCocoa >= 0)
  4047. iFinal = iCocoa;
  4048. # endif
  4049. # ifdef CARLA_OS_WIN
  4050. else if (iWindows >= 0)
  4051. iFinal = iWindows;
  4052. # endif
  4053. # ifdef HAVE_X11
  4054. else if (iX11 >= 0)
  4055. iFinal = iX11;
  4056. # endif
  4057. #endif
  4058. else if (iExt >= 0)
  4059. iFinal = iExt;
  4060. if (iFinal < 0)
  4061. {
  4062. // no suitable UI found, see if there's one which supports ui:showInterface
  4063. bool hasShowInterface = false;
  4064. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  4065. {
  4066. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  4067. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  4068. {
  4069. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  4070. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  4071. continue;
  4072. iFinal = static_cast<int>(i);
  4073. hasShowInterface = true;
  4074. break;
  4075. }
  4076. }
  4077. if (! hasShowInterface)
  4078. {
  4079. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  4080. return;
  4081. }
  4082. }
  4083. fUI.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  4084. // ---------------------------------------------------------------
  4085. // check supported ui features
  4086. bool canContinue = true;
  4087. bool canDelete = true;
  4088. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  4089. {
  4090. if (! is_lv2_ui_feature_supported(fUI.rdfDescriptor->Features[i].URI))
  4091. {
  4092. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUI.rdfDescriptor->Features[i].URI);
  4093. if (LV2_IS_FEATURE_REQUIRED(fUI.rdfDescriptor->Features[i].Type))
  4094. {
  4095. canContinue = false;
  4096. break;
  4097. }
  4098. }
  4099. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__makeResident) == 0)
  4100. canDelete = false;
  4101. }
  4102. if (! canContinue)
  4103. {
  4104. fUI.rdfDescriptor = nullptr;
  4105. return;
  4106. }
  4107. // ---------------------------------------------------------------
  4108. // initialize ui according to type
  4109. const LV2_Property uiType(fUI.rdfDescriptor->Type);
  4110. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  4111. {
  4112. // -----------------------------------------------------------
  4113. // initialize osc-bridge
  4114. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  4115. {
  4116. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  4117. fUI.type = UI::TYPE_OSC;
  4118. pData->osc.thread.setOscData(bridgeBinary, fRdfDescriptor->URI, fUI.rdfDescriptor->URI, fUI.rdfDescriptor->Bundle);
  4119. delete[] bridgeBinary;
  4120. return;
  4121. }
  4122. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3)
  4123. {
  4124. carla_stderr2("Failed to find UI bridge binary, cannot use UI");
  4125. fUI.rdfDescriptor = nullptr;
  4126. return;
  4127. }
  4128. }
  4129. #ifdef LV2_UIS_ONLY_BRIDGES
  4130. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  4131. fUI.rdfDescriptor = nullptr;
  4132. return;
  4133. #endif
  4134. // ---------------------------------------------------------------
  4135. // open UI DLL
  4136. if (! pData->uiLibOpen(fUI.rdfDescriptor->Binary, canDelete))
  4137. {
  4138. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUI.rdfDescriptor->Binary));
  4139. fUI.rdfDescriptor = nullptr;
  4140. return;
  4141. }
  4142. // ---------------------------------------------------------------
  4143. // get UI DLL main entry
  4144. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  4145. if (uiDescFn == nullptr)
  4146. {
  4147. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  4148. pData->uiLibClose();
  4149. fUI.rdfDescriptor = nullptr;
  4150. return;
  4151. }
  4152. // ---------------------------------------------------------------
  4153. // get UI descriptor that matches UI URI
  4154. uint32_t i = 0;
  4155. while ((fUI.descriptor = uiDescFn(i++)))
  4156. {
  4157. if (std::strcmp(fUI.descriptor->URI, fUI.rdfDescriptor->URI) == 0)
  4158. break;
  4159. }
  4160. if (fUI.descriptor == nullptr)
  4161. {
  4162. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  4163. pData->uiLibClose();
  4164. fUI.rdfDescriptor = nullptr;
  4165. return;
  4166. }
  4167. // ---------------------------------------------------------------
  4168. // check if ui is usable
  4169. switch (uiType)
  4170. {
  4171. case LV2_UI_QT4:
  4172. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  4173. fUI.type = UI::TYPE_EMBED;
  4174. break;
  4175. case LV2_UI_QT5:
  4176. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  4177. fUI.type = UI::TYPE_EMBED;
  4178. break;
  4179. case LV2_UI_GTK2:
  4180. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  4181. fUI.type = UI::TYPE_EMBED;
  4182. break;
  4183. case LV2_UI_GTK3:
  4184. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  4185. fUI.type = UI::TYPE_EMBED;
  4186. break;
  4187. #ifdef CARLA_OS_MAC
  4188. case LV2_UI_COCOA:
  4189. carla_stdout("Will use LV2 Cocoa UI");
  4190. fUI.type = UI::TYPE_EMBED;
  4191. break;
  4192. #endif
  4193. #ifdef CARLA_OS_WIN
  4194. case LV2_UI_WINDOWS:
  4195. carla_stdout("Will use LV2 Windows UI");
  4196. fUI.type = UI::TYPE_EMBED;
  4197. break;
  4198. #endif
  4199. case LV2_UI_X11:
  4200. #ifdef HAVE_X11
  4201. carla_stdout("Will use LV2 X11 UI");
  4202. #else
  4203. carla_stdout("Will use LV2 X11 UI, NOT!");
  4204. #endif
  4205. fUI.type = UI::TYPE_EMBED;
  4206. break;
  4207. case LV2_UI_EXTERNAL:
  4208. case LV2_UI_OLD_EXTERNAL:
  4209. carla_stdout("Will use LV2 External UI");
  4210. fUI.type = UI::TYPE_EXTERNAL;
  4211. break;
  4212. }
  4213. if (fUI.type == UI::TYPE_NULL)
  4214. {
  4215. pData->uiLibClose();
  4216. fUI.descriptor = nullptr;
  4217. fUI.rdfDescriptor = nullptr;
  4218. return;
  4219. }
  4220. // ---------------------------------------------------------------
  4221. // initialize ui data
  4222. CarlaString guiTitle(pData->name);
  4223. guiTitle += " (GUI)";
  4224. fLv2Options.windowTitle = guiTitle.dup();
  4225. fLv2Options.opts[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  4226. fLv2Options.opts[Lv2PluginOptions::WindowTitle].value = fLv2Options.windowTitle;
  4227. // ---------------------------------------------------------------
  4228. // initialize ui features (part 1)
  4229. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  4230. uiDataFt->data_access = fDescriptor->extension_data;
  4231. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  4232. uiPortMapFt->handle = this;
  4233. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  4234. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  4235. uiResizeFt->handle = this;
  4236. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  4237. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  4238. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  4239. uiExternalHostFt->plugin_human_id = fLv2Options.windowTitle;
  4240. // ---------------------------------------------------------------
  4241. // initialize ui features (part 2)
  4242. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  4243. fFeatures[j] = new LV2_Feature;
  4244. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  4245. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  4246. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  4247. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  4248. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  4249. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  4250. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  4251. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  4252. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  4253. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  4254. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  4255. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  4256. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  4257. fFeatures[kFeatureIdUiParent]->data = nullptr;
  4258. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  4259. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  4260. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  4261. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  4262. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  4263. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  4264. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  4265. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  4266. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  4267. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  4268. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  4269. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  4270. // ---------------------------------------------------------------
  4271. // initialize ui extensions
  4272. if (fUI.descriptor->extension_data == nullptr)
  4273. return;
  4274. fExt.uiidle = (const LV2UI_Idle_Interface*)fUI.descriptor->extension_data(LV2_UI__idleInterface);
  4275. fExt.uishow = (const LV2UI_Show_Interface*)fUI.descriptor->extension_data(LV2_UI__showInterface);
  4276. fExt.uiresize = (const LV2UI_Resize*)fUI.descriptor->extension_data(LV2_UI__resize);
  4277. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUI.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  4278. // check if invalid
  4279. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  4280. fExt.uiidle = nullptr;
  4281. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  4282. fExt.uishow = nullptr;
  4283. if (fExt.uiresize != nullptr && fExt.uiresize->ui_resize == nullptr)
  4284. fExt.uiresize = nullptr;
  4285. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  4286. fExt.uiprograms = nullptr;
  4287. }
  4288. // -------------------------------------------------------------------
  4289. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  4290. {
  4291. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  4292. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  4293. fAtomBufferIn.put(atom, portIndex);
  4294. }
  4295. void handleUridMap(const LV2_URID urid, const char* const uri)
  4296. {
  4297. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  4298. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  4299. carla_stdout("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count()-1, uri);
  4300. if (urid < fCustomURIDs.count())
  4301. {
  4302. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  4303. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr,);
  4304. if (std::strcmp(ourURI, uri) != 0)
  4305. {
  4306. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  4307. }
  4308. }
  4309. else
  4310. {
  4311. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  4312. fCustomURIDs.append(carla_strdup(uri));
  4313. }
  4314. }
  4315. // -------------------------------------------------------------------
  4316. private:
  4317. LV2_Handle fHandle;
  4318. LV2_Handle fHandle2;
  4319. LV2_Feature* fFeatures[kFeatureCountAll+1];
  4320. const LV2_Descriptor* fDescriptor;
  4321. const LV2_RDF_Descriptor* fRdfDescriptor;
  4322. float** fAudioInBuffers;
  4323. float** fAudioOutBuffers;
  4324. float** fCvInBuffers;
  4325. float** fCvOutBuffers;
  4326. float* fParamBuffers;
  4327. bool fCanInit2; // some plugins don't like 2 instances
  4328. bool fLatencyChanged;
  4329. int32_t fLatencyIndex; // -1 if invalid
  4330. Lv2AtomRingBuffer fAtomBufferIn;
  4331. Lv2AtomRingBuffer fAtomBufferOut;
  4332. LV2_Atom_Forge fAtomForge;
  4333. Lv2PluginEventData fEventsIn;
  4334. Lv2PluginEventData fEventsOut;
  4335. Lv2PluginOptions fLv2Options;
  4336. LinkedList<const char*> fCustomURIDs;
  4337. bool fFirstActive; // first process() call after activate()
  4338. void* fLastStateChunk;
  4339. EngineTimeInfo fLastTimeInfo;
  4340. struct Extensions {
  4341. const LV2_Options_Interface* options;
  4342. const LV2_State_Interface* state;
  4343. const LV2_Worker_Interface* worker;
  4344. const LV2_Programs_Interface* programs;
  4345. const LV2UI_Idle_Interface* uiidle;
  4346. const LV2UI_Show_Interface* uishow;
  4347. const LV2UI_Resize* uiresize;
  4348. const LV2_Programs_UI_Interface* uiprograms;
  4349. Extensions()
  4350. : options(nullptr),
  4351. state(nullptr),
  4352. worker(nullptr),
  4353. programs(nullptr),
  4354. uiidle(nullptr),
  4355. uishow(nullptr),
  4356. uiresize(nullptr),
  4357. uiprograms(nullptr) {}
  4358. CARLA_DECLARE_NON_COPY_STRUCT(Extensions);
  4359. } fExt;
  4360. struct UI {
  4361. enum Type {
  4362. TYPE_NULL,
  4363. TYPE_EMBED,
  4364. TYPE_EXTERNAL,
  4365. TYPE_OSC
  4366. };
  4367. Type type;
  4368. LV2UI_Handle handle;
  4369. LV2UI_Widget widget;
  4370. const LV2UI_Descriptor* descriptor;
  4371. const LV2_RDF_UI* rdfDescriptor;
  4372. CarlaPluginUI* window;
  4373. UI()
  4374. : type(TYPE_NULL),
  4375. handle(nullptr),
  4376. widget(nullptr),
  4377. descriptor(nullptr),
  4378. rdfDescriptor(nullptr),
  4379. window(nullptr) {}
  4380. ~UI()
  4381. {
  4382. CARLA_ASSERT(handle == nullptr);
  4383. CARLA_ASSERT(widget == nullptr);
  4384. CARLA_ASSERT(descriptor == nullptr);
  4385. CARLA_ASSERT(rdfDescriptor == nullptr);
  4386. CARLA_ASSERT(window == nullptr);
  4387. }
  4388. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  4389. } fUI;
  4390. // -------------------------------------------------------------------
  4391. // Event Feature
  4392. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4393. {
  4394. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4395. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4396. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4397. return 0;
  4398. }
  4399. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4400. {
  4401. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4402. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4403. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4404. return 0;
  4405. }
  4406. // -------------------------------------------------------------------
  4407. // Logs Feature
  4408. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4409. {
  4410. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4411. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4412. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4413. #ifndef DEBUG
  4414. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4415. return 0;
  4416. #endif
  4417. va_list args;
  4418. va_start(args, fmt);
  4419. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4420. va_end(args);
  4421. return ret;
  4422. }
  4423. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4424. {
  4425. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4426. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4427. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4428. #ifndef DEBUG
  4429. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4430. return 0;
  4431. #endif
  4432. int ret = 0;
  4433. switch (type)
  4434. {
  4435. case CARLA_URI_MAP_ID_LOG_ERROR:
  4436. std::fprintf(stderr, "\x1b[31m");
  4437. ret = std::vfprintf(stderr, fmt, ap);
  4438. std::fprintf(stderr, "\x1b[0m");
  4439. break;
  4440. case CARLA_URI_MAP_ID_LOG_NOTE:
  4441. ret = std::vfprintf(stdout, fmt, ap);
  4442. break;
  4443. case CARLA_URI_MAP_ID_LOG_TRACE:
  4444. #ifdef DEBUG
  4445. std::fprintf(stdout, "\x1b[30;1m");
  4446. ret = std::vfprintf(stdout, fmt, ap);
  4447. std::fprintf(stdout, "\x1b[0m");
  4448. #endif
  4449. break;
  4450. case CARLA_URI_MAP_ID_LOG_WARNING:
  4451. ret = std::vfprintf(stderr, fmt, ap);
  4452. break;
  4453. default:
  4454. break;
  4455. }
  4456. return ret;
  4457. }
  4458. // -------------------------------------------------------------------
  4459. // Programs Feature
  4460. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4461. {
  4462. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4463. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4464. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4465. }
  4466. // -------------------------------------------------------------------
  4467. // Resize Port Feature
  4468. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4469. {
  4470. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4471. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4472. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  4473. }
  4474. // -------------------------------------------------------------------
  4475. // State Feature
  4476. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4477. {
  4478. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4479. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4480. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4481. File file;
  4482. if (File::isAbsolutePath(path))
  4483. file = File(path);
  4484. else
  4485. file = File::getCurrentWorkingDirectory().getChildFile(path);
  4486. file.getParentDirectory().createDirectory();
  4487. return strdup(file.getFullPathName().toRawUTF8());
  4488. }
  4489. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4490. {
  4491. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4492. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4493. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4494. // may already be an abstract path
  4495. if (! File::isAbsolutePath(absolute_path))
  4496. return strdup(absolute_path);
  4497. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  4498. }
  4499. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4500. {
  4501. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4502. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4503. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4504. // may already be an absolute path
  4505. if (File::isAbsolutePath(abstract_path))
  4506. return strdup(abstract_path);
  4507. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  4508. }
  4509. static LV2_State_Status carla_lv2_state_store(LV2_State_Handle handle, uint32_t key, const void* value, size_t size, uint32_t type, uint32_t flags)
  4510. {
  4511. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4512. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4513. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4514. }
  4515. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4516. {
  4517. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4518. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4519. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4520. }
  4521. // -------------------------------------------------------------------
  4522. // URI-Map Feature
  4523. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4524. {
  4525. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4526. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4527. // unused
  4528. (void)map;
  4529. }
  4530. // -------------------------------------------------------------------
  4531. // URID Feature
  4532. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4533. {
  4534. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4535. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4536. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4537. // Atom types
  4538. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4539. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4540. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4541. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4542. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4543. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4544. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4545. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4546. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  4547. return CARLA_URI_MAP_ID_ATOM_EVENT;
  4548. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4549. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4550. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4551. return CARLA_URI_MAP_ID_ATOM_INT;
  4552. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4553. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4554. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4555. return CARLA_URI_MAP_ID_ATOM_LONG;
  4556. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  4557. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  4558. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4559. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4560. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4561. return CARLA_URI_MAP_ID_ATOM_PATH;
  4562. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4563. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4564. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4565. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4566. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4567. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4568. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  4569. return CARLA_URI_MAP_ID_ATOM_SOUND;
  4570. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4571. return CARLA_URI_MAP_ID_ATOM_STRING;
  4572. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4573. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4574. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4575. return CARLA_URI_MAP_ID_ATOM_URI;
  4576. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4577. return CARLA_URI_MAP_ID_ATOM_URID;
  4578. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4579. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4580. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4581. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4582. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4583. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4584. // BufSize types
  4585. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4586. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4587. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4588. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4589. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4590. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4591. // Log types
  4592. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4593. return CARLA_URI_MAP_ID_LOG_ERROR;
  4594. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4595. return CARLA_URI_MAP_ID_LOG_NOTE;
  4596. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4597. return CARLA_URI_MAP_ID_LOG_TRACE;
  4598. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4599. return CARLA_URI_MAP_ID_LOG_WARNING;
  4600. // Time types
  4601. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4602. return CARLA_URI_MAP_ID_TIME_POSITION;
  4603. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4604. return CARLA_URI_MAP_ID_TIME_BAR;
  4605. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4606. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4607. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4608. return CARLA_URI_MAP_ID_TIME_BEAT;
  4609. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4610. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4611. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4612. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4613. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4614. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4615. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4616. return CARLA_URI_MAP_ID_TIME_FRAME;
  4617. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4618. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4619. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4620. return CARLA_URI_MAP_ID_TIME_SPEED;
  4621. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  4622. return CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT;
  4623. // Others
  4624. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4625. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4626. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4627. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4628. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  4629. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  4630. // Custom
  4631. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  4632. return CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  4633. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  4634. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  4635. // Custom types
  4636. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4637. }
  4638. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4639. {
  4640. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4641. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4642. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4643. // Atom types
  4644. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4645. return LV2_ATOM__Blank;
  4646. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4647. return LV2_ATOM__Bool;
  4648. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4649. return LV2_ATOM__Chunk;
  4650. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4651. return LV2_ATOM__Double;
  4652. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  4653. return LV2_ATOM__Event;
  4654. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4655. return LV2_ATOM__Float;
  4656. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4657. return LV2_ATOM__Int;
  4658. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4659. return LV2_ATOM__Literal;
  4660. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4661. return LV2_ATOM__Long;
  4662. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  4663. return LV2_ATOM__Number;
  4664. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4665. return LV2_ATOM__Object;
  4666. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4667. return LV2_ATOM__Path;
  4668. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4669. return LV2_ATOM__Property;
  4670. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4671. return LV2_ATOM__Resource;
  4672. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4673. return LV2_ATOM__Sequence;
  4674. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  4675. return LV2_ATOM__Sound;
  4676. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4677. return LV2_ATOM__String;
  4678. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4679. return LV2_ATOM__Tuple;
  4680. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4681. return LV2_ATOM__URI;
  4682. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4683. return LV2_ATOM__URID;
  4684. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4685. return LV2_ATOM__Vector;
  4686. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4687. return LV2_ATOM__atomTransfer;
  4688. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4689. return LV2_ATOM__eventTransfer;
  4690. // BufSize types
  4691. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4692. return LV2_BUF_SIZE__maxBlockLength;
  4693. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4694. return LV2_BUF_SIZE__minBlockLength;
  4695. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4696. return LV2_BUF_SIZE__sequenceSize;
  4697. // Log types
  4698. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4699. return LV2_LOG__Error;
  4700. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4701. return LV2_LOG__Note;
  4702. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4703. return LV2_LOG__Trace;
  4704. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4705. return LV2_LOG__Warning;
  4706. // Time types
  4707. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4708. return LV2_TIME__Position;
  4709. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4710. return LV2_TIME__bar;
  4711. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4712. return LV2_TIME__barBeat;
  4713. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4714. return LV2_TIME__beat;
  4715. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4716. return LV2_TIME__beatUnit;
  4717. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4718. return LV2_TIME__beatsPerBar;
  4719. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4720. return LV2_TIME__beatsPerMinute;
  4721. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4722. return LV2_TIME__frame;
  4723. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4724. return LV2_TIME__framesPerSecond;
  4725. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4726. return LV2_TIME__speed;
  4727. if (urid == CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT)
  4728. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  4729. // Others
  4730. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4731. return LV2_MIDI__MidiEvent;
  4732. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4733. return LV2_PARAMETERS__sampleRate;
  4734. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  4735. return LV2_UI__windowTitle;
  4736. // Custom
  4737. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  4738. return URI_CARLA_ATOM_WORKER;
  4739. if (urid == CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID)
  4740. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  4741. // Custom types
  4742. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4743. }
  4744. // -------------------------------------------------------------------
  4745. // Worker Feature
  4746. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4747. {
  4748. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4749. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4750. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4751. }
  4752. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4753. {
  4754. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4755. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4756. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4757. }
  4758. // -------------------------------------------------------------------
  4759. // External UI Feature
  4760. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4761. {
  4762. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4763. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4764. ((Lv2Plugin*)controller)->handleExternalUIClosed();
  4765. }
  4766. // -------------------------------------------------------------------
  4767. // UI Port-Map Feature
  4768. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4769. {
  4770. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4771. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4772. return ((Lv2Plugin*)handle)->handleUIPortMap(symbol);
  4773. }
  4774. // -------------------------------------------------------------------
  4775. // UI Resize Feature
  4776. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4777. {
  4778. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4779. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4780. return ((Lv2Plugin*)handle)->handleUIResize(width, height);
  4781. }
  4782. // -------------------------------------------------------------------
  4783. // UI Extension
  4784. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4785. {
  4786. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4787. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4788. ((Lv2Plugin*)controller)->handleUIWrite(port_index, buffer_size, format, buffer);
  4789. }
  4790. // -------------------------------------------------------------------
  4791. // Lilv State
  4792. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4793. {
  4794. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4795. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4796. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4797. }
  4798. // -------------------------------------------------------------------
  4799. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4800. };
  4801. // -------------------------------------------------------------------------------------------------------------------
  4802. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4803. #ifndef BUILD_BRIDGE
  4804. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4805. {
  4806. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4807. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4808. const int32_t portIndex = argv[0]->i;
  4809. const char* const atomBuf = (const char*)&argv[1]->s;
  4810. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4811. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(atomBuf));
  4812. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4813. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  4814. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4815. return 0;
  4816. }
  4817. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4818. {
  4819. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4820. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4821. const int32_t urid = argv[0]->i;
  4822. const char* const uri = (const char*)&argv[1]->s;
  4823. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4824. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4825. return 0;
  4826. }
  4827. #endif
  4828. #undef lv2PluginPtr
  4829. // -------------------------------------------------------------------------------------------------------------------
  4830. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4831. {
  4832. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  4833. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4834. if (! plugin->init(init.filename, init.name, init.label))
  4835. {
  4836. delete plugin;
  4837. return nullptr;
  4838. }
  4839. plugin->reload();
  4840. bool canRun = true;
  4841. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  4842. {
  4843. if (! plugin->canRunInRack())
  4844. {
  4845. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4846. canRun = false;
  4847. }
  4848. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  4849. {
  4850. init.engine->setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  4851. canRun = false;
  4852. }
  4853. }
  4854. else if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_PATCHBAY && (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0))
  4855. {
  4856. init.engine->setLastError("CV ports in patchbay mode is still TODO");
  4857. canRun = false;
  4858. }
  4859. if (! canRun)
  4860. {
  4861. delete plugin;
  4862. return nullptr;
  4863. }
  4864. return plugin;
  4865. }
  4866. // -------------------------------------------------------------------------------------------------------------------
  4867. CARLA_BACKEND_END_NAMESPACE