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.

5978 lines
226KB

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