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.

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