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.

6003 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. // -----------------------------------------------------
  35. // Maximum default buffer size
  36. const uint MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  37. // Extra Plugin Hints
  38. const uint PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  39. const uint PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  40. const uint PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  41. const uint PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  42. // Extra Parameter Hints
  43. const uint PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  44. const uint PARAMETER_IS_TRIGGER = 0x2000;
  45. // LV2 Event Data/Types
  46. const uint CARLA_EVENT_DATA_ATOM = 0x01;
  47. const uint CARLA_EVENT_DATA_EVENT = 0x02;
  48. const uint CARLA_EVENT_DATA_MIDI_LL = 0x04;
  49. const uint CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  50. const uint CARLA_EVENT_TYPE_MIDI = 0x20;
  51. const uint CARLA_EVENT_TYPE_TIME = 0x40;
  52. // LV2 URI Map Ids
  53. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  56. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  57. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  58. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 22;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 23;
  77. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 24;
  78. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 25;
  79. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 26;
  80. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 27;
  81. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 28;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 29;
  83. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 30;
  84. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 31; // base type
  85. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 32; // values
  86. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 33;
  87. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 34;
  88. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 35;
  89. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 36;
  90. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 37;
  91. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 38;
  92. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 39;
  93. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 40;
  94. const uint32_t CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT = 41;
  95. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 42;
  96. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 43;
  97. const uint32_t CARLA_URI_MAP_ID_UI_WINDOW_TITLE = 44;
  98. const uint32_t CARLA_URI_MAP_ID_CARLA_ATOM_WORKER = 45;
  99. const uint32_t CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID = 46;
  100. const uint32_t CARLA_URI_MAP_ID_COUNT = 47;
  101. // LV2 Feature Ids
  102. const uint32_t kFeatureIdBufSizeBounded = 0;
  103. const uint32_t kFeatureIdBufSizeFixed = 1;
  104. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  105. const uint32_t kFeatureIdEvent = 3;
  106. const uint32_t kFeatureIdHardRtCapable = 4;
  107. const uint32_t kFeatureIdInPlaceBroken = 5;
  108. const uint32_t kFeatureIdIsLive = 6;
  109. const uint32_t kFeatureIdLogs = 7;
  110. const uint32_t kFeatureIdOptions = 8;
  111. const uint32_t kFeatureIdPrograms = 9;
  112. const uint32_t kFeatureIdResizePort = 10;
  113. const uint32_t kFeatureIdRtMemPool = 11;
  114. const uint32_t kFeatureIdRtMemPoolOld = 12;
  115. const uint32_t kFeatureIdStateMakePath = 13;
  116. const uint32_t kFeatureIdStateMapPath = 14;
  117. const uint32_t kFeatureIdStrictBounds = 15;
  118. const uint32_t kFeatureIdUriMap = 16;
  119. const uint32_t kFeatureIdUridMap = 17;
  120. const uint32_t kFeatureIdUridUnmap = 18;
  121. const uint32_t kFeatureIdWorker = 19;
  122. const uint32_t kFeatureCountPlugin = 20;
  123. const uint32_t kFeatureIdUiDataAccess = 20;
  124. const uint32_t kFeatureIdUiInstanceAccess = 21;
  125. const uint32_t kFeatureIdUiIdleInterface = 22;
  126. const uint32_t kFeatureIdUiFixedSize = 23;
  127. const uint32_t kFeatureIdUiMakeResident = 24;
  128. const uint32_t kFeatureIdUiNoUserResize = 25;
  129. const uint32_t kFeatureIdUiParent = 26;
  130. const uint32_t kFeatureIdUiPortMap = 27;
  131. const uint32_t kFeatureIdUiPortSubscribe = 28;
  132. const uint32_t kFeatureIdUiResize = 29;
  133. const uint32_t kFeatureIdUiTouch = 30;
  134. const uint32_t kFeatureIdExternalUi = 31;
  135. const uint32_t kFeatureIdExternalUiOld = 32;
  136. const uint32_t kFeatureCountAll = 33;
  137. // -----------------------------------------------------
  138. struct Lv2EventData {
  139. uint32_t type;
  140. uint32_t rindex;
  141. CarlaEngineEventPort* port;
  142. union {
  143. LV2_Atom_Buffer* atom;
  144. LV2_Event_Buffer* event;
  145. LV2_MIDI midi;
  146. };
  147. Lv2EventData() noexcept
  148. : type(0x0),
  149. rindex(0),
  150. port(nullptr) {}
  151. ~Lv2EventData() noexcept
  152. {
  153. if (port != nullptr)
  154. {
  155. delete port;
  156. port = nullptr;
  157. }
  158. const uint32_t rtype(type);
  159. type = 0x0;
  160. if (rtype & CARLA_EVENT_DATA_ATOM)
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  163. std::free(atom);
  164. atom = nullptr;
  165. }
  166. else if (rtype & CARLA_EVENT_DATA_EVENT)
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  169. std::free(event);
  170. event = nullptr;
  171. }
  172. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  173. {
  174. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  175. delete[] midi.data;
  176. midi.data = nullptr;
  177. }
  178. }
  179. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  180. };
  181. struct Lv2PluginEventData {
  182. uint32_t count;
  183. Lv2EventData* data;
  184. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  185. uint32_t ctrlIndex;
  186. Lv2PluginEventData() noexcept
  187. : count(0),
  188. data(nullptr),
  189. ctrl(nullptr),
  190. ctrlIndex(0) {}
  191. ~Lv2PluginEventData() noexcept
  192. {
  193. CARLA_SAFE_ASSERT_INT(count == 0, count);
  194. CARLA_SAFE_ASSERT(data == nullptr);
  195. CARLA_SAFE_ASSERT(ctrl == nullptr);
  196. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  197. }
  198. void createNew(const uint32_t newCount)
  199. {
  200. CARLA_SAFE_ASSERT_INT(count == 0, count);
  201. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  202. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  203. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  204. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  205. data = new Lv2EventData[newCount];
  206. count = newCount;
  207. ctrl = nullptr;
  208. ctrlIndex = 0;
  209. }
  210. void clear() noexcept
  211. {
  212. if (data != nullptr)
  213. {
  214. for (uint32_t i=0; i < count; ++i)
  215. {
  216. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  217. data[i].port = nullptr;
  218. }
  219. delete[] data;
  220. data = nullptr;
  221. }
  222. count = 0;
  223. ctrl = nullptr;
  224. ctrlIndex = 0;
  225. }
  226. void initBuffers() const noexcept
  227. {
  228. for (uint32_t i=0; i < count; ++i)
  229. {
  230. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  231. data[i].port->initBuffer();
  232. }
  233. }
  234. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginEventData)
  235. };
  236. // -----------------------------------------------------
  237. struct Lv2PluginOptions {
  238. enum OptIndex {
  239. MaxBlockLenth = 0,
  240. MinBlockLenth,
  241. SequenceSize,
  242. SampleRate,
  243. FrontendWinId,
  244. WindowTitle,
  245. Null,
  246. Count
  247. };
  248. int maxBufferSize;
  249. int minBufferSize;
  250. int sequenceSize;
  251. double sampleRate;
  252. int64_t frontendWinId;
  253. const char* windowTitle;
  254. LV2_Options_Option opts[Count];
  255. Lv2PluginOptions() noexcept
  256. : maxBufferSize(0),
  257. minBufferSize(0),
  258. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  259. sampleRate(0.0),
  260. frontendWinId(0),
  261. windowTitle(nullptr)
  262. {
  263. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  264. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  265. optMaxBlockLenth.subject = 0;
  266. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  267. optMaxBlockLenth.size = sizeof(int);
  268. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  269. optMaxBlockLenth.value = &maxBufferSize;
  270. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  271. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  272. optMinBlockLenth.subject = 0;
  273. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  274. optMinBlockLenth.size = sizeof(int);
  275. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  276. optMinBlockLenth.value = &minBufferSize;
  277. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  278. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  279. optSequenceSize.subject = 0;
  280. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  281. optSequenceSize.size = sizeof(int);
  282. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  283. optSequenceSize.value = &sequenceSize;
  284. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  285. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  286. optSampleRate.subject = 0;
  287. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  288. optSampleRate.size = sizeof(double);
  289. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  290. optSampleRate.value = &sampleRate;
  291. LV2_Options_Option& optFrontendWinId(opts[FrontendWinId]);
  292. optFrontendWinId.context = LV2_OPTIONS_INSTANCE;
  293. optFrontendWinId.subject = 0;
  294. optFrontendWinId.key = CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  295. optFrontendWinId.size = sizeof(int64_t);
  296. optFrontendWinId.type = CARLA_URI_MAP_ID_ATOM_LONG;
  297. optFrontendWinId.value = &frontendWinId;
  298. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  299. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  300. optWindowTitle.subject = 0;
  301. optWindowTitle.key = CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  302. optWindowTitle.size = 0;
  303. optWindowTitle.type = CARLA_URI_MAP_ID_ATOM_STRING;
  304. optWindowTitle.value = nullptr;
  305. LV2_Options_Option& optNull(opts[Null]);
  306. optNull.context = LV2_OPTIONS_INSTANCE;
  307. optNull.subject = 0;
  308. optNull.key = CARLA_URI_MAP_ID_NULL;
  309. optNull.size = 0;
  310. optNull.type = CARLA_URI_MAP_ID_NULL;
  311. optNull.value = nullptr;
  312. }
  313. ~Lv2PluginOptions() noexcept
  314. {
  315. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  316. optWindowTitle.size = 0;
  317. optWindowTitle.value = nullptr;
  318. if (windowTitle != nullptr)
  319. {
  320. delete[] windowTitle;
  321. windowTitle = nullptr;
  322. }
  323. }
  324. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginOptions);
  325. };
  326. // -----------------------------------------------------
  327. class Lv2Plugin : public CarlaPlugin,
  328. CarlaPluginUI::CloseCallback
  329. {
  330. public:
  331. Lv2Plugin(CarlaEngine* const engine, const uint id)
  332. : CarlaPlugin(engine, id),
  333. fHandle(nullptr),
  334. fHandle2(nullptr),
  335. fDescriptor(nullptr),
  336. fRdfDescriptor(nullptr),
  337. fAudioInBuffers(nullptr),
  338. fAudioOutBuffers(nullptr),
  339. fCvInBuffers(nullptr),
  340. fCvOutBuffers(nullptr),
  341. fParamBuffers(nullptr),
  342. fCanInit2(true),
  343. fLatencyChanged(false),
  344. fLatencyIndex(-1),
  345. fAtomBufferIn(),
  346. fAtomBufferOut(),
  347. fAtomForge(),
  348. fEventsIn(),
  349. fEventsOut(),
  350. fLv2Options(),
  351. fCustomURIDs(),
  352. fFirstActive(true),
  353. fLastStateChunk(nullptr),
  354. fLastTimeInfo(),
  355. fExt(),
  356. fUI(),
  357. leakDetector_Lv2Plugin()
  358. {
  359. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  360. carla_fill<LV2_Feature*>(fFeatures, nullptr, kFeatureCountAll+1);
  361. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  362. fCustomURIDs.append(nullptr);
  363. #if defined(__clang__)
  364. # pragma clang diagnostic push
  365. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  366. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  367. # pragma GCC diagnostic push
  368. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  369. #endif
  370. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  371. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  372. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  373. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  374. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  375. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  376. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  377. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  378. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  379. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  380. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  381. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  382. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  383. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  384. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  385. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  386. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  387. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  388. #if defined(__clang__)
  389. # pragma clang diagnostic pop
  390. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  391. # pragma GCC diagnostic pop
  392. #endif
  393. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  394. }
  395. ~Lv2Plugin() override
  396. {
  397. carla_debug("Lv2Plugin::~Lv2Plugin()");
  398. // close UI
  399. if (fUI.type != UI::TYPE_NULL)
  400. {
  401. showCustomUI(false);
  402. if (fUI.type == UI::TYPE_OSC)
  403. {
  404. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  405. }
  406. else
  407. {
  408. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  409. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  410. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  411. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  412. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  413. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  414. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  415. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  416. fUI.descriptor = nullptr;
  417. pData->uiLibClose();
  418. }
  419. #ifndef LV2_UIS_ONLY_BRIDGES
  420. if (fUI.window != nullptr)
  421. {
  422. delete fUI.window;
  423. fUI.window = nullptr;
  424. }
  425. #endif
  426. fUI.rdfDescriptor = nullptr;
  427. }
  428. pData->singleMutex.lock();
  429. pData->masterMutex.lock();
  430. if (pData->client != nullptr && pData->client->isActive())
  431. pData->client->deactivate();
  432. if (pData->active)
  433. {
  434. deactivate();
  435. pData->active = false;
  436. }
  437. if (fDescriptor != nullptr)
  438. {
  439. if (fDescriptor->cleanup != nullptr)
  440. {
  441. if (fHandle != nullptr)
  442. fDescriptor->cleanup(fHandle);
  443. if (fHandle2 != nullptr)
  444. fDescriptor->cleanup(fHandle2);
  445. }
  446. fHandle = nullptr;
  447. fHandle2 = nullptr;
  448. fDescriptor = nullptr;
  449. }
  450. if (fRdfDescriptor != nullptr)
  451. {
  452. delete fRdfDescriptor;
  453. fRdfDescriptor = nullptr;
  454. }
  455. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  456. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  457. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  458. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  459. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  460. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  461. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  462. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  463. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  464. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  465. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  466. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  467. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  468. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  469. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  470. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  471. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  472. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  473. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  474. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  475. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  476. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  477. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  478. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  479. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  480. {
  481. if (fFeatures[i] != nullptr)
  482. {
  483. delete fFeatures[i];
  484. fFeatures[i] = nullptr;
  485. }
  486. }
  487. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  488. {
  489. const char* const uri(it.getValue());
  490. if (uri != nullptr)
  491. delete[] uri;
  492. }
  493. fCustomURIDs.clear();
  494. if (fLastStateChunk != nullptr)
  495. {
  496. std::free(fLastStateChunk);
  497. fLastStateChunk = nullptr;
  498. }
  499. clearBuffers();
  500. }
  501. // -------------------------------------------------------------------
  502. // Information (base)
  503. PluginType getType() const noexcept override
  504. {
  505. return PLUGIN_LV2;
  506. }
  507. PluginCategory getCategory() const noexcept override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  510. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  511. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  512. if (LV2_IS_DELAY(cat1, cat2))
  513. return PLUGIN_CATEGORY_DELAY;
  514. if (LV2_IS_DISTORTION(cat1, cat2))
  515. return PLUGIN_CATEGORY_OTHER;
  516. if (LV2_IS_DYNAMICS(cat1, cat2))
  517. return PLUGIN_CATEGORY_DYNAMICS;
  518. if (LV2_IS_EQ(cat1, cat2))
  519. return PLUGIN_CATEGORY_EQ;
  520. if (LV2_IS_FILTER(cat1, cat2))
  521. return PLUGIN_CATEGORY_FILTER;
  522. if (LV2_IS_GENERATOR(cat1, cat2))
  523. return PLUGIN_CATEGORY_SYNTH;
  524. if (LV2_IS_MODULATOR(cat1, cat2))
  525. return PLUGIN_CATEGORY_MODULATOR;
  526. if (LV2_IS_REVERB(cat1, cat2))
  527. return PLUGIN_CATEGORY_DELAY;
  528. if (LV2_IS_SIMULATOR(cat1, cat2))
  529. return PLUGIN_CATEGORY_OTHER;
  530. if (LV2_IS_SPATIAL(cat1, cat2))
  531. return PLUGIN_CATEGORY_OTHER;
  532. if (LV2_IS_SPECTRAL(cat1, cat2))
  533. return PLUGIN_CATEGORY_UTILITY;
  534. if (LV2_IS_UTILITY(cat1, cat2))
  535. return PLUGIN_CATEGORY_UTILITY;
  536. return CarlaPlugin::getCategory();
  537. }
  538. int64_t getUniqueId() const noexcept override
  539. {
  540. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  541. return static_cast<int64_t>(fRdfDescriptor->UniqueID);
  542. }
  543. // -------------------------------------------------------------------
  544. // Information (count)
  545. uint32_t getMidiInCount() const noexcept override
  546. {
  547. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  548. uint32_t count = 0;
  549. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  550. {
  551. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  552. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  553. ++count;
  554. }
  555. return count;
  556. }
  557. uint32_t getMidiOutCount() const noexcept override
  558. {
  559. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  560. uint32_t count = 0;
  561. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  562. {
  563. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  564. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  565. ++count;
  566. }
  567. return count;
  568. }
  569. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  570. {
  571. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  572. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  573. const int32_t rindex(pData->param.data[parameterId].rindex);
  574. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  575. {
  576. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  577. return port->ScalePointCount;
  578. }
  579. return 0;
  580. }
  581. // -------------------------------------------------------------------
  582. // Information (current data)
  583. // nothing
  584. // -------------------------------------------------------------------
  585. // Information (per-plugin data)
  586. uint getOptionsAvailable() const noexcept override
  587. {
  588. const bool hasMidiIn(getMidiInCount() > 0);
  589. uint options = 0x0;
  590. if (fExt.programs != nullptr)
  591. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  592. if (fLatencyIndex == -1 && ! (hasMidiIn || needsFixedBuffer()))
  593. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  594. if (fCanInit2 && pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  595. {
  596. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  597. options |= PLUGIN_OPTION_FORCE_STEREO;
  598. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  599. options |= PLUGIN_OPTION_FORCE_STEREO;
  600. }
  601. if (hasMidiIn)
  602. {
  603. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  604. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  605. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  606. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  607. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  608. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  609. }
  610. return options;
  611. }
  612. float getParameterValue(const uint32_t parameterId) const noexcept override
  613. {
  614. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  615. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  616. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  617. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  618. return fParamBuffers[parameterId];
  619. }
  620. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  621. {
  622. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  623. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  624. const int32_t rindex(pData->param.data[parameterId].rindex);
  625. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  626. {
  627. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  628. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, 0.0f);
  629. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  630. return portScalePoint->Value;
  631. }
  632. return 0.0f;
  633. }
  634. void getLabel(char* const strBuf) const noexcept override
  635. {
  636. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  637. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  638. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  639. }
  640. void getMaker(char* const strBuf) const noexcept override
  641. {
  642. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  643. if (fRdfDescriptor->Author != nullptr)
  644. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  645. else
  646. CarlaPlugin::getMaker(strBuf);
  647. }
  648. void getCopyright(char* const strBuf) const noexcept override
  649. {
  650. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  651. if (fRdfDescriptor->License != nullptr)
  652. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  653. else
  654. CarlaPlugin::getCopyright(strBuf);
  655. }
  656. void getRealName(char* const strBuf) const noexcept override
  657. {
  658. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  659. if (fRdfDescriptor->Name != nullptr)
  660. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  661. else
  662. CarlaPlugin::getRealName(strBuf);
  663. }
  664. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  665. {
  666. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  667. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  668. const int32_t rindex(pData->param.data[parameterId].rindex);
  669. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  670. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  671. else
  672. CarlaPlugin::getParameterName(parameterId, strBuf);
  673. }
  674. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  675. {
  676. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  677. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  678. const int32_t rindex(pData->param.data[parameterId].rindex);
  679. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  680. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  681. else
  682. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  683. }
  684. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  685. {
  686. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  687. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  688. const int32_t rindex(pData->param.data[parameterId].rindex);
  689. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  690. {
  691. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  692. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  693. {
  694. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  695. return;
  696. }
  697. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  698. {
  699. switch (port->Unit.Unit)
  700. {
  701. case LV2_PORT_UNIT_BAR:
  702. std::strncpy(strBuf, "bars", STR_MAX);
  703. return;
  704. case LV2_PORT_UNIT_BEAT:
  705. std::strncpy(strBuf, "beats", STR_MAX);
  706. return;
  707. case LV2_PORT_UNIT_BPM:
  708. std::strncpy(strBuf, "BPM", STR_MAX);
  709. return;
  710. case LV2_PORT_UNIT_CENT:
  711. std::strncpy(strBuf, "ct", STR_MAX);
  712. return;
  713. case LV2_PORT_UNIT_CM:
  714. std::strncpy(strBuf, "cm", STR_MAX);
  715. return;
  716. case LV2_PORT_UNIT_COEF:
  717. std::strncpy(strBuf, "(coef)", STR_MAX);
  718. return;
  719. case LV2_PORT_UNIT_DB:
  720. std::strncpy(strBuf, "dB", STR_MAX);
  721. return;
  722. case LV2_PORT_UNIT_DEGREE:
  723. std::strncpy(strBuf, "deg", STR_MAX);
  724. return;
  725. case LV2_PORT_UNIT_FRAME:
  726. std::strncpy(strBuf, "frames", STR_MAX);
  727. return;
  728. case LV2_PORT_UNIT_HZ:
  729. std::strncpy(strBuf, "Hz", STR_MAX);
  730. return;
  731. case LV2_PORT_UNIT_INCH:
  732. std::strncpy(strBuf, "in", STR_MAX);
  733. return;
  734. case LV2_PORT_UNIT_KHZ:
  735. std::strncpy(strBuf, "kHz", STR_MAX);
  736. return;
  737. case LV2_PORT_UNIT_KM:
  738. std::strncpy(strBuf, "km", STR_MAX);
  739. return;
  740. case LV2_PORT_UNIT_M:
  741. std::strncpy(strBuf, "m", STR_MAX);
  742. return;
  743. case LV2_PORT_UNIT_MHZ:
  744. std::strncpy(strBuf, "MHz", STR_MAX);
  745. return;
  746. case LV2_PORT_UNIT_MIDINOTE:
  747. std::strncpy(strBuf, "note", STR_MAX);
  748. return;
  749. case LV2_PORT_UNIT_MILE:
  750. std::strncpy(strBuf, "mi", STR_MAX);
  751. return;
  752. case LV2_PORT_UNIT_MIN:
  753. std::strncpy(strBuf, "min", STR_MAX);
  754. return;
  755. case LV2_PORT_UNIT_MM:
  756. std::strncpy(strBuf, "mm", STR_MAX);
  757. return;
  758. case LV2_PORT_UNIT_MS:
  759. std::strncpy(strBuf, "ms", STR_MAX);
  760. return;
  761. case LV2_PORT_UNIT_OCT:
  762. std::strncpy(strBuf, "oct", STR_MAX);
  763. return;
  764. case LV2_PORT_UNIT_PC:
  765. std::strncpy(strBuf, "%", STR_MAX);
  766. return;
  767. case LV2_PORT_UNIT_S:
  768. std::strncpy(strBuf, "s", STR_MAX);
  769. return;
  770. case LV2_PORT_UNIT_SEMITONE:
  771. std::strncpy(strBuf, "semi", STR_MAX);
  772. return;
  773. }
  774. }
  775. }
  776. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  777. }
  778. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  779. {
  780. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  781. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  782. const int32_t rindex(pData->param.data[parameterId].rindex);
  783. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  784. {
  785. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  786. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount,);
  787. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  788. if (portScalePoint->Label != nullptr)
  789. {
  790. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  791. return;
  792. }
  793. }
  794. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  795. }
  796. // -------------------------------------------------------------------
  797. // Set data (state)
  798. void prepareForSave() override
  799. {
  800. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  801. if (fExt.state != nullptr && fExt.state->save != nullptr)
  802. {
  803. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  804. if (fHandle2 != nullptr)
  805. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  806. }
  807. }
  808. // -------------------------------------------------------------------
  809. // Set data (internal stuff)
  810. void setName(const char* const newName) override
  811. {
  812. CarlaPlugin::setName(newName);
  813. if (fLv2Options.windowTitle == nullptr)
  814. return;
  815. CarlaString guiTitle(pData->name);
  816. guiTitle += " (GUI)";
  817. delete[] fLv2Options.windowTitle;
  818. fLv2Options.windowTitle = guiTitle.dup();
  819. fLv2Options.opts[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  820. fLv2Options.opts[Lv2PluginOptions::WindowTitle].value = fLv2Options.windowTitle;
  821. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  822. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fLv2Options.windowTitle;
  823. #ifndef LV2_UIS_ONLY_BRIDGES
  824. if (fUI.window != nullptr)
  825. fUI.window->setTitle(fLv2Options.windowTitle);
  826. #endif
  827. }
  828. // -------------------------------------------------------------------
  829. // Set data (plugin-specific stuff)
  830. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  831. {
  832. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  833. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  834. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  835. fParamBuffers[parameterId] = fixedValue;
  836. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  837. }
  838. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  839. {
  840. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  841. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  842. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  843. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  844. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  845. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  846. // we should only call state restore once
  847. // so inject this in CarlaPlugin::loadSaveState
  848. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "CarlaLoadLv2StateNow") == 0 && std::strcmp(value, "true") == 0)
  849. {
  850. if (fExt.state == nullptr)
  851. return;
  852. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  853. {
  854. const ScopedSingleProcessLocker spl(this, true);
  855. try {
  856. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  857. } catch(...) {}
  858. if (fHandle2 != nullptr)
  859. {
  860. try {
  861. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  862. } catch(...) {}
  863. }
  864. }
  865. switch (status)
  866. {
  867. case LV2_STATE_SUCCESS:
  868. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  869. break;
  870. case LV2_STATE_ERR_UNKNOWN:
  871. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  872. break;
  873. case LV2_STATE_ERR_BAD_TYPE:
  874. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  875. break;
  876. case LV2_STATE_ERR_BAD_FLAGS:
  877. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  878. break;
  879. case LV2_STATE_ERR_NO_FEATURE:
  880. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  881. break;
  882. case LV2_STATE_ERR_NO_PROPERTY:
  883. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  884. break;
  885. }
  886. return;
  887. }
  888. CarlaPlugin::setCustomData(type, key, value, sendGui);
  889. }
  890. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  891. {
  892. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  893. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  894. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  895. {
  896. if (LilvState* const state = Lv2WorldClass::getInstance().getState(fRdfDescriptor->Presets[index].URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  897. {
  898. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  899. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  900. if (fHandle2 != nullptr)
  901. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  902. lilv_state_free(state);
  903. }
  904. }
  905. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  906. }
  907. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  908. {
  909. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  910. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  911. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  912. {
  913. const uint32_t bank(pData->midiprog.data[index].bank);
  914. const uint32_t program(pData->midiprog.data[index].program);
  915. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  916. try {
  917. fExt.programs->select_program(fHandle, bank, program);
  918. } catch(...) {}
  919. if (fHandle2 != nullptr)
  920. {
  921. try {
  922. fExt.programs->select_program(fHandle2, bank, program);
  923. } catch(...) {}
  924. }
  925. }
  926. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  927. }
  928. // -------------------------------------------------------------------
  929. // Set ui stuff
  930. void showCustomUI(const bool yesNo) override
  931. {
  932. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  933. if (! yesNo)
  934. pData->transientTryCounter = 0;
  935. if (fUI.type == UI::TYPE_OSC)
  936. {
  937. if (yesNo)
  938. {
  939. pData->osc.data.clear();
  940. pData->osc.thread.startThread();
  941. }
  942. else
  943. {
  944. if (pData->osc.data.target != nullptr)
  945. {
  946. osc_send_hide(pData->osc.data);
  947. osc_send_quit(pData->osc.data);
  948. pData->osc.data.clear();
  949. }
  950. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  951. }
  952. return;
  953. }
  954. // take some precautions
  955. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  956. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr,);
  957. if (yesNo)
  958. {
  959. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->instantiate != nullptr,);
  960. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->cleanup != nullptr,);
  961. }
  962. else
  963. {
  964. if (fUI.handle == nullptr)
  965. return;
  966. }
  967. if (yesNo)
  968. {
  969. if (fUI.handle == nullptr)
  970. {
  971. #ifndef LV2_UIS_ONLY_BRIDGES
  972. if (fUI.type == UI::TYPE_EMBED)
  973. {
  974. const char* msg = nullptr;
  975. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  976. switch (fUI.rdfDescriptor->Type)
  977. {
  978. case LV2_UI_GTK2:
  979. case LV2_UI_GTK3:
  980. case LV2_UI_QT4:
  981. case LV2_UI_QT5:
  982. case LV2_UI_EXTERNAL:
  983. case LV2_UI_OLD_EXTERNAL:
  984. msg = "Invalid UI type";
  985. break;
  986. case LV2_UI_COCOA:
  987. # ifdef CARLA_OS_MAC
  988. fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId);
  989. # else
  990. msg = "UI is for MacOS only";
  991. # endif
  992. break;
  993. case LV2_UI_WINDOWS:
  994. # ifdef CARLA_OS_WIN
  995. fUI.window = CarlaPluginUI::newWindows(this, frontendWinId);
  996. # else
  997. msg = "UI is for Windows only";
  998. # endif
  999. break;
  1000. case LV2_UI_X11:
  1001. # ifdef HAVE_X11
  1002. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, isUiResizable());
  1003. # else
  1004. msg = "UI is only for systems with X11";
  1005. # endif
  1006. break;
  1007. default:
  1008. msg = "Unknown UI type";
  1009. break;
  1010. }
  1011. if (fUI.window == nullptr && fExt.uishow == nullptr)
  1012. {
  1013. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  1014. // unused
  1015. (void)frontendWinId;
  1016. }
  1017. if (fUI.window != nullptr)
  1018. {
  1019. fUI.window->setTitle(fLv2Options.windowTitle);
  1020. fFeatures[kFeatureIdUiParent]->data = fUI.window->getPtr();
  1021. }
  1022. }
  1023. #endif
  1024. fUI.widget = nullptr;
  1025. fUI.handle = fUI.descriptor->instantiate(fUI.descriptor, fRdfDescriptor->URI, fUI.rdfDescriptor->Bundle,
  1026. carla_lv2_ui_write_function, this, &fUI.widget, fFeatures);
  1027. }
  1028. CARLA_SAFE_ASSERT(fUI.handle != nullptr);
  1029. CARLA_SAFE_ASSERT(fUI.type != UI::TYPE_EXTERNAL || fUI.widget != nullptr);
  1030. if (fUI.handle == nullptr || (fUI.type == UI::TYPE_EXTERNAL && fUI.widget == nullptr))
  1031. {
  1032. fUI.widget = nullptr;
  1033. if (fUI.handle != nullptr)
  1034. {
  1035. fUI.descriptor->cleanup(fUI.handle);
  1036. fUI.handle = nullptr;
  1037. }
  1038. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  1039. }
  1040. updateUi();
  1041. #ifndef LV2_UIS_ONLY_BRIDGES
  1042. if (fUI.type == UI::TYPE_EMBED)
  1043. {
  1044. if (fUI.window != nullptr)
  1045. {
  1046. fUI.window->show();
  1047. }
  1048. else if (fExt.uishow != nullptr)
  1049. {
  1050. fExt.uishow->show(fUI.handle);
  1051. # ifndef BUILD_BRIDGE
  1052. pData->tryTransient();
  1053. # endif
  1054. }
  1055. }
  1056. else
  1057. #endif
  1058. {
  1059. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUI.widget);
  1060. #ifndef BUILD_BRIDGE
  1061. pData->tryTransient();
  1062. #endif
  1063. }
  1064. }
  1065. else
  1066. {
  1067. #ifndef LV2_UIS_ONLY_BRIDGES
  1068. if (fUI.type == UI::TYPE_EMBED)
  1069. {
  1070. if (fUI.window != nullptr)
  1071. fUI.window->hide();
  1072. else if (fExt.uishow != nullptr)
  1073. fExt.uishow->hide(fUI.handle);
  1074. }
  1075. else
  1076. #endif
  1077. {
  1078. CARLA_SAFE_ASSERT(fUI.widget != nullptr);
  1079. if (fUI.widget != nullptr)
  1080. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUI.widget);
  1081. }
  1082. fUI.descriptor->cleanup(fUI.handle);
  1083. fUI.handle = nullptr;
  1084. fUI.widget = nullptr;
  1085. }
  1086. }
  1087. void idle() override
  1088. {
  1089. if (fAtomBufferOut.isDataAvailableForReading())
  1090. {
  1091. uint8_t dumpBuf[fAtomBufferOut.getSize()];
  1092. Lv2AtomRingBuffer tmpRingBuffer(fAtomBufferOut, dumpBuf);
  1093. CARLA_SAFE_ASSERT(tmpRingBuffer.isDataAvailableForReading());
  1094. uint32_t portIndex;
  1095. const LV2_Atom* atom;
  1096. const bool hasPortEvent(fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr);
  1097. for (; tmpRingBuffer.get(atom, portIndex);)
  1098. {
  1099. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  1100. {
  1101. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1102. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1103. }
  1104. else if (fUI.type == UI::TYPE_OSC)
  1105. {
  1106. if (pData->osc.data.target != nullptr)
  1107. {
  1108. CarlaString chunk(CarlaString::asBase64(atom, sizeof(LV2_Atom)+atom->size));
  1109. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.buffer());
  1110. }
  1111. }
  1112. else
  1113. {
  1114. if (hasPortEvent)
  1115. fUI.descriptor->port_event(fUI.handle, portIndex, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1116. }
  1117. }
  1118. }
  1119. if (fLatencyChanged && fLatencyIndex != -1)
  1120. {
  1121. fLatencyChanged = false;
  1122. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  1123. if (latency >= 0)
  1124. {
  1125. const uint32_t ulatency(static_cast<uint32_t>(latency));
  1126. if (pData->latency != ulatency)
  1127. {
  1128. carla_stdout("latency changed to %i", latency);
  1129. const ScopedSingleProcessLocker sspl(this, true);
  1130. pData->latency = ulatency;
  1131. pData->client->setLatency(ulatency);
  1132. #ifndef BUILD_BRIDGE
  1133. pData->recreateLatencyBuffers();
  1134. #endif
  1135. }
  1136. }
  1137. else
  1138. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  1139. }
  1140. if (fUI.handle != nullptr && fUI.descriptor != nullptr)
  1141. {
  1142. if (fUI.type == UI::TYPE_EXTERNAL && fUI.widget != nullptr)
  1143. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUI.widget);
  1144. #ifndef LV2_UIS_ONLY_BRIDGES
  1145. else if (fUI.type == UI::TYPE_EMBED && fUI.window != nullptr)
  1146. fUI.window->idle();
  1147. // note: UI might have been closed by ext-ui or window idle
  1148. if (fUI.handle != nullptr && fExt.uiidle != nullptr && fExt.uiidle->idle(fUI.handle) != 0)
  1149. {
  1150. showCustomUI(false);
  1151. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1152. }
  1153. #endif
  1154. }
  1155. CarlaPlugin::idle();
  1156. }
  1157. // -------------------------------------------------------------------
  1158. // Plugin state
  1159. void reload() override
  1160. {
  1161. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1162. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1163. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1164. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1165. carla_debug("Lv2Plugin::reload() - start");
  1166. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1167. // Safely disable plugin for reload
  1168. const ScopedDisabler sd(this);
  1169. if (pData->active)
  1170. deactivate();
  1171. clearBuffers();
  1172. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1173. const uint32_t portCount(fRdfDescriptor->PortCount);
  1174. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1175. aIns = aOuts = cvIns = cvOuts = params = 0;
  1176. LinkedList<uint> evIns, evOuts;
  1177. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1178. bool forcedStereoIn, forcedStereoOut;
  1179. forcedStereoIn = forcedStereoOut = false;
  1180. bool needsCtrlIn, needsCtrlOut;
  1181. needsCtrlIn = needsCtrlOut = false;
  1182. for (uint32_t i=0; i < portCount; ++i)
  1183. {
  1184. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1185. if (LV2_IS_PORT_AUDIO(portTypes))
  1186. {
  1187. if (LV2_IS_PORT_INPUT(portTypes))
  1188. aIns += 1;
  1189. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1190. aOuts += 1;
  1191. }
  1192. else if (LV2_IS_PORT_CV(portTypes))
  1193. {
  1194. if (LV2_IS_PORT_INPUT(portTypes))
  1195. cvIns += 1;
  1196. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1197. cvOuts += 1;
  1198. }
  1199. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1200. {
  1201. if (LV2_IS_PORT_INPUT(portTypes))
  1202. evIns.append(CARLA_EVENT_DATA_ATOM);
  1203. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1204. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1205. }
  1206. else if (LV2_IS_PORT_EVENT(portTypes))
  1207. {
  1208. if (LV2_IS_PORT_INPUT(portTypes))
  1209. evIns.append(CARLA_EVENT_DATA_EVENT);
  1210. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1211. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1212. }
  1213. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1214. {
  1215. if (LV2_IS_PORT_INPUT(portTypes))
  1216. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1217. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1218. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1219. }
  1220. else if (LV2_IS_PORT_CONTROL(portTypes))
  1221. params += 1;
  1222. }
  1223. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1224. {
  1225. if (fHandle2 == nullptr)
  1226. {
  1227. try {
  1228. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1229. } catch(...) {}
  1230. }
  1231. if (fHandle2 != nullptr)
  1232. {
  1233. if (aIns == 1)
  1234. {
  1235. aIns = 2;
  1236. forcedStereoIn = true;
  1237. }
  1238. if (aOuts == 1)
  1239. {
  1240. aOuts = 2;
  1241. forcedStereoOut = true;
  1242. }
  1243. }
  1244. }
  1245. if (aIns > 0)
  1246. {
  1247. pData->audioIn.createNew(aIns);
  1248. fAudioInBuffers = new float*[aIns];
  1249. for (uint32_t i=0; i < aIns; ++i)
  1250. fAudioInBuffers[i] = nullptr;
  1251. }
  1252. if (aOuts > 0)
  1253. {
  1254. pData->audioOut.createNew(aOuts);
  1255. fAudioOutBuffers = new float*[aOuts];
  1256. needsCtrlIn = true;
  1257. for (uint32_t i=0; i < aOuts; ++i)
  1258. fAudioOutBuffers[i] = nullptr;
  1259. }
  1260. if (cvIns > 0)
  1261. {
  1262. pData->cvIn.createNew(cvIns);
  1263. fCvInBuffers = new float*[cvIns];
  1264. for (uint32_t i=0; i < cvIns; ++i)
  1265. fCvInBuffers[i] = nullptr;
  1266. }
  1267. if (cvOuts > 0)
  1268. {
  1269. pData->cvOut.createNew(cvOuts);
  1270. fCvOutBuffers = new float*[cvOuts];
  1271. for (uint32_t i=0; i < cvOuts; ++i)
  1272. fCvOutBuffers[i] = nullptr;
  1273. }
  1274. if (params > 0)
  1275. {
  1276. pData->param.createNew(params, true);
  1277. fParamBuffers = new float[params];
  1278. FloatVectorOperations::clear(fParamBuffers, static_cast<int>(params));
  1279. }
  1280. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1281. {
  1282. fEventsIn.createNew(count);
  1283. for (uint32_t i=0; i < count; ++i)
  1284. {
  1285. const uint32_t& type(evIns.getAt(i, 0x0));
  1286. if (type == CARLA_EVENT_DATA_ATOM)
  1287. {
  1288. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1289. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1290. }
  1291. else if (type == CARLA_EVENT_DATA_EVENT)
  1292. {
  1293. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1294. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1295. }
  1296. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1297. {
  1298. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1299. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1300. fEventsIn.data[i].midi.data = new uchar[eventBufferSize];
  1301. }
  1302. }
  1303. }
  1304. else
  1305. {
  1306. fEventsIn.createNew(1);
  1307. fEventsIn.ctrl = &fEventsIn.data[0];
  1308. }
  1309. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1310. {
  1311. fEventsOut.createNew(count);
  1312. for (uint32_t i=0; i < count; ++i)
  1313. {
  1314. const uint32_t& type(evOuts.getAt(i, 0x0));
  1315. if (type == CARLA_EVENT_DATA_ATOM)
  1316. {
  1317. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1318. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1319. }
  1320. else if (type == CARLA_EVENT_DATA_EVENT)
  1321. {
  1322. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1323. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1324. }
  1325. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1326. {
  1327. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1328. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1329. fEventsOut.data[i].midi.data = new uchar[eventBufferSize];
  1330. }
  1331. }
  1332. }
  1333. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1334. CarlaString portName;
  1335. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1336. {
  1337. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1338. portName.clear();
  1339. 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))
  1340. {
  1341. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1342. {
  1343. portName = pData->name;
  1344. portName += ":";
  1345. }
  1346. portName += fRdfDescriptor->Ports[i].Name;
  1347. portName.truncate(portNameSize);
  1348. }
  1349. if (LV2_IS_PORT_AUDIO(portTypes))
  1350. {
  1351. if (LV2_IS_PORT_INPUT(portTypes))
  1352. {
  1353. const uint32_t j = iAudioIn++;
  1354. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1355. pData->audioIn.ports[j].rindex = i;
  1356. if (forcedStereoIn)
  1357. {
  1358. portName += "_2";
  1359. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1360. pData->audioIn.ports[1].rindex = i;
  1361. }
  1362. }
  1363. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1364. {
  1365. const uint32_t j = iAudioOut++;
  1366. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1367. pData->audioOut.ports[j].rindex = i;
  1368. if (forcedStereoOut)
  1369. {
  1370. portName += "_2";
  1371. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1372. pData->audioOut.ports[1].rindex = i;
  1373. }
  1374. }
  1375. else
  1376. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1377. }
  1378. else if (LV2_IS_PORT_CV(portTypes))
  1379. {
  1380. if (LV2_IS_PORT_INPUT(portTypes))
  1381. {
  1382. const uint32_t j = iCvIn++;
  1383. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1384. pData->cvIn.ports[j].rindex = i;
  1385. }
  1386. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1387. {
  1388. const uint32_t j = iCvOut++;
  1389. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1390. pData->cvOut.ports[j].rindex = i;
  1391. }
  1392. else
  1393. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1394. }
  1395. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1396. {
  1397. if (LV2_IS_PORT_INPUT(portTypes))
  1398. {
  1399. const uint32_t j = iEvIn++;
  1400. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1401. if (fHandle2 != nullptr)
  1402. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1403. fEventsIn.data[j].rindex = i;
  1404. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1405. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1406. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1407. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1408. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1409. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1410. if (evIns.count() == 1)
  1411. {
  1412. fEventsIn.ctrl = &fEventsIn.data[j];
  1413. fEventsIn.ctrlIndex = j;
  1414. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1415. needsCtrlIn = true;
  1416. }
  1417. else
  1418. {
  1419. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1420. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1421. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1422. {
  1423. fEventsIn.ctrl = &fEventsIn.data[j];
  1424. fEventsIn.ctrlIndex = j;
  1425. }
  1426. }
  1427. }
  1428. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1429. {
  1430. const uint32_t j = iEvOut++;
  1431. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1432. if (fHandle2 != nullptr)
  1433. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1434. fEventsOut.data[j].rindex = i;
  1435. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1436. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1437. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1438. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1439. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1440. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1441. if (evOuts.count() == 1)
  1442. {
  1443. fEventsOut.ctrl = &fEventsOut.data[j];
  1444. fEventsOut.ctrlIndex = j;
  1445. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1446. needsCtrlOut = true;
  1447. }
  1448. else
  1449. {
  1450. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1451. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1452. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1453. {
  1454. fEventsOut.ctrl = &fEventsOut.data[j];
  1455. fEventsOut.ctrlIndex = j;
  1456. }
  1457. }
  1458. }
  1459. else
  1460. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1461. }
  1462. else if (LV2_IS_PORT_EVENT(portTypes))
  1463. {
  1464. if (LV2_IS_PORT_INPUT(portTypes))
  1465. {
  1466. const uint32_t j = iEvIn++;
  1467. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1468. if (fHandle2 != nullptr)
  1469. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1470. fEventsIn.data[j].rindex = i;
  1471. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1472. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1473. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1474. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1475. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1476. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1477. if (evIns.count() == 1)
  1478. {
  1479. fEventsIn.ctrl = &fEventsIn.data[j];
  1480. fEventsIn.ctrlIndex = j;
  1481. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1482. needsCtrlIn = true;
  1483. }
  1484. else
  1485. {
  1486. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1487. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1488. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1489. {
  1490. fEventsIn.ctrl = &fEventsIn.data[j];
  1491. fEventsIn.ctrlIndex = j;
  1492. }
  1493. }
  1494. }
  1495. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1496. {
  1497. const uint32_t j = iEvOut++;
  1498. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1499. if (fHandle2 != nullptr)
  1500. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1501. fEventsOut.data[j].rindex = i;
  1502. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1503. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1504. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1505. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1506. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1507. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1508. if (evOuts.count() == 1)
  1509. {
  1510. fEventsOut.ctrl = &fEventsOut.data[j];
  1511. fEventsOut.ctrlIndex = j;
  1512. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1513. needsCtrlOut = true;
  1514. }
  1515. else
  1516. {
  1517. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1518. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1519. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1520. {
  1521. fEventsOut.ctrl = &fEventsOut.data[j];
  1522. fEventsOut.ctrlIndex = j;
  1523. }
  1524. }
  1525. }
  1526. else
  1527. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1528. }
  1529. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1530. {
  1531. if (LV2_IS_PORT_INPUT(portTypes))
  1532. {
  1533. const uint32_t j = iEvIn++;
  1534. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1535. if (fHandle2 != nullptr)
  1536. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1537. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1538. fEventsIn.data[j].rindex = i;
  1539. if (evIns.count() == 1)
  1540. {
  1541. needsCtrlIn = true;
  1542. fEventsIn.ctrl = &fEventsIn.data[j];
  1543. fEventsIn.ctrlIndex = j;
  1544. }
  1545. else
  1546. {
  1547. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1548. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1549. {
  1550. fEventsIn.ctrl = &fEventsIn.data[j];
  1551. fEventsIn.ctrlIndex = j;
  1552. }
  1553. }
  1554. }
  1555. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1556. {
  1557. const uint32_t j = iEvOut++;
  1558. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1559. if (fHandle2 != nullptr)
  1560. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1561. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1562. fEventsOut.data[j].rindex = i;
  1563. if (evOuts.count() == 1)
  1564. {
  1565. needsCtrlOut = true;
  1566. fEventsOut.ctrl = &fEventsOut.data[j];
  1567. fEventsOut.ctrlIndex = j;
  1568. }
  1569. else
  1570. {
  1571. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1572. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1573. {
  1574. fEventsOut.ctrl = &fEventsOut.data[j];
  1575. fEventsOut.ctrlIndex = j;
  1576. }
  1577. }
  1578. }
  1579. else
  1580. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1581. }
  1582. else if (LV2_IS_PORT_CONTROL(portTypes))
  1583. {
  1584. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1585. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1586. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1587. const uint32_t j = iCtrl++;
  1588. pData->param.data[j].index = static_cast<int32_t>(j);
  1589. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1590. float min, max, def, step, stepSmall, stepLarge;
  1591. // min value
  1592. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1593. min = portPoints.Minimum;
  1594. else
  1595. min = 0.0f;
  1596. // max value
  1597. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1598. max = portPoints.Maximum;
  1599. else
  1600. max = 1.0f;
  1601. if (min > max)
  1602. max = min;
  1603. // stupid hack for ir.lv2 (broken plugin)
  1604. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1605. {
  1606. min = 0.0f;
  1607. max = (float)0xffffff;
  1608. }
  1609. if (carla_compareFloats(min, max))
  1610. {
  1611. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", fRdfDescriptor->Ports[i].Name);
  1612. max = min + 0.1f;
  1613. }
  1614. // default value
  1615. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1616. {
  1617. def = portPoints.Default;
  1618. }
  1619. else
  1620. {
  1621. // no default value
  1622. if (min < 0.0f && max > 0.0f)
  1623. def = 0.0f;
  1624. else
  1625. def = min;
  1626. }
  1627. if (def < min)
  1628. def = min;
  1629. else if (def > max)
  1630. def = max;
  1631. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1632. {
  1633. min *= sampleRate;
  1634. max *= sampleRate;
  1635. def *= sampleRate;
  1636. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1637. }
  1638. if (LV2_IS_PORT_TOGGLED(portProps))
  1639. {
  1640. step = max - min;
  1641. stepSmall = step;
  1642. stepLarge = step;
  1643. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1644. }
  1645. else if (LV2_IS_PORT_INTEGER(portProps))
  1646. {
  1647. step = 1.0f;
  1648. stepSmall = 1.0f;
  1649. stepLarge = 10.0f;
  1650. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1651. }
  1652. else
  1653. {
  1654. float range = max - min;
  1655. step = range/100.0f;
  1656. stepSmall = range/1000.0f;
  1657. stepLarge = range/10.0f;
  1658. }
  1659. if (LV2_IS_PORT_INPUT(portTypes))
  1660. {
  1661. pData->param.data[j].type = PARAMETER_INPUT;
  1662. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1663. {
  1664. carla_stderr("Plugin has latency input port, this should not happen!");
  1665. }
  1666. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1667. {
  1668. def = sampleRate;
  1669. step = 1.0f;
  1670. stepSmall = 1.0f;
  1671. stepLarge = 1.0f;
  1672. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1673. }
  1674. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1675. {
  1676. pData->param.special[j] = PARAMETER_SPECIAL_FREEWHEEL;
  1677. }
  1678. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1679. {
  1680. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  1681. }
  1682. else
  1683. {
  1684. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1685. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1686. needsCtrlIn = true;
  1687. }
  1688. // MIDI CC value
  1689. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1690. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1691. {
  1692. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1693. pData->param.data[j].midiCC = static_cast<int16_t>(portMidiMap.Number);
  1694. }
  1695. }
  1696. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1697. {
  1698. pData->param.data[j].type = PARAMETER_OUTPUT;
  1699. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1700. {
  1701. min = 0.0f;
  1702. max = sampleRate;
  1703. def = 0.0f;
  1704. step = 1.0f;
  1705. stepSmall = 1.0f;
  1706. stepLarge = 1.0f;
  1707. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1708. CARLA_SAFE_ASSERT(fLatencyIndex == static_cast<int32_t>(j));
  1709. }
  1710. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1711. {
  1712. def = sampleRate;
  1713. step = 1.0f;
  1714. stepSmall = 1.0f;
  1715. stepLarge = 1.0f;
  1716. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1717. }
  1718. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1719. {
  1720. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1721. }
  1722. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1723. {
  1724. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  1725. }
  1726. else
  1727. {
  1728. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1729. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1730. needsCtrlOut = true;
  1731. }
  1732. }
  1733. else
  1734. {
  1735. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1736. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1737. }
  1738. // extra parameter hints
  1739. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1740. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1741. if (LV2_IS_PORT_TRIGGER(portProps))
  1742. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1743. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1744. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1745. if (LV2_IS_PORT_ENUMERATION(portProps))
  1746. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1747. // check if parameter is not enabled or automable
  1748. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1749. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1750. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps))
  1751. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1752. else if (LV2_IS_PORT_NOT_AUTOMATIC(portProps) || LV2_IS_PORT_NON_AUTOMABLE(portProps))
  1753. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1754. pData->param.ranges[j].min = min;
  1755. pData->param.ranges[j].max = max;
  1756. pData->param.ranges[j].def = def;
  1757. pData->param.ranges[j].step = step;
  1758. pData->param.ranges[j].stepSmall = stepSmall;
  1759. pData->param.ranges[j].stepLarge = stepLarge;
  1760. // Start parameters in their default values (except freewheel, which is off by default)
  1761. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_FREEWHEEL)
  1762. fParamBuffers[j] = min;
  1763. else
  1764. fParamBuffers[j] = def;
  1765. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1766. if (fHandle2 != nullptr)
  1767. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1768. }
  1769. else
  1770. {
  1771. // Port Type not supported, but it's optional anyway
  1772. fDescriptor->connect_port(fHandle, i, nullptr);
  1773. if (fHandle2 != nullptr)
  1774. fDescriptor->connect_port(fHandle2, i, nullptr);
  1775. }
  1776. }
  1777. if (needsCtrlIn)
  1778. {
  1779. portName.clear();
  1780. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1781. {
  1782. portName = pData->name;
  1783. portName += ":";
  1784. }
  1785. portName += "events-in";
  1786. portName.truncate(portNameSize);
  1787. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1788. }
  1789. if (needsCtrlOut)
  1790. {
  1791. portName.clear();
  1792. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1793. {
  1794. portName = pData->name;
  1795. portName += ":";
  1796. }
  1797. portName += "events-out";
  1798. portName.truncate(portNameSize);
  1799. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1800. }
  1801. if (fExt.worker != nullptr || (fUI.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1802. fAtomBufferIn.createBuffer(eventBufferSize);
  1803. if (fExt.worker != nullptr || (fUI.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1804. fAtomBufferOut.createBuffer(eventBufferSize);
  1805. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1806. fEventsIn.ctrl->port = pData->event.portIn;
  1807. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1808. fEventsOut.ctrl->port = pData->event.portOut;
  1809. if (fCanInit2 && (forcedStereoIn || forcedStereoOut))
  1810. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1811. else
  1812. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1813. // plugin hints
  1814. pData->hints = 0x0;
  1815. if (isRealtimeSafe())
  1816. pData->hints |= PLUGIN_IS_RTSAFE;
  1817. if (fUI.type != UI::TYPE_NULL)
  1818. {
  1819. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1820. if (fUI.type == UI::TYPE_EMBED)
  1821. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1822. }
  1823. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1824. pData->hints |= PLUGIN_IS_SYNTH;
  1825. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1826. pData->hints |= PLUGIN_CAN_DRYWET;
  1827. if (aOuts > 0)
  1828. pData->hints |= PLUGIN_CAN_VOLUME;
  1829. if (aOuts >= 2 && aOuts % 2 == 0)
  1830. pData->hints |= PLUGIN_CAN_BALANCE;
  1831. // extra plugin hints
  1832. pData->extraHints = 0x0;
  1833. if (! fCanInit2)
  1834. {
  1835. // can't run in rack
  1836. }
  1837. else if (fExt.state != nullptr || fExt.worker != nullptr)
  1838. {
  1839. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1840. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1841. }
  1842. else
  1843. {
  1844. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1845. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1846. }
  1847. // check latency
  1848. if (fLatencyIndex >= 0)
  1849. {
  1850. // we need to pre-run the plugin so it can update its latency control-port
  1851. float tmpIn [(aIns > 0) ? aIns : 1][2];
  1852. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  1853. for (uint32_t j=0; j < aIns; ++j)
  1854. {
  1855. tmpIn[j][0] = 0.0f;
  1856. tmpIn[j][1] = 0.0f;
  1857. try {
  1858. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  1859. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency input");
  1860. }
  1861. for (uint32_t j=0; j < aOuts; ++j)
  1862. {
  1863. tmpOut[j][0] = 0.0f;
  1864. tmpOut[j][1] = 0.0f;
  1865. try {
  1866. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  1867. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency output");
  1868. }
  1869. if (fDescriptor->activate != nullptr)
  1870. {
  1871. try {
  1872. fDescriptor->activate(fHandle);
  1873. } CARLA_SAFE_EXCEPTION("LV2 latency activate");
  1874. }
  1875. try {
  1876. fDescriptor->run(fHandle, 2);
  1877. } CARLA_SAFE_EXCEPTION("LV2 latency run");
  1878. if (fDescriptor->deactivate != nullptr)
  1879. {
  1880. try {
  1881. fDescriptor->deactivate(fHandle);
  1882. } CARLA_SAFE_EXCEPTION("LV2 latency deactivate");
  1883. }
  1884. const int32_t latency(static_cast<int32_t>(fParamBuffers[fLatencyIndex]));
  1885. if (latency >= 0)
  1886. {
  1887. const uint32_t ulatency(static_cast<uint32_t>(latency));
  1888. if (pData->latency != ulatency)
  1889. {
  1890. carla_stdout("latency = %i", latency);
  1891. pData->latency = ulatency;
  1892. pData->client->setLatency(ulatency);
  1893. #ifndef BUILD_BRIDGE
  1894. pData->recreateLatencyBuffers();
  1895. #endif
  1896. }
  1897. }
  1898. else
  1899. carla_safe_assert_int("latency >= 0", __FILE__, __LINE__, latency);
  1900. fLatencyChanged = false;
  1901. }
  1902. bufferSizeChanged(pData->engine->getBufferSize());
  1903. reloadPrograms(true);
  1904. evIns.clear();
  1905. evOuts.clear();
  1906. if (pData->active)
  1907. activate();
  1908. carla_debug("Lv2Plugin::reload() - end");
  1909. }
  1910. void reloadPrograms(const bool doInit) override
  1911. {
  1912. carla_debug("Lv2Plugin::reloadPrograms(%s)", bool2str(doInit));
  1913. const uint32_t oldCount = pData->midiprog.count;
  1914. const int32_t current = pData->midiprog.current;
  1915. // special LV2 programs handling
  1916. if (doInit)
  1917. {
  1918. pData->prog.clear();
  1919. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  1920. if (presetCount > 0)
  1921. {
  1922. pData->prog.createNew(presetCount);
  1923. for (uint32_t i=0; i < presetCount; ++i)
  1924. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1925. }
  1926. }
  1927. // Delete old programs
  1928. pData->midiprog.clear();
  1929. // Query new programs
  1930. uint32_t newCount = 0;
  1931. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1932. {
  1933. for (; fExt.programs->get_program(fHandle, newCount);)
  1934. ++newCount;
  1935. }
  1936. if (newCount > 0)
  1937. {
  1938. pData->midiprog.createNew(newCount);
  1939. // Update data
  1940. for (uint32_t i=0; i < newCount; ++i)
  1941. {
  1942. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1943. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  1944. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  1945. pData->midiprog.data[i].bank = pdesc->bank;
  1946. pData->midiprog.data[i].program = pdesc->program;
  1947. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1948. }
  1949. }
  1950. #ifndef BUILD_BRIDGE
  1951. // Update OSC Names
  1952. if (pData->engine->isOscControlRegistered())
  1953. {
  1954. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  1955. for (uint32_t i=0; i < newCount; ++i)
  1956. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  1957. }
  1958. #endif
  1959. if (doInit)
  1960. {
  1961. if (newCount > 0)
  1962. {
  1963. setMidiProgram(0, false, false, false);
  1964. }
  1965. else
  1966. {
  1967. // load default state
  1968. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1969. {
  1970. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1971. if (fHandle2 != nullptr)
  1972. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1973. }
  1974. }
  1975. }
  1976. else
  1977. {
  1978. // Check if current program is invalid
  1979. bool programChanged = false;
  1980. if (newCount == oldCount+1)
  1981. {
  1982. // one midi program added, probably created by user
  1983. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1984. programChanged = true;
  1985. }
  1986. else if (current < 0 && newCount > 0)
  1987. {
  1988. // programs exist now, but not before
  1989. pData->midiprog.current = 0;
  1990. programChanged = true;
  1991. }
  1992. else if (current >= 0 && newCount == 0)
  1993. {
  1994. // programs existed before, but not anymore
  1995. pData->midiprog.current = -1;
  1996. programChanged = true;
  1997. }
  1998. else if (current >= static_cast<int32_t>(newCount))
  1999. {
  2000. // current midi program > count
  2001. pData->midiprog.current = 0;
  2002. programChanged = true;
  2003. }
  2004. else
  2005. {
  2006. // no change
  2007. pData->midiprog.current = current;
  2008. }
  2009. if (programChanged)
  2010. setMidiProgram(pData->midiprog.current, true, true, true);
  2011. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  2012. }
  2013. }
  2014. // -------------------------------------------------------------------
  2015. // Plugin processing
  2016. void activate() noexcept override
  2017. {
  2018. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2019. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2020. if (fDescriptor->activate != nullptr)
  2021. {
  2022. try {
  2023. fDescriptor->activate(fHandle);
  2024. } CARLA_SAFE_EXCEPTION("LV2 activate");
  2025. if (fHandle2 != nullptr)
  2026. {
  2027. try {
  2028. fDescriptor->activate(fHandle2);
  2029. } CARLA_SAFE_EXCEPTION("LV2 activate #2");
  2030. }
  2031. }
  2032. fFirstActive = true;
  2033. }
  2034. void deactivate() noexcept override
  2035. {
  2036. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2037. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2038. if (fDescriptor->deactivate != nullptr)
  2039. {
  2040. try {
  2041. fDescriptor->deactivate(fHandle);
  2042. } CARLA_SAFE_EXCEPTION("LV2 deactivate");
  2043. if (fHandle2 != nullptr)
  2044. {
  2045. try {
  2046. fDescriptor->deactivate(fHandle2);
  2047. } CARLA_SAFE_EXCEPTION("LV2 deactivate #2");
  2048. }
  2049. }
  2050. }
  2051. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  2052. {
  2053. // --------------------------------------------------------------------------------------------------------
  2054. // Check if active
  2055. if (! pData->active)
  2056. {
  2057. // disable any output sound
  2058. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2059. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  2060. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2061. FloatVectorOperations::clear(cvOut[i], static_cast<int>(frames));
  2062. return;
  2063. }
  2064. // --------------------------------------------------------------------------------------------------------
  2065. // Event itenerators from different APIs (input)
  2066. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  2067. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  2068. LV2_MIDIState evInMidiStates[fEventsIn.count];
  2069. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2070. {
  2071. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2072. {
  2073. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  2074. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  2075. }
  2076. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2077. {
  2078. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  2079. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  2080. }
  2081. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2082. {
  2083. fEventsIn.data[i].midi.event_count = 0;
  2084. fEventsIn.data[i].midi.size = 0;
  2085. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  2086. evInMidiStates[i].frame_count = frames;
  2087. evInMidiStates[i].position = 0;
  2088. }
  2089. }
  2090. for (uint32_t i=0; i < fEventsOut.count; ++i)
  2091. {
  2092. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2093. {
  2094. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  2095. }
  2096. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2097. {
  2098. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  2099. }
  2100. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2101. {
  2102. // not needed
  2103. }
  2104. }
  2105. // --------------------------------------------------------------------------------------------------------
  2106. // Check if needs reset
  2107. if (pData->needsReset)
  2108. {
  2109. uint8_t midiData[3] = { 0, 0, 0 };
  2110. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  2111. {
  2112. const uint32_t j = fEventsIn.ctrlIndex;
  2113. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2114. {
  2115. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  2116. {
  2117. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2118. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2119. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2120. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2121. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2122. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2123. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2124. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2125. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2126. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2127. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2128. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2129. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2130. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2131. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2132. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2133. }
  2134. }
  2135. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2136. {
  2137. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2138. {
  2139. midiData[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  2140. midiData[1] = k;
  2141. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2142. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2143. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2144. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2145. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2146. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  2147. }
  2148. }
  2149. }
  2150. #ifndef BUILD_BRIDGE
  2151. if (pData->latency > 0)
  2152. {
  2153. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2154. FloatVectorOperations::clear(pData->latencyBuffers[i], static_cast<int>(pData->latency));
  2155. }
  2156. #endif
  2157. pData->needsReset = false;
  2158. }
  2159. // --------------------------------------------------------------------------------------------------------
  2160. // TimeInfo
  2161. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2162. if (fFirstActive || fLastTimeInfo != timeInfo)
  2163. {
  2164. bool doPostRt;
  2165. int32_t rindex;
  2166. // update input ports
  2167. for (uint32_t k=0; k < pData->param.count; ++k)
  2168. {
  2169. if (pData->param.data[k].type != PARAMETER_INPUT)
  2170. continue;
  2171. if (pData->param.special[k] != PARAMETER_SPECIAL_TIME)
  2172. continue;
  2173. doPostRt = false;
  2174. rindex = pData->param.data[k].rindex;
  2175. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2176. switch (fRdfDescriptor->Ports[rindex].Designation)
  2177. {
  2178. // Non-BBT
  2179. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2180. if (fLastTimeInfo.playing != timeInfo.playing)
  2181. {
  2182. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2183. doPostRt = true;
  2184. }
  2185. break;
  2186. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2187. if (fLastTimeInfo.frame != timeInfo.frame)
  2188. {
  2189. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2190. doPostRt = true;
  2191. }
  2192. break;
  2193. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2194. break;
  2195. // BBT
  2196. case LV2_PORT_DESIGNATION_TIME_BAR:
  2197. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2198. {
  2199. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2200. doPostRt = true;
  2201. }
  2202. break;
  2203. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2204. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2205. !carla_compareFloats(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat)))
  2206. {
  2207. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2208. doPostRt = true;
  2209. }
  2210. break;
  2211. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2212. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2213. {
  2214. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2215. doPostRt = true;
  2216. }
  2217. break;
  2218. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2219. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.beatType, timeInfo.bbt.beatType))
  2220. {
  2221. fParamBuffers[k] = timeInfo.bbt.beatType;
  2222. doPostRt = true;
  2223. }
  2224. break;
  2225. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2226. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.beatsPerBar, timeInfo.bbt.beatsPerBar))
  2227. {
  2228. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2229. doPostRt = true;
  2230. }
  2231. break;
  2232. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2233. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.beatsPerMinute, timeInfo.bbt.beatsPerMinute))
  2234. {
  2235. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2236. doPostRt = true;
  2237. }
  2238. break;
  2239. case LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT:
  2240. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && ! carla_compareFloats(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat))
  2241. {
  2242. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.ticksPerBeat);
  2243. doPostRt = true;
  2244. }
  2245. break;
  2246. }
  2247. if (doPostRt)
  2248. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2249. }
  2250. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2251. {
  2252. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2253. continue;
  2254. uint8_t timeInfoBuf[256];
  2255. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2256. LV2_Atom_Forge_Frame forgeFrame;
  2257. lv2_atom_forge_object(&fAtomForge, &forgeFrame, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_TIME_POSITION);
  2258. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2259. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2260. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2261. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2262. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2263. {
  2264. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2265. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2266. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2267. lv2_atom_forge_float(&fAtomForge, static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat)));
  2268. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2269. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2270. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2271. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2272. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2273. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2274. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2275. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2276. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT);
  2277. lv2_atom_forge_double(&fAtomForge, static_cast<float>(timeInfo.bbt.ticksPerBeat));
  2278. }
  2279. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2280. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2281. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2282. // send only deprecated blank object for now
  2283. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, CARLA_URI_MAP_ID_ATOM_BLANK, atom->size, LV2_ATOM_BODY_CONST(atom));
  2284. // for atom:object
  2285. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2286. }
  2287. pData->postRtEvents.trySplice();
  2288. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2289. }
  2290. // --------------------------------------------------------------------------------------------------------
  2291. // Event Input and Processing
  2292. if (fEventsIn.ctrl != nullptr)
  2293. {
  2294. // ----------------------------------------------------------------------------------------------------
  2295. // Message Input
  2296. if (fAtomBufferIn.tryLock())
  2297. {
  2298. if (fAtomBufferIn.isDataAvailableForReading())
  2299. {
  2300. const LV2_Atom* atom;
  2301. uint32_t j, portIndex;
  2302. for (; fAtomBufferIn.get(atom, portIndex);)
  2303. {
  2304. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2305. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  2306. {
  2307. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2308. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2309. }
  2310. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2311. {
  2312. carla_stdout("Event input buffer full, at least 1 message lost");
  2313. continue;
  2314. }
  2315. }
  2316. }
  2317. fAtomBufferIn.unlock();
  2318. }
  2319. // ----------------------------------------------------------------------------------------------------
  2320. // MIDI Input (External)
  2321. if (pData->extNotes.mutex.tryLock())
  2322. {
  2323. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2324. {
  2325. // does not handle MIDI
  2326. pData->extNotes.data.clear();
  2327. }
  2328. else
  2329. {
  2330. const uint32_t j = fEventsIn.ctrlIndex;
  2331. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  2332. {
  2333. const ExternalMidiNote& note(it.getValue());
  2334. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2335. uint8_t midiEvent[3];
  2336. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2337. midiEvent[1] = note.note;
  2338. midiEvent[2] = note.velo;
  2339. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2340. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2341. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2342. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2343. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2344. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2345. }
  2346. pData->extNotes.data.clear();
  2347. }
  2348. pData->extNotes.mutex.unlock();
  2349. } // End of MIDI Input (External)
  2350. // ----------------------------------------------------------------------------------------------------
  2351. // Event Input (System)
  2352. #ifndef BUILD_BRIDGE
  2353. bool allNotesOffSent = false;
  2354. #endif
  2355. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2356. uint32_t startTime = 0;
  2357. uint32_t timeOffset = 0;
  2358. uint32_t nextBankId;
  2359. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2360. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2361. else
  2362. nextBankId = 0;
  2363. const uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2364. for (uint32_t i=0; i < numEvents; ++i)
  2365. {
  2366. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2367. if (event.time >= frames)
  2368. continue;
  2369. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2370. if (isSampleAccurate && event.time > timeOffset)
  2371. {
  2372. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  2373. {
  2374. startTime = 0;
  2375. timeOffset = event.time;
  2376. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2377. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2378. else
  2379. nextBankId = 0;
  2380. // reset iters
  2381. const uint32_t j = fEventsIn.ctrlIndex;
  2382. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2383. {
  2384. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2385. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2386. }
  2387. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2388. {
  2389. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2390. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2391. }
  2392. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2393. {
  2394. fEventsIn.data[j].midi.event_count = 0;
  2395. fEventsIn.data[j].midi.size = 0;
  2396. evInMidiStates[j].position = event.time;
  2397. }
  2398. }
  2399. else
  2400. startTime += timeOffset;
  2401. }
  2402. switch (event.type)
  2403. {
  2404. case kEngineEventTypeNull:
  2405. break;
  2406. case kEngineEventTypeControl: {
  2407. const EngineControlEvent& ctrlEvent(event.ctrl);
  2408. switch (ctrlEvent.type)
  2409. {
  2410. case kEngineControlEventTypeNull:
  2411. break;
  2412. case kEngineControlEventTypeParameter: {
  2413. #ifndef BUILD_BRIDGE
  2414. // Control backend stuff
  2415. if (event.channel == pData->ctrlChannel)
  2416. {
  2417. float value;
  2418. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2419. {
  2420. value = ctrlEvent.value;
  2421. setDryWet(value, false, false);
  2422. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2423. break;
  2424. }
  2425. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2426. {
  2427. value = ctrlEvent.value*127.0f/100.0f;
  2428. setVolume(value, false, false);
  2429. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2430. break;
  2431. }
  2432. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2433. {
  2434. float left, right;
  2435. value = ctrlEvent.value/0.5f - 1.0f;
  2436. if (value < 0.0f)
  2437. {
  2438. left = -1.0f;
  2439. right = (value*2.0f)+1.0f;
  2440. }
  2441. else if (value > 0.0f)
  2442. {
  2443. left = (value*2.0f)-1.0f;
  2444. right = 1.0f;
  2445. }
  2446. else
  2447. {
  2448. left = -1.0f;
  2449. right = 1.0f;
  2450. }
  2451. setBalanceLeft(left, false, false);
  2452. setBalanceRight(right, false, false);
  2453. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2454. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2455. break;
  2456. }
  2457. }
  2458. #endif
  2459. // Control plugin parameters
  2460. uint32_t k;
  2461. for (k=0; k < pData->param.count; ++k)
  2462. {
  2463. if (pData->param.data[k].midiChannel != event.channel)
  2464. continue;
  2465. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2466. continue;
  2467. if (pData->param.data[k].type != PARAMETER_INPUT)
  2468. continue;
  2469. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2470. continue;
  2471. float value;
  2472. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2473. {
  2474. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2475. }
  2476. else
  2477. {
  2478. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2479. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2480. value = std::rint(value);
  2481. }
  2482. setParameterValue(k, value, false, false, false);
  2483. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2484. break;
  2485. }
  2486. // check if event is already handled
  2487. if (k != pData->param.count)
  2488. break;
  2489. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2490. {
  2491. uint8_t midiData[3];
  2492. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2493. midiData[1] = uint8_t(ctrlEvent.param);
  2494. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2495. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2496. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2497. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2498. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2499. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2500. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2501. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2502. }
  2503. break;
  2504. } // case kEngineControlEventTypeParameter
  2505. case kEngineControlEventTypeMidiBank:
  2506. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2507. {
  2508. if (event.channel == pData->ctrlChannel)
  2509. nextBankId = ctrlEvent.param;
  2510. }
  2511. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2512. {
  2513. uint8_t midiData[3];
  2514. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2515. midiData[1] = MIDI_CONTROL_BANK_SELECT;
  2516. midiData[2] = uint8_t(ctrlEvent.param);
  2517. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2518. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2519. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2520. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2521. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2522. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2523. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2524. }
  2525. break;
  2526. case kEngineControlEventTypeMidiProgram:
  2527. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2528. {
  2529. if (event.channel == pData->ctrlChannel)
  2530. {
  2531. const uint32_t nextProgramId = ctrlEvent.param;
  2532. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2533. {
  2534. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2535. {
  2536. const int32_t index(static_cast<int32_t>(k));
  2537. setMidiProgram(index, false, false, false);
  2538. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2539. break;
  2540. }
  2541. }
  2542. }
  2543. }
  2544. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2545. {
  2546. uint8_t midiData[2];
  2547. midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2548. midiData[1] = uint8_t(ctrlEvent.param);
  2549. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2550. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2551. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiData);
  2552. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2553. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiData);
  2554. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2555. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 2, midiData);
  2556. }
  2557. break;
  2558. case kEngineControlEventTypeAllSoundOff:
  2559. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2560. {
  2561. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2562. uint8_t midiData[3];
  2563. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2564. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2565. midiData[2] = 0;
  2566. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2567. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2568. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2569. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2570. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2571. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2572. }
  2573. break;
  2574. case kEngineControlEventTypeAllNotesOff:
  2575. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2576. {
  2577. #ifndef BUILD_BRIDGE
  2578. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2579. {
  2580. allNotesOffSent = true;
  2581. sendMidiAllNotesOffToCallback();
  2582. }
  2583. #endif
  2584. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2585. uint8_t midiData[3];
  2586. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2587. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2588. midiData[2] = 0;
  2589. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2590. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2591. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2592. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2593. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2594. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2595. }
  2596. break;
  2597. } // switch (ctrlEvent.type)
  2598. break;
  2599. } // case kEngineEventTypeControl
  2600. case kEngineEventTypeMidi: {
  2601. const EngineMidiEvent& midiEvent(event.midi);
  2602. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  2603. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  2604. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2605. continue;
  2606. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2607. continue;
  2608. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2609. continue;
  2610. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2611. continue;
  2612. // Fix bad note-off (per LV2 spec)
  2613. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  2614. status = MIDI_STATUS_NOTE_OFF;
  2615. const uint32_t j = fEventsIn.ctrlIndex;
  2616. const uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2617. // put back channel in data
  2618. uint8_t midiData2[midiEvent.size];
  2619. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  2620. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  2621. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2622. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2623. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2624. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2625. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2626. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiData2);
  2627. if (status == MIDI_STATUS_NOTE_ON)
  2628. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiData[1], midiData[2]);
  2629. else if (status == MIDI_STATUS_NOTE_OFF)
  2630. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiData[1], 0.0f);
  2631. } break;
  2632. } // switch (event.type)
  2633. }
  2634. pData->postRtEvents.trySplice();
  2635. if (frames > timeOffset)
  2636. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  2637. } // End of Event Input and Processing
  2638. // --------------------------------------------------------------------------------------------------------
  2639. // Plugin processing (no events)
  2640. else
  2641. {
  2642. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  2643. } // End of Plugin processing (no events)
  2644. #ifndef BUILD_BRIDGE
  2645. // --------------------------------------------------------------------------------------------------------
  2646. // Latency, save values for next callback
  2647. if (fLatencyIndex != -1)
  2648. {
  2649. if (pData->latency != static_cast<uint32_t>(fParamBuffers[fLatencyIndex]))
  2650. {
  2651. fLatencyChanged = true;
  2652. }
  2653. else if (pData->latency > 0)
  2654. {
  2655. if (pData->latency <= frames)
  2656. {
  2657. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2658. FloatVectorOperations::copy(pData->latencyBuffers[i], audioIn[i]+(frames-pData->latency), static_cast<int>(pData->latency));
  2659. }
  2660. else
  2661. {
  2662. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  2663. {
  2664. for (k=0; k < pData->latency-frames; ++k)
  2665. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  2666. for (j=0; k < pData->latency; ++j, ++k)
  2667. pData->latencyBuffers[i][k] = audioIn[i][j];
  2668. }
  2669. }
  2670. }
  2671. }
  2672. #endif
  2673. // --------------------------------------------------------------------------------------------------------
  2674. // MIDI Output
  2675. if (fEventsOut.ctrl != nullptr)
  2676. {
  2677. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2678. {
  2679. const LV2_Atom_Event* ev;
  2680. LV2_Atom_Buffer_Iterator iter;
  2681. uint8_t* data;
  2682. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2683. for (;;)
  2684. {
  2685. data = nullptr;
  2686. ev = lv2_atom_buffer_get(&iter, &data);
  2687. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2688. break;
  2689. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2690. {
  2691. if (fEventsOut.ctrl->port != nullptr)
  2692. {
  2693. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2694. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2695. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2696. }
  2697. }
  2698. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2699. {
  2700. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2701. fAtomBufferOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2702. }
  2703. lv2_atom_buffer_increment(&iter);
  2704. }
  2705. }
  2706. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2707. {
  2708. const LV2_Event* ev;
  2709. LV2_Event_Iterator iter;
  2710. uint8_t* data;
  2711. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2712. for (;;)
  2713. {
  2714. data = nullptr;
  2715. ev = lv2_event_get(&iter, &data);
  2716. if (ev == nullptr || data == nullptr)
  2717. break;
  2718. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2719. {
  2720. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2721. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2722. }
  2723. lv2_event_increment(&iter);
  2724. }
  2725. }
  2726. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2727. {
  2728. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2729. uint32_t eventSize;
  2730. double eventTime;
  2731. uchar* eventData;
  2732. for (;;)
  2733. {
  2734. eventSize = 0;
  2735. eventTime = 0.0;
  2736. eventData = nullptr;
  2737. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2738. if (eventData == nullptr || eventSize == 0)
  2739. break;
  2740. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2741. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2742. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2743. lv2midi_step(&state);
  2744. }
  2745. }
  2746. }
  2747. #ifndef BUILD_BRIDGE
  2748. // --------------------------------------------------------------------------------------------------------
  2749. // Control Output
  2750. if (pData->event.portOut != nullptr)
  2751. {
  2752. uint8_t channel;
  2753. uint16_t param;
  2754. float value;
  2755. for (uint32_t k=0; k < pData->param.count; ++k)
  2756. {
  2757. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2758. continue;
  2759. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2760. if (pData->param.data[k].midiCC > 0)
  2761. {
  2762. channel = pData->param.data[k].midiChannel;
  2763. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2764. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2765. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2766. }
  2767. }
  2768. } // End of Control Output
  2769. #endif
  2770. // --------------------------------------------------------------------------------------------------------
  2771. // Final work
  2772. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2773. {
  2774. fExt.worker->end_run(fHandle);
  2775. if (fHandle2 != nullptr)
  2776. fExt.worker->end_run(fHandle2);
  2777. }
  2778. fFirstActive = false;
  2779. // --------------------------------------------------------------------------------------------------------
  2780. }
  2781. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  2782. {
  2783. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2784. if (pData->audioIn.count > 0)
  2785. {
  2786. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  2787. }
  2788. if (pData->audioOut.count > 0)
  2789. {
  2790. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  2791. }
  2792. if (pData->cvIn.count > 0)
  2793. {
  2794. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  2795. }
  2796. if (pData->cvOut.count > 0)
  2797. {
  2798. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  2799. }
  2800. // --------------------------------------------------------------------------------------------------------
  2801. // Try lock, silence otherwise
  2802. if (pData->engine->isOffline())
  2803. {
  2804. pData->singleMutex.lock();
  2805. }
  2806. else if (! pData->singleMutex.tryLock())
  2807. {
  2808. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2809. {
  2810. for (uint32_t k=0; k < frames; ++k)
  2811. audioOut[i][k+timeOffset] = 0.0f;
  2812. }
  2813. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2814. {
  2815. for (uint32_t k=0; k < frames; ++k)
  2816. cvOut[i][k+timeOffset] = 0.0f;
  2817. }
  2818. return false;
  2819. }
  2820. // --------------------------------------------------------------------------------------------------------
  2821. // Set audio buffers
  2822. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2823. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, static_cast<int>(frames));
  2824. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2825. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  2826. // --------------------------------------------------------------------------------------------------------
  2827. // Set CV buffers
  2828. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  2829. FloatVectorOperations::copy(fCvInBuffers[i], cvIn[i]+timeOffset, static_cast<int>(frames));
  2830. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2831. FloatVectorOperations::clear(fCvOutBuffers[i], static_cast<int>(frames));
  2832. // --------------------------------------------------------------------------------------------------------
  2833. // Run plugin
  2834. fDescriptor->run(fHandle, frames);
  2835. if (fHandle2 != nullptr)
  2836. fDescriptor->run(fHandle2, frames);
  2837. // --------------------------------------------------------------------------------------------------------
  2838. // Handle trigger parameters
  2839. for (uint32_t k=0; k < pData->param.count; ++k)
  2840. {
  2841. if (pData->param.data[k].type != PARAMETER_INPUT)
  2842. continue;
  2843. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2844. {
  2845. if (! carla_compareFloats(fParamBuffers[k], pData->param.ranges[k].def))
  2846. {
  2847. fParamBuffers[k] = pData->param.ranges[k].def;
  2848. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2849. }
  2850. }
  2851. }
  2852. pData->postRtEvents.trySplice();
  2853. #ifndef BUILD_BRIDGE
  2854. // --------------------------------------------------------------------------------------------------------
  2855. // Post-processing (dry/wet, volume and balance)
  2856. {
  2857. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && ! carla_compareFloats(pData->postProc.dryWet, 1.0f);
  2858. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_compareFloats(pData->postProc.balanceLeft, -1.0f) && carla_compareFloats(pData->postProc.balanceRight, 1.0f));
  2859. const bool isMono = (pData->audioIn.count == 1);
  2860. bool isPair;
  2861. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2862. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2863. {
  2864. // Dry/Wet
  2865. if (doDryWet)
  2866. {
  2867. for (uint32_t k=0; k < frames; ++k)
  2868. {
  2869. if (k < pData->latency)
  2870. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  2871. else if (pData->latency < frames)
  2872. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  2873. else
  2874. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  2875. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2876. }
  2877. }
  2878. // Balance
  2879. if (doBalance)
  2880. {
  2881. isPair = (i % 2 == 0);
  2882. if (isPair)
  2883. {
  2884. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2885. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  2886. }
  2887. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2888. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2889. for (uint32_t k=0; k < frames; ++k)
  2890. {
  2891. if (isPair)
  2892. {
  2893. // left
  2894. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2895. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2896. }
  2897. else
  2898. {
  2899. // right
  2900. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2901. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2902. }
  2903. }
  2904. }
  2905. // Volume (and buffer copy)
  2906. {
  2907. for (uint32_t k=0; k < frames; ++k)
  2908. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2909. }
  2910. }
  2911. } // End of Post-processing
  2912. #else // BUILD_BRIDGE
  2913. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2914. {
  2915. for (uint32_t k=0; k < frames; ++k)
  2916. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2917. }
  2918. #endif
  2919. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2920. {
  2921. for (uint32_t k=0; k < frames; ++k)
  2922. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  2923. }
  2924. // --------------------------------------------------------------------------------------------------------
  2925. pData->singleMutex.unlock();
  2926. return true;
  2927. }
  2928. void bufferSizeChanged(const uint32_t newBufferSize) override
  2929. {
  2930. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2931. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2932. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2933. {
  2934. if (fAudioInBuffers[i] != nullptr)
  2935. delete[] fAudioInBuffers[i];
  2936. fAudioInBuffers[i] = new float[newBufferSize];
  2937. }
  2938. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2939. {
  2940. if (fAudioOutBuffers[i] != nullptr)
  2941. delete[] fAudioOutBuffers[i];
  2942. fAudioOutBuffers[i] = new float[newBufferSize];
  2943. }
  2944. if (fHandle2 == nullptr)
  2945. {
  2946. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2947. {
  2948. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2949. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2950. }
  2951. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2952. {
  2953. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2954. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2955. }
  2956. }
  2957. else
  2958. {
  2959. if (pData->audioIn.count > 0)
  2960. {
  2961. CARLA_ASSERT(pData->audioIn.count == 2);
  2962. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2963. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2964. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2965. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2966. }
  2967. if (pData->audioOut.count > 0)
  2968. {
  2969. CARLA_ASSERT(pData->audioOut.count == 2);
  2970. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2971. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2972. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2973. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2974. }
  2975. }
  2976. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  2977. {
  2978. if (fCvInBuffers[i] != nullptr)
  2979. delete[] fCvInBuffers[i];
  2980. fCvInBuffers[i] = new float[newBufferSize];
  2981. fDescriptor->connect_port(fHandle, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  2982. if (fHandle2 != nullptr)
  2983. fDescriptor->connect_port(fHandle2, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  2984. }
  2985. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2986. {
  2987. if (fCvOutBuffers[i] != nullptr)
  2988. delete[] fCvOutBuffers[i];
  2989. fCvOutBuffers[i] = new float[newBufferSize];
  2990. fDescriptor->connect_port(fHandle, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  2991. if (fHandle2 != nullptr)
  2992. fDescriptor->connect_port(fHandle2, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  2993. }
  2994. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2995. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2996. {
  2997. fLv2Options.maxBufferSize = newBufferSizeInt;
  2998. if (fLv2Options.minBufferSize != 1)
  2999. fLv2Options.minBufferSize = newBufferSizeInt;
  3000. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3001. {
  3002. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  3003. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  3004. }
  3005. }
  3006. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  3007. }
  3008. void sampleRateChanged(const double newSampleRate) override
  3009. {
  3010. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  3011. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  3012. if (! carla_compareFloats(fLv2Options.sampleRate, newSampleRate))
  3013. {
  3014. fLv2Options.sampleRate = newSampleRate;
  3015. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3016. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  3017. }
  3018. for (uint32_t k=0; k < pData->param.count; ++k)
  3019. {
  3020. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  3021. {
  3022. fParamBuffers[k] = static_cast<float>(newSampleRate);
  3023. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3024. break;
  3025. }
  3026. }
  3027. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  3028. }
  3029. void offlineModeChanged(const bool isOffline) override
  3030. {
  3031. for (uint32_t k=0; k < pData->param.count; ++k)
  3032. {
  3033. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_FREEWHEEL)
  3034. {
  3035. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  3036. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3037. break;
  3038. }
  3039. }
  3040. }
  3041. // -------------------------------------------------------------------
  3042. // Plugin buffers
  3043. void initBuffers() const noexcept override
  3044. {
  3045. fEventsIn.initBuffers();
  3046. fEventsOut.initBuffers();
  3047. CarlaPlugin::initBuffers();
  3048. }
  3049. void clearBuffers() noexcept override
  3050. {
  3051. carla_debug("Lv2Plugin::clearBuffers() - start");
  3052. if (fAudioInBuffers != nullptr)
  3053. {
  3054. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3055. {
  3056. if (fAudioInBuffers[i] != nullptr)
  3057. {
  3058. delete[] fAudioInBuffers[i];
  3059. fAudioInBuffers[i] = nullptr;
  3060. }
  3061. }
  3062. delete[] fAudioInBuffers;
  3063. fAudioInBuffers = nullptr;
  3064. }
  3065. if (fAudioOutBuffers != nullptr)
  3066. {
  3067. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3068. {
  3069. if (fAudioOutBuffers[i] != nullptr)
  3070. {
  3071. delete[] fAudioOutBuffers[i];
  3072. fAudioOutBuffers[i] = nullptr;
  3073. }
  3074. }
  3075. delete[] fAudioOutBuffers;
  3076. fAudioOutBuffers = nullptr;
  3077. }
  3078. if (fCvInBuffers != nullptr)
  3079. {
  3080. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3081. {
  3082. if (fCvInBuffers[i] != nullptr)
  3083. {
  3084. delete[] fCvInBuffers[i];
  3085. fCvInBuffers[i] = nullptr;
  3086. }
  3087. }
  3088. delete[] fCvInBuffers;
  3089. fCvInBuffers = nullptr;
  3090. }
  3091. if (fCvOutBuffers != nullptr)
  3092. {
  3093. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3094. {
  3095. if (fCvOutBuffers[i] != nullptr)
  3096. {
  3097. delete[] fCvOutBuffers[i];
  3098. fCvOutBuffers[i] = nullptr;
  3099. }
  3100. }
  3101. delete[] fCvOutBuffers;
  3102. fCvOutBuffers = nullptr;
  3103. }
  3104. if (fParamBuffers != nullptr)
  3105. {
  3106. delete[] fParamBuffers;
  3107. fParamBuffers = nullptr;
  3108. }
  3109. fEventsIn.clear();
  3110. fEventsOut.clear();
  3111. CarlaPlugin::clearBuffers();
  3112. carla_debug("Lv2Plugin::clearBuffers() - end");
  3113. }
  3114. // -------------------------------------------------------------------
  3115. // OSC stuff
  3116. bool updateOscDataExtra() override
  3117. {
  3118. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  3119. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i, nullptr));
  3120. osc_send_lv2_urid_map(pData->osc.data, CARLA_URI_MAP_ID_NULL, "Complete");
  3121. return true;
  3122. }
  3123. // -------------------------------------------------------------------
  3124. // Post-poned UI Stuff
  3125. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3126. {
  3127. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3128. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3129. if (fUI.type == UI::TYPE_OSC)
  3130. {
  3131. if (pData->osc.data.target != nullptr)
  3132. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  3133. }
  3134. else
  3135. {
  3136. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr)
  3137. {
  3138. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3139. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  3140. }
  3141. }
  3142. }
  3143. void uiMidiProgramChange(const uint32_t index) noexcept override
  3144. {
  3145. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3146. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3147. if (fUI.type == UI::TYPE_OSC)
  3148. {
  3149. if (pData->osc.data.target != nullptr)
  3150. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3151. }
  3152. else
  3153. {
  3154. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  3155. fExt.uiprograms->select_program(fUI.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3156. }
  3157. }
  3158. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3159. {
  3160. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3161. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3162. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3163. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3164. if (fUI.type == UI::TYPE_OSC)
  3165. {
  3166. if (pData->osc.data.target != nullptr)
  3167. {
  3168. uint8_t midiData[4] = { 0, 0, 0, 0 };
  3169. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3170. midiData[2] = note;
  3171. midiData[3] = velo;
  3172. osc_send_midi(pData->osc.data, midiData);
  3173. }
  3174. }
  3175. else
  3176. {
  3177. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3178. {
  3179. LV2_Atom_MidiEvent midiEv;
  3180. midiEv.event.time.frames = 0;
  3181. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3182. midiEv.event.body.size = 3;
  3183. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3184. midiEv.data[1] = note;
  3185. midiEv.data[2] = velo;
  3186. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3187. }
  3188. }
  3189. }
  3190. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3191. {
  3192. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3193. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3194. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3195. if (fUI.type == UI::TYPE_OSC)
  3196. {
  3197. if (pData->osc.data.target != nullptr)
  3198. {
  3199. uint8_t midiData[4] = { 0, 0, 0, 0 };
  3200. midiData[1] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3201. midiData[2] = note;
  3202. osc_send_midi(pData->osc.data, midiData);
  3203. }
  3204. }
  3205. else
  3206. {
  3207. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3208. {
  3209. LV2_Atom_MidiEvent midiEv;
  3210. midiEv.event.time.frames = 0;
  3211. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3212. midiEv.event.body.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, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::handleStateStore(%i:\"%s\", %p, " P_SIZE ", %i:\"%s\", %i)", key, carla_lv2_urid_unmap(this, key), value, size, type, carla_lv2_urid_unmap(this, type), flags);
  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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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(static_cast<uint32_t>(sizeof(LV2_Atom)+atom->size)), 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("Lv2Plugin::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("Lv2Plugin::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("Lv2Plugin::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 = (LV2_Lib_Descriptor_Function)pData->libSymbol("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 = (LV2_Descriptor_Function)pData->libSymbol("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 = (LV2UI_DescriptorFunction)pData->uiLibSymbol("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[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  4273. fLv2Options.opts[Lv2PluginOptions::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("Lv2Plugin::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("Lv2Plugin::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. Lv2PluginEventData fEventsIn;
  4381. Lv2PluginEventData fEventsOut;
  4382. Lv2PluginOptions 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. ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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. ((Lv2Plugin*)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 ((Lv2Plugin*)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 ((Lv2Plugin*)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. ((Lv2Plugin*)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. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4844. }
  4845. // -------------------------------------------------------------------
  4846. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4847. };
  4848. // -------------------------------------------------------------------------------------------------------------------
  4849. #define lv2PluginPtr ((Lv2Plugin*)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. // -------------------------------------------------------------------------------------------------------------------
  4877. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4878. {
  4879. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  4880. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4881. if (! plugin->init(init.name, init.label))
  4882. {
  4883. delete plugin;
  4884. return nullptr;
  4885. }
  4886. plugin->reload();
  4887. bool canRun = true;
  4888. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  4889. {
  4890. if (! plugin->canRunInRack())
  4891. {
  4892. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4893. canRun = false;
  4894. }
  4895. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  4896. {
  4897. init.engine->setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  4898. canRun = false;
  4899. }
  4900. }
  4901. else if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_PATCHBAY && (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0))
  4902. {
  4903. init.engine->setLastError("CV ports in patchbay mode is still TODO");
  4904. canRun = false;
  4905. }
  4906. if (! canRun)
  4907. {
  4908. delete plugin;
  4909. return nullptr;
  4910. }
  4911. return plugin;
  4912. }
  4913. // -------------------------------------------------------------------------------------------------------------------
  4914. CARLA_BACKEND_END_NAMESPACE