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.

6010 lines
227KB

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