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.

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