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.

5851 lines
219KB

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